鼠标选择物体

随着osg代码发生变化,Delta3d升级后,官方网站上的选取代码也不能应用,现将代码修改如下:

bool  Picker:: MouseButtonPressed (const dtCore::Mouse *mouse, dtCore::Mouse::MouseButton button)

{

     if (button == dtCore::Mouse::MouseButton::RightButton)

     {

         //is there an object picked by the mouse ?

         dtCore::DeltaDrawable* picked_object = NULL;

         osg::Vec3 nearPoint,farPoint;

         float x=0, y=0;

 

         mouse->GetPosition(x, y);

 

osgViewer::Renderer* renderer = static_cast<osgViewer::Renderer*>(GetCamera()->GetOSGCamera()->getRenderer());

         osgUtil::SceneView* sceneView = renderer->getSceneView(0);

    

         int xWin = sceneView->getViewport()->width()*(x+1)/2;

         int yWin = sceneView->getViewport()->height()*(y+1)/2;

 

         sceneView->projectWindowXYIntoObject(xWin,yWin,nearPoint,farPoint);

 

         // create the isector

         dtCore::RefPtr<dtCore::Isector> pickRay = new dtCore::Isector(GetScene());

         pickRay->SetStartPosition(nearPoint);

         pickRay->SetDirection(farPoint-nearPoint);

 

         // shoot the ray

         if(pickRay->Update())

         {

              osgUtil::IntersectVisitor::HitList hlist = pickRay->GetHitList();

 

              osgUtil::Hit& hit = hlist.front();

              osg::NodePath& np = hit._nodePath;

 

              for (int i=np.size()-1; i>=0; --i)

              {

                   osg::Node *tempNode = np[i];

                   std::string strName  = tempNode->getName();

                   std::cout<<"Node name:"<<strName<<std::endl;

              }

 

         }

     }

     return false;

}

在PyOpenGL中,可以通过以下步骤使用鼠标选择物体: 1. 首先,定义一个变量来存储选中的对象的ID。例如,我们可以定义一个"selected_object_id"变量来存储选中的物体的ID。 2. 创建一个函数来检测鼠标是否与场景中的物体相交。这可以通过使用OpenGL的选择模式和名称堆栈来实现。在这个函数中,我们可以使用glRenderMode(GL_SELECT)来进入选择模式,并在场景中绘制所有的对象。然后,我们可以使用glLoadName(object_id)将每个对象的ID添加到名称堆栈中,以便在选择模式下标识它们。最后,我们可以使用glRenderMode(GL_RENDER)退出选择模式,并使用glGetSelectBuffer()获取选择结果。 3. 在每个渲染帧中,我们可以检查鼠标是否与任何物体相交,并将选中的对象ID存储在"selected_object_id"变量中。这可以通过在渲染场景之前调用检测函数来实现。 4. 最后,我们可以在渲染场景时使用"selected_object_id"变量来突出显示选中的物体。例如,我们可以在绘制选中的物体时使用不同的颜色或纹理来突出显示它们。 下面是一个示例代码: ```python import numpy as np from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * # 定义场景中的三个物体 objects = [ {"id": 1, "vertices": np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]], dtype=np.float32)}, {"id": 2, "vertices": np.array([[0, 0, 0], [1, 0, 0], [1, 0, 1], [0, 0, 1]], dtype=np.float32)}, {"id": 3, "vertices": np.array([[0, 0, 0], [0, 1, 0], [0, 1, 1], [0, 0, 1]], dtype=np.float32)}, ] # 定义选中的物体ID selected_object_id = None def check_selection(x, y): global selected_object_id # 进入选择模式 glSelectBuffer(1024) glRenderMode(GL_SELECT) glInitNames() glPushName(0) # 绘制所有物体,并将它们的ID添加到名称堆栈中 glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() viewport = glGetIntegerv(GL_VIEWPORT) gluPickMatrix(x, viewport[3]-y, 1, 1, viewport) gluPerspective(45, viewport[2]/viewport[3], 0.1, 100.0) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() for obj in objects: glLoadName(obj["id"]) glBegin(GL_QUADS) for vertex in obj["vertices"]: glVertex3fv(vertex) glEnd() glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) # 退出选择模式,并获取选中的物体ID hits = glRenderMode(GL_RENDER) if hits > 0: selected_object_id = hits[0].get(0).get(0) else: selected_object_id = None def display(): global selected_object_id glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() gluLookAt(5,5,5, 0,0,0, 0,1,0) # 绘制所有物体,并将选中的物体突出显示 for obj in objects: if obj["id"] == selected_object_id: glColor3f(1,1,0) else: glColor3f(1,1,1) glBegin(GL_QUADS) for vertex in obj["vertices"]: glVertex3fv(vertex) glEnd() glutSwapBuffers() def mouse(button, state, x, y): if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN: check_selection(x, y) glutPostRedisplay() def reshape(width, height): glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(45, width/height, 0.1, 100.0) glMatrixMode(GL_MODELVIEW) glutInit() glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH) glutInitWindowSize(800, 600) glutCreateWindow(b"PyOpenGL Selection Example") glutDisplayFunc(display) glutMouseFunc(mouse) glutReshapeFunc(reshape) glClearColor(0,0,0,1) glEnable(GL_DEPTH_TEST) glutMainLoop() ``` 在这个示例代码中,我们定义了三个简单的物体,每个物体都有一个唯一的ID和一些顶点坐标。当用户单击鼠标左键时,我们使用检测函数来检测鼠标是否与任何物体相交,并将选中的物体ID存储在"selected_object_id"变量中。在每个渲染帧中,我们根据"selected_object_id"变量来突出显示选中的物体
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值