In this chapter we will learn how to add light to our 3D game engine. We will not implement a physically perfect light model because, taking aside the complexity, it would require a tremendous amount of computer resources. Instead we will implement an approximation which will provide decent results: we will use an algorithm named Phong shading (developed by Bui Tuong Phong). Another important thing to point is that we will only model lights but we won’t model the shadows that should be generated by those lights (this will be done in another chapter).
You can find the complete source code for this chapter here.
Some concepts
Before we start, let us define some light types:
Point light: This type of light models a light source that’s emitted uniformly from a point in space in all directions.
Spot light: This type of light models a light source that’s emitted from a point in space, but instead of emitting in all directions is restricted to a cone.
Directional light: This type for light models the light that we receive from the sun, all the objects in the 3D the space are hit by parallel ray lights coming from a specific direction. No matter if the object is close or far away, all the ray lights impact the objects with the same angle.
Ambient light: This type of light comes from everywhere in the space and illuminates all the objects in the same way.
Thus, to model light we need to take into consideration the type of light plus, its position and some other parameters like its color. Of course, we must also consider the way that objects, impacted by ray lights, absorb and reflect light.
The Phong shading algorithm will model the effects of light for each point in our model, that is for every vertex. This is why it’s called a local illumination simulation, and this is the reason why this algorithm will not calculate shadows: it will just calculate the light to be applied to every vertex without taking into consideration if the vertex is behind an object that blocks the light. We will overcome this drawback in later chapters. But, because of that, it's a simple and fast algorithm that provides very good effects. We will use here a simplified version that does not take into account materials deeply.
The Phong algorithm considers three components for lighting:
Ambient light: models light that comes from everywhere, this will serve us to illuminate (with the required intensity) the areas that are not hit by any light, it’s like a background light.
Diffuse reflectance: takes into consideration that surfaces that are facing the light source are brighter.
Specular reflectance: models how light reflects on polished or metallic surfaces.
At the end what we want to obtain is a factor that, multiplied by color assigned to a fragment, will set that color brighter or darker depending on the light it receives. Let’s name our components as A for ambient, D for diffuse and S for specular. That factor will be the addition of those components:
L=A+D+S
In fact, those components are indeed colors, that is the color components that each light component contributes to. This is due to the fact that light components will not only provide a degree of intensity but they can modify the color of the model. In our fragment shader we just need to multiply that light color by the original fragment color (obtained from a texture or a base color).
We can also assign different colors, for the same materials, that will be used in the ambient, diffuse and specular components. Hence, these components will be modulated by the colors associated to the material. If the material has a texture, we will simply use a single texture for each of the components.
So the final color for a non textured material will be: L=A∗ambientColour+D∗diffuseColour+S∗specularColour.
And the final color for a textured material will be:
L=A∗textureColour+D∗textureColour+S∗textureColour
Normals
Normals are a ket element when working with lights. Let’s define it first. The normal of a plane is a vector perpendicular to that plane which has a length equal to one.
As you can see in the figure above, a plane can have two normals. Which one should we use? Normals in 3D graphics are used for lighting, so we should choose the normal which is oriented towards the source of light. In other words, we should choose the normal that points out from the external face of our model.
When we have a 3D model, it is composed by polygons, triangles in our case. Each triangle is composed by three vertices. The Normal vector for a triangle will be the vector perpendicular to the triangle surface which has a length equal to one.
A vertex normal is associated to a specific vertex and is the combination of the normals of the surrounding triangles (of course its length is equal to one). Here you can see the vertex models of a 3D mesh (taken from Wikipedia)
Diffuse reflectance
Let’s talk now about diffuse reflectance. This models the fact that surfaces which face in a perpendicular way to the light source look brighter than surfaces where light is received in a more indirect angle. Those objects receive more light, the light density (let me call it this way) is higher.
But, how do we calculate this? This is where we will first start using normals. Let’s draw the normals for three points in the previous figure. As you can see, the normal for each point will be the vector perpendicular to the tangent plane for each point. Instead of drawing rays coming from the source of light we will draw vectors from each point to the point of light (that is, in the opposite direction).
As you can see, the normal associated to P1, named N1, is parallel to the vector that points to the light source, which models the opposite of the light ray (N1 has been sketched displaced so you can see it, but it’s equivalent mathematically). P1 has an angle equal to 0 with the vector that points to the light source. Its surface is perpendicular to the light source and P1 would be the brightest point.
The normal associated to P2, named N2, has an angle of around 30 degrees with the vector that points the light source, so it should be darker tan P1. Finally, the normal associated to P3, named N3, is also parallel to the vector that points to the light source but the two vectors are in the opposite direction. P3 has an angle of 180 degrees with the vector that points the light source, and should not get any light at all.
So it seems that we have a good approach to determine the light intensity that gets to a point, and this is related to the angle that forms the normal with a vector that points to the light source. How can we calculate this?
There’s a mathematical operation that we can use, the dot product. This operation takes two vectors and produces a number (a scalar) that is positive if the angle between them is acute, or negative if the angle between them is wide. If both vectors are normalized, that is the both have a length equal to one, the dot product will be between −1 and 1. The dot product will be one if both vectors look exactly in the same direction (angle 0); it will be 0 if both vectors form a square angle, and it will be −1 if both vectors point in opposite directions.
Let’s define two vectors, v1 and v2, and let alpha be the angle between them. The dot product is defined by the following formula.
If both vectors are normalized, their length, their module will be equal to one, so the dot product is equal to the cosine if the angle between them. We will use that operation to calculate the diffuse reflectance component.
So we need to calculate the vector that points to the source of light. How we do this? We have the position of each point (the vertex position) and we have the position of the light source. First of all, both coordinates must be in the same coordinate space. To simplify, let’s assume that they are both in world coordinate space: then those positions are the coordinates of the vectors that point to the vertex position (VP) and to the light source (VS), as shown in the next figure.
If we subtract VS from VP we get the vector that we are looking for, which is called L.
Now we can compute the dot product between the vector that points to the light source and the normal. This product is called the Lambert term, due to Johann Lambert who was the first to propose that relation to model the brightness of a surface.
Let’s summarize how we can calculate it. We define the following variables:
vPos: Position of our vertex in model view space coordinates.
lPos: Position of the light in view space coordinates.
intensity: Intensity of the light (from 0 to 1).
lColour: Colour of the light.
normal: The vertex normal.
First we need to calculate the vector that points to the light source from current position: toLightDirection=lPos−vPos. The result of that operation needs to be normalized.
Then we need to calculate the diffuse factor (a scalar): diffuseFactor=normal⋅toLightDirection. It’s calculated as dot product between two vectors, and since we want it to be between −1 and 1 both vectors need to be normalized. Colours need to be between 0 and 1 so if a value is lower than 0 we will set it to 0.
Finally we just need to modulate the light color by the diffuse factor and the light intensity:
Before considering the specular component, we first need to examine how light is reflected. When light hits a surface some part of it is absorbed and the other part is reflected, if you remember from your physics class, reflection is when light bounces off an object.
Of course, surfaces are not totally polished, and if you look at closer distance you will see a lot of imperfections. Besides that, you have many ray lights (photons in fact), that impact that surface, and that get reflected in a wide range of angles. Thus, what we see is like a beam of light being reflected from the surface. That is, light is diffused when impacting over a surface, and that’s the diffuse component that we have been talking about previously.
But when light impacts a polished surface, for instance a metal, the light suffers from lower diffusion and most of it gets reflected in the opposite direction as it hit that surface.
This is what the specular component models, and it depends on the material characteristics. Regarding specular reflectance, it’s important to note that the reflected light will only be visible if the camera is in a proper position, that is, if it's in the area where the reflected light is emitted.
Now that the mechanism behind specular reflection has been explained we are ready to calculate that component. First we need a vector that points from the light source to the vertex point. When we were calculating the diffuse component we calculated just the opposite, a vector that points to the light source. toLightDirection, so let’s calculate it as fromLightDirection=−(toLightDirection).
Then we need to calculate the reflected light that results from the impact of the fromLightDirection into the surface by taking into consideration its normal. There’s the GLSL function reflect that does exactly that. So, reflectedLight=reflect(fromLightSource,normal).
We also need a vector that points to the camera, let’s name it cameraDirection, and it will be calculated as the difference between the camera position and the vertex position: cameraDirection=cameraPos−vPos. The camera position vector and the vertex position need to be in the same coordinate system and the resulting vector needs to be normalized. The following figure sketches the main components we have calculated up to now.
Now we need to calculate the light intensity that we see, which we will call specularFactor. This component will be higher if the cameraDirection and the reflectedLight vectors are parallel and point in the same direction and will take its lower value if they point in opposite directions. In order to calculate this the dot product comes to the rescue again. So specularFactor=cameraDirection⋅reflectedLight. We only want this value to be between 0 and 1 so if it’s lower than 0 it will be set to 0.
We also need to take into consideration that this light must be more intense if the camera is pointing to the reflected light cone. This will be achieved by raising the specularFactor to a parameter named specularPower.
specularFactor=specularFactorspecularPower.
Finally we need to model the reflectivity of the material, which will also modulate the intensity if the light reflected. This will be done with another parameter named reflectance. So the color of the specular component will be: specularColour∗lColour∗reflectance∗specularFactor∗intensity.
Attenuation
We now know how to calculate the three components that will serve us to model a point light with an ambient light. But our light model is still not complete, as the light that an object reflects is independent of the distance from the light source. That is, we need to simulate light attenuation.
Attenuation is a function of the distance and light. The intensity of light is inversely proportional to the square of distance. That fact is easy to visualize, as light propagates its energy along the surface of a sphere with a radius that’s equal to the distance traveled by the light, and the surface of a sphere is proportional to the square of its radius. We can calculate the attenuation factor with this formula: 1.0/(atConstant+atLinear∗dist+atExponent∗dist2).
In order to simulate attenuation we just need to multiply that attenuation factor by the final color.
Directional Light
Directional lighting hits all the objects by parallel rays all coming from the same direction. It models light sources that are far away but have a high intensity such as the Sun.
Another characteristic of directional light is that it is not affected by attenuation. Think again about sunlight: all objects that are hit by rays of light are illuminated with the same intensity, as the distance from the sun is so huge that the position of the objects is irrelevant. In fact, directional lights are modeled as light sources placed at infinity, if it was affected by attenuation it would have no effect in any object (its color contribution would be equal to 0).
Besides that, directional light is composed also by a diffuse and specular components. The only differences with point lights is that it does not have a position but a direction and that it is not affected by attenuation. Let’s get back to the direction attribute of directional light, and imagine we are modeling the movement of the sun across our 3D world. If we are assuming that the north is placed towards the increasing z-axis, the following picture shows the direction to the light source at dawn, mid day and dusk.
Spot Light
Now we will implement spot lights which are very similar to point lights but the emitted light is restricted to a 3D cone. It models the light that comes out from focuses or any other light source that does not emit in all directions. A spot light has the same attributes as a point light but adds two new parameters, the cone angle and the cone direction.
Spot light contribution is calculated in the same way as a point light with some exceptions. The points for which the vector that points from the vertex position to the light source is not contained inside the light cone are not affected by the point light.
How do we calculate if it’s inside the light cone or not? We need to do a dot product again between the vector that points from the light source and the cone direction vector (both of them normalized).
The dot product between L and C vectors is equal to: L⋅C=∣L∣⋅∣C∣⋅Cos(α). If, in our spot light definition we store the cosine of the cutoff angle, if the dot product is higher than that value we will know that it is inside the light cone (recall the cosine graph, when α angle is 0, the cosine will be 1, the smaller the angle the higher the cosine).
The second difference is that the points that are far away from the cone vector will receive less light, that is, the attenuation will be higher. There are several ways of calculating this; we will chose a simple approach by multiplying the attenuation by the following factor:
1−(1−Cos(α))/(1−Cos(cutOffAngle)
(In our fragment shaders we won’t have the angle but the cosine of the cutoff angle. You can check that the formula above produces values from 0 to 1, 0 when the angle is equal to the cutoff angle and 1 when the angle is 0).
Implementing light classes
Let's start first by creating a set of classes to model the different types of lights. We will start with the class that models point lights:
All the lights will be stored in the Scene class, for that we will create a new class named SceneLights which will store references to all the types of lights (note that we only need one ambient light instance and one directional light):
As you can see, we get the material ambient color by getting the AI_MATKEY_COLOR_AMBIENT property. Specular color is got by using the AI_MATKEY_COLOR_SPECULAR property. Shininess is queried using the AI_MATKEY_SHININESS_STRENGTH flag.
In order to load normals, we need to create a new method named processNormals and invoke it in the processMesh method.
publicclassModelLoader {...privatestaticMeshprocessMesh(AIMesh aiMesh) {float[] vertices =processVertices(aiMesh);float[] normals =processNormals(aiMesh);float[] textCoords =processTextCoords(aiMesh);int[] indices =processIndices(aiMesh);// Texture coordinates may not have been populated. We need at least the empty slotsif (textCoords.length==0) {int numElements = (vertices.length/3) *2; textCoords =newfloat[numElements]; }returnnewMesh(vertices, normals, textCoords, indices); }privatestaticfloat[] processNormals(AIMesh aiMesh) {AIVector3D.Buffer buffer =aiMesh.mNormals();float[] data =newfloat[buffer.remaining() *3];int pos =0;while (buffer.remaining() >0) {AIVector3D normal =buffer.get(); data[pos++] =normal.x(); data[pos++] =normal.y(); data[pos++] =normal.z(); }return data; }...}
As you can see we need to modify also the Material and Mesh classes to store the new information. The changes in the Material class are as follows:
As you can see we now have normal data as another input attribute and we just pass that data to the fragment shader. Before we continue with the fragment shader, there’s a very important concept that must be highlighted. From the code above you can see that mvVertexNormal, the variable contains the vertex normal, is transformed into model view space coordinates. This is done by multiplying the normal by the modelViewMatrix as with the vertex position. But there’s a subtle difference, the w component of that vertex normal is set to 0 before multiplying it by the matrix: vec4(vertexNormal, 0.0). Why are we doing this? Because we do want the normal to be rotated and scaled but we do not want it to be translated, we are only interested into its direction but not in its position. This is achieved by setting is w component to 0 and is one of the advantages of using homogeneous coordinates, by setting the w component we can control what transformations are applied. You can do the matrix multiplication by hand and see why this happens.
Changes in the scene.frag fragment shader are most complex, let's go one step at a time:
First, we define the maximum some constants for the maximum number of pont and spot lights we will support. We need this since the data for those lights will be passed as an array of uniforms, which need to have a well defined size at compile time. You can see also that we receive normal data from vertex shader. After that, we define sthe structures that will model lights data:
As you can see we just modulate the ambient light color by a factor which is applied to material ambient color. Now we will define a function which will define how color light is calculated for lal types of lights:
The previous code is relatively straightforward, it just calculates a color for the diffuse component, another one for the specular component and modulates them by the attenuation suffered by the light in its travel to the vertex we are processing. Now we can define the functions that will be called for each type of light, we will start with point light:
As you can see, we just calculate the direction to the light (as a normal), and use that information to calculate the light color, using material's diffuse and specular colors, light color, its intensity, position, its direction and the normal direction. After that, we apply the attenuation. The function for spot lights is as follows:
The procedure is similar to point lights with the exception that we need to control if we are inside the cone of light or not. Inside the cone of light we need also to apply some attenuation as explained previously. Finally, the function for directional light is defined below:
In this case, we already have the direction to the light, and since there is no attenuation we do not need to consider light position. Finally, in the main method we just iterate over the different light types which will contribute to diffuse-specular component of the final fragment color:
Now it is turn to examine how we are going to modify the SceneRender class to include lights in the render process. The first step is to create the new uniforms:
publicclassSceneRender {privatestaticfinalint MAX_POINT_LIGHTS =5;privatestaticfinalint MAX_SPOT_LIGHTS =5;...privatevoidcreateUniforms() {...uniformsMap.createUniform("material.ambient");uniformsMap.createUniform("material.diffuse");uniformsMap.createUniform("material.specular");uniformsMap.createUniform("material.reflectance");uniformsMap.createUniform("ambientLight.factor");uniformsMap.createUniform("ambientLight.color");for (int i =0; i < MAX_POINT_LIGHTS; i++) {String name ="pointLights["+ i +"]";uniformsMap.createUniform(name +".position");uniformsMap.createUniform(name +".color");uniformsMap.createUniform(name +".intensity");uniformsMap.createUniform(name +".att.constant");uniformsMap.createUniform(name +".att.linear");uniformsMap.createUniform(name +".att.exponent"); }for (int i =0; i < MAX_SPOT_LIGHTS; i++) {String name ="spotLights["+ i +"]";uniformsMap.createUniform(name +".pl.position");uniformsMap.createUniform(name +".pl.color");uniformsMap.createUniform(name +".pl.intensity");uniformsMap.createUniform(name +".pl.att.constant");uniformsMap.createUniform(name +".pl.att.linear");uniformsMap.createUniform(name +".pl.att.exponent");uniformsMap.createUniform(name +".conedir");uniformsMap.createUniform(name +".cutoff"); }uniformsMap.createUniform("dirLight.color");uniformsMap.createUniform("dirLight.direction");uniformsMap.createUniform("dirLight.intensity"); }...}
When we are using arrays we need to create a uniform for each element of the list. So, for instance, for the pointLights array we need to create a uniform named pointLights[0], pointLights[1], etc. And of course, this translates also to the structure attributes, so we will have pointLights[0].color, pointLights[1], color, etc.
We will create a new method that will update the uniforms for lights for each render call, which will be named updateLights and is defined like this:
publicclassSceneRender {...privatevoidupdateLights(Scene scene) {Matrix4f viewMatrix =scene.getCamera().getViewMatrix();SceneLights sceneLights =scene.getSceneLights();AmbientLight ambientLight =sceneLights.getAmbientLight();uniformsMap.setUniform("ambientLight.factor",ambientLight.getIntensity());uniformsMap.setUniform("ambientLight.color",ambientLight.getColor());DirLight dirLight =sceneLights.getDirLight();Vector4f auxDir =newVector4f(dirLight.getDirection(),0);auxDir.mul(viewMatrix);Vector3f dir =newVector3f(auxDir.x,auxDir.y,auxDir.z);uniformsMap.setUniform("dirLight.color",dirLight.getColor());uniformsMap.setUniform("dirLight.direction", dir);uniformsMap.setUniform("dirLight.intensity",dirLight.getIntensity());List<PointLight> pointLights =sceneLights.getPointLights();int numPointLights =pointLights.size();PointLight pointLight;for (int i =0; i < MAX_POINT_LIGHTS; i++) {if (i < numPointLights) { pointLight =pointLights.get(i); } else { pointLight =null; }String name ="pointLights["+ i +"]";updatePointLight(pointLight, name, viewMatrix); }List<SpotLight> spotLights =sceneLights.getSpotLights();int numSpotLights =spotLights.size();SpotLight spotLight;for (int i =0; i < MAX_SPOT_LIGHTS; i++) {if (i < numSpotLights) { spotLight =spotLights.get(i); } else { spotLight =null; }String name ="spotLights["+ i +"]";updateSpotLight(spotLight, name, viewMatrix); } }...}
The code is very straight forward, we just start by setting ambient light an directional light uniforms and after that we iterate over point and spot lights which have dedicated methods to set the uniforms for each of the elements o the array:
As we have said, these coordinates for lights must be in view space. Usually we will set up light coordinates in world space coordinates, so we need to multiply them by the view matrix in order to be able to use them in our shader. Finally, we need to update the render method to invoke the updateLights method and also set up properly the new elements of the model materials:
publicclassSceneRender {...publicvoidrender(Scene scene) {...updateLights(scene);...for (Model model : models) {List<Entity> entities =model.getEntitiesList();for (Material material :model.getMaterialList()) {uniformsMap.setUniform("material.ambient",material.getAmbientColor());uniformsMap.setUniform("material.diffuse",material.getDiffuseColor());uniformsMap.setUniform("material.specular",material.getSpecularColor());uniformsMap.setUniform("material.reflectance",material.getReflectance());... } }... }...}
We need also to add a pair of methods to the UniformsMap class to create uniforms fto set values for floats and 3D vectors:
The last step is to use the lights in the Main class. But, prior to that, we will create a GUI, using Imgui, to provide some elements to control light parameters. We will do this in a new class named LightControls. The code is too wordy, but it is very simple to understand, we just need to set up a set of attributes to get the values from the GUI controls and a method to draw the panels and widgets required.
Finally, we need a method to handle GUI input, where we update Imgui based on mouse status and check if the input has been consumed by the GUI controls. If so, we just populate the attributes of the class according to user input:
The last step is to update the Main class to create the lights, remove previous drawGui and handleGuiInput method (we hare handling that now in the LightControls class):