2
votes

I am currently learning GFLW and OpenGL in C++, and I have come across a very weird error that I really can't figure out why it is occurring. I have two Vertex Buffers, one contains the data for a triangle, and the other contains data for a cube. When I create the Vertex Buffer for the Triangle and draw it without creating the Vertex Buffer for the Cube, it works perfectly and draws to the window the expected triangle. However, if I create the second Vertex Buffer and draw only the triangle again it becomes distorted.

Output when only the triangle Vertex Buffer is created: enter image description here

Output when both the triangle Vertex Buffer and Cube Vertex Buffer are created (cube vertex buffer is never used)

enter image description here

Vertex Buffer's constructor

    VertexBuffer(const GLvoid *data, GLsizeiptr size, GLenum mode, GLsizei count, GLsizei stride, ShaderInterface *shader, GLvoid *positionOffset, GLvoid *normalOffset);

Creating the two vertex buffers:

    VertexBuffer *vertexBuffer=new VertexBuffer(vertices,sizeof(vertices),
                                            GL_TRIANGLES,
                                            3,
                                            sizeof(GL_FLOAT)*3,
                                            shaderArray->at(0),
                                            NULL,
                                            NULL);
vertexBufferArray->push_back(vertexBuffer);
VertexBuffer *cubeVertexBuffer=new VertexBuffer(cubeVertices,
                                                sizeof(cubeVertices),
                                                GL_TRIANGLES,
                                                36,sizeof(VertexDataPN),
                                                shaderArray->at(1),
                                                (GLvoid*)(offsetof(VertexDataPN,positionCoordinates)),
                                                (GLvoid*)(offsetof(VertexDataPN,normalCoordinates)));
//vertexBufferArray->push_back(cubeVertexBuffer);

And the contents of the constructor for my Vertex Buffer class:

glGenBuffers(1,&vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER,vertexBufferID);
glBufferData(GL_ARRAY_BUFFER,size,data,GL_STATIC_DRAW);

Also, I have already checked that the vertexBufferID's are different for both of them. The Cube Vertex Buffer is never used or referenced to after it gets created. If anyone knows why this is occurring please let me know.

1

1 Answers

1
votes

OpenGL is state-based, which means that objects that change OpenGL's internal state during their initialization can affect its behaviour even if they aren't subsequently used.

You are calling glBindBuffer in your vertex buffer constructor, which means that unless you call glBindBuffer later, the most recently bound buffer (the cube) will remain bound to GL_ARRAY_BUFFER and will be used during rendering.

You need to change your drawing code to call glBindBuffer with the buffer that you intend to use (e.g. the triangle one), before any calls to glVertexAttribPointer.