CS3241 Lab7

Click here to load reader

download CS3241 Lab7

of 16

description

 

Transcript of CS3241 Lab7

  • 1. Lighting & Material
    CS3241 Lab

2. Objectives
Render illuminated objects by defining the desired light sources and lighting model
Define the material properties of the objects being illuminated
Manipulate the matrix stack to control the position of light sources
3. Resource
Lec7_code
ObjectMaterials.c
ObjectMaterials2.c
4. Lighting
Enable lighting
glEnable(GL_LIGHTING);
Turn on a light source (0-7)
glEnable(GL_LIGHT07);
Turn off a light source (07)
glDisable(GL_LIGHT07);
Setting up light properties
e.g. glLightfv(GL_LIGHT0, GL_POSITION, pos);
5. Properties
Color
GL_AMBIENT
GL_DIFFUSE
GL_SPECULAR
Position
GL_POSITION
Attenuation
(apply to positional light source only)
GL_CONSTANT_ATTENUTATION
GL_LINEAR_ATTENUATION
GL_QUADRATIC_ATTENUATION
6. Point Light
E.g. light bulb
Positional light
Creating Point light (setup a point light at [0,0,0])
Specify by setting position.w = 1
GLfloat position[] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, position);
x, y, z denote its position in 3D space
7. Directional Light
Directional Light
Light rays are shot in specified direction from infinite location
E.g. Sun
Creating Directional light
Specify by setting position.w = 0
GLfloat position[] = { 1.0, 1.0, 1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, position);
x, y, z denote its direction
8. Spot light
Positional light source
Creating a spot light
Glfloat position[] = { 0, 1.0, 0, 1.0 };
GLfloatspot_direction[] = { -1.0, -1.0, 0.0 };
glLightfv(GL_LIGHT1, GL_POSITION, position);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
Properties
GL_SPOT_DIRECTION
GL_SPOT_EXPONENT
GL_SPOT_CUTOFF
9. Moving a light
#1: Setting its position value
E.g. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
#2: Using Transformation Matrix
E.g. glTranslate(), glRotate()
Glfloat position[4] = {0,0,0,0};
glPushMatrix();
glRotatef(rotL, 0, 1, 0);
glTranslatef(1.5, 1.5, 0);
glLightfv(GL_LIGHT1, GL_POSITION, position);
glPopMatrix();
10. Task 1
Starting Point: ObjectMaterials.c
Task 1.1
Turn off light0 by pressing spacebar
Hint: glDisable(GL_LIGHT0);
Task 1.2
Add a new point light source (light1) at [0,0,0]
Hint:
GLfloat position[] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv(GL_LIGHT1, GL_POSITION, position);
glEnable(GL_LIGHT1);
11. Task 1
Task 1.3
Make light1 hover overhead (in a circular motion)
Hint:
Glfloat position[4] = {0,0,0,0};
glPushMatrix();
glRotatef(rotL, 0, 1, 0);
glTranslatef(1.5, 1.5, 0);
glLightfv(GL_LIGHT1, GL_POSITION, position);
glPopMatrix();
12. Material
13. Material
Properties
GL_AMBIENT
ambient color of material
GL_DIFFUSE
diffuse color of material
GL_AMBIENT_AND_DIFFUSE
ambient and diffuse color of material
GL_SPECULAR
specular color of material
GL_SHININESS
specular exponent
GL_EMISSION
emissive color of material
14. Material
Using a material
Setup material
e.g. glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
Using material
OpenGL is a state machine
Following draw call will use the material specified
Example
glPushMatrix();
// setup material
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
// draw sphere
glutSolidSphere(1.0, 16, 16);
glPopMatrix();
15. Task 2
Set the teapot material to Ruby
Ruby
Ambient: (0.1745, 0.01175, 0.01175, 0.55)
Diffuse: (0.61424, 0.04136, 0.04136, 0.55)
Specular: (0.727811, 0.626959, 0.626959, 0.55)
Shininess: 76.8
Solution in ObjectMaterials2.c
16. Reference
OpenGL Red book
http://www.glprogramming.com/red/chapter05.html
Material
http://www.cs.utk.edu/~kuck/materials_ogl.htm