Computer Graphics Classic Rendering Pipeline Overview.

113
Computer Graphics Classic Rendering Pipeline Overview

Transcript of Computer Graphics Classic Rendering Pipeline Overview.

Page 1: Computer Graphics Classic Rendering Pipeline Overview.

Computer Graphics

Classic Rendering Pipeline Overview

Page 2: Computer Graphics Classic Rendering Pipeline Overview.

What is Rendering?

• Rendering is the process of taking 3D models and producing a single 2D picture– The classic rendering approaches all work with

polygons – triangles in particular

• Does not include creating of the 3D models– Modeling

• Does not include movement of the models– Animation/physics/AI

Page 3: Computer Graphics Classic Rendering Pipeline Overview.

What is a Pipeline?

• Classic rendering is done as a pipeline– Triangles travel from stage to stage– At any point in time, there are triangles at all

stages of processing– Can obtain better throughput with a pipeline– Much of the pipeline is now in hardware

Page 4: Computer Graphics Classic Rendering Pipeline Overview.

Classic Rendering Pipeline

ViewSpace

ModelSpace

ScreenSpace

Model & ViewTransformations

Normalized DeviceSpace

ViewportMapping

Projection

Page 5: Computer Graphics Classic Rendering Pipeline Overview.

Model Space

• Model Space is the coordinate system attached to the specific model that contains the triangle

• It is easiest to define models in this local coordinate system– Separation of object design from world location– Multiple instances of the object

Page 6: Computer Graphics Classic Rendering Pipeline Overview.

Model & View Transformations• These are 3D transformations that simply

change the coordinate system with which the triangles are defined– The triangles are not actually moved

ModelCoordinate

Space

WorldCoordinate

Space

ViewCoordinate

Space

Page 7: Computer Graphics Classic Rendering Pipeline Overview.

Model to World Transformation

• Each object is defined w.r.t its own local model coordinate system

• There is one world coordinate system for the entire scene

Page 8: Computer Graphics Classic Rendering Pipeline Overview.

Model to World Transformation

• Transformation can be performed if one knows the position and orientation of the model coordinate system relative to the world coordinate system

• Transformations place all objects into the same coordinate system (the world coordinate system)

• There is a different transformation for each object– An object can consist of many triangles

Page 9: Computer Graphics Classic Rendering Pipeline Overview.

World to View Transformation

• Once all the triangles are define w.r.t. the world coordinate system, we need to transform them to view space

• View space is defined by the coordinate system of the virtual camera– The camera is placed using world space coordinates

• There is one transformation from world to view space for all triangles (if only one camera)

Page 10: Computer Graphics Classic Rendering Pipeline Overview.

World to View Transformation

• The camera’s “film” is parallel to the view xy plane• The camera points down the negative view z axis

– At least for the right-handed OpenGL coordinate system– Things are opposite for the left-handed DirectX system

Page 11: Computer Graphics Classic Rendering Pipeline Overview.

Placing the Camera

• In OpenGL the default view coordinate system is identical to the world coordinate system– The camera’s lens points down the negative z

axis

• There are several ways to move the view from its default position

Page 12: Computer Graphics Classic Rendering Pipeline Overview.

Placing the Camera

• Rotations and Translations can be performed to place the view coordinate system anywhere in the world

• Higher-level functions can be used to place the camera at an exact position– gluLookAt(eye point, center point, up vector)– Similar function in DirectX

Page 13: Computer Graphics Classic Rendering Pipeline Overview.

Transformation Order

• Note that order of transformations is important– Points move from model space to world space– Then from world space to view (camera) space

• This implies an order of: Pview = (Tworld2view) (Tmodel2world) (Pmodel)– That is, the model to world transform needs to be

applied first to the point

Page 14: Computer Graphics Classic Rendering Pipeline Overview.

OpenGL Transformation Order• OpenGL contains a single “global” ModelView matrix set to

the multiplication of the 2 transforms from the previous slide– This ModelView matrix is automatically applied to all points you

send down the pipeline

• You can either set the matrix directly or you can perform the operation: MV’ = (MV) (SomeTransformationMatrix)– Most utility operations (those that hide the details of the 4x4 matrix

such “rotate 30 about the X axis” or “gluLookAt”) perform this second type of operation

• How does this impact the order of the transformations in your OpenGL code?

Page 15: Computer Graphics Classic Rendering Pipeline Overview.

World to View Details

• Just to give you a taste of what goes on behind the scenes with gluLookAt…– It needs to form a 4x4 matrix that transforms world

