如何在OpenGL中单击鼠标绘制多边形?

时间:2012-05-19 04:22:03

标签: mouseevent mouseclick-event onmouseclick

我希望我的程序在鼠标点击我在屏幕上创建的按钮时绘制多边形。我应该在哪里绘制绘制多边形命令?我知道我不能放入我的鼠标功能,因为它的效果在下次显示回调运行时丢失,并且必须在我的显示功能中。但是我可以在显示功能中设置if条件吗?

1 个答案:

答案 0 :(得分:0)

希望这有助于你帮助我。将鼠标坐标转换为3D对象可以使用的内容(请参阅下面的代码)并将其存储在某处后,您可以使用变换在存储的坐标处绘制在循环中的对象。我在我的OpenGL程序的init函数中初始化了我的形状,颜色等,但只有当我在键盘上选择了一个数字然后在我的视口中的某个地方单击时才绘制它。重复这些步骤可将该对象移动/转换为新坐标。

void MouseButton(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
    leftmousebutton_down = (state == GLUT_DOWN) ? TRUE : FALSE;     
    if(leftmousebutton_down)
    {
        cout << "LEFT BUTTON DOWN" << endl;
        //pTransInfo[0].vTranslate  = Vector3( 0.5f, 0.5f, 0.5f ); // center of scene
        GLint viewport[4]; //var to hold the viewport info
        GLdouble modelview[16]; //var to hold the modelview info
        GLdouble projection[16]; //var to hold the projection matrix info
        GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates
        GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates

        glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
        glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get the projection matrix info
        glGetIntegerv( GL_VIEWPORT, viewport ); //get the viewport info

        winX = (float)x;
        winY = (float)viewport[3] - (float)y;
        winZ = 0;

        //get the world coordinates from the screen coordinates
        gluUnProject( winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);

        cout << "coordinates: worldX = " << worldX << " worldY = " << worldY << " worldZ = " <<  worldZ << endl; //THIS IS WHAT YOU WANT, STORE IT IN AN ARRAY OR OTHER STRUCTURE
    }
}

}