100 likes | 183 Views
A Bit More on OpenGL. Matt Wronkiewicz Be Users Group at UIUC. Introduction. This presentation will cover 3D OpenGL programming as seen in Quake 2. Model files: MD2 for models and PCX for textures. Model Files. Files contain vertices, triangles, and texture coordinates.
E N D
A Bit More on OpenGL Matt Wronkiewicz Be Users Group at UIUC
Introduction • This presentation will cover 3D OpenGL programming as seen in Quake 2. • Model files: MD2 for models and PCX for textures.
Model Files • Files contain vertices, triangles, and texture coordinates. • Vertices are groups of three floats • Triangles are groups of three vertices. • Vertices used by more than one triangle are only listed once. • Code for reading model files available from ftp.idsoftware.com
Textures • Texture coordinates contained in model file. • Textures are in PCX image files. • Texture size must be a power of 2 (use gluScaleImage()). • Automatically mapped to drawn triangles.
Texture Settings glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D. GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
Setting up OpenGL for 3D Drawing • glEnable() sets flags in OpenGL • GL_CULL_FACE • GL_TEXTURE_2D • Set drawing mode with glPolygonMode(GL_FRONT, GL_FILL). • Set view with glOrtho(), glViewport(), and operations on the GL_PROJECTION matrix.
Drawing Triangles in 3D // Transform GL_MODELVIEW matrix // Repeat for every triangle glBegin(GL_TRIANGLES); for (i = 0; i < numTriangles; i++) { // Get vertices v1..v3 glVertex3f(v1.x, v1.y, v1.z); glVertex3f(v2.x, v2.y, v2.z); glVertex3f(v3.x, v3.y, v3.z); } glEnd(); BGLView::SwapBuffers();
Lighting the Model • Enable lighting with glEnable(). • Create lights with glLight(). • Eight lights can be created with parameters GL_LIGHT1...GL_LIGHT8. • Use glLightfv(GL_LIGHTi, GL_POSITION, &v) to place light. • Other parameters set the color, intensity, and shape of the light.
Get More Code • OpenGL Programmers Guide and Reference Manual are available online (pester Vik for URLs). • Dr. Dobb’s journal has source code for Quake 2 model viewer at http://www.ddj.com/ftp/1998/1998_07/. • Be’s GLTeapot source code is in the /boot/optional directory. Model format is similar to the Quake format.