coordinate points into view coordinate points

– To do this it simply forms the matrix that represents the series of transformation steps that get the camera coordinate system to line up with the world coordinate system

– How does it do that – what would the steps be if you had to implement the function in the API?

Page 16: Computer Graphics Classic Rendering Pipeline Overview.

View Space

• There are several operations that take place in view space coordinates– Back-face culling– View Volume clipping– Lighting

• Note that view space is still a 3D coordinate system

Page 17: Computer Graphics Classic Rendering Pipeline Overview.

Back-face Culling• Back-face culling removes triangles that are not facing the

viewer– “back-face” is towards the camera

• Normal extends off the front-face– Default is to assume triangles are defined counter clock-wise (ccw)

• At least this is the default for a right-handed coordinate system (OpenGL)• DirectX’s left-handed coordinate system is backwards (cw is front facing)

Page 18: Computer Graphics Classic Rendering Pipeline Overview.

Surface Normal

• Each triangle has a single surface normal– The normal is perpendicular to the plane of the triangle– Easy way to define the orientation of the surface– Again, the normal is just a vector (no position)

Page 19: Computer Graphics Classic Rendering Pipeline Overview.

Computing the Surface Normal1. Let V1 be the vector from point A to point B

2. Let V2 be the vector from point A to point C

3. N = V1 x V2

4. N is often normalized

• Note that order of vertices becomes important– Triangle ABC has an outward facing normal

– Triangle ACB has an inward facing normal

Page 20: Computer Graphics Classic Rendering Pipeline Overview.

Back-face Culling

• Recall that V1 . V2 = |V1| |V2| cos()– If both vectors are unit vectors this simplifies to

V1 . V2 = cos()• Recall that cos() is positive if

[-90..+90]• Thus, if the dot product of the View vector (V)

and the Polygon Normal vector (Np) is positive we can cull (remove) it

Page 21: Computer Graphics Classic Rendering Pipeline Overview.

Back-face Culling

• We do need to compute a different view vector for each triangle rendered– Needs to be as if the camera is looking directly at

the triangle

• Just use a single point on the triangle as the view vector since the triangle is in view space– Camera origin is at (0,0,0) in view space

Page 22: Computer Graphics Classic Rendering Pipeline Overview.

Back-face Culling

• This technique should remove approximately half the triangles in a typical scene at a very early stage in the pipeline– We always want to dump data as early as possible

• Dot products are really fast to compute– Can be optimized further because all that is

necessary is the sign of the dot product

Page 23: Computer Graphics Classic Rendering Pipeline Overview.

Back-face Culling

• When using an API such as OpenGL or DirectX there is a toggle to turn on/off back-face culling– There is also a toggle to select which side is

considered the “front” side of the triangle (the side with the normal or the other side)

Page 24: Computer Graphics Classic Rendering Pipeline Overview.

View Volume Clipping

• View Volume Clipping removes triangles that are not in the camera’s sight

• The View Volume of a perspective camera is a 3D shape that looks like a pyramid with its top cut off– Called a Frustum

• Thus, this step is sometimes called Frustum clipping

• The Frustum is defined by near and far clipping planes as well as the field of view– More info later when talking about projections

Page 25: Computer Graphics Classic Rendering Pipeline Overview.

View Volume Clipping

Page 26: Computer Graphics Classic Rendering Pipeline Overview.

View Volume Clipping

• View Volume Clipping happens automatically in OpenGL and DirectX

• You need to be aware of it because it is easy to get black screens because you set your view volume to be the wrong size

• Also, for some of the game speed-up techniques we will need to perform some view volume clipping by hand in software

Page 27: Computer Graphics Classic Rendering Pipeline Overview.

Lighting

• The easiest form of lighting is to just assign a color to each vertex– Again, color is a state-machine type of thing

• More realistic forms of lighting involve calculating the color value based on simulated physics

Page 28: Computer Graphics Classic Rendering Pipeline Overview.

Real-world Lighting

• Photons emanate from light sources

• Photons collide with surfaces and are:– Absorbed– Reflected– Transmitted

• Eventually some of the photons make it to your eyes enabling you to see

Page 29: Computer Graphics Classic Rendering Pipeline Overview.

Lighting Models

• There are different ways to model real-world lighting inside a computer– Local reflection models

• OpenGL

• Direct3D

– Global illumination models• Raytracing

• Radiosity

Page 30: Computer Graphics Classic Rendering Pipeline Overview.

Local Reflection Models

