Top Banner
Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17
54

Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Jan 01, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Programming for Virtual Reality

Applications

Projection in OpenGL

Lighting and Shading

Lecture 17

Page 2: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

vertex

ModelviewMatrix

ProjectionMatrix

PerspectiveDivision

ViewportTransform

Modelview

Modelview

Projection

object eye

clip normalizeddevice

window

other calculations here material color shade model (flat) polygon rendering mode polygon culling clipping

TransformationPipeline

Page 3: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Matrix Operations

•Specify Current Matrix StackglMatrixMode( glMatrixMode( GL_MODELVIEWGL_MODELVIEW or or GL_PROJECTIONGL_PROJECTION ) )

•Other Matrix or Stack OperationsglLoadIdentity() glLoadIdentity() glPushMatrix()glPushMatrix()glPopMatrix()glPopMatrix()

•Viewport•usually same as window size•viewport aspect ratio should be same as projection transformation or resulting image may be distorted

glViewport( glViewport( x, y, width, heightx, y, width, height ) )

Page 4: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Projection Transformation

Shape of viewing frustum Perspective projection

gluPerspective( gluPerspective( fovy, aspect, zNear, zFarfovy, aspect, zNear, zFar ) )

glFrustumglFrustum(( left,left, right,right, bottom,bottom, top,top, zNear,zNear, zFarzFar ))

Orthographic parallel projectionglOrtho(glOrtho( left,left, right,right, bottom,bottom, top,top, zNear,zNear, zFarzFar ))

gluOrtho2D( gluOrtho2D( left, right, bottom, topleft, right, bottom, top ) ) calls glOrtho with z values near zero

Page 5: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Applying Projection Transformations

Typical use (orthographic projection)glMatrixMode( GL_PROJECTION );

glLoadIdentity();

glOrtho( left, right, bottom, top, zNear, zFar );

Page 6: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Viewing Transformations

Position the camera/eye in the scene place the tripod down; aim camera

To “fly through” a scene change viewing transformation and

redraw scene gluLookAt( eyex, eyey, eyez,

aimx, aimy, aimz, upx, upy, upz ) up vector determines unique orientation

tripod

Page 7: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Modeling Transformations

Move object

glTranslate{fd}( glTranslate{fd}( x, y, zx, y, z ) ) Rotate object around arbitrary axis

glRotate{fd}( glRotate{fd}( angle, x, y, zangle, x, y, z ) ) angle is in degrees

Dilate (stretch or shrink) or mirror object

glScale{fd}( glScale{fd}( x, y, zx, y, z ) )

zyx

Page 8: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Connection: Viewing and Modeling

Moving camera is equivalent to moving every object in the world towards a stationary camera

Viewing transformations are equivalent to several modeling transformationsgluLookAt() has its own command

can make your own polar view or pilot view

Page 9: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Projection is left handed

Projection transformations (gluPerspective, glOrtho) are left handed think of zNear and zFar as distance from view

point Everything else is right handed, including the

vertexes to be rendered

xx

yy

z+

z+

left handed right handed

Page 10: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Building geometry using coordinates

Construct a pyramid by connecting the dots to form triangles

Page 11: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Winding Rule

Using a right-handed coordinate system 3D coordinates are given in a right-handed

coordinate system X = left-to-right . Y = bottom-to-top . Z = back-to-front . Distances are conventionally in meters 扶

Page 12: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Right Hand Rule

Page 13: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Using coordinate order Polygons have a front and back: By default, only the front side of a polygon is rendered . A polygon's winding order determines which side is the

front . Most polygons only need one side rendered . You can turn on double-sided rendering, at a

performance cost .

Page 14: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Using coordinate order

Use the right-hand rule:

Curl your right-hand fingers around the polygon perimeter in the order vertices are given (counter-clockwise)

Your thumb sticks out the front of the polygon

Page 15: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Back-face elimination ... culling

Two Sides to a Polygon: Most polygonal meshes have a well-defined inside and

outside. We use the right-hand rule (CCW counterclockwise

winding) and specify vertices in a counterclockwise order to indicate the outside face.

Culling in OpenGL: By default, OpenGL considers polygons with

counterclockwise specification to be front facing. If we specify, or render, in the opposite direction, we are rendering the back face (inward-facing face of a solid).

Page 16: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

We use the right-hand rule (CCW) to specify the vertices that make up the outside faces of a polygon.

Page 17: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Common Transformation Usage

3 examples of resize() routine restate projection & viewing transformations

Usually called when window resized Registered as callback for glutReshapeFunc()

Page 18: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

resize(): Perspective & LookAt

void resize( int w, int h ){ glViewport( 0, 0, (GLsizei) w, (GLsizei) h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 65.0, (GLdouble) w / h, 1.0, 100.0 );

glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 0.0, 0.0, 5.0,

0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );

}

Page 19: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

19

