opengl 立方体不会渲染

Posted

技术标签:

【中文标题】opengl 立方体不会渲染【英文标题】:opengl cube won't render 【发布时间】:2015-12-09 14:40:41 【问题描述】:

我正在尝试在 OpenGL 上绘制一个立方体。

当我编译并运行这段代码时,我看到的只是一个黑色窗口。我的哪一部分 代码导致这个问题?任何帮助将不胜感激,因为我对 opengl 很陌生 这是我的源文件:

#include <GL/glew.h>
#include <GL/glfw.h>
#include <iostream>
#include <cmath>

void drawCube() 
//vertices of the triangle  
GLfloat vertices[] = 
    -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f,
    1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
    -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
    -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
    -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
    -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f
;

//colors of the triangle
GLfloat colors[] = 
    0.410f, 0.481f, 0.675f,
    0.177f, 0.823f, 0.970f,
    0.604f, 0.516f, 0.611f,
    0.676f, 0.779f, 0.331f,
    0.179f, 0.275f, 0.338f,
    0.041f, 0.616f, 0.984f,
    0.799f, 0.315f, 0.460f,
    0.945f, 0.719f, 0.295f
;

static float alpha = 0;
//attempt to rotate cube
glRotatef(alpha, 0, 1, 0);

/* We have a color array and a vertex array */
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);

/* Send data : 24 vertices */
glDrawArrays(GL_TRIANGLES, 0, 24);

/* Cleanup states */
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
alpha += 1;


;

int main() 
if (!glfwInit()) 
    std::cerr << "Unable to initialize OpenGL!\n";
    return -1;


if (!glfwOpenWindow(1024, 768, //width and height of the screen
    8, 8, 8, 0, //Red, Green, Blue and Alpha bits
    0, 0, //Depth and Stencil bits
    GLFW_WINDOW)) 
    std::cerr << "Unable to create OpenGL window.\n";   
    glfwTerminate();
    return -1;


glfwSetWindowTitle("GLFW Simple Example");

// Ensure we can capture the escape key being pressed below
glfwEnable(GLFW_STICKY_KEYS);


do 
    GLint width, height;
    // Get window size (may be different than the requested size)
    //we do this every frame to accommodate window resizing.
    glfwGetWindowSize(&width, &height);
    glViewport(0, 0, width, height);
    glEnable(GL_DEPTH_TEST); // Depth Testing
    glDepthFunc(GL_LEQUAL);
    glDisable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glfwSwapInterval(1);

    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION_MATRIX);
    glLoadIdentity();

    glMatrixMode(GL_MODELVIEW_MATRIX);
    glTranslatef(0, 0, -5);
    gluPerspective(60, (double)width / (double)height, 0.1, 100);

    drawCube();

    //VERY IMPORTANT: displays the buffer to the screen
    glfwSwapBuffers();

 while (glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS &&
    glfwGetWindowParam(GLFW_OPENED));

glfwTerminate();
return 0;

【问题讨论】:

您的相机正对立方体的一个面,并且您没有透视投影,因为您加载了单位矩阵。查看gluPerspective 我不确定这是否是错误,但您应该在glMatrixMode(GL_MODELVIEW) 及其glLoadIdentity() 之后调用glVertexPointer() 和任何其他特定于绘图的函数。另外你为什么要给glLoadIdentity()打两次电话? glEnable(GL_DEPTH_TEST) 也应该只调用一次(除非你打算一直切换它,这似乎不是你的情况) 这么多顶点,你确定教程里有8个以上? @ebyrob 这可能是为了让立方体的每一面都有不同的颜色,所以顶点位置会重复。 你应该让你的“立方体”旋转,这样你会更清楚地看到发生了什么 【参考方案1】:

问题在于您不使用透视投影,因此您基本上是在正交视图中看到立方体的一个面。

要使用透视,可以使用gluPerspective

glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective(60, (double)width / (double)height, 0.1, 100); // Set up perspective matrix.

【讨论】:

以上是关于opengl 立方体不会渲染的主要内容,如果未能解决你的问题,请参考以下文章

为啥正面剔除用 OpenGL 渲染立方体?

Opengl 3d立方体不渲染

OpenGL:渲染天空盒立方体贴图问题

渲染动态立方体贴图 (OpenGL)

C++/OpenGL - 绘制立方体 VBO

引擎测试:在屏幕上渲染立方体 (OpenGL)