Re: Requesting help with VBO's
Posted by
Wade Walker on
URL: https://forum.jogamp.org/Requesting-help-with-VBO-s-tp4027348p4027380.html
Braindrool wrote
ByteBuffer bytebuffer = gl.glMapBuffer( GL.GL_ARRAY_BUFFER, GL2.GL_WRITE_ONLY );
FloatBuffer floatbuffer = bytebuffer.order( ByteOrder.nativeOrder() ).asFloatBuffer();
Mind elaborating on glMapBuffer a bit? I see several C tutorials. Another question would be, how would I just use my FloatBuffer parameter? It would be more convenient for me.
glMapBuffer() is needed because to write to a VBO using the CPU, the VBO must be in system memory, but to render a VBO with your GPU, the VBO must be in your graphics card's memory. On a desktop PC, those two memories are physically separate, and the only communication between them is via the PCI bus. So (naively speaking) glMapBuffer() copies the VBO from graphics card memory into system memory, then returns a pointer to it that the CPU can use (the pointer is inside the returned ByteBuffer). Then bytebuffer.order( ByteOrder.nativeOrder() ).asFloatBuffer() re-wraps that pointer so you can write floats to it from Java. Finally glUnmapBuffer() flushes the system-memory copy of the VBO back out to graphics card memory so the GPU can render it.
This picture may be a bit more complex in reality, since the OpenGL driver can try to optimize memory traffic over the PCI bus by not copying data that hasn't changed (so it may not copy the VBO back from the graphics card, it may just keep a local copy). The driver can also batch up bus operations, so they may not happen right when you make the OpenGL calls. And finally, on a mobile platform like a cell phone or tablet, the system and GPU memories are not physically separate, so there's no bus flushing at all, though the driver may still have to keep CPU and GPU data in separate address spaces.
Braindrool wrote
And to make sure I have this understood correctly. Use an int to create a VBO handle, generate VBO, allocate space, map data, return the VBO handle as a drawing object. Yes?
glGenBuffers() - create VBO "handle" (really just an integer index to some driver-maintained list of VBOs)
glBufferData() - "allocates space" (exactly where and how is left up to the OpenGL driver)
glMapBuffer() - get system pointer to VBO
glUnmapBuffer() - flush VBO changes out to graphics card
plus:
glBindBuffer() - tells OpenGL which VBO subsequent OpenGL calls will refer to
All these calls are documented in detail at
http://www.opengl.org/, though not in a very beginner-friendly form.