You are on page 1of 33

CSC 307 1.

Graphics Programming

Budditha Hettige Department of Statistics and Computer Science

Lighting
Budditha Hettige

Real-World and OpenGL Lighting

Budditha Hettige

Using a Light Source

Budditha Hettige

Lighting Principles
Lighting simulates how objects reflect light
material composition of object lights color and position global lighting parameters
ambient light two sided lighting

available in both color index and RGBA mode

Budditha Hettige

How OpenGL Simulates Lights


Phong lighting model
Computed at vertices

Lighting contributors
Surface material properties Light properties Lighting model properties

Budditha Hettige

OpenGL Lights

Common Properties

Ambient Diffuse Specular

Types

Directional Light Point Light Spotlight


Budditha Hettige

Lighting.

Budditha Hettige

Surface Normals
CPU

Poly. DL

Per Vertex Raster Texture Frag FB

Pixel

Normals define how a surface reflects light


glNormal3f( x, y, z )

Current normal is used to compute vertexs color Use unit normals for proper lighting
scaling affects a normals length glEnable( GL_NORMALIZE ) or glEnable( GL_RESCALE_NORMAL )
9 Budditha Hettige

Material Properties
Define the surface properties of a primitive
glMaterialfv( face, property, value );
GL_DIFFUSE GL_SPECULAR GL_AMBIENT GL_EMISSION GL_SHININESS
Base color Highlight Color Low-light Color Glow Color Surface Smoothness

separate materials for front and back


10 Budditha Hettige

Light Properties
glLightfv( light, property, value ); light specifies which light
multiple lights, starting with GL_LIGHT0 glGetIntegerv( GL_MAX_LIGHTS, &n );

properties
colors position and type attenuation

11

Budditha Hettige

Surface lighting

Surface lighting properties


Values defined in RGB colorspace Ambient

Light reaching all points on a surface Light scattering from rough surfaces

Diffuse

Specular

Light reflection from shiny surfaces


Defines the spread of the specular term Larger values have more mirror like specularity

Shininess

Budditha Hettige

12

Lighting model

Light Properties

Ambient

Color and intensity of light that interacts with a surface materials ambient property Color and intensity of light that interacts with a surface materials diffuse property Color and intensity of light that interacts with a surface materials specular property

Diffuse

Specular

Budditha Hettige

13

Lighting model

Ambient

Diffuse

Specular

Combination

Budditha Hettige

14

Controlling a Lights Position


Modelview matrix affects a lights position
Different effects based on when position is specified
eye coordinates world coordinates model coordinates

Push and pop matrices to uniquely control a lights position

15

Budditha Hettige

GLUT simple geometry

GLUT can render simple geometry

Sphere, Box, Cone, Torus, Teapot, etc


radius,

void glutSolidSphere(GLdouble
GLint slices, GLint stacks);

radius: radius of the sphere slices: number of longitudinal subdivitions stacks: number of latitudinal subdivitions

void glutSolidCube(GLdouble size);

size: length of all sides of the cube


Budditha Hettige

16

Tips for Better Lighting


Recall lighting computed only at vertices
model tessellation heavily affects lighting results
better results but more geometry to process

Use a single infinite light for fastest lighting


minimal computation per vertex

17

Budditha Hettige

Setting up state for lighting


Lighting and lights are disabled by default Enabling lighting overrides the use of assigned colors from glColor calls glShadeModel(GL_SMOOTH); Set Gouraud shading glShadeModel(GL_FLAT); Set Flat Shading glEnable(GL_LIGHTING); Enable fixed function lighting glEnable(GL_LIGHT0); Enable light 0 in the scene
Budditha Hettige

18

Hidden Surface removal

First Enable Culling


glEnable(GL_CULL_FACE); glCullFace(GLenum face); Face: The polygon facing
GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

Define which face to cull

Budditha Hettige

19

Assigning Material Properties

void glMaterialfv( GLenum

face,

GLenum pname, const GLfloat * params);

face: The geometry facing to assign

GL_FRONT,GL_BACK, or GL_FRONT_AND_BACK

pname: The material property to assign

GL_AMBIENT,GL_DIFFUSE,GL_SPECULAR, and others

