PDA

View Full Version : Drawing a cube with VertexArrays



Raistlin666
05-31-2012, 05:38 AM
Hello there,
I'm a bloody beginner in OpenGL (and I'm not very good in C++) and tried to draw a cube with VertexArrays. I know that there is another thread concerning VertexArrays but it didn't help.
I work with the fabled "red book" and, so I thouhgt, wrote my code right. This is it:



void cube(void){
glEnableClientState(GL_VERTEX_ARRAY);

static GLfloat vertices[] = {0.25, 0.25, 1.00, 3.25, 1.75, 0.25, 1.75, 3.25, 2.50, 0.25, 3.25, 3.25};
glVertexPointer(2, GL_FLOAT, 0, vertices);

//Nummerierung der Punkte des Wuerfels
static GLubyte frontIndices[] = {4, 5, 6, 7};
static GLubyte rightIndices[] = {1, 2, 6, 5};
static GLubyte bottomIndices[] = {0, 1, 5, 4};
static GLubyte backIndices[] = {0, 3, 2, 1};
static GLubyte leftIndices[] = {0, 4, 7, 3};
static GLubyte topIndices[] = {2, 3, 7, 6};

glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, frontIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, rightIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, bottomIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, backIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, leftIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, topIndices);
glDisableClientState(GL_VERTEX_ARRAY);
}


The only thing I see is an edge of something that doesn't look like a cube.

What is wrong?

tonyo_au
05-31-2012, 06:36 AM
You only seem to have 6 vertices but you are referencing 8

Raistlin666
05-31-2012, 07:26 AM
Damn, why didn't I see this. Thanx a lot.
But there's another problem. The position of the cube is strange. The first vertex is exactly in the middle of my window. I thought the middle of my cube has to sit in the middle of my window. Do I have to work with some matrix-multiplication?

menzel
05-31-2012, 08:32 AM
What you see depends on you camera and modelview matrices, these will get multiplied to your points automatically if you use the fixed function pipeline, otherwise you should show us your vertex shader.

Dan Bartlett
05-31-2012, 09:37 AM
As well as only having 6 points, you are using 2D points instead of 3D, so won't be able to form a cube with those.

Instead of:

static GLfloat vertices[] = {0.25, 0.25, 1.00, 3.25, 1.75, 0.25, 1.75, 3.25, 2.50, 0.25, 3.25, 3.25};
glVertexPointer(2, GL_FLOAT, 0, vertices);

You need something like (untested):

static GLfloat vertices[] = {-0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5};
glVertexPointer(3, GL_FLOAT, 0, vertices);

mhagain
05-31-2012, 11:31 AM
Another thing you may not be aware of is that you don't need a separate draw call for each face for the cube - you can draw the entire thing in a single call, like so:

static GLubyte allIndices[] = {4, 5, 6, 7, 1, 2, 6, 5, 0, 1, 5, 4, 0, 3, 2, 1, 0, 4, 7, 3, 2, 3, 7, 6};

glDrawElements (GL_QUADS, 24, GL_UNSIGNED_BYTE, allIndices);

Raistlin666
06-01-2012, 01:32 AM
First of all thanx for your help.
I changed my code so it now looks like this:



void cube(void){
glEnableClientState(GL_VERTEX_ARRAY);
//Anlegen eines Arrays mit den Punktkooridanten
static GLfloat vertices[] = {0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0,1,0};

glVertexPointer(3, GL_FLOAT, 0, vertices);

//Nummerierung der Punkte des Wuerfels
static GLubyte frontIndices[] = {0, 1, 2, 3};
static GLubyte rightIndices[] = {3, 2, 4, 5};
static GLubyte bottomIndices[] = {0, 3, 5, 6};
static GLubyte backIndices[] = {6, 5, 4, 7};
static GLubyte leftIndices[] = {0, 6, 7, 1};
static GLubyte topIndices[] = {1, 2, 4, 7};

glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, frontIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, rightIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, bottomIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, backIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, leftIndices);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, topIndices);

// glDrawElements (GL_QUADS, 24, GL_UNSIGNED_BYTE, vertices);
}

What mhagain suggested didn't work. so I did it again with separate draw calls. I think my problem is what menzel said but I don't have a clue how to get this work. Maybe I have to read a little more in the red bible ;)