# Thread: Drawing a cube with VertexArrays

1. ## Drawing a cube with VertexArrays

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:

Code :
```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};

glDisableClientState(GL_VERTEX_ARRAY);
}```

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

What is wrong?

2. You only seem to have 6 vertices but you are referencing 8

3. 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?

4. 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.

5. 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.

Code :
```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):
Code :
```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([B]3[/B], GL_FLOAT, 0, vertices);```

6. 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:
Code :
```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};

7. First of all thanx for your help.
I changed my code so it now looks like this:

Code :
```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};