resize(): Perspective & Translate

Same effect as previous LookAtvoid resize( int w, int h ){ glViewport( 0, 0, (GLsizei) w, (GLsizei) h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 65.0, (GLdouble) w/h, 1.0, 100.0 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glTranslatef( 0.0, 0.0, -5.0 );}

Page 20: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

resize(): Orthovoid resize( int width, int height ){ GLdouble aspect = (GLdouble) width / height; GLdouble left = -2.5, right = 2.5; GLdouble bottom = -2.5, top = 2.5; glViewport( 0, 0, (GLsizei) w, (GLsizei) h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity();

if ( aspect < 1.0 ) { left /= aspect; right /= aspect; } else { bottom *= aspect; top *= aspect; } glOrtho( left, right, bottom, top, near, far ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity();}

Page 21: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Compositing Modeling Transformations

Problem 1: hierarchical objects one position depends upon a previous position robot arm or hand; sub-assemblies

Solution 1: moving local coordinate system modeling transformations move coordinate

system post-multiply column-major matrices OpenGL post-multiplies matrices

Page 22: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Compositing Modeling Transformations

Problem 2: objects move relative to absolute world origin my object rotates around the wrong origin

make it spin around its center or something else Solution 2: fixed coordinate system

modeling transformations move objects around fixed coordinate system

pre-multiply column-major matrices OpenGL post-multiplies matrices must reverse order of operations to achieve desired effect

Page 23: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Lighting, Shading

Lighting and shading give objects “shape” Important effects

shading shiny highlights reflections shadows

Local techniques simplify these effects to improve performance

Page 24: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Shading/ lighting

Diffuse ambient light creates no shading... Simplest

Illumination can vary by angle between N (normal to the polygon) and L (the source)

Source of illumination can be a point or a region (expressed as cosn ). The larger the n the narrower the beam

Compute N and L across polygon face

Page 25: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Shading

Can interpolate shade across a polygon Gouraud shading interpolates shade across

edges, reduces effect of intensity change. Phong shading (and illumination) interpolates

surface normal vector across polygons then interpolates illumination.

Page 26: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Shading

A reflection (diffuse + specular + florescence) model describes the interaction of light with a surface, in terms of the properties of the surface and the nature of the incident light.

Surface : surface normal + material (combination of ka, kd, ks and n)

I = Iaka + Ii[kd(L•N) + ks (R•V)n]/(r+k)

1V

1R

NL

Page 27: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Shading Compute lighting based on angle of light

on polygon surface.Surface normal

Page 28: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Gouraud Shading Compute shading for each pixel by averaging

shading based on distance and shading of vertices.

Page 29: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Gouraud

Each face has a normal.

Vertex normals are the averages of the faces surrounding them.

Note: with OpenGL, user supplies vertex normals directly.

Page 30: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Gouraud

Once normals of vertices of face P known, then the illumination of those points can be computed with lighting equation.

Then, when scan-converting polygon P, the pixel colour (lighting) is simply linearly interpolated from these vertex lighting values.

In example right, only 4 lighting computations done; rest of face is interpolated from these values.

Page 31: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Different Illumination

Page 32: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Different Illumination

Page 33: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Global illumination Global techniques provide more accuracy by sim

ulating light propagation among all surfaces in a 3D world.

Local shading (Gauroud shading, Phong shading) does not calculate global effect (shadow, reflection, refraction, scattering, etc)

Technique ray tracing radiosity

Page 34: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Solid Modeling Which surfaces should be drawn? Object space methods

Hidden Surface Removal Painters Algorithm BSP Trees

Image space methods Z-Buffering Ray Casting

Page 35: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Volume Rendering

Ray Traced Texture Mapped

Page 36: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Ray Tracing

Image space technique that mimics physical processes of light

Extremely computationally intensive, but beautiful Hidden surface removal Transparency Reflections Refraction Ambient lighting Point source lighting Shadows

Page 37: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Shadows and beyond

Can be computationally intensive, project each polygon on light source find projection on other polygons

3D shadows, transparency, fog and atmospherics all complicate the computation.

Page 38: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Why Lighting?

What light source is used and how the object response to the light makes difference Ocean looks bright bluish green in sunny day but dim gray

green in cloudy day

Lighting gives you a 3D view to an object A unlit sphere looks no different from a 2D disk

To get realistic pictures, the color computation of pixels must include lighting calculations

Page 39: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Types of Light

Ambient Light that’s been scattered so much by the environment

that its direction is impossible to determine - it seems to come from all directions

Diffuse Light that comes from one direction, but it gets scattered

equally in all directions

Specular Light comes from a particular direction, and its tends to

bounce off the surface in a preferred direction

Page 40: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Important factors in illumination

surface properties light properties

(colour, intensity) position of light(s),

viewer eye, object (polygon):

Page 41: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Diffuse and Specular Reflection

•n - normal of point/polygon

•s - direction to light

•r - reflection angle to s

•v - direction to user eye

•B - angle between v and r

Page 42: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.
Page 43: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Materials Colors

A material’s color depends on the percentage of the incoming different lights it reflects

Materials have different ambient, diffuse and specular reflectances

Material can also have an emissive color which simulates light originating from an object Headlights on a automobile

Page 44: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

OpenGL Lighting Model Lighting has four independent components that are computed

independently Emission, Ambient, Diffuse, and Specular

OpenGL approximates lighting as if light can be broken into red, green, and blue components

The RGB values for lights mean different than for materials For light, the numbers correspond to a percentage of full intensity

for each color For materials, the numbers correspond to the reflected proportions

of those colors

Total effect is a combination of corresponding components of incoming light and illuminated material surface

(LR*MR, LG*MG, LB*MB)

Page 45: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Adding Lighting to the Scene Define normal vectors for each vertex of each

object

Create, select, and position one ore more light sources

Create and select a lighting model

Define material properties for the objects in the scene

Page 46: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Creating Light Sources Properties of light sources are color, position, and direction

void glLight{if}(GLenum light, GLenum pname, TYPE param);

void glLight{if}v(GLenum light, GLenum pname, TYPE *param);

Creates the light specified by light that can be GL_LIGHT0, GL_LIGHT1, … or GL_LIGHT7

Pname specifies the characteristics of the light being set Param indicates the values to which the pname characteristic

is set

glEnable(GL_LIGHT0);

Page 47: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Color for a Light Source GLfloat light_ambient[] = {0.0,0.0,0.0,1.0}; GLfloat light_diffuse[] = {1.0,1.0,1.0,1.0}; GLfloat light_specular[] = {1.0,1.0,1.0,1.0};

glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

Page 48: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Position of Light Source Positional light source

(x, y, z) values specify the location of the light GLfloat light_position[] = {x, y, z, w}; glLightfv(GL_LIGHT0, GL_POSITION, light_position);

Directional light source (x, y, z) values specify the direction of the light located at the

infinity No attenuation

GLfloat light_position[] = {x, y, z, 0.0}; glLightfv(GL_LIGHT0, GL_POSITION, light_position);

Page 49: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Multiple Lights

You can define up to eight light sources Need to specify all the parameters defining the position and

characteristics of the light

OpenGL performs calculations to determine how much light each vertex from each source

Increasing numbers of lights affects performance

Page 50: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Controlling a Light’s Position and Direction

A light source is subject to the same matrix transformations as a geometric model

Position or direction is transformed by the current modelview matrix and stored in eye coordinates

Keeping the light stationary Specify the light position after modelview transformations

Independently moving the light Set the light position after the modeling transformation that

you want to apply for light

Moving the light together with the viewpoint Set the light position before the viewing transformation

Page 51: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Selecting a Lighting Model How to specify a lighting model

glLightModel{if}(GLenum pname, TYPE param);glLightModel(if}v(GLenum pname, TYPE *param); Sets properties of the lighting model Pname defines the characteristic of the model being set Param indicates the values to which the pname characteristic is

set

Needs to be enabled or disabled glEnable(GL_LIGHTING); glDisable(GL_LIGHTING);

Page 52: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Components of Lighting Model Global ambient light

Ambient light from not any particular source Glfloat lmodel_ambient[] = {0.2, 0.2, 0.2, 1.0} glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

Local or Infinite viewpoint Whether the viewpoint position is local to the scene or whether

it should be considered to be an infinite distance away glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,

GL_TRUE); Default is an infinite viewpoint

Two-sided lighting Whether lighting calculations should be performed differently for

both the front and bacl faces of objects glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

Page 53: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Defining Material Properties Specifying the ambient, diffuse, and specular colors, the

shininess, and the color of any emitted light

void glMaterial{if}(GLenum face, GLenum pname, TYPE param);

void glMaterial{if}v(GLenum face, GLenum pname, TYPE *param);

Specifies a current material property for use in lighting calculations

Face can be GL_FRONT, GL_BLACK, or GL_FRONT_AND_BACK

Pname identifies the particular material property being set Param defines the desired values for that property

Page 54: Programming for Virtual Reality Applications Projection in OpenGL Lighting and Shading Lecture 17.

Reflectances Diffuse and ambient reflection

Gives color GLfloat mat_amb_diff[] = {0.1, 0.5,0.8,1.0}; glMaterialfv(GL_FRONT_AND_BACK,

GL_AMBIENT_AND_DIFFUSE, mat_amb_diff); Specular reflection

Produces highlights GLfloat mat_specular[] = {1.0,1.0,1.0,1.0); Glfloat low_shininess[] = {5.0}; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);

Emission Make an object glow (to simulate lamps and other light

sources GLfloat mat_emission[] = {0.3,0.2,0.2,0.0}; glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);