Practical 1: Local Illumination Models

The deadline for giving back TP1 is: Tuesday, November 8th (midnight, European Time)

Code structure

First, uncompress the archive.

Change your working directory to OpenGL_TP, and use the shell commands qmake-qt5, followed by make (you can also use the GUI qtcreator).

The compiler should return after a few warnings (but no errors). The command ./viewer/myViewer starts the actual viewer. It should look like the picture below. The program has 3 windows: the main window with the OpenGL display, an auxiliary window with the parameters, and the third with the menus:

Screen capture
Screen capture of the program

The code C++ contains:

For almost all practicals and exercises (except for shadow maps, in Practical 3), you shouldn't have to change anything in the C++ source files. You might need to open them to know the parameters names.

The Shaders menu is automatically generated from the Qt resource core-profile.qrc. Shaders are displayed in alphabetical order, except those whose name begins with h_ (for hidden). Each shader has a vertex shader, shader.vert, and a fragment shader, shader.frag.

Phong model

Your first work will be to edit the shader 2_phong to implement the Phong local reflection model, with ambiant, diffuse and specular lighting.

In the vertex shader, you have a 3 transformation matrices, matrix, perspective and normalMatrix. The first two contain the transformation from object coordinates to screen coordinates. The last one does the same transformation for normals.


For each vertex, you have 3 variables: vertex, normal et color. You also have the light source position, lightPosition and a boolean noColor, telling whether the model has vertex color information.

You work is to compute the lighting. It starts with compute the vectors you will need: the normal at that point, the vector, direction from the point to the light source, the vector, direction from the point to the camera.

C being the point color, I being the light source intensity, the lighting components are defined by:

The object color will be .

Phong shading in the fragment shader

Write a fragment shader computing Phong shading. You will have to pass certain variables from the vertex shader to the fragment shader. They will be linearly interpolated; remember that you have to normalize them.

To debug the program, display each lighting component separately: ambient alone, diffuse alone, specular alone, then combine them together.

teapot, ambient term + teapot, diffuse term + teapot, specular term = teapot, full lighting
Ambient+ Diffuse+ Specular= Together

Blinn-Phong shading

Rewrite the previous shader to have a Blinn-Phong shading. This time, the specular component is defined by:

where is the half-vector between the view direction vector and the light direction vector :

You should get (almost) the same picture as with Phong shading if you multiply the shininess exponent s by 4. The graphical user interface (through the auxiliary window) gives you two useful parameters:

Fresnel Coefficients

The intensity of the specular component depends on the Fresnel coefficient, F, itself a function of the viewing direction and the index of refraction of the material, :


Rewrite your shader so that the specular color is multiplied by the Fresnel coefficient, F. The user interface gives the varying parameter eta (a float, strictly positive).

When is the Fresnel coefficient most visible? When is the specular component most visible?

If the math formulas in this page appear as garbage, force your browser to use UTF-8, and reload the page.