Cant draw multiple objects in opengl

前端 未结 1 967
傲寒
傲寒 2021-02-10 08:54

I cant work out why the following code doesn\'t work, I am attempting to draw 2 shapes, a triangle that is coloured red and a cube that is multi coloured, by themselves they dra

1条回答
  •  失恋的感觉
    2021-02-10 09:59

    I see you are trying to use VAOs but you create only one.Also why do you prepare:

    glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,(void*)0);

    glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, colourBuffer); glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,0,(void*)0);

    and delete:

    glDeleteBuffers(1, &vertexBuffer); glDeleteBuffers(1, &vertexBuffer2); glDeleteBuffers(1, &colourBuffer); glDeleteBuffers(1, &colourBuffer2);

    your VAOs on each render call?The whole purpose of VAOs is to init all the needed buffers and attributes once and then bind only the VAO itself for the actual rendering.Do it on the initialization setup and delete only when you don't need those objects anymore.Here is a good example how it is done properly .

    What you should do is this: Initiate your VAOs in some init method only once.Then in render loop :

       glBindVertexArray(m_VAO1);
    
       ///draw geometry here
    
        glBindVertexArray(0);
    

    Do this for each geometry you want to render.

    After deeper review of your code I see you do this :

    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);
    

    So you do bind VAO.But you create and bind only one!You need to create one per renderable object,Attach to it all required buffers and then use them in the render loop as I explained above.

    0 讨论(0)
提交回复
热议问题