Coding in OpenGL

Phong's local reflection model

Implemented in OpenGL and GLSL

Summary

A project in OpenGL with the purpose of learning how to implement models in OpenGL and write shaders in GLSL. Parts of the project was performed as part of the course Computer Graphics at Linköping University, which I developed further on my own.

The main task for the project was to set up an OpenGL environment in Visual Studio and learn how to define objects using triangles, how to send data to the vertex buffer and implement basic vertex and fragment shaders.

The shader part of the lab assignment was to implement Phong's local reflection model which I continued working on and comparing different values on reflection constants.

Technical Walkthrough

The Phong Lightning Model consists of three terms: The ambient, the diffuse and the specular term.

The ambient term comes from the physical model of light and the fact that it scatters around us and when hitting other things it will reflect and therefore result in that some light reaches the object looked at. This is called ambient lightning and in this simple model it is implement as just the product between the ambient strength (that we set between 0 and 1) and the color of the light.

The diffuse light simulates the light that actually hits the object itself. We will give the pixels that are closer to the light rays from the light source more brightness than the pixels farther away.

The difference between only ambient lightning and when the diffuse term is added.

Specular light is simulated based on the reflections of the light and how the viewer sees this. With the help of the position of the light and the viewer and the normal of the surface we can calculate the angular difference between the reflected light and the view direction. The closer angle between them, the greater influence of the specular light we will get. We will raise the specular component to a power of a number which we define as the 'shininess' of the object. The higher the value, the more the object reflects light instead of scattering and therefore we will get a more defined circle and smaller highlight.

Specular light added. The difference between an exponent of 4 and one of 64.

I continued on with adding imagebased textures to the model. This was done by first assigning texture coordinates to the object and then loading an image that was added to the object.

As a last step the lightning model was added onto the textured objects. The diffuse term in Phong's model now comes from the texture.

I continued the project with adding a sphere that was placed at the same place as the simulated light source. Using keyboard input it is now possible to move the light around the object to see the appearance when the light hits from different angles.

Implementation of moving light source for two different objects. In the right image there is a high specular reflectance.

Return to all projects