0
\$\begingroup\$

In the case of having the vertex attributes (i.e. position, color, etc) hardcoded in the shaders, why do I need to explicitly create a vertex array object (VAO)? If I remove the VAO, nothing is drawn. With VAO, the below code works fine and a single point is drawn successfully.

const char *vertex_shader_source =
"#version 330 core\n"
"void main()\n"
"{\n"
"    gl_Position = vec4(0.0,0.0,0.0,1.0);\n"
"}\n";

const char *fragment_shader_source = 
"#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"    color = vec4(1.0,0.0,0.0,1.0);\n"
"}";


main()
{
    prepare window
    prepare shader program

    unsigned int VAO;
    glGenVertexArrays(1,&VAO);
    
    while( window open )
    {
        .
        .
        .
        glClearColor(0.2f,0.3f,0.3f,1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shaderProgram);
        glBindVertexArray(VAO); <-- is this required?
        glDrawArrays(GL_POINTS,0,1);
        .
        . 
        .
    }
}

OpenGL 3.3, Ubuntu OS.

\$\endgroup\$
2
  • \$\begingroup\$ If I remove the VAO, nothing is drawn - that seems to be your answer. If you pass 0 points to be drawn, you can not change their coordinates to be gl_Position = vec4(0.0,0.0,0.0,1.0) .. \$\endgroup\$
    – Kromster
    Commented Jan 5 at 21:02
  • \$\begingroup\$ @Kromster I don't understand your comment. \$\endgroup\$
    – CroCo
    Commented Jan 7 at 18:13

1 Answer 1

2
\$\begingroup\$

Short and sweet: The Vertex Shader is run once for each vertex. If you remove the VAO you are passing zero vertices so the shader is run zero times.

There is a way to still draw it without passing any vertices and that's by generating them dynamically in a geometry shader before the normal shaders.

\$\endgroup\$
2
  • \$\begingroup\$ I thought when glDrawArrays is called, the shader program gets executed in GPU. \$\endgroup\$
    – CroCo
    Commented Jan 7 at 18:12
  • 1
    \$\begingroup\$ @CroCo Well like I mentioned in my answer: The Vertex Shader is run once for each vertex. If you remove the VAO you are passing zero vertices so the shader is run zero times. \$\endgroup\$
    – Charanor
    Commented Jan 7 at 18:35

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.