分享

Chai 3D之鼠标拾取

 yvette多宝 2023-03-10 发布于湖北
推荐:将 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)和表面法线。

  可以设置碰撞设置以过滤某些类型的数据或加快过程。

例07-鼠标选择:操作员可以用鼠标选择和移动对象

  在从示例 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);
}
}
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多