Help on glMapBuffer*** Calls

Started by
2 comments, last by cgrant 9 years, 6 months ago

Edited ignore everything, I spoke too soon smile.png

Final code just in case it help someone


GLvoid *p = glMapBufferRange(GL_ARRAY_BUFFER, 0, FLOATS_PER_QUAD * sizeof(GLfloat), GL_MAP_WRITE_BIT);
 
if (p == NULL)
{
std::cout << "Null ptr: " << glGetError()<< std::endl;
glUnmapBuffer(GL_ARRAY_BUFFER);
return;
}
 
((GLfloat*)p)[0] = 0.0f;
((GLfloat*)p)[1] = 0.0f;
((GLfloat*)p)[2] = 0.0f;
 
((GLfloat*)p)[3] = 0.0f;
((GLfloat*)p)[4] = 32.0f;
((GLfloat*)p)[5] = 0.0f;
 
((GLfloat*)p)[6] = 32.0f;
((GLfloat*)p)[7] = 32.0f;
((GLfloat*)p)[8] = 0.0f;
 
((GLfloat*)p)[9] = 32.0f;
((GLfloat*)p)[10] = 0.0f;
((GLfloat*)p)[11] = 0.0f;
 
glUnmapBuffer(GL_ARRAY_BUFFER);
 
glUseProgram(shaderProgramID);
glUniformMatrix4fv(matrixUniform, 1, GL_FALSE, uniformMatrix->matrix);
 
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
glBindVertexArray(NULL);
 
glUseProgram(NULL);
 

---------------------------------------------------------------

Edit2:

My real question:

What am I doing wrong here? I keep getting back a NULL pointer to my buffer. glGetError() gives me back a 1282 error code [ GL_INVALID_OPERATION ]


 
glBindBuffer(GL_ARRAY_BUFFER, vbo);
GLvoid *p = glMapBufferRange(GL_ARRAY_BUFFER, 0, FLOATS_PER_QUAD * sizeof(GLfloat), GL_WRITE_ONLY);
 
if (p == NULL)
{
std::cout << "Null ptr: " << glGetError()<< std::endl;
glUnmapBuffer(GL_ARRAY_BUFFER);
return;
}
 
((GLfloat*)p)[0] = 0.0f;
((GLfloat*)p)[1] = 0.0f;
((GLfloat*)p)[2] = 0.0f;
 
((GLfloat*)p)[3] = 0.0f;
((GLfloat*)p)[4] = 32.0f;
((GLfloat*)p)[5] = 0.0f;
 
((GLfloat*)p)[6] = 32.0f;
((GLfloat*)p)[7] = 32.0f;
((GLfloat*)p)[8] = 0.0f;
 
((GLfloat*)p)[9] = 32.0f;
((GLfloat*)p)[10] = 0.0f;
((GLfloat*)p)[11] = 0.0f;
 
glUnmapBuffer(GL_ARRAY_BUFFER);
 

-------------------------------------------------------------------------------------

Hey everyone, I was wondering if someone could better explain the glMapBuffer*** calls.

I understand that glMapBuffers*** returns a void pointer to the buffer, but the thing I don't understand is how do I map the buffer?

All the examples I see do a memcpy of sorts to place data into it.

EG:


void * vbo = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(vbo_buffer, vertexs, vertex_size);
glUnmapBuffer(GL_ARRAY_BUFFER);

But is there way to directly place data into the buffer? Through pointer arithmetic or something?

EG [I know this is wrong, but somehting like]:


void * vbo = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
 
*(vbo+1) = 0.0f;
*(vbo+2) = 0.0f;
*(vbo+3) = 0.0f;
*(vbo+4) = 32.0f;
*(vbo+4) = 0.0f;
//etc....
 
glUnmapBuffer(GL_ARRAY_BUFFER);
Advertisement

The glMapBufferRange function has slightly different access flags from glMapBuffer to allow more fine-grained control. You should be using GL_MAP_WRITE_BIT instead of GL_WRITE_ONLY. See https://www.opengl.org/sdk/docs/man/html/glMapBufferRange.xhtml :)

You should consider using the debug output functions if you're able to use it, which should give you more specific error messages in actual text and makes debugging a hell of a lot easier!

Xycaleth already mentioned you are using the flags wrong, check out his links.

To increase readability you can also static-cast the returned pointer only once, and access him like a float-array.

For advanced information about buffer transfers I can recommend this GL 4.2 article:
http://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-AsynchronousBufferTransfers.pdf

Also, you didn't post the code that created the buffer you are trying to map.

This topic is closed to new replies.

Advertisement