Open GL Son of the Survival Guide Last

  • Slides: 68
Download presentation
Open. GL Son of the Survival Guide

Open. GL Son of the Survival Guide

Last Time on Open. GL • • • Windowing … glut Rendering Primatives Transformations

Last Time on Open. GL • • • Windowing … glut Rendering Primatives Transformations Projections State Management

Today on Open. GL • • • Z-buffer (a little bit). Colors. Lights. Materials.

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

The Depth Buffer The Z-Buffer

The Depth Buffer The Z-Buffer

Z-Buffer • The Depth buffer, also known as the Zbuffer, is simply another buffer

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

Z-Buffer Before writing to a pixel, a test is made to determine whether the

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

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

COLORS

Colors Open. GL can have one of two color modes, Indexed colors or RGBA.

Colors Open. GL can have one of two color modes, Indexed colors or RGBA. When we defined the window we defined the color mode: glut. Init. Display. Mode( GLUT_RGBA … ) Once defined it cannot be changed.

Colors gl. Color 3 fv(face color) render_face() gl. Color 3 fv(eye color) render_eyes() gl.

Colors gl. Color 3 fv(face color) render_face() gl. Color 3 fv(eye color) render_eyes() gl. Color 3 fv(hair color) render_hair() gl. Color 3 fv(teeth color) render_teeth()

Colors in RGBA mode are specified by one of the following commands: void gl.

