Skip to main content
added 78 characters in body
Source Link
CroCo
  • 255
  • 1
  • 2
  • 10

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.

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.

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.

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.

Source Link
CroCo
  • 255
  • 1
  • 2
  • 10

Do I need vertex array object in this case?

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.

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.