• Calculates the reflected light intensity from a point on the surface of an object using only direct illumination– As if the object was alone in the scene

• Some important artifacts not taken into account by local reflection models are:– Shadows from other objects

– Inter-object reflection

– Refraction

Page 31: Computer Graphics Classic Rendering Pipeline Overview.

Phong Local Reflection Model

• 3 types of lighting are considered in the Phong model:– Diffuse– Specular– Ambient

• These 3 types of light are then combined into a color for the surface at the point in question

Page 32: Computer Graphics Classic Rendering Pipeline Overview.

Diffuse

• Diffuse reflection is what happens when light bounces off a matte surface

• Perfect diffuse reflection is when light reflects in all directions

Page 33: Computer Graphics Classic Rendering Pipeline Overview.

Diffuse

• We don’t actually cast rays from the light source and scatter them in all directions, hoping one of them will hit the camera– This technique is not very efficient!

• Even offline techniques such as radiosity which try and simulate diffuse lighting don’t go this far!

• We just need to know the amount of light falling on a particular surface point

Page 34: Computer Graphics Classic Rendering Pipeline Overview.

Diffuse

• The amount of light reflected (the brightness) of the surface at a point is proportional to the angle between the surface normal, N, and the direction of the light, L.

• In particular: Id = Ii cos() = Ii (N . L)

– Where Id is the resulting diffuse intensity, Ii is the incident intensity, and N and L are unit vectors

Page 35: Computer Graphics Classic Rendering Pipeline Overview.

Diffuse

• A couple of examples:– Ii = 0.8, = 0 Id = 0.8

• The full amount is reflected

– Ii = 0.8, = 45 Id = 0.57• 71% is reflected

Page 36: Computer Graphics Classic Rendering Pipeline Overview.

Diffuse

• Diffuse reflection only depends on:– Orientation of the surface

– Position of the light

• Does not depend on:– Viewing position

• Bottom sphere is viewed from a slightly lower position than the top sphere

Page 37: Computer Graphics Classic Rendering Pipeline Overview.

Specular

• Specular highlights are the mirror-like reflections found on shinny metals and plastics

Page 38: Computer Graphics Classic Rendering Pipeline Overview.

Specular

• N is again the normal of the surface at the point in we are lighting

• L is again the direction to the light source

• R is the reflection vector

• V is the direction to the viewer (camera)

Page 39: Computer Graphics Classic Rendering Pipeline Overview.

Specular

• We want the intensity to be greatest in the direction of the reflection vector and fall off quite fast around the reflection vector

• In particular: Is = Ii cosn() = Ii (R . V)n

– Where Is is the resulting specular intensity, Ii is the incident intensity, R and V are unit vectors, and n is an index that simulates the degree of surface imperfection

Page 40: Computer Graphics Classic Rendering Pipeline Overview.

Specular

