Download presentation
Presentation is loading. Please wait.
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 & double mode are between 0.0 and 1.0. See the book for the other modes.
12
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. We’ll get to this later … when we talk about blending!
13
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
14
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(); Shading Models
15
Lights
16
No Lights
17
Lights
18
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 pname can 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
Light source stays stationary Controlling Position & Direction The light is defined in after the viewing transformations, but before the modeling transformations.
30
Controlling Position & Direction Light source moves around objects During rendering additional transformations are applied specifically for the light source, after which we define the light position.
31
Controlling Position & Direction Light source moves with viewer The light position is defined in the initialization stage, after the camera and modelview matrix have been initialized.
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
The Alpha Channel A = 1.0A = 0.6A = 0.2 A = 0.8A = 0.4
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: S factors * S colors + D factors * D colors = Pixel colors 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
47
How Do We Use Textures? 1.Create a texture object and specify a texture for that object. 2.Indicate how the texture is to be applied to each pixel. 3.Enable texture mapping. 4.Draw the scene, supplying both texture and geometric coordinates.
48
Specifying a Texture
49
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 = 2 n + 2b ( where b is the ) height= 2 m + 2b( border width ).
51
Border
52
Mip Maps The further away you are from a texture, the less detail you need. We build a pyramid of textures and use the one with sufficient resolution.
53
Specifying a Texture level specifies the mipmapping level of the current texture. 0 - is the base level (highest resolution). n - is the n th level of resolution. When using mipmaps ALL levels from the highest resolution to a 1x1 map MUST be defined
54
Pixels and Type Pixels is a pointer to an array that stores the image out of which we are creating the texture. Type specifies the data type of the pixels array (int, float …).
55
Format & InternalFormat Format describes the way the pixels are stored in the pixels array. InternalFormat describes the way we want to store the texels, the way we want to store the textures in memory.
56
More Texture Stuff
57
Building MipMaps int gluBuild2DMipmaps(GLenum target, GLint components, GLint width, GLint height, GLenum format, GLenum type, void *data); Constructs a series of mipmaps and calls glTexImage*D() to load the images. A value of 0 is returned if all the mipmaps are constructed successfully; otherwise, a GLU error code is returned.
58
void glTexParameter*(target, pname, *param); Repeat Clamp
59
void glTexParameter*(target, pname, *param); Texel needs to be magnified Texel needs to be minified Here we define if mipmaps are used.
60
void glTexEnv*(target, pname, *param); With this function we can decide if we want the texture to replace the colors of the polygon or be blended and modulated with the colors of the polygon.
61
Enough Parameters How do we use textures?
62
Texture Objects void glGenTextures(n, *textureNames); Returns n currently unused names for texture objects. The names acquire texture state and dimensionality (1D or 2D) only when they are first bound. Zero is a reserved texture name and is never returned as a texture name by glGenTextures().
63
Texture Objects void glBindTexture(target, textureName); glBindTexture() does three things: 1. Creates a new texture object and assigns it the given name. 2. When binding to a previously created texture object, that texture object becomes active. 3. When binding to zero, OpenGL stops using texture objects and returns to the unnamed default texture.
64
Texture Objects void glDeleteTextures(n, *textureNames); Don’t forget to clear the memory once you’re done using it.
65
How Do We Use Textures? 1.Create a texture object and specify a texture for that object. 2.Indicate how the texture is to be applied to each pixel. 3.Enable texture mapping. 4.Draw the scene, supplying both texture and geometric coordinates.
66
Texture Coordinates void glTexCoord*(TYPEcoords); You can specify 1,2,3 or 4 texture coordinates using this function. Usually we use 2. You have to specify texture coordinates for each vertex.
67
Texture Coordinates (0,0) (1,0) (0,1) (1,1)
68
That’s All For Today More to Come…
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.