In this assignment you will learn
Lighting,
Shading, and
Tesselation.
In your third assignment you rendered a terrain surface using triangle strips. You must have noticed that these terrains don’t look appealing. The reason for this is that since all the polygons are colored using a single color, it becomes really hard to notice the undulations of the surface. The scene will start looking more realistic with addition of a light source.
You can add a light source in OpenGL by calling glLight*() (example glLightfv(GL_LIGHT0, GL_POSITION, light_position);
where,
· GL_LIGHT0 is a constant used by OpenGL to specify a light source (there can be a minimum of 8 light sources in a scene specified as GL_LIGHT0, GL_LIGHT1 … )
· GL_POSITION specifies that the next vector specifies where to position the light source.
· light_position specifies the actual position of the light source. You will need to position your light source at a fixed position that does not change with the modelview transformations. For extra credit however, you need to provide additional rollers/sliders for moving the light source.
Likewise you can also specify other parameters such as the amount of ambient diffuse and specular light from the light source.
The next thing to do is to specify material properties for the surface on which light strikes (in our case, the terrain). You can do this by calling glMaterial*() (example glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
where,
· GL_FRONT indicates the side of the polygon.(each polygon has 2 sides front and back)
· GL_SPECULAR specifies the specular attribute to be changed.
· mat_specular specifies the actual position values for the specular component.
Now while you render your polygons you will need to compute normal vectors for each polygon. The code snippet that is provided on the assignment page helps you do that. You can write your own code if you like. The normal vectors are used to indicate the direction in which the polygon is facing. This will help computing the colors for the vertices for that polygon. For example if a polygon is not facing the light source the colors for that polygon are attenuated.
In this assignment we are dealing only with simple positional light sources. For extra credit you can create directional light sources with spot-light illumination effects.
Finally enable lighting by doing a glEnable(GL_LIGHTING) and glEnable(GL_LIGHT0) (one for every light source).
For more details on lighting read Chapter 5 on Lighting.
A line or a filled polygon can be drawn either with a single color (flat shading) or with many different colors (smooth shading). You can specify the desired shading technique by calling glShadeModel(). The parameters to the shading model can be GL_SMOOTH (the default) or GL_FLAT. With smooth shading, neighboring pixels have slightly different color values.
More details in Chapter 4 on Colors.
Tessellation is a technique for breaking a polygon into several smaller polygons.
In order to create a tessellation we need a tessellation object. You can create this using gluNewTess(). This returns an object of type GLUtesslator*.
In order to tessellate a polygon you must set a series of callbacks to tell the GLU library how to draw the tessellated polygon. This can be done with gluTessCallback.
To draw a tessellated polygon instead of using glBegin() and glEnd(). Instead you will use gluTessBeginPolygon() and gluTessEndPolygon(). In additon to this one or more pairs of gluTessBeginContour() and gluEndCountour() must be present.
Instead of glVertex() you will use gluTessVertex(), these must occur between a gluTessBeginCountour() and gluTessEndContour().