C ++ OpenGL Cube没有出现在屏幕上

时间:2011-04-29 03:09:41

标签: c++ opengl

喂 这是我在stackflow上发表的第一篇文章。 我最近一直在尝试使用OpenGL和C ++,当我尝试渲染一个带有光照的立方体时,我遇到了一个问题......但它似乎没有出现我认为这是由于某种裁剪错误但我没有线索:(任何帮助将不胜感激。

这是我的代码......

初始化函数......

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:变量或字段`gluLookAt'声明为void |

最终编辑
感谢大家的帮助...原来我失败了:(我没有宣布翻译......或者其他关键组件的感知......但是我把它全部工作了:D并感谢基思我没有如何控制相机

2 个答案:

答案 0 :(得分:1)

您可能需要定义投影矩阵

这可以通过例如gluLookAt()

来完成

答案 1 :(得分:0)

修改后: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,其初始化(构造)有一堆双精度。