推荐:将 NSDT场景编辑器 加入你的3D开发工具链
介绍
鼠标拾取是一种常用的直观操作,用于与各种 3D 图形应用程序中的 3D 场景进行交互。CHAI3D 提供了一些基本功能来检测对象是否已被选中。鼠标选择过程需要首先设置碰撞记录器和所需的碰撞设置。下面的清单说明了一个基本示例。
using namespace chai3d;
cCollisionRecorder recorder;
cCollisionSettings settings;
// detect for any collision between mouse and scene
bool hit = camera->select(x, y, windowWidth, windowHeight, recorder, settings);
碰撞记录器首先是空的,并累积位于鼠标指针下方的选定对象。为每个碰撞事件返回的信息存储在 cCollisionEvent 结构中。这样的结构将包含指向对象的指针、鼠标点击的3D位置信息、选定的三角形(cMesh)和表面法线。
可以设置碰撞设置以过滤某些类型的数据或加快过程。
在从示例 12 多边形提取的以下清单中,对鼠标单击回调进行了编程。如果选择了网格对象中的三角形,则其顶点将涂成红色。
using namespace chai3d;
void mouseClick(int button, int state, int x, int y)
{
// mouse button down
if (state == GLUT_DOWN)
{
cCollisionRecorder recorder;
cCollisionSettings settings;
// detect for any collision between mouse and scene
bool hit = camera->select(x, y, windowW, windowH, recorder, settings);
if (hit)
{
// set color to red
cColorf color;
color.setRed();
// retrieve triangle selected by mouse
cTriangle* triangle = recorder.m_nearestCollision.m_triangle;
if (triangle != NULL)
{
// paint each vertex with the selected color
triangle->getVertex0()->setColor(color);
triangle->getVertex1()->setColor(color);
triangle->getVertex2()->setColor(color);
}
}
}
}
3D建模学习工作室翻译整理,转载请标明出处!