1 / 15

Lights and Materials

Lights and Materials. OpenGL Part II. 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

sokanon
Télécharger la présentation

Lights and Materials

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • It’s 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 it’s 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_BIT|GL_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, it’s 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_ambient[]={0.33, 0.22, 0.03, 1.0}; GLfloat brass_diffuse[]={0.78, 0.57, 0.11, 1.0}; GLfloat brass_specular[]={0.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 ambient[4]; GLfloat diffuse[4]; GLfloat specular[4]; 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->ambient);

  14. teapots.c • http://www.opengl.org/resources/code/basics/redbook/

  15. materials.c • http://www.opengl.org/resources/code/basics/redbook/

More Related