0

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 ?

  • While it's not an obvious duplicate from just looking at the question, it's the same problem as http://stackoverflow.com/q/24048666/3530129. – Reto Koradi Dec 28 '14 at 18:28

1 Answers1

1

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.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
  • @Brett_Hale I've editted my question could you look at it one more time ? – kemal gunes Dec 28 '14 at 18:22
  • @kemalgunes - you need to setup the buffers, a VAO, and the vertex attributes before `glUseProgram` - assuming the GL program is correct. – Brett Hale Dec 28 '14 at 18:39
  • @Brett_Hale, GL program is correct, I had done similar things before as I did like that, without using VAO, I guess my problem is different – kemal gunes Dec 28 '14 at 18:44
  • @Brett_Hale in my init function I setup all vertex buffer, indices buffer and normal buffer btw – kemal gunes Dec 28 '14 at 18:46