Monday, February 15, 2010

Lighting models for games I

This is going to be the first in a series of lectures focusing on covering some lighting models for games and also the actual physical properties of light and materials they are based upon. We will also work with examples booth from real life and from a simple engine using some art courtesy of the students from TheGameAssembly.

But for this first part we will be focusing our attention on the different properties of light and actually understanding what is happening in the world around us. As in the end this is what we are going to try to simulate in the computer.

This is going to be the first in a series of lectures focusing on covering some lighting models for games and also the actual physical properties of light and materials they are based upon. We will also work with examples booth from real life and from a simple engine using some art courtesy of the students from TheGameAssembly.

But for this first part we will be focusing our attention on the different properties of light and actually understanding what is happening in the world around us. As in the end this is what we are going to try to simulate in the computer.

However before we go deeper into this we have to remember the golden rule of Computer Graphics, "If it looks right it is right". So with this in mind why are we obsessing about how reality works when our only goal is to make it look good? The problem is what I will refer to as visual dead ends. This is what happens when you are pursuing a certain technology with great results and your game is looking better and better but you hit a wall front on visually and you just can't raise the visual fidelity of your game.

The main reason for those dead ends is when you are working with technology you just don't understand you know the effect but you don't know the physical backings and therefore you can adapt it properly or get it to coexist with other techniques. So the reason to obsess about the real world is that the real world doesn’t have dead ends, because everything is coexisting in an extremely complex system. We don't need to understand this system to make nice graphics but it does help. It tells us what it is we are trying to simulate so we know what techniques to use to achieve it. This allows us to recognize how far back we need to go and where to restart when we reach a dead end. Because the answer is almost always go back to the real world and watch what you are trying to model. Because reaching a dead end is often the result of applying a technique in a way it was not meant to be used instead of using it for what it is simulating.

All right enough about why we need to learn this stuff and over to the meat of the update. Before we start I will issue a warning to anyone who has seriously studied the properties of light and its effect. I will try to use laymen terms for these updates so anyone who has studied radiometry or colorimetry will notice that I will grossly simplify things in order to keep it simple and to the point.

You are probably aware of that all we see in the world is light. We don't object we just see the light reflected of objects. What makes this all interesting is that you don't just see light that has come from the light source and bounced on the object instead light bounces on everything multiple times until finally the light dissipates this creates an effect of objects being nearer to each other  mixes their colours together,

All the light we see in the world around us is composed of multiple sources, it is really rare in reality that we are watching light that comes straight from a light source it has already bounced or passed through multiple surfaces.

Consider a normal table lamp, it emits light in all possible directions from a wide area of the copper thread, then this light passes through the material of the glow bulb which might be glass but it can also be a dimmed or colored material that affects the light the reason for using a dimmed light bulb is to diffuse the light to make it feel softer. Then the light that passes through the light bulb is bounced vs. the inside of the table lamp in an attempt to direct it in a correct direction so that we get a sort of directed light from  the copper thread who just emits light in all directions.

So the light we are watching coming out of the lamp has already been bounced around a bit, but this is not all it also bounces and hits a lot of small particles which floats around in the air around us.

Let’s look at an example to make this effect of bouncing clearer.

If you look at this image it should be obvious that we have a light source coming from the upper left.

You should also be noticing the clear red and green colouring on the sphere. This is due to light bouncing on the red and green papers place there.

If you look closely at the bottom of the sphere you will notice that it is not only greener at the bottom but it is also brighter.

This is because there is a larger area that the light might bounce vs. to hit that part.

If we look at this example image I have decided to represent rays of light as arrows and when they have reflected vs. a material I have colored the arrows accordingly.

Now is a good time to mention that light does not really consists of rays it consists of small particles called photons.

So what we are seeing is photons bouncing on the different surfaces they encounter. And some of them bounce into our eyes and that makes up what we are seeing.

Or rather it is interpreted by our mind as an image. We will later on discuss how the inner workings of our eyes and our brains also affect what we see but that is for a much later lecture.

By now it should feel obvious that there is no way we can simulate all of this in real time on the current generation of hardware. So what do we do? We cheat, we use different approximations to try to get a light that looks like reality without having to perform all the calculations necessary to really approximate reality.

We will look at those approximations and also look at their effect in real life using a pair of spheres. I have opted to represent light as we see it as 4 different components (I have not made these up myself obviously these are based on a long tradition of research in computer graphics)
  •  Ambient Light
  •  Diffuse Light
  •  Specular Light
  •  Reflections (these also consists of light since all we see is light)
 Alright now we have selected our representation. These might be a bit mysterious as to what they mean right now(or not) So we are going to find them one by one by examining reality and se how they occur and how we can reproduce that effect.

I wrote earlier that what we physically see is light that is reflected vs. our eyes from a surface. When light hits a surface part of it is absorbed by the surface and part of it is reflected back it is this reflected light we see.

