giawa / opengl4csharp

OpenGL 4 Bindings (partially based on OpenTK) for C#
Other
232 stars 61 forks source link

Updating a VBO? #8

Closed tomb18 closed 7 years ago

tomb18 commented 7 years ago

Hi, I have been following all of your tutorials and using your library with freeglut. It's been great for a C# programmer. However I have now run into something I cannot figure out. I am using your program to draw a pyramid for an example. I would like to update the vertices of the pyramid on a regular basis. I cannot figure out how to do this. I know I have to change the pyramid VBO, but how does one do this? Also how do I call glMapBuffer which I believe I need to use to update only the VBO? I figure if I can solve this, your library will do everything I need. Thanks, Tom

giawa commented 7 years ago

Hey there,

Thanks for the kind comments :) You can definitely update a VBO. Here's an example:

Gl.BindBuffer(verticesVBO);
Gl.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, (IntPtr)(12 * vertices.Length), verticesPinned.AddrOfPinnedObject());

The 'verticesPinned' object is a GCHandle to a Vector3 array. You can build something similar doing something like this:

verticesPinned = GCHandle.Alloc(vertices, GCHandleType.Pinned);

Then you simply update the vertices array and then call BufferSubData. Make sure you bind your buffer first! Hopefully this helps,

Giawa

tomb18 commented 7 years ago

Thanks. Works great!