2012-05-19 62 views
1

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

回答

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 
    } 
} 

}