1 / 68

OpenGL

OpenGL. Son of the Survival Guide. Last Time on OpenGL. Windowing … glut Rendering Primatives Transformations Projections State Management. Today on OpenGL. Z-buffer (a little bit). Colors. Lights. Materials. Blending. Texture Mapping. The Depth Buffer. The Z-Buffer. Z-Buffer.

loren
Download Presentation

OpenGL

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. OpenGL Son of the Survival Guide

  2. Last Time on OpenGL • Windowing … glut • Rendering Primatives • Transformations • Projections • State Management

  3. Today on OpenGL • Z-buffer (a little bit). • Colors. • Lights. • Materials. • Blending. • Texture Mapping.

  4. The Depth Buffer The Z-Buffer

  5. Z-Buffer • The Depth buffer, also known as the Z-buffer, is simply another buffer where depth information is kept for each pixel. • Depth testing need to be enabled before use, GL_DEPTH_TEST. • glDepthFunc(…) defines what is considered to be a passing test.

  6. Z-Buffer Before writing to a pixel, a test is made to determine whether the new pixel is closer to the viewer then the stored pixel.

  7. Z-Buffer More details will be given in the future. What you need to remember is that we have a mechanism that finds the correct polygon fragment that is closest to the viewer for each pixel.

  8. COLORS

  9. Colors OpenGL can have one of two color modes, Indexed colors or RGBA. When we defined the window we defined the color mode: glutInitDisplayMode( GLUT_RGBA … ) Once defined it cannot be changed.

  10. Colors glColor3fv(face color) render_face() glColor3fv(eye color) render_eyes() glColor3fv(hair color) render_hair() glColor3fv(teeth color) render_teeth()

  11. Colors Colors in RGBA mode are specified by one of the following commands: void glColor3{b s i f d ub us ui} (r,g,b); void glColor4{b s i f d ub us ui} (r,g,b,a); void glColor3{b s i f d ub us ui}v (v); void glColor4{b s i f d ub us ui}v (v); RGBA values in float&doublemode are between 0.0 and 1.0. See the book for the other modes.

  12. We’ll get to this later … when we talk about blending! Colors As you can see, color can have 3 of 4 values: R - the Red color value. G - the Green color value. B - the Blue color value. A - the Transparent color value.

  13. Shading Models glShadeModel(GL_FLAT); glBegin(GL_QUADS); glColor3f (1.0,0.0,0.0); glVertex3f(0.0,0.0,0.0); glColor3f (0.0,1.0,0.0); glVertex3f(1.0,0.0,0.0); glColor3f (0.0,0.0,1.0); glVertex3f(1.0,1.0,0.0); glColor3f (1.0,1.0,0.0); glVertex3f(0.0,1.0,0.0); glEnd();

  14. Shading Models glShadeModel(GL_SMOOTH); glBegin(GL_QUADS); glColor3f (1.0,0.0,0.0); glVertex3f(0.0,0.0,0.0); glColor3f (0.0,1.0,0.0); glVertex3f(1.0,0.0,0.0); glColor3f (0.0,0.0,1.0); glVertex3f(1.0,1.0,0.0); glColor3f (1.0,1.0,0.0); glVertex3f(0.0,1.0,0.0); glEnd();

  15. Lights

  16. No Lights

  17. Lights

  18. Lights How do we use them?

  19. Lights Define normal vectors for each vertex of all the objects. Call glNormal*() before glVertex*().

  20. Lights Create, select, and position one or more light sources.

  21. Lights Create and select a lighting model. Define material properties for the objects in the scene. And most important … Enable the lights: glEnable(GL_LIGHTING); glEnable(GL_LIGHT#);

  22. Creating & Positioning Lights void glLight*(light, pname, param); • Creates the light specified by light, which can be GL_LIGHT0, ... , or GL_LIGHT7 • The characteristic of the light being set is defined by pname, which specifies a named parameter • param indicates the values to which the pname characteristic is set.

  23. Creating & Positioning Lights pnamecan get one of several values: GL_AMBIENT, GL_DIFFUSE & GL_SPECULAR define the light RGBA values for each of the light components.

  24. Directional and Positional Lights Directional light source is positioned at infinity (like the sun). Positional light source is positioned near the scene and its exact position determines its effect.

  25. Creating & Positioning Lights when GL_POSITION is passed as an argument to glLight*() four values (x,y,z,w) are passed as parameters. W determines the type of light we are defining: 0  directional - (x,y,z) is the direction. 1  positional - (x,y,z) is the position.

  26. Additional Options GL_CONSTANT_ATTENUATION, GL_LINEAR_ATTENUATION, GL_QUADRATIC_ATTENUATION. These define the attenuation of the light. Usually disabled for directional lights. GL_SPOT_DIRECTION, GL_SPOT_EXPONENT, GL_SPOT_CUTOFF. These define spotlights and spotlight properties.

  27. Multiple Light Sources You can define several light sources by calling glLight*() several times with different light names: glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);

  28. Shadows OpenGL lights DO NOT create shadows!!! They create shading. If you want shadows, you have to create them yourselves!!!

  29. Controlling Position & Direction The light is defined in after the viewing transformations, but before the modeling transformations. Light source stays stationary

  30. Controlling Position & Direction During rendering additional transformations are applied specifically for the light source, after which we define the light position. Light source moves around objects

  31. Controlling Position & Direction The light position is defined in the initialization stage, after the camera and modelview matrix have been initialized. Light source moves with viewer

  32. Lighting Model void glLightModel*(pname, param); • GL_LIGHT_MODEL_AMBIENT defines the global ambient RGBA values. • GL_LIGHT_MODEL_LOCAL_VIEWER determines whether we are using a local or infinite viewpoint. • GL_LIGHT_MODEL_TWO_SIDE determines whether we are using two sided lighting.

  33. Materials Colors When Using Lights

  34. Materials void glMaterial*(face, pname, param); • Specifies a current material property for use in lighting calculations. • Face can be GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK to indicate which face of the object the material should be applied to.

  35. Materials You can define the ambient, diffuse, specular, shininess and emission of a material. The calculations for the final color of a pixel are done like you did in the Ray Casting exercise.

  36. Cheating … There’s a simple way to define materials fast … the results are not as nice but usually you get the effect that you want. Enable GL_COLOR_MATERIAL and use glColor to define materials.

  37. Blending See-Through Polygons

  38. A = 1.0 A = 0.6 A = 0.2 A = 0.8 A = 0.4 The Alpha Channel

  39. How Do We Use Blending? The first thing you need to do is ENABLE them: glEnable(GL_BLEND); Blending is done per-pixel. The pixel that is already stored is called source and the one we are trying to blend is called destination.

  40. Blending Blending is computed by the following equation: Sfactors* Scolors + Dfactors * Dcolors = Pixelcolors S represents the source. D represents the destination. S D

  41. Blending void glBlendFunc(sfactor, dfactor); Different constants are defined (see the book) for the source and destination factors in order to achieve different blending effects.

  42. Order Matters Back-to-Front Arbitrary Order

  43. Order Matters • Render all opaque polygons first. • Make the depth buffer read only. • Render transparent polygons. • Enable depth buffer writing. Opaque First

  44. Texture Mapping Drawing Pictures on Polygons

  45. Texture Mapping

  46. Texture Mapping

  47. How Do We Use Textures? • Create a texture object and specify a texture for that object. • Indicate how the texture is to be applied to each pixel. • Enable texture mapping. • Draw the scene, supplying both texture and geometric coordinates.

  48. Specifying a Texture

  49. Specifying a Texture void glTexImage2D( GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);

  50. Specifying a Texture width and height specify the size of the texture. border indicates the width of the border which can be either 0 or 1 (to be explained soon). The dimensions of the texture MUST be: width = 2n + 2b ( where b is the ) height = 2m + 2b ( border width ).

More Related