color and lighting
DESCRIPTION
Color and Lighting. Color 1/5. Two Color modes: RGBA Color Index Choosing between RGBA and Color Index glutInitDisplayMode(); (GLUT) glutInitDisplayMode(GL_RGBA)for RGBA glutInitDisplayMode(GL_INDEX)for color index. Color 2/5. RGBA mode: - PowerPoint PPT PresentationTRANSCRIPT
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Color and LightingColor and Lighting
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
ColorColor1/51/5
• Two Color modes:– RGBA– Color Index
• Choosing between RGBA and Color Index– glutInitDisplayMode(); (GLUT)
• glutInitDisplayMode(GL_RGBA) for RGBA• glutInitDisplayMode(GL_INDEX)for color index
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
ColorColor2/52/5
• RGBA mode:– Four components Red, Green, Blue, and
Alpha.– Each component is normalize to 0.0~1.0– glColor{34}{sifd}[v](TYPE colors);
Ex.• glColor3f(0.0, 0.0, 0.0); black color• glColor4f(0.0, 1.0, 0.0, 1.0); green color
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
ColorColor3/53/5
• Color Index mode:– Use a color map (lookup table) to prepare f
or a paint-by-number scene. OpenGL does not provide any routing to load c
olor map (it’s load by window system).
– glIndex{sidf…}(TYPE c);
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
ColorColor4/54/5
• Clearing the color buffer– Use glClearColor() or glClearIndex() to s
pecify the clear color.– Call glClear(GL_COLOR_BUFFER_BIT); i
f need to clear the color buffer.
• Specifying shading model– glShadeModel(GLenum mode);
The parameter is either GL_FLAT or GL_SMOOTH (default).
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
ColorColor5/55/5
GL_FLAT GL_SMOOTH
Consider the following code with different shading model:
glBegin(GL_POLYGON);glColor3f(1.0, 1.0, 1.0);glVertex2f(-0.5, 0.-0.5);glColor3f(0.0, 0.0, 1.0);glVertex2f(0.5, 0.-0.5);glColor3f(1.0, 1.0, 0.0);glVertex2f(0.0, 0.5);glEnd();
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Hidden-Surface Hidden-Surface RemovalRemoval• How to apply HSR in OpenGL:
glutInitDisplayMode(GLUT_DEPTH);• Enable the depth buffer.
glEnable(GL_DEPTH_TEST);• Enable depth test.
– In display function glClear(GL_DEPTH_BUFFER_BIT); wh
en you wish to clean the depth buffer.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lights in OpenGLLights in OpenGL1/21/2
• Ambient light– Light that has been scattered by the environment.
• Diffuse light– Light that come from one direction.
• Specular light– Light that come from particular direction that tend t
o bounce off the surface in a preferred direction.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lights in OpenGLLights in OpenGL2/22/2
Three different kinds of lights on a white ball.
Specular light : whiteDiffuse light : yellowAmbient light : red
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Material in OpenGLMaterial in OpenGL1/31/3
• Material– Describe the percentages of the incoming red,
green, blue light it reflects on a surface.– Diffuse
The most important role in determining what you perceive the color of an object to be.
Affected by the color of the incident diffuse light and the angle of the incident light relative to the normal direction
– Ambient Affected by the global ambient light and ambient light
from individual light sources.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Material in OpenGLMaterial in OpenGL2/32/3
– Specular & Shininess Specular reflection from an object produces highlights. The amount of specular reflection seen by a viewer does
depend on the location of the viewpoint. Shininess control the size and brightness of the highlight.
– Emission Make an object appear to be giving off light of that color. Since most real-world objects (except lights) don't emit li
ght, you'll probably use this feature mostly to simulate lamps and other light sources in a scene.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Material in OpenGLMaterial in OpenGL3/33/3
A white light on the ball that has following materials:
Specular : blueDiffuse : redAmbient : green
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Put light into OpenGLPut light into OpenGL
• How to apply lighting in OpenGL– Use glEnable( GL_LIGHTING ) to enable lighting.– Enable and set properties for light sources.
Up to 8 lights are available (GL_LIGHT0 ~ GL_LIGHT7). Enable a light by calling to glEnable( GL_LIGHTi ) . Set light properties by calling to glLightfv( … ) .
– Set light model by calling to glLightModelfv( … ).– Define material properties for objects in the scene
by calling to glMaterialfv( … ) .– Assign normal vectors for each vertex of every
object.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting SampleLighting Sample1/41/4
#include <GL/glut.h>
void init(){
GLfloat mat_diffuse[] = {1.0, 0.0, 0.0, 1.0};GLfloat mat_specular[] = {0.0, 0.0, 1.0, 1.0};GLfloat mat_ambient[] = {0.0, 1.0, 0.0, 1.0};GLfloat mat_shininess[] = {50.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};GLfloat light_position[] = {5.0, 5.0, 10.0, 0.0};
glClearColor(0.0, 0.0, 0.0, 0.0);glShadeModel(GL_SMOOTH);
// z buffer enableglEnable(GL_DEPTH_TEST);
// enable lightingglEnable(GL_LIGHTING);
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting SampleLighting Sample2/42/4
// set light propertyglEnable(GL_LIGHT0);glLightfv(GL_LIGHT0, GL_POSITION, light_position);glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
// set material propertyglMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
void GL_display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glutSolidSphere(0.8, 128, 128);glFlush();
}
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting SampleLighting Sample3/43/4
void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity(); glOrtho (-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}
int main(int argc, char** argv){
glutInit(&argc, argv);glutInitWindowSize(400, 400);glutInitWindowPosition(0, 0);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH);glutCreateWindow("Planet");init();glutDisplayFunc(GL_display);glutReshapeFunc(reshape);glutMainLoop();
}
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting SampleLighting Sample4/44/4
Lighting enabled
Lighting disabled
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Normal Lighting: Normal VectorVector1/21/2
• Assign normal vectors for each vertex of every object– glNormal{34}{isfd}[v](TYPE* normal);
Assign normal vector for vertices (the normal vector should be assigned before you assign vertex).
Normal vectors must be normalize. OpenGL can automatically normalize normal vector by glEnable( GL_NORMALIZE ) .
In the previous sample, the normal for the sphere are defined as part of the glutSolidSphere() routine.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Normal Lighting: Normal VectorVector2/22/2
• Examplevoid GL_Display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, -1.0);
glVertex3f(-1.0, 0.0, 0.0);
glEnd();
glFlush();
}
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Light SourceLighting: Light Source1/71/7
• Enable and set properties for light sources– Use glEnable(GL_LIGHT0); to enable light zero.
You can use at most 8 light sources in OpenGL spec. (GL_LIGHT0~GL_LIGHT7)
GL_LIGHTi can be rewritten as GL_LIGHT0 + i.
– glLight{if}[v](GLenum light, Glenum pname, TYPE param);
Specify the attribute of light light can be GL_LIGHT0 ~ GL_LIGHT7 pname is the characteristic of the light param is the value of pname.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Light SourceLighting: Light Source2/72/7
Pname Def. Value Meaning
GL_AMBIENT (0.0, 0.0, 0.0, 0.0) ambient RGBA intensity of lightGL_DIFFUSE (1.0, 1.0, 1.0, 1.0) diffuse RGBA intensity of lightGL_SPECULAR (1.0, 1.0, 1.0, 1.0) specular RGBA intensity of lightGL_POSITION (0.0, 0.0, 1.0, 0.0) (x, y, z, w) position of lightGL_SPOT_DIRECTION (0.0, 0.0, -1.0) (x, y, z) direction of spotlightGL_SPOT_EXPONENT 0.0 spotlight exponentGL_SPOT_CUTOFF 180.0 spotlight cutoff angleGL_CONSTANT_ATTENUATION 1.0 constant attenuation factorGL_LINEAR_ATTENUATION 0.0 linear attenuation factorGL_QUADRATIC_ATTENUATION 0.0 quadratic attenuation factor
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Light SourceLighting: Light Source3/73/7
– Color The default values listed for GL_DIFFUSE and
GL_SPECULAR apply only to GL_LIGHT0. For other lights, the default value is (0.0, 0.0, 0.0, 1.0 )
for GL_DIFFUSE, GL_SPECULAR and GL_AMBIENT.
– Position ( x, y, z, w ) W is ZERO, Directional light, (x,y,z) specify its direction. W is NONZERO, Positional light, (x,y,z) specify the
location of the light source.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Light SourceLighting: Light Source4/74/7
– Attenuation
d = distance between the light's position and the vertex
kc = GL_CONSTANT_ATTENUATION kl = GL_LINEAR_ATTENUATION kq = GL_QUADRATIC_ATTENUATION If light is directional light, the attenuation is alwa
ys 1
2
1
dkdkkFacrornAttenuatio
qlc
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Light SourceLighting: Light Source5/75/7
– How to create spot light Define your light as positional light Define light spot direction
GLfloat spot_direction[] = { -1.0, -1.0, 0.0 };glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,
spot_direction);
Define light spot exponentglLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
Define light spot cutoff• The value for GL_SPOT_CUTOFF is restricted to being
within the range [0.0,90.0] (unless it has the special value 180.0 (default)).
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Light SourceLighting: Light Source6/76/7
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Light SourceLighting: Light Source7/77/7
• Control a Light’s Position and Direction– OpenGL treats the position and direction of
a light source just as it treats the position of a geometric primitive.
– The MODELVIEW transformation will also be applied to light sources.
– Any matrix operations can also apply to light sources.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Lighting Lighting: Lighting ModelModel1/21/2
• Selecting a lighting model– void glLightModel{if}[v](GLenum pname,
TYPE[*] param); Sets properties of the lighting model. The lighting model is used to control the global
behaviors of all the lights.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: Lighting Lighting: Lighting ModelModel2/22/2
Pname Def. Value MeaningGL_LIGHT_MODEL_AMBIENT (0.2, 0.2, 0.2, 1.0) ambient RGBA
intensity of the entire scene
GL_LIGHT_MODEL_LOCAL_VIEWER 0.0 or GL_FALSE how specularreflection anglesare computed
GL_LIGHT_MODEL_TWO_SIDE 0.0 or GL_FALSE choose betweenone-sided or two-sided lighting
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: MaterialLighting: Material1/61/6
• Define material properties for objects in the scene.– void glMaterial{if}[v](GLenum face, GLenum pn
ame, TYPE[*] param); Specifies a current material property for use in lighting ca
lculations. face can be GL_FRONT, GL_BACK, or GL_FRONT_A
ND_BACK. The particular material property being set is identified by
pname and the desired values for that property are given by param.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: MaterialLighting: Material2/62/6
Pname Def. Value Meaning
GL_AMBIENT (0.2, 0.2, 0.2, 1.0) ambient color of materialGL_DIFFUSE (0.8, 0.8, 0.8, 1.0) diffuse color of materialGL_AMBIENT_AND_DIFFUSE ambient and diffuse color of
materialGL_SPECULAR (0.0, 0.0, 0.0, 1.0) specular color of materialGL_SHININESS 0.0 specular exponentGL_EMISSION (0.0, 0.0, 0.0, 1.0) emissive color of materialGL_COLOR_INDEXES (0, 1, 1) ambient, diffuse, and specular color
indices
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: MaterialLighting: Material3/63/6
No Ambien
t
Gray Ambien
t
Blue Ambien
t
Diffuse Only
Specular
Higher Shinines
s
Emission
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: MaterialLighting: Material4/64/6
• Performance issue– glMaterialfv(); is called repeatedly to set different
material properties for different objects.– glMaterial*() has a performance cost associate wit
h its use.– Another technique for minimizing performance cos
ts associated with changing material properties is to use glColorMaterial();
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: MaterialLighting: Material5/65/6
– glColorMaterial(GLenum face, GLenum mode); Causes the material property of the specified material fac
e to track the value of the current color at all times. face parameter can be GL_FRONT, GL_BACK, or GL_
FRONT_AND_BACK (default). mode parameter can be GL_AMBIENT, GL_DIFFUSE,
GL_AMBIENT_AND_DIFFUSE (the default), GL_SPECULAR, or GL_EMISSION.
A change to the current color (using glColor*()) immediately updates the specified material properties.
Use glEnable(GL_COLOR_MATERIAL); to enable color material.
glColorMaterial() has no effect on color-index lighting.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Lighting: MaterialLighting: Material6/66/6
• Following is a example that using color material to change the ambient and diffuse
glEnable(GL_COLOR_MATERIAL);glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);glBegin(GL_POLYGON);glNormal3f(0.0, 1.0, 0.0);glColor3f(1.0, 0.0, 0.0);glVertex3f(-0.5, -0.5, -0.5);glColor3f(0.0, 0.0, 1.0);glVertex3f(0.5, -0.5, -0.5);glColor3f(0.0, 1.0, 0.0);glVertex3f(0.0, 0.5, 0.5);glEnd();
Tan-Chi Ho, CGGM Lab., CSIE of NCTU
Any Question?Any Question?
?