This isn’t exactly completely true the light that is absorbed does in reality pass into the surface. If the surface is thin or the material isn't too dense some of this light might pass through it. The human skin as an example of a material light easily passes though this is quite observable on the ears. But what happens inside the material is also interesting materials in real life don’t have a perfectly consistent density which means that inside the material the light might hit more denser areas that change its direction this causes the light to scatter around inside the material and some of this light might pass back out though the material again and out in the rest of the world. This effect is known as sub surface scattering. But for what we are going to look at for now the approximation I mentioned first is sufficient.

Let’s first establish a couple of terms and concepts that we are going to use to discuss the rest of the material with.
  • Radiant flux is the amount of energy it emits per second. Simply put the collected energy from the photons emitted under a second.
  • Irradiance is the term for radiant flux with respect to an area, normally the surface of an object, Sp irradiance is used for the light flowing into a surface with each light source in the world having their own irradiance contribution to the surface. Quite simply it's the incoming light to that area.
  • Exitance Exitance is the amount of light that exits a surface the difference between exitance and irradiance is what we will refer to as the material function the function the represents the properties of the material.
  • Radiance is what we actually see what eyes and cameras capture radiance is basically the flow of light for an area in a specific direction. The direction towards the sensor(eye or camera etc) that captures the light. This is different form exitance that is just what leaves the surface this is what leaves it in a specific direction.
I think we have enough terms now to get started. This might have seen as overkill for what we are going to do but this means we can use these terms in our equations.

