无法渲染颜色立方体

时间:2013-11-17 07:18:16

标签: c opengl graphics 3d

我正在尝试在OpenGL中绘制一个颜色立方体。我得到的只是一个正方形。 (在输出中只能看到立方体的一个面?我尝试调用gluLookAt(),但这对输出没有任何影响。)

这是我的代码:

#include<GL/glut.h>

#include<math.h>

float vertices[8][3]={{0,0,0},{0,0,200},{0,200,0},{200,0,0},{0,200,200},{200,0,200},{200,200,0},{200,200,200}};

float colors[8][3]={{0.6,0.9,0.1},{0.2,0.1,0.3},{0.7,0.7,0.5},{0.2,0.7,0.4},{0.6,0.6,0.4},{0.1,0.1,0.5},{0.7,0.2,0.5},{0.9,0.7,0.4}};

void display()

{

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        glBegin(GL_POLYGON);
        glColor3fv(colors[0]);
        glVertex3fv(vertices[0]);
        glColor3fv(colors[2]);
        glVertex3fv(vertices[2]);
        glColor3fv(colors[5]);
        glVertex3fv(vertices[5]);
        glColor3fv(colors[1]);
        glVertex3fv(vertices[1]);
        glEnd();

        glBegin(GL_POLYGON);
        glColor3fv(colors[0]);
        glVertex3fv(vertices[0]);
        glColor3fv(colors[3]);
        glVertex3fv(vertices[3]);
        glColor3fv(colors[6]);
        glVertex3fv(vertices[6]); 
        glColor3fv(colors[2]);
        glVertex3fv(vertices[2]);
        glEnd();

        glBegin(GL_POLYGON);
        glColor3fv(colors[1]);
        glVertex3fv(vertices[1]);
        glColor3fv(colors[4]);
        glVertex3fv(vertices[4]);
        glColor3fv(colors[7]);
        glVertex3fv(vertices[7]);
        glColor3fv(colors[5]);
        glVertex3fv(vertices[5]);
        glEnd();

        glBegin(GL_POLYGON);
        glColor3fv(colors[4]);
        glVertex3fv(vertices[4]);
        glColor3fv(colors[3]);
        glVertex3fv(vertices[3]);
        glColor3fv(colors[6]);
        glVertex3fv(vertices[6]);
        glColor3fv(colors[7]);
        glVertex3fv(vertices[7]);
        glEnd();

        glBegin(GL_POLYGON);
        glColor3fv(colors[2]);
        glVertex3fv(vertices[2]);
        glColor3fv(colors[6]);
        glVertex3fv(vertices[6]);
        glColor3fv(colors[7]);
        glVertex3fv(vertices[7]);
        glColor3fv(colors[5]);
        glVertex3fv(vertices[5]);
        glEnd();

        glBegin(GL_POLYGON);
        glColor3fv(colors[0]);
        glVertex3fv(vertices[0]);
        glColor3fv(colors[3]);
        glVertex3fv(vertices[3]);
        glColor3fv(colors[4]);
        glVertex3fv(vertices[4]);
        glColor3fv(colors[1]);
        glVertex3fv(vertices[1]);
        glEnd();
        glFlush();
}

void init()
{

        glClearColor(1.0,1.0,1.0,0);
        glEnable(GL_DEPTH_TEST);
        glOrtho(-960,960,-720,720,-600,600);
}

int main(int argc, char *argv[])
{ 

        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
        glutInitWindowSize(960,720);
        glutInitWindowPosition(0,0);
        glutCreateWindow("Color Cube");
        glutDisplayFunc(display);
        init();
        glutMainLoop();
}

3 个答案:

答案 0 :(得分:1)

您正在通过GLUT_DOUBLE标志请求双缓冲帧缓冲。因此,您有一个正在绘制的后缓冲区,以及一个显示在窗口中的前缓冲区。完成绘图后,必须交换这些缓冲区,以便现在绘制的内容变得可见,并且可以绘制下一帧(不破坏当前可见的图像)。只需通过glFlush();调用替换display()函数末尾的glutSwapBuffers(),您的呈现就会变得可见。

答案 1 :(得分:0)

尝试使用push和pop矩阵 恩。

glPushMatrix();
//your cube's code
glPopMatrix();
glFlush();

答案 2 :(得分:0)

如果要查看多维数据集的多个侧面,则需要旋转多维数据集。

查找glRotate()函数,该函数将当前矩阵乘以旋转。应用此旋转(在绘制立方体之前)将有效地围绕指定的轴旋转您的立方体一个角度。