41
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2014 OpenGL Graphics L04-Lighting

OpenGL L04-Lighting

Embed Size (px)

DESCRIPTION

OpenGL L04-Lighting

Citation preview

Page 1: OpenGL L04-Lighting

Mohammad Shaker

mohammadshaker.com

@ZGTRShaker

2014

OpenGL Graphics

L04-Lighting

Page 2: OpenGL L04-Lighting

What we’ve learnt so far..

Page 3: OpenGL L04-Lighting

RULETO SEE A 3D SCENE YOU SHOULD SET UP:

CAMERA (Singleton, for all objects)

PROJECTION (Singleton, for all objects)

WORLD MATRIX (For each object separately)

Page 4: OpenGL L04-Lighting

Lighting

Page 5: OpenGL L04-Lighting

How We See Things?

Page 6: OpenGL L04-Lighting

How We See Things?

Page 7: OpenGL L04-Lighting

Light and Color Systems

Additive Colors Subtractive Colors

Page 8: OpenGL L04-Lighting

Colors in OpenGL

• glColor3f(red, green, blue) [0,1]

• glColor4f(red, green, blue, alpha) [0,1]

Page 9: OpenGL L04-Lighting

Colors in OpenGL

• glColor3f(red, green, blue) [0,1]

• glColor4f(red, green, blue, alpha) [0,1]– alpha for transparency [0; full,1: no]

Page 10: OpenGL L04-Lighting

Color Filling Mode

• Filling Mode

– glShadeModel(GL_FLAT);

• Fill the polygon with solid color (the same as last vertex color)

– glShadeModel(GL_SMOOTH);

• Interpolate colors between polygon edges

Page 11: OpenGL L04-Lighting

Light’s Type

• DIFFUSE color's alpha value actually determines the transparency of the polygon

Only ambient light only specular lightonly diffuse light

Page 12: OpenGL L04-Lighting

The Modified Phong Model

• Computes a color or shade for each vertex using a lighting model (the modified

Phong model) that takes into account

– Diffuse reflections

– Specular reflections

– Ambient light

– Emission

• Vertex shades are interpolated across polygons by the rasterizer

Page 13: OpenGL L04-Lighting

Lighting - Types

• Ambient Light: doesn’t come from any particular direction. It has an original

source somewhere, but the rays of light have bounced around the room or scene

and become directionless.

Page 14: OpenGL L04-Lighting

Lighting - Types

• Diffuse Light: The diffuse part of an OpenGL light is the directional component

that appears to come from a particular direction and is reflected off a surface with

an intensity proportional to the angle at which the light rays strike the surface.

Thus, the object surface is brighter if the light is pointed directly at the surface

than if the light grazes the surface from a greater angle.

Page 15: OpenGL L04-Lighting

Lighting - Types

• Specular light: Like diffuse light, specular light is a highly directional property, but

it interacts more sharply with the surface and in a particular direction.

Page 16: OpenGL L04-Lighting

The Modified Phong Model

• The model is a balance between simple computation and physical realism

• The model uses

– Light positions and intensities

– Surface orientation (normals)

– Material properties (reflectivity)

– Viewer location

• Computed for each source and each color component

Page 17: OpenGL L04-Lighting

Material Properties

• Define the surface properties of a primitive

• you can have separate materials for front and back

Property Description

Diffuse Base object color

Specular Highlight color

Ambient Low-light color

Emission Glow color

Shininess Surface smoothness

Page 18: OpenGL L04-Lighting

Material Properties

• Define the surface properties of a primitive

• you can have separate materials for front and back

Property Description

GL_DIFFUSE Base color

GL_SPECULAR Highlight Color

GL_AMBIENT Low-light Color

GL_EMISSION Glow Color

GL_SHININESS Surface Smoothness

Page 19: OpenGL L04-Lighting

Color Theory

Page 20: OpenGL L04-Lighting

Color Theory

Page 21: OpenGL L04-Lighting

Lighting in OpenGL

Page 22: OpenGL L04-Lighting

Lighting in OpenGLLight Source and Object Material

