0
votes

When I tried to use vertex buffer objects (VBO) to my simple opengl program, I get segmentation fault. First of all let me share part of code which causes seg fault.

   //GLuint objectVerticesVBO; type of my VBO
    vector<glm::vec4> objectVertices; // I fill this vector somewhere in the code 
    glGenBuffers(1, &objectVerticesVBO);
    glBindBuffer(GL_ARRAY_BUFFER, objectVerticesVBO);
    glBufferData(GL_ARRAY_BUFFER,  sizeof(glm::vec4)* objectVertices.size(), &objectVertices, GL_STATIC_DRAW );
    glVertexPointer(3, GL_FLOAT, 0,0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

Now, here is some output that I've printed out:

cout<< sizeof(glm::vec4)<<endl;
16
cout<< objectVertices.size()<<endl;
507

Is there anyone who knows what causes to segmentation fault ?

Another question is that :

When I add this part also to my code in the renderScene function, I get only an empty screen.

   glUseProgram(gProgramShader);
    glBindBuffer(GL_ARRAY_BUFFER, objectVerticesVBO);
    glVertexPointer(3, GL_FLOAT, 0, 0); 
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, objectIndicesVBO);
    glDrawElements(GL_TRIANGLES, objectVertices.size(), GL_UNSIGNED_INT,  0);

What would be the problem ?

1
While it's not an obvious duplicate from just looking at the question, it's the same problem as stackoverflow.com/q/24048666/3530129.Reto Koradi

1 Answers

1
votes

Replace & objectVertices with & objectVertices[0] or objectVertices.data() (C++11), in the glBufferData call.

The std::vector container elements are contiguous in memory, not the container object itself.