如何在OpenGL中实现按住鼠标左键就可以旋转图形的功能
1个回答
展开全部
首先在windows下获取鼠标按下/抬起/移动的消息
然后再进行判断
如果鼠标按下且移动
则进行glTranslatef/gluLookat操作
左键按下时使glRotatef里的变量相应改变。我是这样做的,不过右键也响应啦,呵呵,参考一下:)
void ArrowKeys(int key, int x, int y)
{
if (key == GLUT_KEY_LEFT) // rotate in the negative z
MyImage.zRot -= 5.0f;
if (key == GLUT_KEY_RIGHT) // rotate in the positive z
MyImage.zRot += 5.0f;
if (key == GLUT_KEY_UP) // rotate in the negative x
MyImage.xRot -= 5.0f;
if (key == GLUT_KEY_DOWN) // rotate in the postive x
MyImage.xRot += 5.0f;
if (key == GLUT_KEY_PAGE_UP) // rotate in the negative y
MyImage.yRot -= 5.0f;
if (key == GLUT_KEY_PAGE_DOWN) // rotate in the postive y
MyImage.yRot += 5.0f;
// Check x,y and z rotation values to keep 0-355
if (MyImage.xRot > 356.0f)
MyImage.xRot = 0.0f;
if (MyImage.xRot < 0.0f)
MyImage.xRot = 355.0f;
if (MyImage.yRot > 356.0f)
MyImage.yRot = 0.0f;
if (MyImage.yRot < 0.0f)
MyImage.yRot = 355.0f;
if (MyImage.zRot > 356.0f)
MyImage.zRot = 0.0f;
if (MyImage.zRot < 0.0f)
MyImage.zRot = 355.0f;
// Refresh the window
glutPostRedisplay();
}
// Do any needed rotation
glRotatef(MyImage.xRot, 1.0f, 0.0f, 0.0f);
glRotatef(MyImage.yRot, 0.0f, 1.0f, 0.0f);
glRotatef(MyImage.zRot, 0.0f, 0.0f, 1.0f);
然后再进行判断
如果鼠标按下且移动
则进行glTranslatef/gluLookat操作
左键按下时使glRotatef里的变量相应改变。我是这样做的,不过右键也响应啦,呵呵,参考一下:)
void ArrowKeys(int key, int x, int y)
{
if (key == GLUT_KEY_LEFT) // rotate in the negative z
MyImage.zRot -= 5.0f;
if (key == GLUT_KEY_RIGHT) // rotate in the positive z
MyImage.zRot += 5.0f;
if (key == GLUT_KEY_UP) // rotate in the negative x
MyImage.xRot -= 5.0f;
if (key == GLUT_KEY_DOWN) // rotate in the postive x
MyImage.xRot += 5.0f;
if (key == GLUT_KEY_PAGE_UP) // rotate in the negative y
MyImage.yRot -= 5.0f;
if (key == GLUT_KEY_PAGE_DOWN) // rotate in the postive y
MyImage.yRot += 5.0f;
// Check x,y and z rotation values to keep 0-355
if (MyImage.xRot > 356.0f)
MyImage.xRot = 0.0f;
if (MyImage.xRot < 0.0f)
MyImage.xRot = 355.0f;
if (MyImage.yRot > 356.0f)
MyImage.yRot = 0.0f;
if (MyImage.yRot < 0.0f)
MyImage.yRot = 355.0f;
if (MyImage.zRot > 356.0f)
MyImage.zRot = 0.0f;
if (MyImage.zRot < 0.0f)
MyImage.zRot = 355.0f;
// Refresh the window
glutPostRedisplay();
}
// Do any needed rotation
glRotatef(MyImage.xRot, 1.0f, 0.0f, 0.0f);
glRotatef(MyImage.yRot, 0.0f, 1.0f, 0.0f);
glRotatef(MyImage.zRot, 0.0f, 0.0f, 1.0f);
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询