Colors in RGBA mode are specified by one of the following commands: void gl. Color 3{b s i f d ub us ui} (r, g, b); void gl. Color 4{b s i f d ub us ui} (r, g, b, a); void gl. Color 3{b s i f d ub us ui}v (v); void gl. Color 4{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.

Colors As you can see, color can have 3 of 4 values: R G

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

Shading Models gl. Shade. Model(GL_FLAT); gl. Begin(GL_QUADS); gl. Color 3 f (1. 0, 0.

Shading Models gl. Shade. Model(GL_FLAT); gl. Begin(GL_QUADS); gl. Color 3 f (1. 0, 0. 0); gl. Vertex 3 f(0. 0, 0. 0); gl. Color 3 f (0. 0, 1. 0, 0. 0); gl. Vertex 3 f(1. 0, 0. 0); gl. Color 3 f (0. 0, 1. 0); gl. Vertex 3 f(1. 0, 0. 0); gl. Color 3 f (1. 0, 0. 0); gl. Vertex 3 f(0. 0, 1. 0, 0. 0); gl. End();

Shading Models gl. Shade. Model(GL_SMOOTH); gl. Begin(GL_QUADS); gl. Color 3 f (1. 0, 0.

Shading Models gl. Shade. Model(GL_SMOOTH); gl. Begin(GL_QUADS); gl. Color 3 f (1. 0, 0. 0); gl. Vertex 3 f(0. 0, 0. 0); gl. Color 3 f (0. 0, 1. 0, 0. 0); gl. Vertex 3 f(1. 0, 0. 0); gl. Color 3 f (0. 0, 1. 0); gl. Vertex 3 f(1. 0, 0. 0); gl. Color 3 f (1. 0, 0. 0); gl. Vertex 3 f(0. 0, 1. 0, 0. 0); gl. End();

Lights

Lights

No Lights

No Lights

Lights

Lights

Lights How do we use them?

Lights How do we use them?

Lights Define normal vectors for each vertex of all the objects. Call gl. Normal*()

Lights Define normal vectors for each vertex of all the objects. Call gl. Normal*() before gl. Vertex*().

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

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

Lights Create and select a lighting model. Define material properties for the objects in

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

Creating & Positioning Lights void gl. Light*(light, pname, param); • Creates the light specified

Creating & Positioning Lights void gl. Light*(light, pname, param); • Creates the light specified by light, which can be GL_LIGHT 0, . . . , or GL_LIGHT 7 • 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 pname can get one of several values: GL_AMBIENT, GL_DIFFUSE &

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.

Directional and Positional Lights Directional light source is positioned at infinity (like the sun).

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 gl. Light*()

Creating & Positioning Lights when GL_POSITION is passed as an argument to gl. Light*() 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

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 gl. Light*() several

Multiple Light Sources You can define several light sources by calling gl. Light*() several times with different light names: gl. Lightfv(GL_LIGHT 0, GL_AMBIENT, light 0_ambient); gl. Lightfv(GL_LIGHT 1, GL_AMBIENT, light 1_ambient);

Shadows Open. GL lights DO NOT create shadows!!! They create shading. If you want

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

Controlling Position & Direction Light source stays stationary The light is defined in after

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

Controlling Position & Direction Light source moves around objects During rendering additional transformations are

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.

Controlling Position & Direction Light source moves with viewer The light position is defined

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.

Lighting Model • • • void gl. Light. Model*(pname, param); GL_LIGHT_MODEL_AMBIENT defines the global

Lighting Model • • • void gl. Light. Model*(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 Colors When Using Lights

Materials void gl. Material*(face, pname, param); • Specifies a current material property for use

Materials void gl. Material*(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.

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

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 gl. Color to define materials.

Blending See-Through Polygons

Blending See-Through Polygons

The Alpha Channel A = 1. 0 A = 0. 8 A = 0.

The Alpha Channel A = 1. 0 A = 0. 8 A = 0. 6 A = 0. 4 A = 0. 2

How Do We Use Blending? The first thing you need to do is ENABLE

How Do We Use Blending? The first thing you need to do is ENABLE them: gl. Enable(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 is computed by the following equation: Sfactors* Scolors + Dfactors * Dcolors =

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

Blending void gl. Blend. Func(sfactor, dfactor); Different constants are defined (see the book) for

Blending void gl. Blend. Func(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 Back-to-Front Arbitrary Order

Order Matters • Render all opaque polygons first. • Make the depth buffer read

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

Texture Mapping Drawing Pictures on Polygons

Texture Mapping

Texture Mapping

Texture Mapping

Texture Mapping

How Do We Use Textures? 1. Create a texture object and specify a texture

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.

Specifying a Texture

Specifying a Texture

Specifying a Texture void gl. Tex. Image 2 D( GLenum GLint GLsizei GLint GLenum

Specifying a Texture void gl. Tex. Image 2 D( GLenum GLint GLsizei GLint GLenum const GLvoid target, level, internal. Format, width, height, border, format, type, *pixels);

Specifying a Texture width and height specify the size of the texture. border indicates

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 + 2 b ( where b is the ) height = 2 m + 2 b ( border width ).

Border

Border

Mip Maps The further away you are from a texture, the less detail you

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.

Specifying a Texture level specifies the mipmapping level of the current texture. 0 -

Specifying a Texture level specifies the mipmapping level of the current texture. 0 - is the base level (highest resolution). n - is the nth level of resolution. When using mipmaps ALL levels from the highest resolution to a 1 x 1 map MUST be defined

Pixels and Type Pixels is a pointer to an array that stores the image

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 …).

Format & Internal. Format describes the way the pixels are stored in the pixels

Format & Internal. Format describes the way the pixels are stored in the pixels array. Internal. Format describes the way we want to store the texels, the way we want to store the textures in memory.

More Texture Stuff

More Texture Stuff

Building Mip. Maps int glu. Build 2 DMipmaps( GLenum target, GLint components, GLint width,

Building Mip. Maps int glu. Build 2 DMipmaps( GLenum target, GLint components, GLint width, GLint height, GLenum format, GLenum type, void *data); Constructs a series of mipmaps and calls gl. Tex. Image*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.

void gl. Tex. Parameter*(target, pname, *param); Repeat Clamp

void gl. Tex. Parameter*(target, pname, *param); Repeat Clamp

void gl. Tex. Parameter*(target, pname, *param); Texel needs to be magnified Texel needs to

void gl. Tex. Parameter*(target, pname, *param); Texel needs to be magnified Texel needs to be Here we define if mipmaps are used. minified

void gl. Tex. Env*(target, pname, *param); With this function we can decide if we

void gl. Tex. Env*(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.

Enough Parameters How do we use textures?

Enough Parameters How do we use textures?

Texture Objects void gl. Gen. Textures(n, *texture. Names); Returns n currently unused names for

Texture Objects void gl. Gen. Textures(n, *texture. Names); Returns n currently unused names for texture objects. The names acquire texture state and dimensionality (1 D or 2 D) only when they are first bound. Zero is a reserved texture name and is never returned as a texture name by gl. Gen. Textures().

Texture Objects void gl. Bind. Texture(target, texture. Name); gl. Bind. Texture() does three things:

Texture Objects void gl. Bind. Texture(target, texture. Name); gl. Bind. Texture() 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, Open. GL stops using texture objects and returns to the unnamed default texture.

Texture Objects void gl. Delete. Textures(n, *texture. Names); Don’t forget to clear the memory

Texture Objects void gl. Delete. Textures(n, *texture. Names); Don’t forget to clear the memory once you’re done using it.

How Do We Use Textures? 1. Create a texture object and specify a texture

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.

Texture Coordinates void gl. Tex. Coord*(TYPEcoords); You can specify 1, 2, 3 or 4

Texture Coordinates void gl. Tex. Coord*(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.

Texture Coordinates (0, 1) (1, 1) (0, 0) (1, 0)

Texture Coordinates (0, 1) (1, 1) (0, 0) (1, 0)

That’s All For Today More to Come…

That’s All For Today More to Come…