C ++ OpenGL Cube未显示在屏幕上
Posted
技术标签:
【中文标题】C ++ OpenGL Cube未显示在屏幕上【英文标题】:C++ OpenGL Cube not showing up on screen 【发布时间】:2011-04-29 03:09:40 【问题描述】:我最近一直在尝试将 OpenGL 与 C++ 一起使用,但在尝试使用光照渲染立方体时遇到了一个问题,但我认为这是由于某种剪裁错误造成的。
p>这是我的代码。初始化函数:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING); //Enable lighting
glEnable(GL_LIGHT0); //Enable light #0
glEnable(GL_LIGHT1); //Enable light #1
glEnable(GL_NORMALIZE); //Automatically normalize normals
这是绘制代码
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0.0f, 0.0f, -8.0f);
//Add ambient light
GLfloat ambientColor[] = 0.2f, 0.2f, 0.2f, 1.0f; //Color (0.2, 0.2, 0.2)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add positioned light
GLfloat lightColor0[] = 0.5f, 0.5f, 0.5f, 1.0f; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = 4.0f, 0.0f, 8.0f, 1.0f; //Positioned at (4, 0, 8)
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
//Add directed light
GLfloat lightColor1[] = 0.5f, 0.2f, 0.2f, 1.0f; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = -1.0f, 0.5f, 0.5f, 0.0f;
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
glRotatef(Rotation, 0.0f, 1.0f, 0.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
//Front
glNormal3f(0.0f, 0.0f, 1.0f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//Right
glNormal3f(1.0f, 0.0f, 0.0f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, 1.0f, 1.5f);
//glNormal3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.5f, -1.0f, 1.5f);
//Back
glNormal3f(0.0f, 0.0f, -1.0f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, 1.0f, -1.5f);
//glNormal3f(1.0f, 0.0f, -1.0f);
glVertex3f(1.5f, -1.0f, -1.5f);
//Left
glNormal3f(-1.0f, 0.0f, 0.0f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, -1.0f, -1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, -1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 1.0f, 1.5f);
//glNormal3f(-1.0f, 0.0f, -1.0f);
glVertex3f(-1.5f, 1.0f, -1.5f);
glEnd();
SwapBuffers(hDC);
Sleep (1);
在听从 Keith 的建议后,我尝试像这样使用 gluLookAt:
void gluLookAt ( 10.0 , 10.0 , 10.0 , 1.5, -1.0, 1.5, 0.0, 0.0, 1.0 );
但我得到一个错误
|74|error: variable or field `gluLookAt' declared void|
【问题讨论】:
NeHe 有一个很好的初学者教程:nehe.gamedev.net/data/lessons/lesson.asp?lesson=02 ...我的渲染代码没有问题...而且该教程是 2d .... 【参考方案1】:您可能需要定义投影矩阵。
这可以通过例如gluLookAt()来完成
【讨论】:
是的,但是应该使用glFrustum()
或gluPerspective()
来定义投影矩阵。 gluLookAt()
应该用在模型视图矩阵上!【参考方案2】:
编辑后:gluLookAt
是一个类似于glVertex3f
等的函数。因此只需调用即可调用它:
gluLookAt(10.0 , 10.0 , 10.0 , 1.5, -1.0, 1.5, 0.0, 0.0, 1.0 );
注意这行代码中没有void
!
如果在这一行前面有一个void
,编译器会看到一个名为gluLookAt
、类型为void
的变量声明,它使用一堆双精度进行初始化(构造)。
【讨论】:
大声笑是的...我看到的示例是在 C 中...我忘记更新此线程但我发现了我的错误..无论如何感谢您的帮助:D以上是关于C ++ OpenGL Cube未显示在屏幕上的主要内容,如果未能解决你的问题,请参考以下文章