COMP341 Computer Graphics by gabyion

VIEWS: 27 PAGES: 24

									Real World Lighting
Incident Light Reflected Light Surface
• If only green is reflected, red and blue are absorbed, then object appears green
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh 1

Lights Reflected Towards Viewer
• Emissive Light
– Originate from objects that glow by themselves

• Reflected Light
– Reflect part of the incoming light from light sources towards the viewer

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

2

OpenGL Lighting
• Light source is characterized by the amount of RGB emitted • Material of surface is characterized by the amount of RGB reflected towards the viewer

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

3

Different Kinds of Light Sources
• Distant (directional) light source
– Located infinitely far away – Specified as w=0

• Point (positional) light source
– Specified as w=1 – By default, radiate in all directions – Spotlight, radiate within a cone
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh 4

Defining Light in OpenGL
• glLight{if}v (Glenum light, Glenum pname, TYPE *param)
– light: GL_LIGHT0, …, GL_LIGHT7 – pname: e.g. GL_AMBIENT, GL_DIFFUSE, GL_POSITION – *param: a pointer to RGBA value, position of light, etc
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh

5

Multiple Lights
• Can support at least 8 lights • Each light is identified by constants: GL_LIGHTn, where n = 0, 1, …, 7

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

6

pname in glLight
• GL_AMBIENT
– Specifies the ambient RGBA intensities of light source

• GL_DIFFUSE
– Specifies the diffuse RGBA intensities of light source
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh

7

pname in glLight (Con’t)
• GL_SPECULAR
– Specifies the specular RGBA intensities of a light source

• GL_POSITION
– Specifies the (x, y, z, w) position of a light source
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh

8

Constructing Spotlights
• glLight{if} (lightNo, GL_SPOT_CUTOFF, degree) • glLight{if}v (lightNo, GL_SPOT_DIRECTION, spot_direction)
GL_SPOT_DIRECTION GL_SPOT_CUTOFF

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

9

Switch on the Lights
• glEnable(GL_LIGHTING)
– Enable light calculations

• glEnable(GL_LIGHT0)
– Turn on a light source

• glDisable(GL_LIGHT0)
– Turn off a light source

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

10

Defining Material in OpenGL
• glMaterial{if}v (GLenum face, GLenum pname, TYPE *param)
– face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK – pname: GL_AMBIENT_AND_DIFFUSE, GL_SPECULAR, GL_SHININESS – *param: a pointer to the value RGBA value
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh 11

pname in glMaterial
• GL_AMBIENT
– Specifies fraction of ambient light reflected

• GL_DIFFUSE
– Specifies fraction of diffuse light reflected

• GL_SPECULAR
– Specifies fraction of specular light reflected

• GL_SHININESS
– Specifies the specular exponent (shininess)
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh 12

Material Properties
• Ambient light
– Light being scattered by the environment so much that its direction is impossible to determine

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

13

Material Properties
• Diffuse
– Defines the fraction of light scattered into all directions – Defines the color of the surface

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

14

Material Properties
• Specular
– Defines the fraction of light reflected near the mirror reflection direction – The shinier the object, the more focused is the highlight
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh 15

Rendering Polygonal Mesh
• Polygonal mesh: A collection of
– Vertices – Edges – Faces

Edge

Face

Normal Vector
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh 16

Normal Vector
• A vector which is perpendicular to a surface at a point • Determines how the point reflects the incident light

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

17

Normal Vector (Con’t)
• Planar surface (e.g. cube faces)
– Same normal for entire surface

• Curved surface
– Different normals at different points

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

18

Finding Vertex Normals
• Compute from analytical representation (e.g. Bezier surfaces) • Average face normals • Special effects, e.g.bump mapping
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh 19

Hints for Building Polygonal Surfaces
• Consistent polygon orientation
– Counterclockwise when looking from outside

• Avoid non-triangular polygons
– Because they may not be planar

• Display speed vs quality
– Small polygons better quality

• Avoid T-intersections

A
COMP341 Fall 2002

B

C

Lab5: Lighting, Rendering Polygonal Mesh

20

Polygonal mode
• glPolygonMode(GL_FILL)
– Shade the interior of polygon

• glPolygonMode(GL_LINE)
– Draw the edges of polygon – For drawing wireframe model

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

21

Shading Model
• Flat shading
– glShadeModel(GL_FLAT) – With a single color for each polygon

• Smooth shading (Gourand shading)
– glShadeModel(GL_SMOOTH) – Interpolate colors at vertices to find color for interior pixels
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh 22

Rendering Polygon
glShadeModel(GL_FLAT); glBegin(GL_POLYGON) glNormal3fv(n1); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v4); glEnd();

v3

v4
v2 v1

n1

COMP341 Fall 2002

Lab5: Lighting, Rendering Polygonal Mesh

23

Rendering Polygon (Con’t)
glShadeModel(GL_SMOOTH); glBegin(GL_POLYGON) glNormal3fv(n1); glVertex3fv(v1); glNormal3fv(n2); glVertex3fv(v2); glNormal3fv(n3); glVertex3fv(v3); glNormal3fv(n4); glVertex3fv(v4); glEnd();
COMP341 Fall 2002 Lab5: Lighting, Rendering Polygonal Mesh

n3 n4 n1

n2

24


								
To top