PDA

View Full Version : Trouble getting VBOs to work



FlayedOne
07-15-2009, 02:27 PM
Hello,
After a good few hours of reading and trying, I still can't get VBOs to work. Below are relevant parts of the code, but the code was made in C# using Tao Framework(OpenGl library). keyword "out" means the same as & in C++, and (IntPtr)x is the equivalent of (char *)NULL + x.

//init

Gl.glClearColor(0, 0, 0, 0.5f);

Gl.glViewport(0, 0, width, height);

Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();

Gl.glFrustum(-1, 1, -1, 1, 1, 1000);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();

float[] a = new float[16] {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}; //first 3 floats - coors, last 4 - color
float[] b = new float[16] {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}; // 16 floats in a single vertex = 64 Bytes
float[] c = new float[16] {1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0};

int [] VerticeIndice = new int[3] {0,1,2} //3 ints = 12 bytes



//VBOs creating

Gl.glGenBuffers(1, out VBOadress);
Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, VBOadress);
Gl.glBufferData(Gl.GL_ARRAY_BUFFER, (IntPtr)192, null, Gl.GL_STATIC_DRAW);

Gl.glBufferSubData(Gl.GL_ARRAY_BUFFER, (IntPtr)0, (IntPtr)64, a);
Gl.glBufferSubData(Gl.GL_ARRAY_BUFFER, (IntPtr)64, (IntPtr)64, b);
Gl.glBufferSubData(Gl.GL_ARRAY_BUFFER, (IntPtr)128, (IntPtr)64, c);


Gl.glGenBuffers(1, out IVBOadress);
Gl.glBindBuffer(Gl.GL_ELEMENT_ARRAY_BUFFER, IVBOadress);
Gl.glBufferData(Gl.GL_ELEMENT_ARRAY_BUFFER, (IntPtr)12, null, Gl.GL_STATIC_DRAW);

Gl.glBufferSubData(Gl.GL_ELEMENT_ARRAY_BUFFER, (IntPtr)0, (IntPtr)12, VerticeIndice);



//rendering

Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
Gl.glLoadIdentity();

Gl.glPushMatrix();
Gl.glTranslatef(0, 0, -10);

Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
Gl.glEnableClientState(Gl.GL_COLOR_ARRAY);

Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, VBOadress);

Gl.glVertexPointer(3, Gl.GL_FLOAT, 64, (IntPtr)0);
Gl.glColorPointer(4, Gl.GL_FLOAT, 64, (IntPtr)12);

Gl.glBindBuffer(Gl.GL_ELEMENT_ARRAY_BUFFER, IVBOadress);

Gl.glDrawElements(Gl.GL_TRIANGLES, 3, Gl.GL_UNSIGNED_INT, (IntPtr)0);

Gl.glDisableClientState(Gl.GL_INDEX_ARRAY);
Gl.glDisableClientState(Gl.GL_COLOR_ARRAY);
Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);

Gl.glPopMatrix();
Gl.glFlush();

The code produces a black screen - nothing get's drawn.
Thanks in advance;)

scratt
07-15-2009, 08:11 PM
You have to be careful about the order you enable client states in, and AFAIK should minimize calls to glVertexPointer as that's the one that actually does a lot of configuration prior to drawing.

Also GL_INDEX_ARRAY is *not* what you think it is! ;)
http://www.opengl.org/wiki/Common_Mistakes#glEnableClientState.28GL_INDEX_ARR AY.29

Below is an iPhone tutorial, but it's basically the same for your purposes, just don't use the OES extensions, use the ones you are using already: I simply found it a nice clear and concise tutorial which I stumbled on by accident when I was looking for details on the glMapBuffer extension for the iPhone.

http://playcontrol.net/ewing/jibberjabber/opengl_vertex_buffer_object.html

Here's another tutorial for you..
http://www.songho.ca/opengl/gl_vbo.html

Looking at your code above, specifically the data at the top, it doesn't look like there is much there to draw though... Perhaps I am missing something but your geometry doesn't look right, and the colour data is strange....

FlayedOne
07-16-2009, 01:35 AM
Thank you very much,
without "Gl.glEnableClientState(Gl.GL_INDEX_ARRAY);"(which I didn't even write in the above text, but it was there alright) it works like charm. It probably somehow set the color to (0,0,0).

The data is alrght;) A tri color triangle.

This leads me to another question - I've read that "glvertexpointer"(and similar) and "glenablestate" are deprecated. How do I make it work without them?

zeoverlord
07-16-2009, 03:06 AM
Look no further but here (http://www.flashbang.se/) to see how it's done, you may also check out the tutorial on VAO in the same site.

FlayedOne
07-16-2009, 03:04 PM
Thanks! Great tutorials - very helpfull, and just what I've been looking for. It's damn hard to find up to date material. When are you planning to release part 3?;)

zeoverlord
07-17-2009, 06:57 AM
in a week or so i hope