1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

41
1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL

Transcript of 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

Page 1: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

1

MAE152Computer Graphics for Scientists and Engineers

Lighting in OpenGL

Page 2: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

2

Graphics: Conceptual Model

Real Object

Real Object

Human Eye

Human Eye

DisplayDevice

Graphics System

SyntheticModel

SyntheticCamera

Real Light

SyntheticLight Source

Page 3: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

3

Lights, Surfaces, and Imaging

Proj. Plane

Viewer

Objects

Page 4: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

4

Lights, Surfaces, and Imaging

Proj. Plane

Viewer

Light Source

Objects

Page 5: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

5

Modes of Interaction of Light with Materials

SpecularSurface

DiffuseSurface Translucent

Surface

Note: Any given surface can have some of all three properties.

Page 6: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

6

Illuminating Surfaces

z

x

yI( ,,)

I(x, y, z,,, )We can define illumination function:To obtain total light, must integrate over total surface.

Itotal ( ) I(x, y, z, ,, ) dxdydz

Page 7: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

7

Simplified Model

Exact model can easily get complicated!Three simplifications help.

1. We can consider four classes of light sources

• ambient• point• spotlight• distant

2. Human color perception allows us to consider illuminationfunction in terms of the three primary colors.

3. We can neglect (OpenGL caveats):

• multiple reflections• obstruction of light path by objects

Page 8: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

8

Light SourcesAmbient light- no identifiable source or direction- hack for replacing true global illumination

= (light bouncing off from other objects)

Page 9: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

9

Ambient Light

Simulates situations where light sources are designed to produceuniform lighting throughout a scene.

Characterized by a scalar intensity, Ia , that is identical at every pointin the scene.

Ia Iar

Iag

Iab

Although every surface receives the same illumination, eachsurface can reflect the light differently.

Page 10: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

10

Point Sources

An ideal point source radiates equally in all directions. It canbe characterized by:

I(p0 ) I r (p0 )

Ig (p0 )

Ib (p0 )

Intensity of illumination from a point source is proportionalto the inverse square of the distance between the point and theilluminated surface.

I(p,p0 ) 1

p p0

2 I(p0 )

Page 11: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

11

Light Sources

Types of light sources

- glLightfv(GL_LIGHT0,GL_POSITION,light[])

- directional/parallel lights= real-life example: sun= infinitely far source: homogeneous co-ord. w=0

- point lights= same intensity in all directions

- spot lights= limited set of directions:

point+direction+cutoff angle

0

z

y

x

1

z

y

x

Page 12: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

12

Spotlights

Spotlights are point sources for which the angle through whichlight has been emitted has been limited.

l

s

I k cose ( ) k(ls)e

Page 13: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

13

Distant Sources

Most shading calculations require the direction from the point ona surface to the light source. As light sources are moved to larger distances, this direction approaches a constant.

Therefore the point source location will be replaced by a vectorindicating the direction of the source.

p0

x

y

z

1

will be replaced by : p0

x

y

z

0

Page 14: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

14

Lighted Sphere Demo and Source Code

Page 15: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

15

Reflection

Rough Surface:Light is reflected equally in all directions.

Diffuse

Smooth Surface:Light is reflected at an anglenear to the incident angle.Specular

Page 16: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

16

Vectors Used byPhong Lighting Model

p is an arbitrary point on a surface.n is the unit normal to the surface at p.l is a unit vector in the direction of the light source.r is a unit vector in the direction of a perfectly (specular) reflectionv is a unit vector in the direction of the viewer.

n

l r

v

p

Viewer

Page 17: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

17

Elements of the Phong Lighting Model

R i Rira Riga Riba

Rird Rigd Ribd

Rirs Rigs Ribs

At each point p there is a reflection matrix forthe ith light source:

I Ia Id Is La Ra Ld Rd Ls Rs

Assuming the calculations will be done separately foreach primary, we can sum over light sources to get:

L i Lira Liga Liba

Lird Ligd Libd

Lirs Ligs Libs

At each point p thereis an illumination matrixfrom the ith light source:

red green blue

ambient

diffuse

specular

Page 18: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

18

Elements of the Phong Lighting ModelAmbient Reflection

Ia ka La

0 ka 1

The intensity of ambient light is the same at every pointon the surface. The percentage of light reflected is givenby: Ra ka

Note that the ambient reflection terms can be differentfor red, green and blue.

Page 19: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

19

Elements of the Phong Lighting ModelDiffuse Reflection

Intensity of illumination is dependent upon the incidence angleof light from the source:

Adding a term for attenuationwith distance from source:

Id kd

a bd cd 2(ln)Ld

l

n

Rd cos ln

Id kd (ln)Ld

Page 20: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

20

Elements of the Phong Lighting ModelSpecular Reflection

For a shiny surface, most light is reflected around vector r,corresponding to a reflected angle equal to the angle of incidence.If is the angle between v and r,

Is ks Ls cos Adding a distance term, and expressing the cosine in terms ofa dot product:

Is 1

a bd cd 2ks Ls (r v)

n

l r

v

p

Viewer

Page 21: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

21

The Complete Lighting Model

globalambglobalambm

aassdd

n

i

LkE

LkLkLkcdbda

