150 likes | 353 Vues
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
E N D
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 • It’s a way of faking physically realistic lights
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)
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
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)
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); }
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);
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(); }
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
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!
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);
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);
teapots.c • http://www.opengl.org/resources/code/basics/redbook/
materials.c • http://www.opengl.org/resources/code/basics/redbook/