Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Drawing a cube with VertexArrays

  1. #1
    Junior Member Newbie
    Join Date
    May 2012
    Location
    Germany
    Posts
    3

    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};
     
    	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?

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    728
    You only seem to have 6 vertices but you are referencing 8

  3. #3
    Junior Member Newbie
    Join Date
    May 2012
    Location
    Germany
    Posts
    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. #4
    Member Regular Contributor
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    302
    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. #5
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    381
    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:
    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. #6
    Advanced Member Frequent Contributor
    Join Date
    Jan 2007
    Posts
    965
    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};
     
    glDrawElements (GL_QUADS, 24, GL_UNSIGNED_BYTE, allIndices);

  7. #7
    Junior Member Newbie
    Join Date
    May 2012
    Location
    Germany
    Posts
    3
    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};
     
    	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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •