2014-03-07 116 views
1

我想用OpenGL繪製一個使用我寫的着色器的立方體,我可以看到形狀但沒有顏色。OpenGL glDrawElements沒有顏色

#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

您正在使用您告訴OpenGL的自定義片段輸出。否則,如果使用兼容性配置文件,則希望通過特殊的內置變量gl_FragColor發出碎片顏色。但是使用核心配置文件,您需要使用glBindFragDataLocation將片段着色器輸出與渲染緩衝區目標相關聯。在大多數情況下,您只有一個渲染目標0。所以在你的情況下創建程序對象後,致電

glBindFragDataLocation(program, 0, "outColor"); 
+0

嗨,謝謝你的回覆。這個問題實際上是由我自己的愚蠢造成的。拼寫錯誤導致我的片段着色器沒有鏈接到着色器程序。我發現沒有必要明確指定片段數據位置。不管怎樣,謝謝你 !!! :) – Linlix