Lights and Materials - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Lights and Materials

Description:

Ambient, diffuse, specular, emit. If 4th element in light position is 0, it's a sun ... Specular. Also define the amount of shininess. Must specify the material ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 16
Provided by: mm197
Category:

less

Transcript and Presenter's Notes

Title: Lights and Materials


1
Lights and Materials
  • OpenGL Part II

2
Light Properties
  • The amount of light at one pixel is determined
    by
  • Specular
  • Diffuse
  • Ambient
  • Emit
  • Both the materials and the lights have this
    property!
  • Interaction between the two is what determines
    the color
  • Its a way of faking physically realistic lights

3
OpenGL
  • OpenGL has point lights and directional lights
  • By default, OpenGL scenes have no light
  • The number of lights is hardware dependent
    (typically a minimum of 8)

4
Getting things going
  • First, enable lighting
  • glEnable (GL_LIGHTING)
  • This disables any colors from glColor()!
  • Turn on the individual lights
  • glEnable (GL_LIGHT0)
  • Setup light position and properties

5
Basic Light Source
  • // Setup light position
  • GLfloat light_position 100.0f, 100.0f,
    100.0f, 0.0f
  • glLightfv (GL_LIGHT0, GL_POSITION,
    light_position)
  • // Setup diffuse property
  • GLfloat diffuse_light 0.9f, 0.9f, 0.9f,
    0.0f
  • glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse_light)
  • // Setup ambient property
  • GLfloat ambient_light 0.2f, 0.2f, 0.2f,
    0.0f
  • glLightfv (GL_LIGHT0, GL_AMBIENT, ambient_light)
  • // Setup specular property
  • GLfloat specular_light 0.5f, 0.5f, 0.5f,
    0.0f
  • glLightfv (GL_LIGHT0, GL_SPECULAR,
    specular_light)
  • Note the v is for vector

NOTE! If this is a 0, then itis
locatedinfinitelyfar away!The lightis now
adirectionallight! (i.e. a sun)
6
A function
  • void setupLights()
  • glEnable (GL_LIGHTING)
  • glEnable (GL_LIGHT0)
  • glLightfv (GL_LIGHT0, GL_POSITION,
    light_position)
  • glLightfv (GL_LIGHT0, GL_DIFFUSE,
    diffuse_light)
  • glLightfv (GL_LIGHT0, GL_AMBIENT,
    ambient_light)
  • glLightfv (GL_LIGHT0, GL_SPECULAR,
    specular_light)
  • glLightfv (GL_LIGHT0, GL_EMISSION,
    emission_light)

7
Spot Lights
  • Still specify all that other stuff
  • Make sure its 4th position is not 0
  • Use glLightfv to specify other properties
  • // Direction of light
  • glLightfv (GL_LIGHT0, GL_SPOT_DIRECTION,
    spot_dir)
  • // size of cone
  • glLightfv (GL_LIGHT0, GL_SPOT_CUTOFF, 1.2f)
  • // amount of dropoff
  • glLightfv (GL_LIGHT0, GL_SPOT_EXPONENT, 20.0f)
  • // quadratic, linear and constant dropoff
  • glLightfv (GL_LIGHT0, GL_QUADRATIC_ATTENUATION,
    0.0f)
  • glLightfv (GL_LIGHT0, GL_LINEAR_ATTENUATION,
    0.0f)
  • glLightfv (GL_LIGHT0, GL_CONSTANT_ATTENUATION,
    1.0f)

8
Rotating a Light Source
  • void display()
  • glClear (GL_COLOR_BUFFER_BITGL_DEPTH_BUFFER_BIT)
  • glMatrixMode(GL_MODELVIEW)
  • glPushMatrix()
  • glLoadIdentity()
  • glTranslatef(0.0, 0.0, -5.0)
  • glRotatef (deg, 0.0, 1.0, 0.0)
  • glLightfv (GL_LIGHT0, GL_POSITION,
    light_position)
  • glutSolidTeapot(1.0)
  • glPopMatrix()
  • glutSwapBuffers()
  • glFlush()

9
Summary on Lights
  • Must specify
  • Must enable lighting and individual lights
  • Ambient, diffuse, specular, emit
  • If 4th element in light position is 0, its a sun
  • A point light can become a spotlight
  • Light properties interact with materials

10
Materials(not textures)
  • Materials define the color of
  • Diffuse
  • Specular
  • Ambient
  • Specular
  • Also define the amount of shininess
  • Must specify the material each time before
    drawing!

11
Example brass
  • GLfloat brass_ambient0.33, 0.22, 0.03, 1.0
  • GLfloat brass_diffuse0.78, 0.57, 0.11, 1.0
  • GLfloat brass_specular0.99, 0.91, 0.81, 1.0
  • GLfloat brass_shininess 27.8
  • glMaterialfv(GL_FRONT, GL_AMBIENT,
    brass_ambient)
  • glMaterialfv(GL_FRONT, GL_DIFFUSE,
    brass_diffuse)
  • glMaterialfv(GL_FRONT, GL_SPECULAR,
    brass_specular)
  • glMaterialf(GL_FRONT, GL_SHININESS,
    brass_shininess)

12
Output
13
Defining a Materials Structure
  • typedef struct materialStruct
  • GLfloat ambient4
  • GLfloat diffuse4
  • GLfloat specular4
  • GLfloat shiniess
  • materialStruct brass
  • 0.33, 0.22, 0.03, 1.0,
  • 0.78, 0.57, 0.11, 1.0,
  • 0.99, 0.91, 0.81, 1.0,
  • 27.8
  • glMaterialfv(GL_FRONT, GL_AMBIENT,
    brass-gtambient)

14
teapots.c
  • http//www.opengl.org/resources/code/basics/redboo
    k/

15
materials.c
  • http//www.opengl.org/resources/code/basics/redboo
    k/
Write a Comment
User Comments (0)
About PowerShow.com