sddvxd
sddvxd

Reputation: 35

Why is a triangle not displayed?

I'm trying to draw a triangle in GLFW:

//... init GLFW, compiling and linking shaders, init float array with vertices
//-- all success
GLuint VBO, VAO;
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexTriangle), vertexTriangle, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);

glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

//scene loop
while(!glfwWindowShouldClose(window))
{
    glClear(GL_COLOR_BUFFER_BIT);
    glUseProgram(shaderProgram);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glfwSwapBuffers(window);
    glfwPollEvents();
}

After I start my program, it does not draw a triangle. It only draws an empty gray screen.

Upvotes: 0

Views: 79

Answers (1)

Rabbid76
Rabbid76

Reputation: 210877

The vertex array object has to be bound when you do the draw call:

glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);

In your code the VAO is not bound at this point, because the default vertex array object (0) is bound by glBindVertexArray(0);, right before the main loop.

The information about the vertex attributes is stored int the vertex array object. glDrawArrays uses the attribute information stored in the currently bound vertex array object to perform the drawing.

See OpenGL 4.6 API Core Profile Specification - 10.3. VERTEX ARRAYS; page 347

The currently bound vertex array object is used for all commands which modify vertex array state, such as VertexAttribPointer and EnableVertexAttribArray; all commands which draw from vertex arrays, such as DrawArrays and DrawElements;

Upvotes: 1

Related Questions