我正在尝试使用opengl开始构建一个非常基本的房间。由于某些原因,在函数drawWalls()中,左墙没有被绘制或者我误解了摄像机的位置。有没有人知道为什么我在运行此代码时看不到左墙?
我的代码:
#include <gl/gl.h>
#include <gl/glut.h>
/*defining colors used in the room*/
GLfloat black[] = {0.0, 0.0, 0.0, 1.0};
GLfloat white[] = {1.0, 1.0, 1.0, 1.0};
GLfloat brown[] = {165.0/255.0, 42.0/255.0, 42.0/255.0, 1.0};
void drawFloor()
{
glBegin(GL_QUAD_STRIP);
glColor4fv(black);
glVertex3f(0.0, 0.0, -3.0);
glVertex3f(1.0, 0.0, -3.0);
glVertex3f(2.0, 0.0, -3.0);
glEnd();
}
void drawWalls()
{
glBegin(GL_QUAD_STRIP);
glColor4fv(white);
glPolygonMode(GL_FRONT, GL_FILL);
//draw left wall
glVertex3f(0.0, 0.0, 2.0);
glVertex3f(0.0, 1.0, 2.0);
glVertex3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 1.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
//draw front wall
glVertex3f(0.0, 0.0, 3.0);
glVertex3f(0.0, 1.0, 3.0);
glVertex3f(1.0, 0.0, 3.0);
glVertex3f(1.0, 1.0, 3.0);
glVertex3f(2.0, 0.0, 3.0);
glVertex3f(2.0, 1.0, 3.0);
glEnd();
}
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 0.0, 0.8, 0.0, 1.0, 0.0, 1.0, 0.0);
drawFloor();
drawWalls();
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat) w / (GLfloat) h, 1, 500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void idleFunc()
{
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("My room");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idleFunc);
glutMainLoop();
}
答案 0 :(得分:0)
为什么你期望看到左墙?它在屏幕外(对于窗口的任何合理的宽高比)。如果我们想象一些“世界空间”,你将会有以下情况:你的相机被放置在原点并且看着几乎对角线的矢量。 lfet墙也放置在x = 0处,因此在视图方向和墙壁之间几乎有45度。您的视锥体从相机前方的距离1开始,垂直视场角为60度,水平视场角由窗口的宽高比定义。因此,如果你使窗户比高大的窗户宽,你就有可能看到那堵墙。