Tuesday 19 March 2013

Lighting in Bullet Devil

Welcome back to another episode of lighting in games! Today's special we will be serving up lighting from the game Bullet Devil, produced by Studio 8 for the game development workshop (GDW). We will be taking a look at the difference lighting makes in the game using shaders. Then we will go into some detail on the code (just the shaders) on how we go about doing this. Let's get to it.


So lets start by showing the game as it was before we introduced it to the magical world of shaderland. The image below shows a very plain and almost 2D looking game without any source of shader power. The main character model is textured red as a temporary texture because the models are changing anyways for the game.


Now it's hard to see in these pictures because they are so small, so I recommend you go and play the game, but there is shading on a per fragment basis applied to the models in the game. Now if you don't know what per fragment lighting is then i suggest you go back to my previous blog and do some reading! 

Time for the fun details on how this was accomplished. Again, the theory is all explained in my previous blog so I'm just going to jump into how we did it. Most of the reference for how we did this comes from what we learned in the tutorial with some modifications. Those modifications were to change the per vertex lighting model shown in class and make it per fragment lighting. We also have shadow mapping developed into the shader but won't be used until the FBO's are properly working. If you don't know what an FBO is read on to my next blog where i discuss frame buffer objects. 

Alright, now when we were taught this the version used is 120. We plan to continue to use 120 to the end of the semester. Next year our team will step up to a better version like 330 or 420 but for now, 120 is doing the job for us. Here is some sample code of our vertex shader:

Next is the sample code for our fragment shader:




So as you can see, it stays very close to what I explained in the last blog. Only thing that's there that I haven't explained in detail is the shadow mapping. I did touch on it in my post processing blog but when it is actually in game I will have a blog for that. At the end of the day it has it's own math for the algorithm and it can simply be added to the end result of the per fragment lighting in game to give the proper results.  


Lighting in Games

Today I'm going to go over some aspects of my game's graphics! Last blog was all about other games that are so fancy with their graphics so today I'll bring it down to a student level. I'll go over some of the concepts I've learned in class as well as the implementation of lighting in my studio's game so far.

Now lighting is a simple and complex entity of games, it all depends on how you plan on doing lights in your game. The development of lights in a game can be as simple as not even programming any of it and simply faking it all with textures having lighting baked onto them. This is quick and easy but not very effective. The reason for that is because all of the lighting must be static as well as your objects in the scene. Baking textures with fake lights is very computationally efficient, however it shouldn't comprise your entire lighting. A step up from light baking, is programming lighting on the CPU. It is a bad way to do lighting because the game is limited to very few lights before the computer starts to cry and frame rate tanks. Now to take lighting to the next level, one would move the computation of lighting to the GPU and calculate it within their shaders.

To write lighting in shaders can be approached in many different ways. In our tutorials we learned how to compute lighting on a per vertex basis. What that means is that in our vertex shader, we calculate the color of the vertices based on a few different parameters such as ambiance, diffuse, and shininess. Now this is an ok method of doing lighting in game but there are better ways. The best example of better is moving these calculations of lighting over to the fragment shader and doing lighting on a per pixel basis rather than per vertex. This will give far better quality lighting as each pixel is being computed to have the perfect color rather than linearly interpolating the color in between the vertices. Here below is a perfect example of the difference between the two:
On the left is lighting calculated on a per vertex basis and on the right we have lighting done on a per fragment basis. This method is useful because it is dynamic and fast. However, with this method it is fast with a limit of lights. Trying to do more than a dozen lights will again cause the game to lag hard trying to keep up with all the computation. There is a way to have all the lights one desires, do not stress. This can be done with a method of deferred rendering, a topic which deserves it's own post.

So much more can be said about lighting, but I will end the theory on a mathematical note. I will briefly discuss how light is calculated in real life and how it is done in games. So for real life, its a very accurate approximation that incorporates all aspects of lighting such as the relativity of other objects in the scene and how they absorb/reflect light and so on. In short, this is the equation for real life lighting:
Now I'm going to be honest, I'm not too sure whats going on in this equation but that's because I make games so I approximate. Now we want to focus on the ambient, diffuse, and specular of lights. 

Ambient refers to the intensity of light in a scene so it can be mathematically described as: Intensity = K * IlluminationAmbient. Having K being a value between 0 and 1.

Now for diffuse lighting we look at Lambert's Laws and specifically the law where if the rays meet the surface at an angle, then the illuminance is proportional to the cosine of the angle with the normal. We can take this an form an equation: I = KdIpcosx. Kd is diffuse lighting constant (0..1). Ip is the intensity of the point light source. x is angle between light source and Normal. Now we know that the dot product is proportional to cosine theta. We must also incorporate the distance of the light to the surface point we are lighting. This distance would be squared and divided by the above equation but we could end up with a division by zero error so we can add tuning parameters which will give us a final equation of:
Specular lighting is calculated similar to diffuse lighting but the angle we are interested in is the angle from viewer and the reflected ray.So this equation has instead of the dot product of the normal and light source, the dot product of the reflected ray and the viewer squared similar to this:
Now to sum things up, we literally sum up the three equations and we have lighting in games. This theory was longer than I thought it would be, so in my next blog you will see more lighting but strictly for Studio 8's Bullet Devil.

Post Processing in Games

This week's blog will be covering the wonders of post processing. For this segment of my blog I will do a reel of photos from several different AAA titles and go over the different post processing effects applied in their games. So lets dive right into it shall we.

The first game title we are going to be looking at is Gears of War. The effect I'm going to focus on for this title is what is called bloom. Now, bloom is in simplest terms making the bright parts of the image brighter and the darker parts of the image darker. You would split the scene into two frames where one creates a blur effect, which I will talk about later, and the other is the original with amplified light to the bright areas. When this happens, you will see a light glow around characters that have high amounts of light behind them. This creates a high contrast and can set a certain mood in specific scenes in a game. Here in the image you can clearly see the use of bloom in game where the sky has been brightened and the foreground characters have been darkened and the glow is evident on the characters.
 
The next title we are going to look at is Assassin's Creed 3. In this image of the game we will examine the effect of depth of field. Depth of field is when the outside of the view of the cameras focus is blurred. So in the example below you can see that the people on the left hand side are very blurring because they are outside of the depth of field. This can be done by again using two frame buffers, one being your depth buffer and the other one being your blur buffer. 

Modern Warfare is a very widely known game for it's gameplay and graphics. They too implement post processing effects in their game such as motion blur. This effect causes the game to add extra sense of realism as this happens when someone is focusing in on one position. The image below is a good example of motion blur. The way how they create motion blur is when render a frame of the game to a frame buffer, down size it, then stretch it back to normal size, making the image very blurry, finally adding that image to the original scene. 

Now another shader and post processing effect we are going to discuss is cell shading or commonly known as toon shading. Now this non-photo realistic effect is partly a post processing effect because the black line drawn around the object is actually done after the graphics pipeline. This is done by a method of edge detection of an object. With edge detection, many other effects can be done. The shading of the object using cell shading is done based on a blocky gradient. Below is an image with the difference between normal shaders and toon shading. There is also an example of this effect in Borderlands.
Now for the next post processing effect, shadow mapping. This is a very important effect to give lots of depth and realism to a game. Although, many times it is faked in games, this is because computing shadows for the entirety of the game is very computationally expensive, some objects are dynamic and not static in which they need accurate shadows calculated for them. In the image below, you can see the dynamic shadow casted by the player while the shadows in the background are baked into the textures of the terrain. More commonly, one would see baked textures on walls in hallways that have many lights in them to save on computation.