Zaki Mirza’s Blog


… About software and beyond!

using glDrawElement for more logically building 3d objects in opengl

Working on CG again, i was seeing how we have to do a lot of things in glBegin() and glEnd block to create simple to complex objects. What i just found out is the use of Vertex arrays in openGL. (checkout the Red Book Chapter 2). Ill just give one example of this.

Suppose you want to create a cube (im sure most of them are already hating the sight of the “cube” :p). Simple one would go on create the 6 faces using GL_QUADS like:


.. and so on for rest of the faces traversing the vertices in counter-clockwise direction. thats like, 6×4 24 function calls! (imagine the overhead, on the PC and on… well ur hands:p). So GL has this really cool mechanism of vertex arrays. I wont go in details since the red-book is “the” definitive guide on openGL stuff but heres the example. First you make an array of the vertices like: (oh im lazy so im just pasting it from my own code, basically here width,height and depth are comming in the constructor, CP is a point with x,y,z fields).

//vertex definition of cube
GLfloat va[] = {
cp.x-width/2.0f, cp.y+height/2.0f, cp.z+depth/2.0f,
cp.x+width/2.0f, cp.y+height/2.0f, cp.z+depth/2.0f,
cp.x+width/2.0f, cp.y-height/2.0f, cp.z+depth/2.0f,
cp.x-width/2.0f, cp.y-height/2.0f, cp.z+depth/2.0f,
cp.x-width/2.0f, cp.y+height/2.0f, cp.z-depth/2.0f,
cp.x+width/2.0f, cp.y+height/2.0f, cp.z-depth/2.0f,
cp.x+width/2.0f, cp.y-height/2.0f, cp.z-depth/2.0f,
cp.x-width/2.0f, cp.y-height/2.0f, cp.z-depth/2.0f
//face drawing indices
GLubyte ia[] = {
/* front */
0,4,7,3, /* left */
3,7,6,2, /* botom */
3,1,5,6, /* right */
6,7,4,5, /* back */
5,4,0,3 /* top */
if you work these indices out, youll end up with a solid cube. (remember the join-the-dots game aah i miss them ). So now we have the vertices, we have an indices list that says how to join the dots. we know we’ll do the quads here. What now?First we enable the openGL vertex array state.glEnableClientState(GL_VERTEX_ARRAY);Then we tell where our vertices are:glVertexPointer(3,GL_FLOAT,0,_vertices);and then we draw this element using out indicesglDrawElements(_drawMode,_numIndices,GL_UNSIGNED_BYTE,_indices);(GL_UNSIGNED_BYTE tell what is the type of indices, _numIndices is the number of indices in the array. can be less than the acutal array but then only those vertices will be traversed. and _drawMode is my variable, i set it to GL_QUADS for solid cube.)So basically we have made a cube with a lot less function calls and in a lot logical manner. Check out what these functions do and how to best utilize them in the red-book chapter 2. i have the red book for version 1.4 of OpenGL (i dunno yet if they will be available  in previous versions).The possibilities are endless using this technique. There are also ways to specify color array for the element like this as well. 

Be cautious though. make sure you change the glVertexPointer everytime you make a new object. Also to disable the vertex array mode when you are done with it. Also dont use glDrawElements within GlBegin and glEnd because glDrawElement will do it for you. if you do it just like i wrote here, you might get a single colored tasteless cube which doesnt even look like a cube. try using GL_COLOR_ARRAY to color different vertices. Ill put of a post about lighting in openGL soon (as soon as i learn it myself :p)


Filed under: C++, CG, opengl, programming

One Response

  1. needa says:

    please can u tell me how to draw a solid sphere in openGl

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

RSS Google Shared Items

  • An error has occurred; the feed is probably down. Try again later.

RSS Google Reader Starred Items

  • An error has occurred; the feed is probably down. Try again later.

Top Clicks

  • None
%d bloggers like this: