我的 opengl 程序有问题。我只能显示一个立方体。我试图显示两个立方体,但似乎无法弄清楚为什么只显示一个立方体。有人可以告诉我为什么以及我需要做些什么来纠正它吗?我必须使用的立方体函数是:
void myCube(double length)
{
double half = length/2;
// face 1
glBegin(GL_POLYGON);
glVertex3d(half, -half, half);
glVertex3d(half, half, half);
glVertex3d(-half, half, half);
glVertex3d(-half, -half, half);
glEnd();
// face 2
glPushMatrix();
glBegin(GL_POLYGON);
glVertex3d(half, -half, -half);
glVertex3d(half, half, -half);
glVertex3d(-half, half, -half);
glVertex3d(-half, -half, -half);
glEnd();
// face 3
glBegin(GL_POLYGON);
glVertex3d(half, half, half);
glVertex3d(half, -half, half);
glVertex3d(half, -half, -half);
glVertex3d(half, half, -half);
glEnd();
// face 4
glBegin(GL_POLYGON);
glVertex3d(-half, -half, half);
glVertex3d(-half, half, half);
glVertex3d(-half, half, -half);
glVertex3d(-half, -half, -half);
glEnd();
// face 5
glBegin(GL_POLYGON);
glVertex3d(half, half, half);
glVertex3d(half, half, -half);
glVertex3d(-half, half, -half);
glVertex3d(-half, half, half);
glEnd();
// face 6
glBegin(GL_POLYGON);
glVertex3d(half, -half, half);
glVertex3d(half, -half, -half);
glVertex3d(-half, -half, -half);
glVertex3d(-half, -half, half);
glEnd();
}
这是我的显示功能:
void myDisplay(void)
{
//cubeCount = (rand() % 15) + 7;
//int r = rand()%18;
//r = r - 9;
//double rd = (double)r / 2.0;
//glMatrixMode(GL_PROJECTION); // set the view volume shape
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glTranslatef(1.0f, 4.0f, 2.0f);
//double factor = 05;
gluPerspective(60, screenWidth/screenHeight, 0.1, 100);
gluLookAt(3.0, 3.0, -3.0, 3.0, 3.0, 0.0, 0.0, 1.0, 0.0);
//for (int i = 0; i < 5; i++)
//{
glPushMatrix();
glTranslatef(4.9f, 1.2f, 1.0f);
glRotated(45.0, 0.0, 1.0, 0.0);
glColor3d(1, 1, 1); // draw white cube
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0, 1.0);
myCube(0.5);
glDisable(GL_POLYGON_OFFSET_FILL);
glColor3d(0, 0, 0); // draw black lines
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
myCube(0.5);
glPopMatrix();
//glFlush();
glPushMatrix();
glTranslatef(2.0f, 4.0f, 2.0f);
glScalef(1.0, 2.0, 1.0);
//glRotated(45.0, 0.0, 1.0, 0.0);
glColor3d(1, 1, 1); // draw white cube
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(1.0, 1.0);
myCube(0.5);
glDisable(GL_POLYGON_OFFSET_FILL);
glColor3d(0, 0, 0); // draw black lines
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
myCube(0.5);
glPopMatrix();
//}
}
//glutSwapBuffers();
glFlush();
}
我的显示器左下角有一个立方体,但另一个立方体没有显示任何位置。如果需要,我会上传更多代码。