1 / 59

Module 04

Module 04. Texture mapping, Texture filtering, Lighting and Blending. Overview. Understanding texture mapping Understanding texture filtering techniques Basics about the bitmap (BMP ) file OpenGL lighting Blending and transparency Bump mapping Programming OpenGL program

asher-mcgee
Download Presentation

Module 04

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 04 Texture mapping, Texture filtering, Lighting and Blending

  2. Overview • Understanding texture mapping • Understanding texture filtering techniques • Basics about the bitmap (BMP) file • OpenGL lighting • Blending and transparency • Bump mapping • Programming OpenGL program (loading BMP files, lighting, keyboard control , blending)

  3. Texture mapping (1) • OpenGL provides texture image mapping functions that fit images onto polygons • Texture maps are composed of rectangular arrays of data. Each element of these arrays is called a texel. Although they are rectangular arrays of data, texture maps can be mapped to non-rectangular objects, such as spheres, cylinders, and other 3D object models • Usually, developers use the 2D texture in their graphics, however, using 1D, 3D and 4D textures have some unique benefits (shading, …) • When you map a texture onto a polygon, the texture will be transformed as the polygon is transformed

  4. Texture mapping (2) • The 1D textures have a width and a height equal to only 1 pixel • The 2D textures have a width and a height • The 3D textures have a width, height, and depth and are sometimes called volume textures • The 4D textures have a width, height depth and the texture image “time” coordinate

  5. Texture mapping (3) Texture coordinates Texture coordinates are used to determine exactly how to apply a texture map to a polygon. The lower-left corner of a texture is given the coordinates (0, 0), while the upper-right corner of a texture is given the coordinates (1, 1). Texture coordinates for 2D textures are given the notation (s, t), where s and t are equal to a value from 0 to 1.

  6. Texture mapping (4) • Syntax of setting the current texture coordinates in OpenGL: glBegin(type of primitive mode); … glTexCoordXT(s, t, r, q);// texture coordinates … glEnd(); • Understanding function naming glTexCoordXT(s, t, r, q); gl – gl library TexCoord – root command for texture coordination X – number of arguments (1, 2, 3, 4) T – type of arguments (d, f, i, s) s – the horizontal texture image coordinate t – the vertical texture image coordinate r – the texture image depth coordinate q – the texture image “time” coordinate

  7. Texture mapping (5) Example: … glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); glEnd() … -1

  8. Texture mapping (6) Advance example: Triangle mesh textured with base texture + = Triangle mesh Base texture Result of texture mapping

  9. Texture filtering techniques (1) • After a texture map has been applied to a transformed polygon, a single screen pixel can represent a fraction of a texel if the viewpoint is close to the texture, or a pixel can represent a collection of texels when the viewpoint is further away. Texture filtering tells OpenGL how it should map the texels to pixels when calculating the final image. • In texture filtering, magnification refers to when a screen pixel represents a small portion of a texel. Minification refers to when a pixel contains a number of texels. You can tell OpenGL how to handle both of these filtering cases with the glTexParameter() function

  10. Texture filtering techniques (2) Examples: Texture without texture filtering (GL_NEAREST) Texture with texture filtering (GL_LINEAR)

  11. Texture filtering techniques (3) Texture image filters: Filter Description GL_NEAREST Nearest-neighbor filtering GL_LINEAR Linear interpolation GL_NEAREST_MIPMAP_NEAREST Nearest-neighbor mipmappedfiltering GL_NEAREST_MIPMAP_LINEAR Linear interpolated mipmaps GL_LINEAR_MIPMAP_NEAREST Linear interpolation of mipmaps GL_LINEAR_MIPMAP_LINEAR Linear interpolation of interpolated mipmaps

  12. Texture filtering techniques (4) • Syntax of setting the texture image parameters: … glTexParameterT(GLenum target, GLenum pname, GLfloat param); … • Understanding function naming gl – gl library TexParameter – root command for texture image parameters T – type of arguments (i, f) target – must be one of GL_TEXTURE_1D or GL_TEXTURE_2D pname – the texturing parameter to set param – the texture filtering parameter type

  13. Texture filtering techniques (5) Example: int LoadGLTextures() { … //type of texture filtering (GL_NEAREST) to use when the image is larger // (GL_TEXTURE_MAG_FILTER) or stretched on the screen than the original texture, glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); //type of texture filtering to use (GL_LINEAR) when the image is smaller // (GL_TEXTURE_MIN_FILTER) on the screen than the actual texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); … }

  14. Basics about the bitmap (BMP) file • Despite their limitations, Windows .BMP files are probably the most common and widely supported files used by PCs capable of from 2 to 16.7 million colors. • With only a few exceptions, .BMP files do not utilize data compression schemes, so it’s simple to read and use these files in OpenGL programs. • A .BMP file is organized into three or four sections, depending on the type of colors used.

  15. OpenGL lighting Lighting is atechnique which adds realism to the 3D scene. Light types: Ambient light simulates light bouncing between surfaces so many times that the source of the light is no longer apparent. This component is not affected by the position of either the light or the viewer. Diffuse light comes from a certain direction, but once it strikes a surface, it is reflected equally in all directions. The diffuse lighting component is affected by the position or direction of the light, but not the position of the viewer. Specular light is directional and reflected off a surface in a particular direction. Specularity is often referred to as shininess. The specular term is affected by the position of both the light and the eye. Emissive light is a cheap way to simulate objects that emit light. OpenGL does not actually use the emissive term to illuminate surrounding objects; it simply causes the emissive object to be more intensely lit.

  16. Blending and transparency • Most special effects in OpenGL rely on some type of blending. • Blending is used to combine the color of agiven pixel that is about to be drawn with the pixel that is already on the screen. How the colors arecombined is based on the alpha value of the colors, and/or the blending function that is being used. 0 3D cube without blending 3D cube with blending

  17. Drawing scene Steps to do: • load a bitmap (BMP) file • add a texture filtering and keyboard control to the project • add lighting to the scene • add blending to the textures

  18. Load a bitmap (BMP) file (1) Steps to do: • include header file which allows to work with files • declare new global function for loading files • declare new global function for converting loaded files into textures • declare new global variable which sets storage space for one texture • define function for loading files • define function for converting loaded files into textures • load and enable the new texture wtile setting up the OpenGL environment • draw a texture mapped 3D cube

  19. Load a bitmap (BMP) file (2) 1) include header file which allows to work with files //-------------------------------------------------------------------------------- // Header Files //-------------------------------------------------------------------------------- … #include <stdio.h> // Header File For Standard Input/Output … 2) declare new global function for loading files //-------------------------------------------------------------------------------- // Global Functions //-------------------------------------------------------------------------------- … AUX_RGBImageRec *LoadBMP(char *Filename); // Declaration For LoadBMP …

  20. Load a bitmap (BMP) file (3) 3) declare new global function for converting loaded files into textures //-------------------------------------------------------------------------------- // Global Functions //-------------------------------------------------------------------------------- … int LoadGLTextures(); // Declaration For LoadGLTextures … 4) declare new global variable which sets storage space for one texture //-------------------------------------------------------------------------------- // Global Variables //-------------------------------------------------------------------------------- … GLuint texture[1]; // Storage For One Texture …

  21. Load a bitmap (BMP) file (4) 5) define function for loading files AUX_RGBImageRec *LoadBMP(char *Filename) // Loads A Bitmap Image { FILE *File=NULL; // File Handle if (!Filename) // Make Sure A Filename Was Given { return NULL; // If Not Return NULL } File=fopen(Filename,"r"); // Check To See If The File Exists if (File) // Does The File Exist? { fclose(File); // Close The Handle return auxDIBImageLoad(Filename); // Load The Bitmap And Return A Pointer } return NULL; // If Load Failed Return NULL } // end of LoadBMP()

  22. Load a bitmap (BMP) file (5) 6) define function for converting loaded files into textures int LoadGLTextures() / / Load Bitmaps And Convert To Textures { int Status=FALSE; // Status Indicator AUX_RGBImageRec *TextureImage[1]; // Create Storage Space For The Texture memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit if (TextureImage[0]=LoadBMP("Data/Texture.bmp")) { Status=TRUE; // Set The Status To TRUE glGenTextures(1, &texture[0]); // Create The Texture // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture[0]); … } // end of LoadGLTextures()

  23. Load a bitmap (BMP) file (6) 6) define function for converting loaded files into textures int LoadGLTextures() { … glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } if (TextureImage[0]) // If Texture Exists { if (TextureImage[0]->data) // If Texture Image Exists { free(TextureImage[0]->data); // Free The Texture Image Memory } free(TextureImage[0]); // Free The Image Structure } return Status; // Return The Status }// end of LoadGLTextures()

  24. Load a bitmap (BMP) file (7) 7) load and enable the new texture while setting up the OpenGL environment int InitGL(GLvoid) // All Setup For OpenGL Goes Here { if (!LoadGLTextures()) // Jump To Texture Loading Routine { return FALSE // If Texture Didn't Load Return FALSE } glEnable(GL_TEXTURE_2D); // Enable Texture Mapping … } // end of InitGL()

  25. Load a bitmap (BMP) file (8) 8) draw a texture mapped 3D cube int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { … glLoadIdentity(); // Reset The View glTranslatef(0.0f,0.0f,-5.0f); glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture glBegin(GL_QUADS); // Front Face glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); … } // end of DrawGLScene()

  26. Load a bitmap (BMP) file (9) 8) draw a texture mapped 3D cube int DrawGLScene(GLvoid) { … glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Face glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Bottom Face glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); ... } // end of DrawGLScene()

  27. Load a bitmap (BMP) file (10) 8) draw a texture mapped 3D cube int DrawGLScene(GLvoid) { … // Right face glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Left Face glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glEnd(); } // end DrawGLScene()

  28. Final result Loading a bitmap (BMP) file

  29. Practice Open: Lab04 / SimpleTexture /SimpleTexture.sln

  30. Add texture filtering and keyboard control to the project(1) Steps to do: 1) declare new variables to store whether or not the “F” key has been pressed, new variables to keep track of which filtering technique we want to use, and new variables to change and control axis angles and rotation speed. 2) generate textures for nearest, linear and mippapped filtering in LoadGLTextures 3) check for keyboard F, up, down, left, right, page up and page down pressing in while(!done) loop in WinMain() function 4) draw a 3D cube with appropriate respond to keyboard actions (rotating the cube, translating the cube and changing the cubes texture filtering mode)

  31. Add texture filtering and keyboard control to the project (2) 1) declare new variables to store whether or not the “F” key has been pressed, new variables to keep track of which filtering technique we want to use, and new variables to change and control axis angles and rotation speed. //-------------------------------------------------------------------------------- // Global Variables //-------------------------------------------------------------------------------- … bool fp; // F Pressed? GLuint filter; // Which Filter To Use GLfloat xrot; // X Rotation GLfloat yrot; // Y Rotation GLfloat xspeed; // X Rotation Speed GLfloat yspeed; // Y Rotation Speed GLfloat z=-5.0f; // Depth Into The Screen GLuint texture[3]; // Storage For Three Textures …

  32. Add texture filtering and keyboard control to the project (3) 2) generate textures for nearest, linear and mippapped filtering in LoadGLTextures Int LoadGLTextures() { … if (TextureImage[0]=LoadBMP("Data/Texture.bmp")) { … glGenTextures(3, &texture[0]); // Create Three Textures // Create Nearest Filtered Texture glBindTexture(GL_TEXTURE_2D, texture[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); … } // end of LoadGLTextures()

  33. Add texture filtering and keyboard control to the project (4) 2) generate textures for nearest, linear and mippapped filtering in LoadGLTextures Int LoadGLTextures() { … // Create Linear Filtered Texture glBindTexture(GL_TEXTURE_2D, texture[1]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); // Create MipMapped Texture glBindTexture(GL_TEXTURE_2D, texture[2]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); } // end of LoadGLTextures()

  34. Add texture filtering and keyboard control to the project (5) 3) check for keyboard F, up, down, left, right, page up and page down pressing in while(!done) loop in WinMain() function Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow) { … while(!done) // Loop That Runs While done=FALSE { … else // If There Are No Messages { DrawGLScene() … else // Not Time To Quit, Update Screen { … } // end of WinMain()

  35. Add texture filtering and keyboard control to the project (6) 3) check for keyboard F, up, down, left, right, page up and page down pressing in while(!done) loop in WinMain() function Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow) { … SwapBuffers(hDC); if (keys['F'] && !fp) { fp=TRUE; filter+=1; if (filter>2) { filter=0; } } if (!keys['F']) { fp=FALSE; } … } // end of WinMain()

  36. Add texture filtering and keyboard control to the project (7) 3) check for keyboard F, up, down, left, right, page up and page down pressing in while(!done) loop in WinMain() function Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow) { … if (keys[VK_PRIOR]) { z-=0.02f; } if (keys[VK_NEXT]) { z+=0.02f; } if (keys[VK_UP]) { xspeed-=0.01f; } … } // end of WinMain()

  37. Add texture filtering and keyboard control to the project (8) 3) check for keyboard F, up, down, left, right, page up and page down pressing in while(!done) loop in WinMain() function Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow) { … if (keys[VK_DOWN]) { xspeed+=0.01f; } if (keys[VK_RIGHT]) { yspeed+=0.01f; } if (keys[VK_LEFT]) { yspeed-=0.01f; } … } //end while(!done) }// end of WinMain()

  38. Add texture filtering and keyboard control to the project (9) 4) draw a 3D cube with appropriate respond to keyboard actions (rotating the cube, translating the cube and changing the cubes texture filtering mode) int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { … glLoadIdentity(); glTranslatef(0.0f,0.0f,z); glRotatef(xrot,1.0f,0.0f,0.0f); glRotatef(yrot,0.0f,1.0f,0.0f); glBindTexture(GL_TEXTURE_2D, texture[filter]); glBegin(GL_QUADS); …// here draw textured 3D cube glEnd(); xrot+=xspeed; yrot+=yspeed; … }//end of DrawGLScene()

  39. Final result Texture filtering and keyboard control

  40. Practice Open: Lab04 / SimpleTexture2 / SimpleTexture2.sln

  41. Add lighting to the scene (1) Steps to do: 1) declare new variables to store whether or not the “L” key has been pressed, declare new variable to keep track of whether or not the lighting is on or off, set up arrays, that will be used to create the light 2) while setting up OpenGL, set up ambient and diffuse lighting, position the light, and enable the light 3) check for keyboard L pressing in while(!done) loop in WinMain() function 4) draw a 3D cube with appropriate normal vectors for lighting

  42. Add lighting to the scene (2) 1) declare new variables to store whether or not the “L” key has been pressed, declare new variable to keep track of whether or not the lighting is on or off, set up arrays, that will be used to create the light //-------------------------------------------------------------------------------- // Global Variables //-------------------------------------------------------------------------------- … bool lp; // L Pressed? bool light; // Lighting ON/OFF GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f }; …

  43. Add lighting to the scene (3) 2) while setting up OpenGL, set up ambient and diffuse lighting, position the light, and enable the light Int InitGL(GLvoid) { … glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light glEnable(GL_LIGHT1); // Enable Light One … } // end of InitGL()

  44. Add lighting to the scene (4) 3) check for keyboard L pressing in while(!done) loop in WinMain() function Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow) { … while(!done) // Loop That Runs While done=FALSE { … else // If There Are No Messages { DrawGLScene() … else // Not Time To Quit, Update Screen { … } // end of WinMain()

  45. Add lighting to the scene (5) 3) check for keyboard L pressing in while(!done) loop in WinMain() function Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow) { … if (keys['L'] && !lp) { lp=TRUE; light=!light; if (!light) { glDisable(GL_LIGHTING); } else { glEnable(GL_LIGHTING); } } … } // end of WinMain()

  46. Add lighting to the scene (6) 3) check for keyboard L pressing in while(!done) loop in WinMain() function Int WINAPI Winmain (HINSTANCE hInstance, HINSTANCE h PrevInstance, LPSTR lpCmdLine, int nCmdShow) { … if (!keys['L']) { lp=FALSE; } … } //end while(!done) } // end of WinMain()

  47. Add lighting to the scene (7) 4) draw a 3D cube with appropriate normal vectors for lighting int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing { … glBegin(GL_QUADS); // Front Face glNormal3f( 0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Back Face glNormal3f( 0.0f, 0.0f,-1.0f); // Normal Pointing Away From Viewer glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); … }//end of DrawGLScene()

  48. Add lighting to the scene (8) 4) draw a 3D cube with appropriate normal vectors for lighting int DrawGLScene(GLvoid) { … // Top Face glNormal3f( 0.0f, 1.0f, 0.0f); // Normal Pointing Up glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Bottom Face glNormal3f( 0.0f,-1.0f, 0.0f); // Normal Pointing Down glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); … }//end of DrawGLScene()

  49. Add lighting to the scene (9) 4) draw a 3D cube with appropriate normal vectors for lighting int DrawGLScene(GLvoid) { … // Right face glNormal3f( 1.0f, 0.0f, 0.0f); // Normal Pointing Right glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Left Face glNormal3f(-1.0f, 0.0f, 0.0f); // Normal Pointing Left glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); glEnd(); … } // end DrawGLScene()

  50. Final result OpenGL lighting

More Related