I implemented a shader builder class to create all the shaders of the engine.

Lighting implementation is one of the most important things in game engine development, and I decided to start with implementing them. There are two types of lighting movability: static and dynamic. Static lights are lights whose intensity, position, direction etc cannot be changed, whereas dynamic lights can be updated during game time.

ShaderBuilder needs to handle both types of lights in order to obtain more optimal software. Since dynamic lights require uniform variables, and static lights do not builder class goes by this case. ShaderBuilder class, lighting renders and corresponding shaders are shown here:

Shader Builder

ShaderBuilder class is builder of all shaders of the scene. It consists of three main parts: uniforms, outside main, and inside main scripts. When a game is loaded, firstly it builds scene’s fragment shader. Then if any object has a special shader class, it can be used to create them as well. By doing this, I aimed a shader management system away from complication.

Static Lighting

Point Light

Point lights are the first light types implemented when making a game engine since they are the types of lights we see in our everyday life; for example, in our office, home, street lights and much more. A point light is consisting of a position, light color, and intensity. In the shader, color variable of it is sent to the fragment shader as a multiplication by its intensity in order to get rid of one more variable.

Corresponding render:

Corresponding fragment shader built by ShaderBuilder:

Directional Light

A directional light is a light lighting everywhere with the same direction and intensity. Huge light systems emit these types of lights such as the sun.

Corresponding render:

Corresponding fragment shader built by ShaderBuilder:

Spot Light

Let’s talk about the fanciest light ever! A spot light is the light of the stages. It is used in theaters, museums, stadiums and so on in order to point something out. Spot light is nothing more than a point light except for having a direction and fallout and coverage angles.

Calculating spot light is fairly easy. If dot product of light position to fragment position and spot light’s direction is less then cos of the falloff angle then calculate this area’s light like a point light. If it is more then cos of falloff angle but less then cos of coverage angle then the light is ((cosTheta – cosCoverage) / (cosFalloff – cosCoverage))^4 (Basic proportion). If none of these two options, then no light from spotlight at that position.

Corresponding render:

Corresponding fragment shader built by ShaderBuilder:

All three light types:

Corresponding render:

Corresponding fragment shader built by ShaderBuilder:

Dynamic Lighting

I added 1 point lights and 2 spot lights, and I updated point light and spot lights at runtime. Since this is a temporary change I put and updated them in Game class.

Corresponding render:

Corresponding fragment shader built by ShaderBuilder: