2

I'm confused on the concept of buffers. I understand the very basics with glBufferData and glBufferSubData. Using glBufferSubData before a render function in your main loop you can use a offset and size parameter to store multiple model objects in a vertex buffer and a index buffer. Then at render you bind the single vertex buffer and call glDrawElements with the correct offsets and sizes to render multiple objects.

Is this the case with glMapBuffer? Or am I suppose to call glMapBuffer to link vertex and index data at render, then draw?

GLuint vertexArrayId;
GLuint verticesBufferId;
GLuint indicesBufferId;

void setupBuffers() {
    glGenVertexArrays(1, &vertexArrayId);
    glBindVertexArray(vertexArrayId);
    glGenBuffers(1, &verticesBufferId);
    glGenBuffers(1, &indicesBufferId);

    glBindBuffer(GL_ARRAY_BUFFER, verticesBufferId);
    glBufferData(GL_ARRAY_BUFFER, VERTICES_SIZE, NULL, GL_STATIC_DRAW);
    // glMapBuffer stuff here 

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBufferId);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, INDICES_SIZE, NULL, GL_STATIC_DRAW);
    // glMapBuffer stuff here
}

void render() {
    glBindBuffer(GL_ARRAY_BUFFER, verticesBufferId);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)((sizeof(GLfloat) * 0)));

    glEnableVertexAttribArray(0);
    // glDrawElements stuff here
    glDisableVertexAttribArray(0);

    // Model, View, Projection transformations here
    // glUniformMatrix4fv 
}

1 Answer 1

2

You can use glMapBuffer to upload data from ram to the GPU:

    glBindBuffer(GL_ARRAY_BUFFER, verticesBufferId);
    void *data = glMapBuffer( GL_ARRAY_BUFFER, ... );
    // copy vertex data from instance 
    ::memcpy( data, vertices, vertexSize );
    ...
    glUnmapBuffer( ... );

For instance when you want to upload vertex- and index-data from a model. You can use the pointer returned from glMapBuffer like a raw-c-pointer ( just for convenience )

Keep in mind: glMapBuffer is not fast, so try to avoid using it during your render loop. After calling the glUnMap the data will be transfered to the GPU.

There are much better ways when you have to upload data from the CPU to the GPU periodically like using using Uniform-Blocks. I found this blog-post, where the mapping is explained really well ( with all its drawbacks ): Mapping in OpenGL

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

5 Comments

Thanks will check out your link. For learning purposes, could show an explain the solution to rendering multiple object models (1 array of vertex, 1 array of index data PER object)? If I shouldn't call glMapBuffer during render to point to the data, how to I tell glDrawElements what to draw?
you can take a look onto this tutorial: mbsoftworks.sk/… The trick is: uload the data once before the rendering. During the rendering just say which buffers shall be used and call glDrawArray.
But doesn't that mean I would have 2 VBO's per object? 1 VBO for vertex and 1 for index? I thought it was better to have 1 large VBO for each data type which store many objects
glMapBuffer is not necessarily slow either. What really slows it down is the fact that you are modifying memory that is probably needed to finish a frame that the GPU is currently working on. Mapping and unmapping isn't what you should avoid, it's that modify-use cycle that needs to be avoided; often by establishing multiple duplicate buffers or telling GL to invalidate regions of the buffer that do not need to persist.
As I said here: Keep in mind: glMapBuffer is not fast, so try to avoid using it during your render loop. After calling the glUnMap the data will be transfered to the GPU.

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.