700 likes | 1.01k Views
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.
E N D
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 • 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.
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.
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.
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.
Colors glColor3fv(face color) render_face() glColor3fv(eye color) render_eyes() glColor3fv(hair color) render_hair() glColor3fv(teeth color) render_teeth()
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.
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.
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();
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();
Lights How do we use them?
Lights Define normal vectors for each vertex of all the objects. Call glNormal*() before glVertex*().
Lights Create, select, and position one or more light sources.
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#);
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.
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.
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.
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.
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.
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);
Shadows OpenGL lights DO NOT create shadows!!! They create shading. If you want shadows, you have to create them yourselves!!!
Controlling Position & Direction The light is defined in after the viewing transformations, but before the modeling transformations. Light source stays stationary
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
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
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.
Materials Colors When Using Lights
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.
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.
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.
Blending See-Through Polygons
A = 1.0 A = 0.6 A = 0.2 A = 0.8 A = 0.4 The Alpha Channel
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.
Blending Blending is computed by the following equation: Sfactors* Scolors + Dfactors * Dcolors = Pixelcolors S represents the source. D represents the destination. S D
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.
Order Matters Back-to-Front Arbitrary Order
Order Matters • Render all opaque polygons first. • Make the depth buffer read only. • Render transparent polygons. • Enable depth buffer writing. Opaque First
Texture Mapping Drawing Pictures on Polygons
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.
Specifying a Texture void glTexImage2D( GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
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 ).