params: pointer to an array of values


Budditha Hettige

20

OpenGl calls array pointers

Some sets of OpenGL methods take multiple numbers and types of arguments

glVertex2i glMaterialfv

Type Suffix GLbyte b GLshort s GLint, GLsizei i GLfloat, GLclampf f Type of argument Number of arguments GLdouble, GLclampd d Method takes an array GLubyte, pointer argument GLboolean ub GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; GLushort us GLuint, glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); GLenum, GLbitfield ui
Budditha Hettige

21

Assign Material Property


//Define GLfloat arrays to hold material data GLfloat mat_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; GLfloat mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };

//Shininess will be an array of length 1


GLfloat mat_shininess[] = { 50.0 };

//Apply the material properties to the front facing


glMaterialfv(GL_FRONT, glMaterialfv(GL_FRONT, glMaterialfv(GL_FRONT, glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); GL_DIFFUSE, mat_diffuse); GL_SPECULAR, mat_specular); GL_SHININESS, mat_shininess);
22

Budditha Hettige

Assigning Light Properties

Similar to Materials
void glLightfv(GLenum
light, GLenum pname, const GLfloat *

params);

light: The light to assign

GL_LIGHT0, GL_LIGHT1, , GL_LIGHT8

pname: The light property to assign

GL_AMBIENT,GL_DIFFUSE, GL_SPECULAR, and others

params: pointer to an array of values


Budditha Hettige

23

Assigning Light Properties


//Define GLfloat arrays to hold light data GLfloat light_ambient[] = { 0.3, 0.3, 0.3, 1.0 }; GLfloat light_diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 }; //Apply the light properties to LIGHT0 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);

Budditha Hettige

24

Point Light

A light source originating from a zerovolume point in the scene Casts light in all directions
For Positions this should be 1.0

Position

//Light position GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0 }; //Apply the light position Budditha Hettige glLightfv(GL_LIGHT0, GL_POSITION, light_position);

25

Directional Light

A light infinitely far away from the drawn scene Used most often for emulating sunlight

Distance from sun to earth is large Light direction can be considered the same
Direction -should be normalizedFor Directions this should be 0.0

//Light direction down the negative x axis GLfloat light_direction[] = { -1.0, 0.0, 0.0, 0.0 }; //Apply the light direction glLightfv(GL_LIGHT0, GL_POSITION, light_ambient);
Budditha Hettige

26

Spotlights

Budditha Hettige

27

Spotlight

A light source originating from a zerovolume point in the scene Direction

Direction the light is focused on

Cutoff

angle that defines light cone


Concentration of the light Brightest around the center
Budditha Hettige

Exponent

28

Spotlight example
//Spotlight properties

GLfloat light_position[] = { 50.0, 100.0, -50.0, 1.0 }; GLfloat light_spot_direction[] = { 0.0, 0.0, -1.0}; GLfloat light_spot_cutoff[] = { 25.0 }; GLfloat light_spot_exp[] = { 2.0 };
//Apply the light position glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION , light_spot_direction); glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF , light_spot_cufoff); glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT , light_spot_exp);

Budditha Hettige

29

Shading
Flat Shading
Flat shading selects the computed color of just one vertex

Budditha Hettige

30

Shading
Gouraud Shading Lighting color calculated per-vertex

saves computation

Vertex color is bilinearly interpolated across polygon

Budditha Hettige

31

Flat vs Gouraud shading

Flat
Budditha Hettige

Gouraud
32

Drawing a Smooth-Shaded Triangle


// Enable smooth shading glShadeModel(GL_SMOOTH); // Draw the triangle glBegin(GL_TRIANGLES); // Red Apex glColor3ub((GLubyte)255,(GLubyte)0,(GLubyte)0); glVertex3f(0.0f,200.0f,0.0f); // Green on the right bottom corner glColor3ub((GLubyte)0,(GLubyte)255,(GLubyte)0); glVertex3f(200.0f,-70.0f,0.0f); // Blue on the left bottom corner glColor3ub((GLubyte)0,(GLubyte)0,(GLubyte)255); glVertex3f(-200.0f, -70.0f, 0.0f); glEnd();
Budditha Hettige

33

You might also like