Page 23: OpenGL L04-Lighting

Lighting in OpenGLLight Source and Object Material

Page 24: OpenGL L04-Lighting

Turning on the Lights

• For any type of light, you should first

– Flip each light’s switch

glEnable(GL_LIGHT0);

– Turn on the power

glEnable(GL_LIGHTING);

Page 25: OpenGL L04-Lighting

Lighting in OpenGL – Light Source

• For the light source:

– Set light properties:float Light_Ambient [ ] = {0.2,0.2,0.2,1};

float Light_Diffuse [ ] = {1,1,1,1};

float Light_Specular [ ] = {1,1,1,1};

float Light_Position [ ] = {0,0,20,0};

– Enable a specific light (GL_LIGHT0):glEnable(GL_LIGHTING);

glEnable(GL_LIGHT0);

– Bind the light properties with the specific light (GL_LIGHT0):glLightfv(GL_LIGHT0 , GL_AMBIENT , Light_Ambient);

glLightfv(GL_LIGHT0 , GL_DIFFUSE , Light_Diffuse);

glLightfv(GL_LIGHT0 , GL_SPECULAR , Light_Specular);

glLightfv(GL_LIGHT0 , GL_POSITION , Light_Position);

Page 26: OpenGL L04-Lighting

Lighting in OpenGL – Object Material

• For the object material:

– Set material properties:float Cone_Ambient [ ] = {0.1,0.1,0.1,1};

float Cone_Diffuse [ ] = {0.2,0.1,0.9,1};

float Cone_Specular [ ] = {1,1,1,1};

float Cone_Shininess = 100;

– Bind the material properties with a specific object:glMaterialfv(GL_FRONT_AND_BACK , GL_AMBIENT , Cone_Ambient);

glMaterialfv(GL_FRONT_AND_BACK , GL_DIFFUSE , Cone_Diffuse);

glMaterialfv(GL_FRONT_AND_BACK , GL_SPECULAR , Cone_Specular);

glMaterialf (GL_FRONT_AND_BACK , GL_SHININESS , Cone_Shininess);

• Now draw whatever you want.GLUquadric *quadric = gluNewQudric();

gluCylinder(quadric, 4, 1, 7, 32, 32);

Page 27: OpenGL L04-Lighting

Lighting ExampleComplete Code

Page 28: OpenGL L04-Lighting

Lighting Example

Page 29: OpenGL L04-Lighting

SpotLight Lighting ExampleComplete Code

Page 30: OpenGL L04-Lighting

Lighting Example

Page 31: OpenGL L04-Lighting

Normals for Lighting

Page 32: OpenGL L04-Lighting

Why Normals are essential for lighting?

Page 33: OpenGL L04-Lighting

Why Normals are essential for lighting?

Page 34: OpenGL L04-Lighting

Normals for Lighting

• Vertex Normals– Prefixed w/ “vn” (Wavefront)

– Contains x,y,z of normal

– Not necessarily unit length

– Not necessarily in vertex order

– Indexed as with vertices(x0,y0,z0)

(a0,b0,c0)

(u0,v0)

(x1,y1,z1)

(a1,b1,c1)

(u1,v1)

(x2,y2,z2)

(a2,b2,c2)

(u2,v2)

Page 35: OpenGL L04-Lighting

Surface Normals

• Normals define how a surface reflects light

– Application usually provides normals as a vertex atttribute

– Current normal is used to compute vertex’s color

– Use unit normals for proper lighting

• scaling affects a normal’s length

Page 36: OpenGL L04-Lighting

Vector Normalization

Page 37: OpenGL L04-Lighting

Vector NormalizationWhy to?

Page 38: OpenGL L04-Lighting

Advanced Lighting, Spotlight

Page 39: OpenGL L04-Lighting

Shadowing

Page 40: OpenGL L04-Lighting

Shadowing

• How to make a shadow?

– Draw the whole object again, projecting it on the shadow plane (the plane in which the

shadow will appear)

Page 41: OpenGL L04-Lighting

Shadowing