Click to See Complete Forum and Search --> : Vertex arrays, cubes and triangles


starkr
April 26th, 2009, 02:37 PM
Hey gurus. I've got a challenge for you, or maybe I'm just retarded and my problem is very simple. Can't figure it out though.

Here's some context to my problem.
Case is that I'm trying to draw a cube using a vertex array, and drawing the sides out by triangles. I want to use triangles rather than quads in order to use the same method for drawing different shapes just changing the number of vertices that glDrawArrays() use.
Thus far I've been using two shapes: 1. 3D triangle. 8 triangle faces, 24 vertices. 2. 3D cube. 12 triangle faces, 36 vertices. My method checks the type of shape I want to draw and determines how many vertices it contains. This way I should be able to use the same set of vertex array calls to draw the shape, just giving glDrawArrays() the number of vertices the current shape actually contains. This works fine for the triangles, but leaves my cubes a complete mess. Thus I've written the code below, which should produce a cube made up of 12 triangles. I've double checked all the vertices and they check out. Thus I believe my trouble lies somewhere in the actual drawing below my array definition.

GLfloat testArray2[] = {
-5,-5,5, 5,-5,5, 5,5,5,
-5,-5,5, 5,5,5, -5,5,5

-5,5,5, 5,5,5, 5,5,-5,
-5,5,5, 5,5,-5, -5,5,-5,

5,-5,5, 5,-5,-5, 5,5,-5,
5,-5,5, 5,5,-5, 5,5,5,

-5,-5,5, 5,-5,-5, 5,-5,5,
-5,-5,5, -5,-5,-5, 5,-5,-5,

-5,-5,5, -5,5,-5, -5,-5,-5,
-5,-5,5, -5,5,5, -5,5,-5,

-5,-5,-5, 5,5,-5, 5,-5,-5,
-5,-5,-5, -5,5,-5, 5,5,-5};

glPushMatrix();
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT,0,testArray2);
glDrawArrays(GL_TRIANGLES,0,36);
glDisableClientState(GL_VERTEX_ARRAY);
glPopMatrix();

Have I missed some vital information as to how the vertex arrays work? Or do I simply HAVE to use quads?

JVene
April 26th, 2009, 03:31 PM
You're on the right track for the most part.

It's important to think in terms of a generic drawing method which can handle any number of objects.

I'm not going to check the list of points you've indicated - on a cursory view they seem appropriate, though you'll look forward to a time when this data is loaded from storage rather than hard coded into an array.

The glVertexPointer call is part of your problem, I think.

You've supplied a stride of zero, but your stride appears to be 12.

I'm also not seeing a call to glBindBuffer, which is how you select a buffer for use, but perhaps you assumed one was already available.

Oh, and don't use quads for performance work, they're generally much slower. Almost all real time graphics work is done in triangles, often in triangle lists, strips whenever we can manage it.

starkr
April 26th, 2009, 03:50 PM
Thanks for the reply, but how is the stride defined? From what I've gathered that would skip a few indexes, the way my array is set up, I definitely wouldn't want that, seeing as my array only contains vertice information. Anyways, if stride was the issue it should make trouble with my triangles as well, since it's set as 0 drawing those too.

Thus far in my course I haven't encountered glBindBuffer(), again, my triangles would likely not work if this was essential.

My problem only seems to apply to drawing the cube polygons. Using the vertex array method works fine when drawing my 3D triangles. The only real difference to the code drawing those is that the glDrawArrays() count variable receives 24 vertices instead of 36, and the array's four bottom lines would contain 0's, effectively removing 12 vertices from 36 down to 24.

What is rendered to my screen in the case of the cube is a clutter of triangles, none of which appear to have been drawn in the positions I've defined in the array.

JVene
April 26th, 2009, 04:09 PM
The stride is the distance between one entry in the array and the next.

They could be thought of as records. You might have, as an example:

3 floats as a vertex,
followed by 3 floats as a normal
followed by 2 floats as a UV map pair

Thus, each 'record' would be 32 bytes long.

That's the stride.

If all you have is 3 floats for a vertex, your stride is 12


If you don't specify a stride, I don't know what you'd get. Probably nonsense.


glBindBuffer is the means by which you select one of several buffers. If you're not familiar with it, then you probably aren't famililar with glGenBuffers,
Which is needed to create a buffer id to use when calling glBindBuffer.

If you're not careful about that, and you're trying to draw multiple objects, you could be confusing storage.

starkr
April 26th, 2009, 04:47 PM
Thanks again, I tried setting my stride to 12 as you suggested, but that didn't make any difference whatsoever as far as the drawing is concerned.

Thanks for the heads up on buffers, I'm sure it's due for one of the next lectures. :P

*Enters the think tank again*

starkr
April 26th, 2009, 05:31 PM
Blast, I was a retard! One blasted comma was missing from my array, conveniently before a negative number, so I ended up with one coordinate at 0 (5 -5), the rest of the thing got skewed resulting in this whole mess. Thanks anyways! Thought I was going crazy...
Well, crazier than I already am that is. Cheers!

Lindley
April 27th, 2009, 06:16 PM
If you don't specify a stride, I don't know what you'd get. Probably nonsense.

If you specify a stride of zero, OpenGL assumes that the array is "tightly packed". There's some kind of gotcha on this anyway, but I don't recall what it is right now.