blackbee's picture

VBO problem

HI.
I have a little problem with VBO. When I display one object there is no problem everything works fine (In code till the comment). But when I want another object I GenBuffer, BindBuffer again and call BufferData with other data and whichever vbo I bind before drawning it draws only the last one. (In data I have triangle near left edge, in data2 near right edge) With this code I always get the right side triangle, from the secon buffer. I'm new to VBO so dont bite :D

GL.GenBuffers(1, out vbo);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
 
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.NormalArray);
 
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(data.Length * Marshal.SizeOf(typeof(Vertex))), data, BufferUsageHint.StaticDraw);
 
GL.VertexPointer(3, VertexPointerType.Float, Marshal.SizeOf(typeof(Vertex)), 32);
GL.NormalPointer(NormalPointerType.Float, Marshal.SizeOf(typeof(Vertex)), 20);
 
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
////======== copy from above but with new data
GL.GenBuffers(1, out vbo2);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo2);
 
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.NormalArray);
 
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(data2.Length * Marshal.SizeOf(typeof(Vertex))), data2, BufferUsageHint.StaticDraw);
 
GL.VertexPointer(3, VertexPointerType.Float, Marshal.SizeOf(typeof(Vertex)), 32);
GL.NormalPointer(NormalPointerType.Float, Marshal.SizeOf(typeof(Vertex)), 20);
 
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

And display:

GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);    // it always display triangle from second bufferdata why O.o?
GL.DrawArrays(BeginMode.Triangles, 0, 3);

Data structue looks like this but it doesnt really matter

[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
	public Vector2 textcord;
	public Vector3 color;
	public Vector3 normals;
	public Vector3 vertex;
}

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

First of all, you should add Pack=1 to your StructLayout attribute, otherwise the various fields may be aligned differently on different platforms. (For instance, 'normals' won't be at offset 20 on platforms with 8byte alignment).

Other than that, the code looks correct to me. Just make sure you bind the correct vbo id before GL.DrawArrays.

zahirtezcan's picture

i guess you need to set pointers before each draw call such as

bind vbo
set vertex pointer
set normal pointer
draw

bind vbo2
set vertex pointer
set normal pointer
draw

blackbee's picture

thanks both of you. Zahirtezcan you were right! But is this always necessary? Shoudn't Binding remember pointers as well? It's couple more functions for render loop (texture and color ptr may be there to).

zahirtezcan's picture

you can use attribpointers + vertex array objects if you want to... check here.