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 ?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@Brett_Hale I've editted my question could you look at it one more time ?
@kemalgunes - you need to setup the buffers, a VAO, and the vertex attributes before glUseProgram - assuming the GL program is correct.
@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
@Brett_Hale in my init function I setup all vertex buffer, indices buffer and normal buffer btw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.