glutMouseWheelFunc 的缩放问题
Zooming Issues with glutMouseWheelFunc
我厌倦了使用 VC++ 中的 glutMouseWheelFunc
实现简单的缩放 in/out。我能够实现缩放 in/out 但在这样做时,轴(GLlines)在缩放超过一定级别后往往会消失。
我在 glTranslatef
中使用 glutMouseWheelFunc
到 increase/decrease z 轴值。
我已将 'z' 定义为 glTranslatef
作为相机距离:
float cDist= 30.0f; // camera distance
然后在glTranslatef中使用,
glTranslatef(0.0f, 0.0f, -cDist);
在下面的显示函数中。
void display(void) {
glClearColor(0.0, 0.0, 0.0, 1.0); //clear the screen to black
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear the color buffer and the depth buffer
enable();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -cDist);
glRotatef(xrot, 1.0, 0.0, 0.0);
---------------
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-500, 0, 0);
glVertex3f(500, 0, 0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0, -500, 0);
glVertex3f(0, 500, 0);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0, 0, -500);
glVertex3f(0, 0, 500);
glTranslated(-xpos, 0.0f, -zpos); //translate the screento the position of our camera
glColor3f(1.0f, 1.0f, 1.0f);
glutSwapBuffers();
}
之后,我将 wheelfunc 定义为,
void mouseWheel(int button, int dir, int x, int y)
{
if (dir > 0)
{
cDist = cDist++;
}
else
{
cDist= cDist--;
}
return;
}
并在主函数中用 glutMouseWheelFunc(mouseWheel);
调用它
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Window");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMotionFunc(mouseMovement);
glutMouseWheelFunc(mouseWheel);---- here
-----
---
glutMainLoop();
return 0;
}
这种放大的方法对吗?如果没有,还有什么选择?另外,我如何定义轴(我画的线)到屏幕的整个范围?
感谢您的帮助。好像我错过了为 gluPerspective
设置适当的深度值。增加深度值后,缩放 out/in 工作正常
我厌倦了使用 VC++ 中的 glutMouseWheelFunc
实现简单的缩放 in/out。我能够实现缩放 in/out 但在这样做时,轴(GLlines)在缩放超过一定级别后往往会消失。
我在 glTranslatef
中使用 glutMouseWheelFunc
到 increase/decrease z 轴值。
我已将 'z' 定义为 glTranslatef
作为相机距离:
float cDist= 30.0f; // camera distance
然后在glTranslatef中使用,
glTranslatef(0.0f, 0.0f, -cDist);
在下面的显示函数中。
void display(void) {
glClearColor(0.0, 0.0, 0.0, 1.0); //clear the screen to black
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear the color buffer and the depth buffer
enable();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -cDist);
glRotatef(xrot, 1.0, 0.0, 0.0);
---------------
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-500, 0, 0);
glVertex3f(500, 0, 0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0, -500, 0);
glVertex3f(0, 500, 0);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0, 0, -500);
glVertex3f(0, 0, 500);
glTranslated(-xpos, 0.0f, -zpos); //translate the screento the position of our camera
glColor3f(1.0f, 1.0f, 1.0f);
glutSwapBuffers();
}
之后,我将 wheelfunc 定义为,
void mouseWheel(int button, int dir, int x, int y)
{
if (dir > 0)
{
cDist = cDist++;
}
else
{
cDist= cDist--;
}
return;
}
并在主函数中用 glutMouseWheelFunc(mouseWheel);
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Window");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMotionFunc(mouseMovement);
glutMouseWheelFunc(mouseWheel);---- here
-----
---
glutMainLoop();
return 0;
}
这种放大的方法对吗?如果没有,还有什么选择?另外,我如何定义轴(我画的线)到屏幕的整个范围?
感谢您的帮助。好像我错过了为 gluPerspective
设置适当的深度值。增加深度值后,缩放 out/in 工作正常