c++ gluLookAt 的 OpenGL 显示问题
我一直在尝试解决这个程序:
绘制颜色立方体并允许用户适当移动相机以尝试透视观察的程序。立方体也应该旋转。
我已经完成了编码,就在这里。它似乎工作正常,除了 1 个简单的问题:
//Program to draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing
#include <stdio.h>
#include <glut.h>
float v[][3] = {{-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1},{-1,1,-1},{1,1,-1},{1,-1,-1},{-1,-1,-1}};
void drawPolygon(float a[3],float b[3],float c[3],float d[3])
{
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glVertex3fv(d);
glEnd();
glFlush();
}
void drawCube(float v[8][3])
{
glColor3f(1,0,0);
drawPolygon(v[0],v[1],v[2],v[3]);
glColor3f(0,1,0);
drawPolygon(v[0],v[1],v[6],v[7]);
glColor3f(0,0,1);
drawPolygon(v[7],v[6],v[5],v[4]);
glColor3f(1,1,0);
drawPolygon(v[2],v[3],v[4],v[5]);
glColor3f(0,1,1);
drawPolygon(v[1],v[2],v[5],v[6]);
glColor3f(1,0,1);
drawPolygon(v[0],v[3],v[4],v[7]);
glFlush();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0);
//glRotatef(60,1,1,0);
drawCube(v);
glFlush();
}
void reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-2,2);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void mouse(int btn,int state,int x,int y)
{
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
glRotatef(2,1,0,0);
if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
glRotatef(2,0,1,0);
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
glRotatef(2,0,0,1);
glutPostRedisplay();
}
float ex=0,ey=0,ez=-10,cx=0,cy=0,cz=0,ux=0,uy=1,uz=0;
void keyboard(unsigned char key,int x,int y)
{
if(key == 'x')
ex += 0.1;
if(key == 'X')
ex -= 0.1;
if(key == 'y')
ey += 0.1;
if(key == 'Y')
ey -= 0.1;
if(key == 'z')
ez += 0.1;
if(key == 'Z')
ez -= 0.1;
glMatrixMode(GL_PROJECTION);
gluLookAt(ex,ey,ez,cx,cy,cz,ux,uy,uz);
glutPostRedisplay();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,800);
glutCreateWindow("spin cube");
glClearColor(1,1,1,0);
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
立方体通过鼠标点击旋转
相机按 x/X y/Y z/Z 移动
但问题是,当我第一次按下某个键时,显示消失。然后第二次它带有正确移动的相机。为什么?我希望相机只是移动而不是消失显示。怎么了?
I have been trying to solve this program:
Program to draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing. Also the cube should rotate.
I have completed the coding and here it is. It seems to work fine except for 1 simple problem:
//Program to draw a color cube and allow the user to move the camera suitably to experiment with perspective viewing
#include <stdio.h>
#include <glut.h>
float v[][3] = {{-1,-1,1},{1,-1,1},{1,1,1},{-1,1,1},{-1,1,-1},{1,1,-1},{1,-1,-1},{-1,-1,-1}};
void drawPolygon(float a[3],float b[3],float c[3],float d[3])
{
glBegin(GL_POLYGON);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glVertex3fv(d);
glEnd();
glFlush();
}
void drawCube(float v[8][3])
{
glColor3f(1,0,0);
drawPolygon(v[0],v[1],v[2],v[3]);
glColor3f(0,1,0);
drawPolygon(v[0],v[1],v[6],v[7]);
glColor3f(0,0,1);
drawPolygon(v[7],v[6],v[5],v[4]);
glColor3f(1,1,0);
drawPolygon(v[2],v[3],v[4],v[5]);
glColor3f(0,1,1);
drawPolygon(v[1],v[2],v[5],v[6]);
glColor3f(1,0,1);
drawPolygon(v[0],v[3],v[4],v[7]);
glFlush();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,0,0);
//glRotatef(60,1,1,0);
drawCube(v);
glFlush();
}
void reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-2,2);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
void mouse(int btn,int state,int x,int y)
{
if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
glRotatef(2,1,0,0);
if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
glRotatef(2,0,1,0);
if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
glRotatef(2,0,0,1);
glutPostRedisplay();
}
float ex=0,ey=0,ez=-10,cx=0,cy=0,cz=0,ux=0,uy=1,uz=0;
void keyboard(unsigned char key,int x,int y)
{
if(key == 'x')
ex += 0.1;
if(key == 'X')
ex -= 0.1;
if(key == 'y')
ey += 0.1;
if(key == 'Y')
ey -= 0.1;
if(key == 'z')
ez += 0.1;
if(key == 'Z')
ez -= 0.1;
glMatrixMode(GL_PROJECTION);
gluLookAt(ex,ey,ez,cx,cy,cz,ux,uy,uz);
glutPostRedisplay();
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,800);
glutCreateWindow("spin cube");
glClearColor(1,1,1,0);
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
The cube rotates by mouse clicks
The camera moves by x/X y/Y z/Z
But the problem is that when i first press a key the display vanishes. Then the 2nd time it comes with properly moved camera. Why? I want the camera to just move and not vanish the display. What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的程序中有两个错误:
两者从根本上来说都是错误的。这是代码的更正版本:
There are two mistakes in your program:
Both is fundamentally wrong. Here's a corrected version of your code:
gluLookAt
不应进入投影矩阵,而应进入模型视图。我还建议您跟踪立方体旋转和眼睛位置,并将它们应用到显示函数中:这并不 100% 相当于您最初想要实现的目标,因为它围绕 x 应用了三个累积的连续旋转, y 轴和 z 轴分别代替增量轴。
此外,您不需要所有的
glFlush
调用,在渲染结束时对glFinish
的一次调用就足够了,甚至更好;交换到双缓冲渲染并使用 glutSwapBuffers 代替。gluLookAt
shouldn't go in to the projection matrix but into the modelview. I would also suggest that you keep track of your cube rotation and eye position and apply them inside the display function instead:This isn't 100% equivalent to what you were originally trying to achieve since it applies three accumulated consecutive rotations around the x, y and z axis respectively instead of incremental ones.
Also you don't need all the
glFlush
calls, a single call toglFinish
at the end of the rendering should be sufficient, or even better; swap to double buffered rendering and useglutSwapBuffers
instead.