glDrawElements不会渲染任何东西

时间:2014-11-09 13:46:43

标签: c++ opengl shader

我想绘制简单的方块。首先我使用glDrawArrays,但现在我想将其更改为glDrawElements。我阅读了很多教程,但由于某种原因它没有渲染任何东西。

渲染器类:

class Renderer_t {
private:
    ...
    glm::mat4 projectionMatrix; // Store the projection matrix
    glm::mat4 viewMatrix; // Store the view matrix
    glm::mat4 modelMatrix; // Store the model matrix

    unsigned int vaoID[1]; // Our Vertex Array Object
    unsigned int vboID[3]; // Our Vertex Buffer Object
    ...
};

场景初始化:

Renderer_t::Renderer_t(SDL_Window* window): scene(nullptr), width(800), height(600) {
    LOG(info) << "Renderer_t constructor";

    gl = SDL_GL_CreateContext(window);

    glbinding::Binding::initialize();

    //Initialize scene
    glClearColor(0.4f, 0.6f, 0.9f, 0.0f);

    shader = new Shader("../assets/shader.vert", "../assets/shader.frag");
    float ratio = width/height;
    projectionMatrix = glm::perspective(60.0f, ratio, 0.1f, 100.f);  // Create our perspective projection matrix

    int vertnum = 4 * 3; //6x
    //Create square
    float* vertices = new float[vertnum];  // Vertices for our square
    float* colors = new float[vertnum]; // Colors for our vertices
    unsigned int* indices = new unsigned int[6];

    indices[0] = 0; indices[0] = 1; indices[0] = 2;
    indices[0] = 2; indices[0] = 3; indices[0] = 0;

    vertices[0] = -0.5; vertices[1] = -0.5; vertices[2] = 0.0; // Bottom left corner
    colors[0] = 1.0; colors[1] = 1.0; colors[2] = 1.0; // Bottom left corner

    vertices[3] = -0.5; vertices[4] = 0.5; vertices[5] = 0.0; // Top left corner
    colors[3] = 1.0; colors[4] = 0.0; colors[5] = 0.0; // Top left corner

    vertices[6] = 0.5; vertices[7] = 0.5; vertices[8] = 0.0; // Top Right corner
    colors[6] = 0.0; colors[7] = 1.0; colors[8] = 0.0; // Top Right corner

    vertices[9] = 0.5; vertices[10] = -0.5; vertices[11] = 0.0; // Bottom right corner
    colors[9] = 0.0; colors[10] = 0.0; colors[11] = 1.0; // Bottom right corner
/*
    vertices[12] = -0.5; vertices[13] = -0.5; vertices[14] = 0.0; // Bottom left corner
    colors[12] = 1.0; colors[13] = 1.0; colors[14] = 1.0; // Bottom left corner

    vertices[15] = 0.5; vertices[16] = 0.5; vertices[17] = 0.0; // Top Right corner
    colors[15] = 0.0; colors[16] = 1.0; colors[17] = 0.0; // Top Right corner
*/
    glGenVertexArrays(1, &vaoID[0]); // Create our Vertex Array Object
    glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object so we can use it

    glGenBuffers(3, &vboID[0]); // Generate our Vertex Buffer Objects

    glBindBuffer(GL_ARRAY_BUFFER, vboID[0]); // Bind our Vertex Buffer Object
    glBufferData(GL_ARRAY_BUFFER, vertnum * sizeof(GLfloat), vertices, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
    glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
    glEnableVertexAttribArray(0); // Enable our Vertex Array Object

    glBindBuffer(GL_ARRAY_BUFFER, vboID[1]); // Bind our second Vertex Buffer Object
    glBufferData(GL_ARRAY_BUFFER, vertnum * sizeof(GLfloat), colors, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW
    glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
    glEnableVertexAttribArray(1); // Enable the second vertex attribute array

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID[2]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(GLuint), indices, GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)2, 3, GL_INT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(2);

    glBindVertexArray(0); // Disable our Vertex Buffer Object

    delete[] vertices; // Delete our vertices from memory
    delete[] colors; // Delete our vertices from memory
    delete[] indices;

    LOG(info) << "Renderer_t constructor done";
}

渲染:

void Renderer_t::render() {
    LOG(info) << "Renderer_t.render()";

    glViewport(0, 0, width, height); // Set the viewport size to fill the window
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Clear required buffers

    viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.f)); // Create our view matrix
    modelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(1.0f));  // Create our model matrix


    shader->bind(); // Bind our shader

    int projectionMatrixLocation = glGetUniformLocation(shader->id(), "projectionMatrix"); // Get the location of our projection matrix in the shader
    int viewMatrixLocation = glGetUniformLocation(shader->id(), "viewMatrix"); // Get the location of our view matrix in the shader
    int modelMatrixLocation = glGetUniformLocation(shader->id(), "modelMatrix"); // Get the location of our model matrix in the shader

    glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, &projectionMatrix[0][0]); // Send our projection matrix to the shader
    glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &viewMatrix[0][0]); // Send our view matrix to the shader
    glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &modelMatrix[0][0]); // Send our model matrix to the shader

    glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object

//  glDrawArrays(GL_TRIANGLES, 0, 6); // Draw our square
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID[2]);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
    glBindVertexArray(0); // Unbind our Vertex Array Object

    shader->unbind(); // Unbind our shader
    LOG(info) << "Renderer_t.render() done";
}

我也使用着色器: shader.vert

version 130

uniform mat4 projectionMatrix;  
uniform mat4 viewMatrix;  
uniform mat4 modelMatrix;  

in vec3 in_Position;
in vec3 in_Color;

out vec3 pass_Color;

void main(void)
{
     gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_Position, 1.0);
     pass_Color = in_Color;
}

shader.fraq

#version 130

in vec3 pass_Color;

out vec4 out_Color;

void main(void)
{
      out_Color = vec4(pass_Color, 1.0);
}

2 个答案:

答案 0 :(得分:1)

您尝试将ELEMENT_ARRAY_BUFFER绑定到不同的着色器

glVertexAttribPointer((GLuint)2, 3, GL_INT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(2);

索引缓冲区不必绑定到着色器,因为它们不直接用作输入。除此之外,您的着色器只有两个内部变量(最可能编号为0和1),因此使用2将永远不会起作用。

另一个提示:

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboID[2]);
你的render()中的

是没有必要的,因为在初始化期间绑定已经存储在vao中了。但这不应影响输出。

答案 1 :(得分:1)

您知道,包含元素的三角形列表:0,1,2 0,2,3glDrawArrays(GL_TRIANGLE_FAN, 0, 4)相同。你可以在这里使用三角扇,而不必使用索引。

您的实际问题是您继续将元素数组中的每个元素写入[0]

现在你的代码是:

indices[0] = 0; indices[0] = 1; indices[0] = 2;
indices[0] = 2; indices[0] = 3; indices[0] = 0;

但是,要正常运行,需要阅读:

indices[0] = 0; indices[1] = 1; indices[2] = 2;
indices[3] = 2; indices[4] = 3; indices[5] = 0;  // This is equivalent to 0, 2, 3

BDL答案中的所有内容也是您应该注意的重要事项