
VBO Example
Posted Friday, 27 June, 2008 - 01:08 by JTalton inI wanted to play with vertex arrary buffers so I took a look at the VBO example code.
It only setup a single vertex array and I wanted to know how to specify normals or texture coordinates
I updated it to use a vertex array, normal array, texture array, and color array.
I then started wondering about dynamically updating the VBO so I added some methods to update the VBO from both the source array with the BufferData call and with getting and modifying the data from the VBO using MapBuffer.
I have uploaded the source if anyone is interested or if it would be good to have in the OpenTK examples.
As for performance, what do people suggest for updating an existing VBO? Copying over new data with BufferData or updating the data with MapBuffer? With MapBuffer you get an IntPtr back so I ended up wrapping it in unsafe so that I could work with the data.
| Attachment | Size |
|---|---|
| T08_VBO.cs | 18.33 KB |
| Cube.cs | 2.64 KB |


Comments
Re: VBO Example
Nice example there! However, I have a question related to VBOs. How would I go about specifying multiple normals (hard edges) or texcoord sets per vertex while using DrawElements? So far I've come up with the idea of specifying the given vertex multiple times like so:
so V1 is bound with two different normals during its processing with DrawElements. Any other thoughts?
Re: VBO Example
You are correct in that you have to specify the vertex position multiple times, each with their own corresponding normal.
Example:
Cube without hard edges would only need 8 vertices.
Cube with hard edges would need 4 * 6 = 24 vertices so that they could have corresponding normals for each vertex.
Of course this only applies to hard edges.
The same also applies to texture coordinates where the face vertices happen to not be sharing the same texture coordinate.
Re: VBO Example
I didn't search for this technique it just came to me. So, I was thinking someone smarter might know more.
It isn't perfect but I'm left with no other choice, I guess.
Thanks for the help.
Re: VBO Example
Hello !
I started to use opentk and vbo and I tried this example too. But I really don't know how to add for loading normals, colors and textures. Could anyone help me ? Thanks !
Re: VBO Example
The code in the opening post of this thread shows how to specify normals, textures, etc. A simple class with the necessary functionality distilled:
For better performance, you could also create an element array and draw with
DrawElementsand that's it, more or less.Edit: Updated the code.
Edit 2: Incorporated fixes from post below.
Re: VBO Example
Thank you for the code ! It works very well, with minor modifications
( public static readonly Stride = Marshal.SizeOf(default(Vertex)); ---> public static readonly int Stride = Marshal.SizeOf(default(Vertex));
GL.NormalPointer(3, VertexPointerType.Float, Vertex.Stride, new IntPtr(Vector3.SizeInBytes)); ---> GL.NormalPointer(3, NormalPointerType.Float, Vertex.Stride, new IntPtr(Vector3.SizeInBytes)); )
But I have one more problem with GL.DrawArrays(BeginMode.TriangleStrip, 0, data.Length);
It draws the same thing as BeginMode.Triangles and I need triangleStrip mode.
Cheers !
Re: VBO Example
Thanks, I wrote the code from memory which explains the mistakes. I've updated it with your fixes, in case someone reads it in the future.
I'm not sure I understand what the problem with TriangleStrip is. Can you provide an example (or even better some screenshots)?
Re: VBO Example
Hey Fiddler! Thanks for condensing the VBO code. I looks useful but I'm having a hard time to get it working. After fixing some compiler errors (I assume the API changed between then and 1.0.0 RC1) I can get my application to run but nothing is drawn to the screen from the VBOs. There are no errors from OpenGL or anywhere else. Here is my 'fixed' version:
One slight difference is that my Vertex struct has the Serializable attribute. Otherwise, it's the same.
Can you verify this as correct or suggest anything I might be doing wrong?