OpenGL glDrawElements没有颜色

时间:2014-03-07 13:50:32

标签: opengl rendering

我正在尝试使用我写的着色器使用OpenGL绘制一个立方体,我可以看到形状但没有颜色。

#define EDGE_LENGTH 0.6
enum VertexAttrib
{
    ATTRIB_VERTEX = 0, ATTRIB_COLOR
};
int main()
{
    GLfloat cubeAttribs[] =
    {
        -EDGE_LENGTH / 2,   EDGE_LENGTH / 2,    -EDGE_LENGTH / 2,   // 0 front left top vertex
        1.0f,   0.0f,   0.0f,   // red
        -EDGE_LENGTH / 2,   -EDGE_LENGTH / 2,   -EDGE_LENGTH / 2,   // 1 front left bottom vertex
        1.0f,   1.0f,   0.0f,   // yellow
        EDGE_LENGTH / 2,    -EDGE_LENGTH / 2,   -EDGE_LENGTH / 2,   // 2 front right bottom vertex
        0.0f,   1.0f,   0.0f,   // green
        EDGE_LENGTH / 2,    EDGE_LENGTH / 2,    -EDGE_LENGTH / 2,   // 3 front right top vertex
        0.0f,   0.0f,   1.0f,   // blue
        EDGE_LENGTH / 2,    EDGE_LENGTH / 2,    EDGE_LENGTH / 2,    // 4 back right top vertex
        1.0f,   0.0f,   1.0f,   // purple
        EDGE_LENGTH / 2,    -EDGE_LENGTH / 2,   EDGE_LENGTH / 2,    // 5 back right bottom vertex
        1.0f,   0.5f,   0.2f,   // orange
        -EDGE_LENGTH / 2,   -EDGE_LENGTH / 2,   EDGE_LENGTH / 2,    // 6 back left bottom vertex
        1.0f,   1.0f,   1.0f,   // white
        -EDGE_LENGTH / 2,   EDGE_LENGTH / 2,    EDGE_LENGTH / 2,    // 7 back left top vertex
        0.0f,   1.0f,   1.0f    // cyan
    };
    GLushort cubeIndices[] =
    {
        0, 1, 2, 3, // front face
        3, 2, 5, 4, // right face
        4, 5, 6, 7, // back face
        7, 6, 1, 0, // left face
        0, 3, 4, 7, // top face
        6, 5, 2, 1  // bottom face
    };
/* create window and make GL contex */
    ... ...
/* create shader program and use the program */
    ... ...
    GLuint vertexArrayObject;
    GLuint vertexBufferObject;
    GLuint indexBuffer;

/* create vertex array object and bind to it */
    glGenVertexArrays(1, &vertexArrayObject);
    glBindVertexArray(vertexArrayObject);
/* create vertex buffer object */
    glGenBuffers(1, &vertexBufferObject);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
/* enable Attributes */
    glEnableVertexAttribArray(ATTRIB_VERTEX);
    glEnableVertexAttribArray(ATTRIB_COLOR);
/* send vertex position and color data to graphic card memory */
    glBufferData(GL_ARRAY_BUFFER, sizeof(cubeAttribs), cubeAttribs, GL_STREAM_DRAW);
    glVertexAttribPointer( ATTRIB_VERTEX, 3, GL_FLOAT, GL_FALSE,
                        6 * sizeof(GLfloat), (const GLvoid *)0 );
    glVertexAttribPointer( ATTRIB_COLOR, 3, GL_FLOAT, GL_FALSE,
                        6 * sizeof(GLfloat), (const GLvoid *) ( 3 * sizeof(GLfloat) ) );

/* create index buffer object */
    glGenBuffers(1, &indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
/* send index data to graphic card memory */
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), cubeIndices, GL_STREAM_DRAW);

/* bind to the VAO */
    glBindVertexArray(vertexArrayObject);
    while ( !glfwWindowShouldClose(window) )
    {
        glClearColor(0.0f, 0.5f, 1.0f, 1.0f);   CHECK_GL_ERROR
        glClear(GL_COLOR_BUFFER_BIT);   CHECK_GL_ERROR
    /* draw the cube */
        glDrawElements(GL_QUADS, 4 * 6, GL_UNSIGNED_SHORT, (const GLvoid *)0);  CHECK_GL_ERROR
        glFlush();
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    return 0;
}

着色器相当简单。

顶点着色器:

#version 440
layout(location = 0) in vec4 vertexPos;
layout(location = 1) in vec3 vertexColor;
smooth out vec3 vColor;
void main()
{
    gl_Position = vertexPos;
    vColor = vertexColor;
}

片段着色器:

#version 440
smooth in vec3 vColor;
out vec3 outColor;
void main()
{
    outColor = vColor;
}

程序编译并运行,我看到蓝色背景和后方,但没有颜色。我错过了什么?

以前我写过我的“第一个三角形”,它不是基于索引的渲染,它使用glDrawArrays进行渲染,颜色看起来很完美。着色器和程序结构几乎相同,我试图找到差异,但不知道。

1 个答案:

答案 0 :(得分:1)

您正在使用自定义片段输出,您已告诉OpenGL。如果没有它,如果使用兼容性配置文件,它希望片段颜色通过特殊的内置变量gl_FragColor发出。但是使用核心配置文件,您需要使用glBindFragDataLocation将片段着色器输出与renderbuffer目标相关联。在大多数情况下,您只有一个渲染目标0。所以在你的情况下创建程序对象后调用

glBindFragDataLocation(program, 0, "outColor");