Basics of Light in 3D Computer Graphics

If you ever wondered how your desktop/laptop computer calculates and presents all the 3D graphics you see, if you are a beginner and want to learn how – this is the article for you!

It presents basics of how light and surfaces are interacting, the simply presented math (the math is actually quite complex, but presented in “plain english” so anyone can understand) will shed a lot of light on the subject. It also contains functions used by popular shader languages so one can learn to use those too.

In the following series we shall go deeper and deeper into more complex structures of 3D lightning, shaders, optimizations, normal mapping, displacement mapping… and I shall  explain how you should think to achieve to programme your favorite graphics card (may it be NVidia or ATI, they both work just fine) and have the most out of it!

Examples and work is accompanied with NVidia’s Cg shader code, OGRE materials and meshes, so you can implement what you learned right on the spot!

So, let’s dig in!

How light affects objects

“At the beginning, there was darkness, so God (yeah, in this case it’s You) created light, and everything was good.”

I’m not much of a religious guy but in this case I think it’s a really appropriate quote to use.  Without light you can not see your world, and you embedded so much time in creation of those 3d objects. As for the simplest case, let us for now reduce everything to 2d and consider the next image:

Surface and light example

Surface and light example

There is our basic plane (e.g. ground surface) and a light source just above it. The ground flat, and our light in this case is point based. What this means is that the light is coming out of a point in space and spreads it’s rays in every direction. Also known as omni light.

Equation of Light

In reality, light is bounced off the surface or absorbed. The steeper the angle, more is “eaten” by the object, thus getting darker. The light we see it actually reflected by the surfaces to our eyes. Direct angle -> more light, steep angle ->darkness. Simple.

How we actually calculate the amount of light in a given point is that we take the direction of where the light came from, and the normal of the surface. The amount of light recieved on the given spot will be the cosine of the angles between the normal and the light direction.

Light amount  = cos(angle between (Normal and Light Direction))

The good people designing graphics hardware gave us a helper so we do not have to manually calculate the angle and do a cosine, we only do a dot product:

Light amount  = dot(N, L)

where N is the normal of the surface, and L is the direction to the light. So, once you have the normal and the light direction, it’s a piece of cake to calculate the amount of light recieved at a given point on the surface.

Light Decay and Distance

In reality light spreads to infinite, but in computer graphics we do not want that because everything has to be calculated and it needs to be done optimally. The more lights you have on the scene the more time it takes for the GPU (Graphics Processor Unit, the BIG square chip on your graphics card with the LARGE heat sink) to calculate everything. Unless, we could approximate/optimize light.

In 3d computer simulations we use a model that has a final range of effect. Modern graphics engines, or some custom project someone might create, should contain effective optimization methods for calculating if a light effects an object. This is important. If we just kick in all the lights with an infinite range and all the geometry together we shall have a problem. We would have to calculate effect of every light, even if it’s effect is undetectable (minor, too small to effect the scene). Thus we introduce the “optimized” light source distance decay model:

Decay of light intesity in real and simulated worlds

Decay of light intesity in real and simulated worlds

The real life model has light influencing objects to infinity. The optimized square distance has an ending that is defined with the light source and the intermediate values are modified with math to simulate the square distance decay. The simplest model is the optimized linear falloff linearly decaying to the finite range. All these models are well known, and actually without the “optimized” word in front of them. I added it so you understand that we optimized the reality model to better fit our 3d graphics needs.

OGRE game engine has an automatic mode of calculating does a light reach a given object, so it is done for you, and you do not have to think about it. Of course, you need to feed OGRE with the range data, otherwise the light’s range is infinite by default.

Light Types

We already mentioned the point/omni light source. Other basic light types are:

  • directional light – light source is at infinite distance, so we are having parallel light rays. E.g. a Sun. It’s very far, so we do not bother with calculating where it comes from, we only declare it as a direction from where it arrived.
  • spot light – has a source point and a target point.  It defines where the light comes and where it goes. There are two subcategories here. The light can be parallel or perspective. The point is that it always spreads from source to target, and behind the source point there shall be no light.

You may ask, what’s the difference? Well, a directional light as all present on the scene, no matter where you go, and basically used as a “day light” simulation. On the other hand the spot lights are good for simulating localized effects like car lights, to bring light to your baseball field in the evening and such. Here is more visual example:

Basic Light Types

Basic Light Types

Summary

You may wonder why all this is important? Well, lights, scene and shaders  set up in a proper way will bring you rich and fast 3d graphics. It is basically easy to overload your graphics card. Of course it’s a form of art to bring users colorful, precise, rich and also fast 3d graphics.

The following articles in this series will bring you closer to 3d graphics and shaders, introducing gradually all the new things, so you don’t get overloaded.

Next part: Basics of Light in 3D Computer Graphics – Your First Cg Shader (coming soon)

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

4 comments to Basics of Light in 3D Computer Graphics

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">