I

)])()((1

[2

1

0

vrnl

Distance(Attenuation)

Ambient

Diffuse Specular

EmissionGlobalAmbient

n

l r

v

p

Viewer

()

()

()

glMaterial

glLight

elglLightMod

k

L

E

Page 22: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

22

Calculation of Normals

Normal exists at every point for mathematically defined smooth surfaces.

Exists for each polygon in surface defined by flat polygons.What about the shared lines/points?

Polygonal Case:

p0

p1

p2

n

n (p1 p0 ) (p2 p0 )

Page 23: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

23

Calculation of NormalsMathematically Defined Surface: Sphere

Sphere is defined implicitly by equation:

f (x, y, z) x2 y2 z2 1 0

n

f

xf

yf

z

2x

2y

2z

2p

Page 24: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

24

Specification of the Normal in OpenGL

Specification of the current normal is modal and associated withvertices.

glNormal3f(nx,ny,nz);glNormal3fv(pointer_to_normal_vector);

You have to calculate the normals yourself.

Page 25: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

25

Shading in OpenGL:Flat Shading

OpenGL uses the normal of the first vertex of a single polygon to determine the color.

Requested by: glShadeModel(GL_FLAT);

Flat shading exaggerates the visual effect of the boundaries between polygons due to the Mach band effect.

Page 26: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

26

Mach Band Effect(Mach, 1865)

Color Science, Concepts and MethodsWyszecki, Stiles

Page 27: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

27

Mach Band Effect

Page 28: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

28

Mach Band Effect

Page 29: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

29

Shading in OpenGL:Smooth Shading

Requested by: glShadeModel(GL_SMOOTH);

Lighting calculations will be done at each vertex using the material properties, and the vectors v and l calculated for that vertex.

Bilinear interpolation is used to determine color values in the interior of the polygon.

Gouraud Shading:

Normal at a vertex is the normalized average of the normals of the polygons that share that vertex.

Page 30: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

30

Gouraud Shading

Page 31: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

31

Lighting Implementation in OpenGL

Page 32: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

32

Lighted Sphere Demo and Source Code

Page 33: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

33

Enable Depth BufferFor Hidden Surface Removal

//before enablingwhile (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT); draw_3d_object_A(); draw_3d_object_B(); }

//after enablingglutInitDisplayMode (GLUT_DEPTH | .... ); glEnable(GL_DEPTH_TEST); ... while (1) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); get_viewing_point_from_mouse_position(); draw_3d_object_A();draw_3d_object_B();}

Page 34: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

34

Specifying Light Sources in OpenGL

General form: glLightf(source, parameter, value); glLightfv(source, parameter, *array);

source is one of at least eight lights: GL_LIGHTi

Parameters:

GL_AMBIENT contains four values that specify the ambient RGBAintensity of the light. Default is (0.0, 0.0, 0.0, 1.0).

GL_DIFFUSE contains four values that specify the diffuse RGBAintensity of the light. Default is (1.0, 1.0, 1.0, 1.0).

GL_SPECULAR contains four values that specify the secular RGBAintensity of the light. Default is (1.0, 1.0, 1.0, 1.0).

Page 35: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

35

Specifying Light Sources in OpenGLGeneral form: glLightf(source, parameter, value); glLightfv(source, parameter, *array);

GL_POSITION specifies the position of the light in homogeneousobject coordinates. If the w component is zero, the lightis treated as a directional source.

GL_SPOT_DIRECTION specifies the direction of the light in homogeneous object coordinates. Default is (0.0, 0.0, -1.0)

GL_SPOT_EXPONENT spotlight exponent, default 0.0

GL_SPOT_CUTOFF spot cutoff angle in [0,90] or (default) 180.

l

s

I k cose ( ) k(ls)e

Page 36: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

36

Specifying Light Sources in OpenGLGeneral form: glLightf(source, parameter, value); glLightfv(source, parameter, *array);

GL_CONSTANT_ATTENUATION constant atten. factor, default 1.0

GL_LINEAR_ATTENUATION linear atten. factor, default 0.0

GL_QUADRATIC_ATTENUATION quadratic atten. factor, default 0.0

Id kd

a bd cd 2(ln)Ld

n

l rv

p

Viewer

Page 37: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

37

Enabling the Lights and Lighting

Enabling a specific light source: glEnable(GL_LIGHTi );

Enabling the lighting model: glEnable (GL_LIGHTING);

Page 38: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

38

Specifying Materials in OpenGL

General form: glMaterialf(face, parameter,value); glMaterialfv(face, parameter,*array);

face is GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

parameter is:

GL_AMBIENT four values that specify the ambient RGBA reflectance of the material. (0.2,0.2,0.2,1.0)

GL_DIFFUSE four values that specify the diffuse RGBA reflectance of the material. (0.8,0.8,0.8,1.0)

GL_SPECULAR four values that specify the ambient RGBA reflectance of the material. (0.0,0.0,0.0,1.0)

GL_SHININESS specifies the specular reflectance exponent of the material. 0.0

Page 39: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

39

1. Ambient light in a scene with 3

spheres.

2. Diffuse light hitting the surface

of 3 spheres. Notice, the spheres

look matte and almost plastic like.

3. The three spheres illuminated by specular light. Imagine an extremely shiny billiard ball and the sheen it creates

Types of Light and effects

Page 40: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

40

Demo

Rotating light

Page 41: 1 MAE152 Computer Graphics for Scientists and Engineers Lighting in OpenGL.

55

End of Lighting