c - 无法渲染颜色立方体

标签 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();
}

最佳答案

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

关于c - 无法渲染颜色立方体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20028254/

相关文章:

opengl - glGen* 与 glCreate* 命名约定

c++ - boolean 值的大小是多少? 1 位还是 1 字节?

c - OpenGL-4 : How to create a circle and circle of lines using VOA

java - GLSL 不受支持的版本

java - 如何检查两个drawString()字符串是否相交?

c++ - 图片/"most resembling pixel"搜索优化?

使用 C 信号的精密计时器

c - 如何将 ./a.out 的结果存储到文本文件

c - autotools中不同文件的不同实现

r - 了解如何读取设备列表