Adding light sources

Ambient lighting is fine, but it's obvious that you do not get far with it. For a really lively scene you need more dynamic light sources, things that do not light everything in the same way. For this reason Ultimate 3D supports per-vertex lighting. This works in the following way. You have to add a couple of light sources to the scene. Ultimate 3D will then use these light sources to calculate one color value for every vertex. This color will be interpolated over the triangle that is connected with the vertex. Ultimate 3D also supports per pixel lighting. But that's one of the techniques for advanced users.

There are three different types of light sources. The first one is called directional light. The classic example for a directional light is the sun. It lights everything from the same direction. No matter where you are (ignoring the fact that the earth is a sphere) the sun always seems to be at the same place in the sky. The opposite of a directional light is a point light. A point light can be compared to a light bulb that's hanging in the middle of a room. The left wall will be lit from the right and the right wall will be lit from the left because the light bulb is hanging between them. The farther away something is from the point light, the less it will be lit by the light source. The third type of light source is a spot light. A spot light is similar to a point light. But in contrast to the point light, it does not brighten up everything around it, but only the parts that lie in a defined cone of light.

As with primitive objects you need an instance of an object for every light source. And again you set up a few variables first, then you add a function call to create the light source and finally you put Step() and Destroy() into the corresponding events.


Directional light

So here are the variables you need to set up before creating a directional light.

rotx, roty
The names of these two variables are relics from very old versions of Ultimate 3D, because I did not want to break the backwards compatibility. Today I would call them longitude and latitude. They define the direction of the directional light source. To get a description of what longitude and latitude mean have a look at the description of the Move(...) function in the tutorial "Getting started".

r, g, b
These variables define the color of the light source. The values are usually in the range from 0 to 255. By increasing the values of these variables you increase the intensity of the light. You can also specify negative values for these variables. Then the light source will darken the objects that are lit by it.


Directional lights do not have a position and a range, because it simply does not matter. They are omnipresent. After setting up these variables you can call LightDirectional() to initialize the directional light source. If you haven't forgotten to include Step() in the step event you can change all parameters of all light sources at any time.


Point light

Let's go on with the variables of point lights.

x, y, z
These variables define the position of the point light source. The closer something is to this point, the stronger it will be lit by the point light.

range
The range of the point light. Everything that's farther away from the light source will not be affected by it. The bigger the range, the stronger the light's intensity.

r, g, b
These variables define the color of the light source. The values are usually in the range from 0 to 255. By increasing the values of these variables you increase the intensity of the light. You can also specify negative values for these variables. Then the light source will darken the objects that are lit by it.


After setting up these variables you can call LightPoint() to initialize the point light. Again you can change all properties of the light source at any time if you do not forget about Step().


Spot light

Let's get to the last type of light sources, the spot lights. They are a bit more complex than point lights and directional lights and they require more computing time. Here are their variables:

x, y, z
These variables define the position of the spot light source. The closer something is to this point, the stronger it will be lit by the light.

range
The range of the spot light. Everything that's farther away than this from the light source will not be affected by it. The bigger the range, the stronger the light's intensity.

rotx, roty
These two variables define the direction of the spot light. rotx is equivalent to longitude and roty is equivalent to latitude.

deg
This defines the angle that is lighted by the spot light. If the angle between the ray from the light source to the vertex and the direction vector of the light source defined by rotx and roty is bigger than this, the vertex will not be lit by the light source. In other words, it controls how wide the spotlight's area is.

r, g, b
These variables define the color of the light source. The values are usually in the range from 0 to 255. By increasing the values of these variables you increase the intensity of the light. You can also specify negative values for these variables. Then the light source will darken the objects that are lit by it.


After setting up these variables you can call LightSpot() to initialize the spot light and if you do not forget about the call to Step() in the step event you can change all properties at any time.


Subdividing primitives

Time to fulfill a promise. In the chapter about primitives I said that I did not list all the variables of the different primitive objects and that a few additional variables would be introduced here. You would not have understood why these variables exist without knowing about vertex based lighting. So here are the new variables:

partsx, partsy, (partsz)
These variables can be used to subdivide the primitives along the particular axes. partsz is listed in brackets because only cubes need it. Of course polygons do not have these variables. By subdividing a primitive you increase its vertex count. So a subdivided primitive will be lit with more detail since lighting is done on a per-vertex basis. The variables can be set to any positive integer value. But do not overdo this! It can greatly lower your fps.



© Christoph Peters. Some rights reserved.

Creative Commons License XHTML 1.0 Transitional