【发布时间】:2019-12-30 15:23:23
【问题描述】:
是否可以通过简单的鼠标事件进行场景平移(有点像我们在 gmaps 中获得的)?目前我有两个鼠标事件:
-
鼠标滚轮通过在
glTranslatef中更改cRadius来缩放 -
通过更改
glRotatef中的xrot和yrot来拖动LMB。下面是函数:
float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, zrot = 0, cRadius = 30.0f, lastx, lasty, lastz;
void mouseMovement(int x, int y)
{
int diffx = x - lastx;
int diffy = y - lasty;
lastx = x;
lasty = y;
xrot += (float)diffy;
yrot += (float)diffx;
}
下面还有鼠标按键初始化函数
void mouseFunc(int button, int state, int x, int y)
{
lastx = x;
lasty = y;
}
是否可以使用与旋转相同的逻辑来启用平移功能,例如用 @987654331 中的不同变量替换 xrot 和 yrot @(如果不是应该在哪里应用翻译)?下面是我的显示功能和重塑功能。我使用glPerspective 而不是glLookat
void display(void)
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -cRadius);
glRotatef(xrot, 1.0, 0.0, 0.0);
glRotatef(yrot, 0.0, 1.0, 0.0);
glBegin(GL_LINES);
------------
------
glTranslated(-xpos, 0.0f, zpos);
glutSwapBuffers();
glEnd();
}
鼠标函数在主循环中被调用
int OpenGL(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Window");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMouseFunc(mouseFunc);
glutMotionFunc(mouseMovement);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
//重塑
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w / (GLfloat)h, 0.1, 500.0);
glMatrixMode(GL_MODELVIEW);
}
【问题讨论】:
-
透视投影平移取决于对象的深度。相关:Qmatrix4x4 translate does not take any effect
-
我浏览了链接,但我不知道如何在我的代码中实现它。如您所见,我当前的鼠标功能没有定义任何按钮或状态(GLUT_UP 或 GLUT_DOWN)。到目前为止,我的 LMB 和 RMB 都在做同样的事情。 gltranslatef 可以根据鼠标进行更改吗?通过更改 gltranslatef 中的 cRadius(在显示下),我使用 glutMouseWheelFunc 的“缩放”相同
标签: opengl visual-c++ glut freeglut glu