So how do we calculate the light at a specific point on a surface ? Well first there are no such things as a point as everything is areas but to simplify this let's assume that we can use an areas with the size of a pixel.
float irradiance=0;
float exitance=0;
for(int i=0;i
radiance=MaterialReflectionFunction( eyePosition,surfacePosition,exitance);

As you can see I have split our material function into two parts one that determines the properties of the material in regards to absorption of light. And one that represents the material according to how it reflects light, Observes that this has nothing to do with its absorption but is simply a representation of the roughness of the surface which affects how scattered the directions of the photons will be after they have hit the surface.

Basic physics says that the exit direction of a bouncing ray on a surface will have the same angular relation to the surface normal as the incoming direction. The problem is that our surfaces has small microscopic irregularities that  will cause the normal to change and the light to scatter since a pixel does represent quite a large area in this perspective. So the photons will bounce around in all different directions while obviously more will bounce according to the basic angle equation there will be a gradient of light strength depending on how far away from that angle you are. And that's what this function represents in our equation observe that will refer to code as equations because that's what they are representing just in a format that is more readable for more people.

Now let’s talk about those light terms we talked about earlier. First out is Ambient light, This is the term we use to represent bounced light in the world, Since we obviously can't calculate all the bounces we use this term instead The ambient light is considered to have bounced on so many surfaces that it can be considered direction less so we think it's coming equally much from all directions.  This means that no matter what direction a surface is facing it will receive the same ambient light ( we will later talk on methods for directional ambient but for now this will hold true) considering this it would mean that if there are no direct light sources all objects in the scene would receive the same amount of light. Basically ambient light represents all the bounced light in the world and all global illumination models like radiosity etc are working on getting a more accurate version of this than what we have just described. Important is that in the end the ambient light just adds to the irradiance of the surface as an irradiance contributor.

For now let's move on to the Diffuse light component We talked earlier about how the surface aren’t perfectly flat on the molecular level so the light that hits it will bounce in different directions diffusing the light that hits our eye compared to the case where it is all reflected sprightly according to the angle vs. the normal. The diffuse lightning component models these reflections and contributes different amount of lights depending on the angle between the eye and the surface. Of course there won’t be any diffuse light in the case of the photons being blocked by an object because this means we are in the shadow seen from that light source.

Let’s reflect on it based on this image this is from a  one light source environment but with tons of bounced light in the room. We can clearly see the sphere being lighter on the left half where the light source is. And if you look closely you will see a line around the sphere where the diffuse light stops contributing. If you have trouble seeing it please look at the image below.

This line has many names in artistic tradition, the most formal is the termination line. But most famous is probably the bed bug line, showing how horrible environments art students used to live in where the bugs appeared as soon as the light disappears. What’s important with this line is that it is where the diffuse contribution from our light source stops. All other  visual effects on the sphere are created by bounced light, which we models as ambient. So all that happens in there is the ambient light, the same holds for the area in the shadow from the sphere while the rest is lit with diffuse light. So what happens if we remove our diffuse light which in real world is removing the light source.

One thing to notice except everything getting a lot darker is that we can hardly se any bounced light hitting the sphere. This is because we have removed the light source that cast those strong rays that bounced on the ground and hit the sphere. So because we no longer have any strong light source we aren't seeing a bounced light. This might be confusing because this means what happened in our ambient part of the light changed when we removed the light creating our diffuse light. The reason for this is that the lighting models we use in games by necessity are gross oversimplifications of reality we simply can’t model these light bounces in real time.

One thing however to notice is that there is still some kind of shadow near the bottom of the sphere. If all we saw was ambient light which is directionless then there shouldn’t have been any shadow there. Is it because or ambient model is flawed ? Well it is flawed but what we see here is something we can model with our current model. Even if the ambient light is coming from all directions at once if there is something blocking of some of these directions then the bounce light can’t reach the position because it is occluded, this is called ambient occlusion. What it means is that  down there at the bottom of the sphere the light can't come much from below because it is blocked and it can't come from above because it is blocked too so that area receives less of the ambient light because some of the ambient directions are occluded. How to calculate this occlusion is however beyond the scope of this lecture. Thankfully we don’t need to, modern 3d programs can calculate if for us.

Now let’s get on to specular light.  Diffuse light was representing the bouncing of the light when it hits a surface, Specular represent the rays that are reflected straight into our eyes on an even reflection angle. How much specularity we see depends on the smoothness of the material on the molecular lever. Plastic and iron have very visible specular if you look at them being lit by a lamp you will see a very bright spot and as you moves  your head that spot will move. This is because it represents the light directly hitting your eye on the bounce which means if you move your eyes you will change what point on the surface that creates the correct angle for a direct reflection towards you. The area of the reflected light depends on the smoothness of the material. In real life you will almost always perceive specular light as white. This isn't because it is white it's because it so much brighter than the rest that your eyes  thinks of it as white. In the real world also almost all light sources have some small part of all colours. if you had a perfectly red lamp for example however it would produce a red specular highlight this can be confirmed by experimenting, but remember your eyes are trying to trick you because they are used to assign the brightest spot as white (which incidentally is why cameras has white balance correction to make sure that it gets white no matter what colour it really does have)

Finally we have the last term the Reflections if we think back reflections are just light that had bounced of an object and hit your eyes, Which means that all reflections are at least second degree bounces for light must first have hit an object bounced on that hit the reflecting object and bounced on that before it hit's our eye. Now a lot of people will say that reflections are only useful for modelling special effects on very reflective objects like glass, steel etc while it is true that we mostly see reflections on those objects it doesn’t mean reflections are limited to them in fact there are reflections on all objects they might just not be clearly visible to the eye (This makes sense as all light we see are bounced that all materials might receive bounced light from other objects) So how come that we see it clearly in some materials and not others ? Once again it is done to the structure of the material some materials scatter the light to much for there to be any meaning full reflection for us to see just mixing the reflection in with the rest of the bounced light while some structures like a mirror has an almost perfectly flat surface and surface structure and incredibly low absorption allowing it to return the light virtually as it was seen creating a very clear reflective image.

One interesting properties of reflections is that they are not affected at all by the lighting conditions of the object they are being reflected in. Which means that reflections is a technique that brings a lot of life into those dull shadow areas that easily turns out to have only one intensity everywhere. I know a lot of people would say this is gibberish so I will here show 3 images showing how reflections react in reality with changing light conditions.

If you look at the small sphere you will clearly see the large sphere reflected in it, these are normal light conditions both object are in the light from a single light source and the reflection is passing over the bedbug line. If you look closely you will see that the reflection looks slightly cleared below the bedbug line it’s because there is less light there the light from the reflection stands out more. If you can’t see it then lets look at this next image.

What I am doing here is that I am holding up credit card blocking the light source, I have controlled this so that it only blocks the light on the right part of the left sphere. if you look just at the reflection you will see that the reflection is just as bright as before despite that spot on the sphere being deprived of direct light. It well even seems clearer and stronger now but that is just due to the increased contrast due to the rest of the sphere being darker. You can also again notice that we have much less bounced light on the left sphere since we have blocked the photons that would produce it.

In this final experiment I hold the credit card over the right sphere instead blocking the light to it, what you will notice is that the reflection has disappeared almost completely. This is because there are not as many photons bouncing of the right sphere because we are blocking them out.

So based on this we can state clearly that the strength of reflections are dependent on the lighting conditions of the object being reflected and is not concerned at all with the lighting conditions of the object it is being reflected in. (just put a mirror in a dark room and put yourself so you can see it and shine light on yourself and you will see your reflection clearly.)

Well this was all for today. The next time around we will actually look at creating equations for what we have talked about for games and look at the visual differences we get playing around with the different parts of light.

1 comment:

  1. I've never read anything with such atrocious grammar and spelling errors. These errors make it very difficult to read this article, but it's also one of the most explanatory things I've read on the subject and it's helping me understand a lot.

    Thanks for taking the time to write this out... But I think people would appreciate it if you revised and corrected it.