Zaki Mirza’s Blog

Icon

… 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:

glBegin(GL_QUADS);
glVertex3i(0,0,0);
glVertex3i(2,0,0);
glVertex3i(2,2,0);
glVertex3i(0,2,0);

.. 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[] = {
0,3,2,1,
/* 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)

Advertisements

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

Setting up SDL-OPENGL in Visual Studio

And this CG assignment’s been giving some people pain in the gut so i thought okay why not share this with others. hopefully they’ll learn from it. So here goes.

Well the problem lies in the fact that people cant setup SDL and OpenGL together in Visual Studio (6,7,8).

Here are the steps you can follow to make it all work:

Downloading and setting up SDL:

1. For the Easiest way to do this, (on your home PC) download the sdl libraries and headers from www.libsdl.org . extract the file (in my case its: SDL_devel-1.2.11-VC6.zip). hunt the folder where you installled the visual studio. Find the folder (VC98 on VC6… VC7 on .net 2003 and vc8 on .net 2005).

2. in the include folder, create a folder called “sdl” and copy the contents of the “sdl_devel-1.2.11/include” folder ( where you jus extracted the sdl headers). These will be many header files. in other words copy *.* from sdl_devel-1.2.11/include to vc6/include. 🙂

3. in the lib folder (of visual studio) copy *.lib of the “lib” folder in sdl_devel-1.2.11 folder.

4. copy the sdl.dll file in the sdl_devel-1.2.11/lib folder to your windows/system32 folder.
Creating The SDL-OPENGL Project:

1. fireup Visual Studio of your choice.

2. Create a new Windows 32 Console Application. Name it .. i_suck :p

3. Click finish. The visual studio will create a default cpp file with the main() function declared in it.

4. In VC6 goto Project>Settings, in VC7 and VC8 right click the project in the solution explorer and click properties.

5. Under Linker>Input option: include the following libraries:
opengl32.lib glu32.lib sdl.lib sdlmain.lib

6. (OPTIONAL STEP) in the Linker>System option (im not sure if this is in VC6, but it is in VC7. This step is optional. Only if you do not want to show the console window with the graphics window) Set the subsystem to windows (or add /SUBSYSTEM:WINDOWS to the commandline option in the linker). in the linker>advanced option, set the entry point to “mainCRTStartup” or add “/ENTRY:mainCRTStartup” to commandline option of linker.

7. In c/C++ > Code Generation option, set the Runtime Library option to MULTI-THREADED DEBUG DLL.

8. Click ok and the project is setup for opengl and sdl.

The Code:

1. In you code, include the following files:
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <sdl/sdl.h>

Note that the ordering of the file should be right. because gl.h file requires that windows.h be included beforehand.

2. build your code. it should be successful. if it isnt, reconsider the above steps.
The Coding Part:
Do it yourself :p. Read the SDL Documentation that came with the SDL libraries and headers. Its right there in the doc folder :p. And ah yes, it will be very comfy once you switch to Visual Studio .net 2003 (and even more to .net 2005, but .net 2003 is more stable. Thats from my experience and mishi and umar manzoor recommend that as well). I have tested these steps on .Net 2003. ( dunno about VC6, i left using it ages ago). Any problems you find drop in a comment. Yes i do take donations as well :p
 

Filed under: opengl, sdl, Troubleshooter, visual studio

Blog Stats

  • 104,467 landed here so far...
November 2017
M T W T F S S
« Jan    
 12345
6789101112
13141516171819
20212223242526
27282930  

tweet

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

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