• As n gets bigger the drop-off around R is faster– At n = , the surface is a perfect mirror (all

reflection is directly along R• cos() = 1 and 1 = 1

• cos(anything bigger than 0) = number < 1 and(number < 1) = 0

Page 41: Computer Graphics Classic Rendering Pipeline Overview.

Specular

• Examples of various values of n:– Left: diffuse only

– Middle: low n specular added to diffuse

– Right: high n specular added to diffuse

Page 42: Computer Graphics Classic Rendering Pipeline Overview.

Specular

• Calculation of N, V and L are easy– N with a cross product on the triangle vertices– V and L with the surface point and the camera

or light position, respectively

• Calculation of R requires mirroring L about N, which requires a bit of geometry:

R = 2 N ( N . L ) – L– Note: Foley p.730 has a good explanation of this geometry

Page 43: Computer Graphics Classic Rendering Pipeline Overview.

Specular

• The reflection vector, R, is time consuming to compute, so often it is approximated with the halfway vector, H, which is halfway between the light direction and the viewing direction: H = (L + V) / 2

• Then the equation is:Is = Ii (H . N)n

Page 44: Computer Graphics Classic Rendering Pipeline Overview.

Specular

• Specular reflection depends on:– Orientation of the surface

– Position of the light

– Viewing position

• The bottom picture was taken with a slightly lower viewing position– The specular highlights changes

when the camera moves

Page 45: Computer Graphics Classic Rendering Pipeline Overview.

Ambient

• Note in the previous examples that the part of the sphere not facing the light is completely black

• In the real-world light would bounce off of other objects (like floors and walls) and eventually some light would get to the back of the sphere

• This “global bouncing” is what the ambient component models– And “models” is a very loose term here because it isn’t

at all close to what happens in the real-world

Page 46: Computer Graphics Classic Rendering Pipeline Overview.

Ambient

• The amount of ambient light added to the point being lit is simply: Ia

• Note that this doesn’t depend on:– surface orientation– light position– viewing direction

Page 47: Computer Graphics Classic Rendering Pipeline Overview.

Phong Local Illumination Model

• The 3 components of reflected light are combined to form the total reflected light

I = KaIa + KdId + KsIs

– Where Ia, Id and Is are as computed previously and Ka, Kd and Ks are 3 constants that control how to mix the components

– Additionally, Ka + Kd + Ks = 1

• The OpenGL and DirectX models are both based on the Phong local illumination model

Page 48: Computer Graphics Classic Rendering Pipeline Overview.

OpenGL Model – Light Color

• Incident light (Ii)– Represents the color of the light source– We need 3 (Iir Iib Iig) values

• Example: (1.0, 0.0, 0.0) is a red light

– Lighting calculations to determine Ia, Id, and Is now must be done 3 times each

• Each color channel is calculated independently

– Further control is gained by defining separate (Iir Iib Iig) values for ambient, diffuse, specular

Page 49: Computer Graphics Classic Rendering Pipeline Overview.

OpenGL Model – Light Color

• So for each light in the scene you need to define the following colors:– Ambient (r, g, b)– Diffuse (r, g, b)– Specular (r, g, b)

• The ambient Iis are used in the Ia equation• The diffuse Iis are used in the Id equation• The specular Iis are used in the Is equation

Page 50: Computer Graphics Classic Rendering Pipeline Overview.

OpenGL Model – Material Color

• Material properties (K values)– The equations to compute Ia, Id and Is just

compute how must light from the light source is reflected off the object

– We must also define the color of the object• Ambient color: (r, g, b)

• Diffuse color: (r, g, b)

• Specular color: (r, g, b)

Page 51: Computer Graphics Classic Rendering Pipeline Overview.

OpenGL Model - Color

• The ambient material color is multiplied by the amount of reflected ambient light– Ka Ia

• Similar process for diffuse and specular• Then, just like in the Phong model, they are all

added together to produce the final color• Note that each K and I are vectors of 3 color values

that are all computed independently• Also need to define a “shininess” material value to

be used as the n value in the specular equation

Page 52: Computer Graphics Classic Rendering Pipeline Overview.

OpenGL Model - Color

• By mixing the material color with the lighting color, one can get realistic light– White light,

red material

– Green light,same red material

Page 53: Computer Graphics Classic Rendering Pipeline Overview.

OpenGL Model - Emissive

• The OpenGL model also allows one to make objects “emissive”– They look like they produce light (glow)– The extra light they produce isn’t counted as an

actual light as far as the lighting equations are concerned

– This emissive light values (Ke) are simply added to the resulting reflected values

Page 54: Computer Graphics Classic Rendering Pipeline Overview.

OpenGL Model - Attenuation

• One can also specify how fast the light will fade as it travels away from the light source

• Controlled by an attenuation equationA = 1 / (kc + kl d + kq d2)

• Where the 3 Ks can be set by the programmer and d represents the distance between the light source and the vertex in question

Page 55: Computer Graphics Classic Rendering Pipeline Overview.

OpenGL Model - Equation

• So the total equation is:Vertex Color = Ke + A ( ( Ka La ) + ( Kd Ld (L . N) ) + ( Ks Ls (((L+V)/2) . N)shininess ) )– For each of the 3 colors (R,G,B) independently– For each light turned on in the scene– For each vertex in the scene– Note that the above equation is slightly simplified:

• If either of the dot products is negative, use 0• Spotlight effect is not included• Global ambient light is not included

Page 56: Computer Graphics Classic Rendering Pipeline Overview.

Light Sources

• There are several classifications of lights:– Point lights

– Directional lights

– Spot lights

– Extended lights

Page 57: Computer Graphics Classic Rendering Pipeline Overview.

Projection

• Projection is what takes the scene from 3D down to 2D

• There are several type of projection– Orthographic

• CAD

– Perspective• Normal camera

– Stereographic• Fish-eye lens

Page 58: Computer Graphics Classic Rendering Pipeline Overview.

Orthographic Projections

• Equations:

x’ = x

y’ = y

• Main property that is preserved is that parallel lines in 3D remain parallel in 2D

Page 59: Computer Graphics Classic Rendering Pipeline Overview.

Perspective Projections

• Equations:

x’ = f x / z

y’ = f y / z

• Creates a foreshortening effect

• Main property that is preserved is that straight lines in 3D remain straight in 2D

Page 60: Computer Graphics Classic Rendering Pipeline Overview.

Projection in OpenGL

• Set the projection matrix instead of the modelview matrix– The equations given previously can be turned into 4x4

matrix form – what else would you expect!

• Orthographic (view volume is a rectangle):– glOrtho(left, right, bottom, top, near, far)

• Perspective (view volume is a frustum):– gluPerspective(horzFOV, aspectRatio,

nearClipPlane, farClipPlane)

Page 61: Computer Graphics Classic Rendering Pipeline Overview.

FOV Calculation

• It is important to pick a good FOV– If the image on the screen stays the same size

• The bigger the FOV the closer the center of projection is to the image plane

Page 62: Computer Graphics Classic Rendering Pipeline Overview.

FOV Calculation

• This implies that the human viewer needs to move their eye closer to the actual screen to keep the scene from being distorted as the FOV increases

• To pick a good FOV:– Put the actual size window on the screen– Sit at a comfortable viewing distance– Determine how much that window subtends of your eye’s

viewing angle

• This method effectively places your eye at the center of projection and will create the least distortion

Page 63: Computer Graphics Classic Rendering Pipeline Overview.

Normalized Device Space

• This is our first 2D space– Although some 3D information is often kept

• The major operations that happen in this space are:– 2D clipping– Pixel Shading– Hidden surface removal– Texture mapping

Page 64: Computer Graphics Classic Rendering Pipeline Overview.

2D Clipping

• When 3D objects were clipped against the view volume, triangles that were partially inside the volume were kept

• When these triangles make it to this stage they have parts that hang outside the window these are clipped

Page 65: Computer Graphics Classic Rendering Pipeline Overview.

Pixel Shading

• The lighting equations we have seen are all about obtaining a color at a particular vertex

• Pixel shading is all about taking those colors and coloring each pixel in the triangle

• There are 3 main methods:– Flat shading

– Gouraud shading

– Phong shading (not to be confused with the Phong local reflectance model previously discussed)

Page 66: Computer Graphics Classic Rendering Pipeline Overview.

Flat Shading

• A single color is computed the triangle at– The center of the triangle– Using the normal of the triangle surface as N

• The computed color is used to shade every pixel in the triangle uniformly

• Produces images that clearly show the underlying polygons

• OpenGL: glShadeModel(GL_FLAT);

Page 67: Computer Graphics Classic Rendering Pipeline Overview.

Flat Shading Example

Page 68: Computer Graphics Classic Rendering Pipeline Overview.

Gouraud Shading

• 3 colors are computed for the triangle at:– Each vertex– Using neighbor averaged normals as each N

• What is a neighbor averaged normal?– The average of the surface normals of all triangles that

share this vertex

• If triangle model is approximating an analytical surface then normals could be computed directly from the surface description– I did this in my sphere examples for the lighting model

Page 69: Computer Graphics Classic Rendering Pipeline Overview.

Gouraud Shading

• Bi-linear interpolation is used to shade the pixels from the 3 vertex colors– Interpolation happens in 2D

• The advantage Gouraud shading has over flat shading is that the underlying polygon structure can’t be seen

• OpenGL: glShadeModel(GL_SMOOTH);

Page 70: Computer Graphics Classic Rendering Pipeline Overview.

Gouraud Shading Example

• The problem with Gouraud shading is that specular highlights don’t interpolate correctly– If the object isn’t constructed with enough triangles

artifacts can be seen (left 320 s, right 5120 s)

Page 71: Computer Graphics Classic Rendering Pipeline Overview.

Phong Shading

• Many colors are computed for the triangle at:– The back projection of each pixel onto the 3D triangle

– Using normals that have been bi-linearly interpolated (in 2D) from the normals at the 3 vertices

– The normals at the 3 vertices are still computed as neighbor averaged normals

• Each pixel gets its own computed color

Page 72: Computer Graphics Classic Rendering Pipeline Overview.

Phong Shading

• The advantage Phong shading has over Gouraud shading is that it allows the interior of a triangle to contain specular highlights

• The disadvantage is that it is easily 4-5 times more expensive

• OpenGL does not support Phong shading

Page 73: Computer Graphics Classic Rendering Pipeline Overview.

Phong Shading Example

Page 74: Computer Graphics Classic Rendering Pipeline Overview.

Shading Comparison Examples

• Wireframe

• Flat

• Gouraud

• Phong

Page 75: Computer Graphics Classic Rendering Pipeline Overview.

Hidden Surface Removal

• The problem is that we have polygons that overlap in the image and we want to make sure that the one in front shows up in front

• There are several ways to solve this problem:– Painter’s algorithm– Z-buffer algorithm

Page 76: Computer Graphics Classic Rendering Pipeline Overview.

Painter’s Algorithm

• Sort the polygons by depth from camera

• Paints the polygons in order from farthest to nearest

Page 77: Computer Graphics Classic Rendering Pipeline Overview.

Painter’s Algorithm

• There are two major problems with the painter’s algorithm:– Wasteful of time because every polygon gets

drawn on the screen even if it is entirely hidden– Only handles polygons that don’t overlap in the

z-coordinate

Page 78: Computer Graphics Classic Rendering Pipeline Overview.

Z-buffer Algorithm

• The Z-buffer algorithm is pixel based– The Painter’s is object based

• A buffer of identical size to the color buffer is created, called the Z-buffer (or depth buffer)– Recall that the color buffer where the resulting colors are

placed (2 color buffers when double-buffering)

• The values in the Z-buffer are all set to the max– The range of depth values in the view volume is mapped to 0.0

to 1.0– OpenGL:

• glClearDepth(1.0f);• glClear(GL_DEPTH_BUFFER_BIT);

Page 79: Computer Graphics Classic Rendering Pipeline Overview.

Z-buffer Algorithm

• For each object– For each projected pixel in the object: (x, y)

• If the z value of the current pixel is less than the Z-buffer’s value at (x, y) then:

– Color the pixel at (x, y) in the color buffer

– Replace the value at (x, y) in the Z-buffer with the current z value

• Else – don’t do anything because a previously rendered object is closer to the viewer at the projected (x, y) location

Page 80: Computer Graphics Classic Rendering Pipeline Overview.

Z-buffer Algorithm

• Pros:– Objects can be drawn in any order– Objects can overlap in depth– Hardware supported in almost every graphics card

• Cons:– Memory cost (1024x768 > 786K pixels)

• At 4 bytes per pixel > 3M bytes• 4 bytes often necessary to get the resolution we want in depth

– Some pixels are still drawn and then replaced– Problems with transparent objects

Page 81: Computer Graphics Classic Rendering Pipeline Overview.

Z-buffer Algorithm and Transparency

• Transparent colors need to be blended with the colors of the opaque objects behind– There are different blending functions (more later)

• To make blending work, the correct opaque color needs to be known the opaque objects need to be drawn before the transparent ones

• However, we still have the following problem:– Black: opaque (drawn first)– Blue: transparent (drawn second)– Red: transparent (drawn third)

Page 82: Computer Graphics Classic Rendering Pipeline Overview.

Z-buffer Algorithm and Transparency

• The problem:– If blue sets the Z-buffer to its depth value then the red

is assumed to be blocked by the blue and won’t get its color blended properly

• Solutions:– Order the transparent objects from back to front

• Fails: transparent objects can overlap in depth

– Turn off Z-Buffer test for transparent objects• Fails: transparent objects won’t be blocked by opaque

Page 83: Computer Graphics Classic Rendering Pipeline Overview.

Z-buffer Algorithm and Transparency

• Correct Solution:– Make the Z-buffer be read-only during the drawing of

the transparent objects• Z-buffer tests are still done so opaque objects block

transparent objects that are behind them

• But Z-buffer values are not changed so transparent objects don’t block other transparent objects that are behind them

– OpenGL:• glDepthMask(GL_TRUE) // read/write

• glDepthMask(GL_FALSE) // read-only

Page 84: Computer Graphics Classic Rendering Pipeline Overview.

Texture Mapping

• Real objects contain subtle changes in both color and orientation

• We can’t model these objects with tons of little triangles to capture these changes– Modeling would be too hard– Rendering would be too time consuming

• Use Mapping techniques to simulate it– Texture mapping handles changes in color

Page 85: Computer Graphics Classic Rendering Pipeline Overview.

Texture Mapping

• Model objects with normal sized polygons

• Map 2D images onto the polygons

Page 86: Computer Graphics Classic Rendering Pipeline Overview.

The Stages of Texture Mapping

• There are 4 major stages to texture mapping– Obtaining texture parameter space coordinates

from 3D coordinates using a projector function– Mapping the texture parameter space

coordinates into texture image space coordinates using a corresponder function

– Sampling the texture image at the computed texture space coordinates

– Blending the texture value with the object color

Page 87: Computer Graphics Classic Rendering Pipeline Overview.

Projector Functions

• A projector function is simply a way to get from a 3D point on the object to a 2D point in texture parameter space

• Texture Parameter space is represented by two coordinates (u, v) both in the range [0..1)

Page 88: Computer Graphics Classic Rendering Pipeline Overview.

Projector Functions

• Projector functions can be computed automatically during the rendering process by using an intermediate object– Spherical mapping

– Cylindrical mapping

– Planar mapping

• Or projector functions can be pre-computed during the modeling stage and their results stored with each vertex

Page 89: Computer Graphics Classic Rendering Pipeline Overview.

Intermediate Objects in Projector Functions

• An imaginary “intermediate” object is placed around the the modeled object being textured

• Points on the modeled object are projected onto the intermediate object

• The texture parameter space is wrapped onto the intermediate object in a known way

Page 90: Computer Graphics Classic Rendering Pipeline Overview.

Intermediate Objects in Projector Functions

• Also see p.121 Real-time rendering

Page 91: Computer Graphics Classic Rendering Pipeline Overview.

Intermediate Objects in OpenGL

• OpenGL has a glTexGen function that allows one to specify the type of the type of projector function used– Often this is used to do special types of

mapping, such as environment mapping (later)

• The quadric objects, which are basically the same shape as the intermediate objects, have their texture coordinates generated in this way

Page 92: Computer Graphics Classic Rendering Pipeline Overview.

Pre-computing Projector Functions

• “Texture coordinates” are simply defined at each vertex that directly map the 3D vertex into 2D parameter space

• In OpenGL:glBegin(GL_QUADS)

glTexCoord2f(0, 1); glVertex3f(20, 20, 2); // AglTexCoord2f(0, 0); glVertex3f(20, 10, 2); // BglTexCoord2f(1, 0); glVertex3f(30, 10, 2); // CglTexCoord2f(1, 1); glVertex3f(30, 20, 2); // D

glEnd( );

Page 93: Computer Graphics Classic Rendering Pipeline Overview.

Texture Editors

• Texture editors can be used to help in the manual placement of texture coordinates

Page 94: Computer Graphics Classic Rendering Pipeline Overview.

Interpolating Texture Coordinates

• Texture coordinates only provide (u, v) values at the vertices of the polygon

• We still need to fill in each pixel location in the interior of the polygon

• These are filled by bi-linearly interpolating the texture parameter space coordinate in 2D space– This can be done at the same time as we do the

interpolation for lighting and depth calculations

Page 95: Computer Graphics Classic Rendering Pipeline Overview.

Corresponder Functions

• The Corresponder function takes the (u, v) values and maps them into the texture image space (e.g. 128 pixels by 64 pixels)

Page 96: Computer Graphics Classic Rendering Pipeline Overview.

Corresponder Functions

• Corresponder function allow us to:– Change the size of the image used without

having to redefine our projector functions (or redefine all our texture coordinates)

– Map to subsections of the image– Specify what happens outside the range [0..1]

Page 97: Computer Graphics Classic Rendering Pipeline Overview.

Mapping to a Subsection

• Allows you to store several small texture images into a single large texture image– By default it maps to the entire texture image

Page 98: Computer Graphics Classic Rendering Pipeline Overview.

What Happens Outside [0..1]

• The 3 main approaches are:– Repeat/Tile: Image is repeated multiple times by

simply dropping the integer part of the value

– Clamp: Values are clamped to the range, resulting in the edge values being repeated

– Border: Values outside the range are displayed in a given border color

Page 99: Computer Graphics Classic Rendering Pipeline Overview.

Sampling

• In general, the size of the texture image and the size of the projected surface is not the same

• If the size of the projected surface is larger than the size of the texture image then the image will need to be “blown up” to fit on the surface– This process is called Magnification

• If the size of the projected surface is smaller than the size of the texture image then the image will need to be “shrunk” to fit on the surface– This process is called Minification

Page 100: Computer Graphics Classic Rendering Pipeline Overview.

Magnification

• Recall that there are more pixels than texels

• Thus, we need to sample the texels for each pixel to determine the pixel’s texture color– That is, there is no 1-1 correlation

• There are 2 main ways to sample texels:– Nearest neighbor– Bi-linear interpolation

Page 101: Computer Graphics Classic Rendering Pipeline Overview.

Magnification

• Nearest neighbor sampling simply picks the texel closest to the projected pixel

• Bi-linear interpolation samples the 4 texels closest to the projected pixel and linearly interpolates their values in both the horizontal and vertical directions

Page 102: Computer Graphics Classic Rendering Pipeline Overview.

Magnification

• Nearest neighbor can give a crisper feel when little magnification is occurring, but bi-linear is usually the safer choice– Bi-linear also takes 4+ times as long

• Also see p.130 Real-time rendering

Page 103: Computer Graphics Classic Rendering Pipeline Overview.

Minification

• Recall that there are more texels than pixels

• Thus, we need to integrate the colors from many texels to form a pixel’s texture color

• However, integration of all the associated texels is nearly impossible in real-time

• We need to use a sampling technique

Page 104: Computer Graphics Classic Rendering Pipeline Overview.

Minification

• 2 common sampling techniques are– Nearest neighbor samples the texel value at

the center of the group of associated texels– Bi-linear interpolation samples 4 texel values

in the group of associated texels and bi-linearly interpolates them

• Note that the sampling techniques are the same as in Magnification, but the results are quite different

Page 105: Computer Graphics Classic Rendering Pipeline Overview.

Minification

• For nearest neighbor, severe aliasing artifacts can be seen

• They are even more noticeable as the surface moves with respect to the viewer– Temporal aliasing

• See NeHe Lesson 7 (f to change cycle through filtering modes, page up/down to go forward and back)

• See p.132 Real-time rendering

Page 106: Computer Graphics Classic Rendering Pipeline Overview.

Minification

• Bi-linear interpolation is only slightly better than nearest neighbor for minification

• When more than 4 texels need to be integrated together this filter shows the same aliasing artifacts as nearest neighbor

• See NeHe Lesson 7 (second filter in cycle)

Page 107: Computer Graphics Classic Rendering Pipeline Overview.

Mipmaps

• “mip” stands for “multum in parvo” which is Latin for “many things in a small place”

• The basic idea is to improve Minimization by providing down sampled versions of the original texture image – a pyramid of texture images

Page 108: Computer Graphics Classic Rendering Pipeline Overview.

Mipmaps

• When Minification would normally occur, instead use the mipmap image that most closely matches the size of the projected surface

• If the projected surface falls in between mipmap images:– Use nearest neighbor to pick the mipmap image closest

to the projected surface size

– Or use linear interpolation to pick combine values from the 2 closest mipmap images

Page 109: Computer Graphics Classic Rendering Pipeline Overview.

Sampling in OpenGL

• OpenGL allows you to select a Magnification filter from:– Nearest or Linear

• OpenGL allows you to select a Minification filter from:– Nearest or Linear (without mipmaps)– Nearest or Linear texel sampling with nearest or linear

mipmap selection (4 distinct choices)• (Bi-)linear texel sampling with linear mipmap selection is

often called tri-linear filtering• See NeHe Lesson 7 (adjust choice in code)

Page 110: Computer Graphics Classic Rendering Pipeline Overview.

Blending the Texture Value

• Once a sample texture value has been obtained, we need to blend it with the computed color value

• There are 3 main ways to perform blending:– Replace: Replace the computed color with the texture

color, effectively removing all lighting

– Decal: Like replace but transparent parts of the texture are blended with the underlying computed color

– Modulate: Multiply the texture color by the computed color, producing a shaded and textured surface

Page 111: Computer Graphics Classic Rendering Pipeline Overview.

Blending Restrictions

• The main problem with this simple form of texture map blending is that we can only blend with the final computed color

• Thus, the texture will dim both the diffuse and specular terms, which can look unnatural– A dark object still may have a bright highlight

• If diffuse and specular components can be interpolated across the pixels independently then we could blend the texture with just the diffuse– This is not part of the “Classic Rendering Pipeline” but

several vendors have tried to add implementations

Page 112: Computer Graphics Classic Rendering Pipeline Overview.

Texture Set Management

• Each graphics card can handle a certain number of texture in memory at once– Even though memory in 3D cards has increased

dramatically recently, the general rule of thumb is that you never have enough texture memory

• The card usually has a built-in strategy, like LRU, to manage the working set

• OpenGL allows you to set priorities on the textures to enable you to adjust this process

Page 113: Computer Graphics Classic Rendering Pipeline Overview.

Viewport Mapping

• This is the final transformation that occurs in the Classic Rendering Pipeline

• The Viewport transformation simply maps the generated 2D image to a portion of the 2D window used to display it– By default the entire window is used

• This is useful if you want several “views” of a scene in the same window