Results 1 to 4 of 4

Thread: The best way to draw a cube

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2002
    Posts
    1

    The best way to draw a cube

    I'm new to openGL and am have been successful at drawing a cube However, I'm concerned about the efficiency of which I drew the cube. Currently, this is what I'm doing:

    I'm using vertex arrays, and filling it with 24 vertices (4 for each face). I was kinda hoping I would be able to just specify 8 vertices total, and then have it just figure it out itself... Is this possible? If so, can someone provide an example for me to work from?

    Also, I'm not sure if this matters, but I do plan on using lighting in my program, so I'm not sure if this throws off any possible solutions.

    Finally, I was reading somewhere that it would be better to store it as a set of triangles instead of rectangles. Is this the case?

    TIA,
    -Mike

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,068

    Re: The best way to draw a cube

    First you can make a routine that only takes 8 vertex points and makes a cube, but in the routine you will still have to have 24 vertex defined for the cube to be drawn.

    I think Triangles are processed somewhat faster, then quads when using hardware.

    As for lighting,


    Originally posted by mjriggio:
    I'm new to openGL and am have been successful at drawing a cube However, I'm concerned about the efficiency of which I drew the cube. Currently, this is what I'm doing:

    I'm using vertex arrays, and filling it with 24 vertices (4 for each face). I was kinda hoping I would be able to just specify 8 vertices total, and then have it just figure it out itself... Is this possible? If so, can someone provide an example for me to work from?

    Also, I'm not sure if this matters, but I do plan on using lighting in my program, so I'm not sure if this throws off any possible solutions.

    Finally, I was reading somewhere that it would be better to store it as a set of triangles instead of rectangles. Is this the case?

    TIA,
    -Mike

  3. #3
    Member Contributor
    Join Date
    Oct 2002
    Posts
    55

    Re: The best way to draw a cube

    I Had this same thought when i started opengl... Heres 2 good ideas
    #1, use the cube function, opengl has a built in cube drawing function =P
    #2, use 2 matricies.
    static float vertex[8][3] = {{x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}};
    // that is where you put your 8 verticies
    static float link[6][4] = {0,1,2,3}, {4,5,6,7}, {0,1,5,4}, {1,2,6,5}, {2,3,7,6}, {3,0,7,4}};
    // those are the order in which the verticies are linked
    now draw each of of those quads
    glBegin(GL_QUADS)
    for (int a=0; a<6; a++){
    glVertexf(vertex[link[a][0]][0], vertex[link[a][0]][1], vertex[link[a][0][2]);
    glVertexf(vertex[link[a][1]][0], vertex[link[a][1]][1], vertex[link[a][1][2]);
    glVertexf(vertex[link[a][2]][0], vertex[link[a][2]][1], vertex[link[a][2][2]);
    glVertexf(vertex[link[a][3]][0], vertex[link[a][3]][1], vertex[link[a][3][2]);
    }
    glEnd();
    // thats just written on the fly... but hopefully you get the idea... it draws the verticies based on what order you have them linked in the link[] matrix. You could do the same thing with triangles... just separate each face into 2 triangles

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Nov 2002
    Location
    Latvia
    Posts
    626

    Re: The best way to draw a cube

    1st create vertex array with 8 vertices, 2nd create indices array, 3rd use DrawElements if I remember correct & by the way, you can put all this thing in display list

Similar Threads

  1. how draw cube and cylinder?
    By wa00ad in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-13-2016, 06:26 AM
  2. draw cube using VBO
    By ChuckE in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 12-13-2013, 11:40 PM
  3. draw 3D cube
    By gandrakar in forum OpenGL ES
    Replies: 1
    Last Post: 06-14-2010, 02:02 AM
  4. draw a line on a cube
    By Dastagir in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 09-27-2009, 05:39 AM
  5. how to draw a boc/cube
    By mimo in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-03-2004, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean