Tutorial 10 IND Light

From IndieLib
Jump to: navigation, search


Knowing lights

For what are lights? In real world our lights are: Sun, Moon, Lamp etc. Everything that is lighting another object like: our body, chair, computer Wink. Light manager implemented in IndieLib will help us to do that cool effect.



- Class managed by IND_LightManager for managing lights. - It used for setting position of light source, color, direction, range etc.

In IndlieLib there are four different types of lights:

Ambient light

Represents the background light in a scene. In the real world light bounces off many objects and creates a low light level so even geometry facing away from any directional light is still partially lit. Ambient light is very inexpensive in terms of frame rate - almost free. It is applied to all surfaces irrespective of the direction they are facing. There can be only one ambient light enabled at a time.

Directional light

Directional light is used to simulate distant light sources like the sun. It has a direction but no position. e.g. you could set it to point downward (0,-1,0) which would simulate the sun being directly overhead. Directional light is relatively inexpensive although if you add lots of them you may find your frame rate dropping somewhat. The lighting effect on objects depends on which way they are facing relative to the light direction (defined in the vertex normal). So a triangle with normal pointing straight up (0,1,0) would be lit by our sun (0,-1,0) fully - it would receive the maximum amount of light. As the angle of the normal to light increases less light is applied. If the triangle normal faces away from the light it is not lit at all. Methods you can use for changing the attributes of these type of lights:

- IND_Light::SetDirection()

- IND_Light::SetRange()

- IND_Light::SetColor()

Point light

A point light represents a point source. It has a position in the world and radiates light in all directions. A bare bulb on a stand would be an example of this. It radiates light in every direction and has a position in the world. This is a more expensive type of light in terms of frame rate than a directional light. A point light has an attenuation (use IND_Light::SetAttenuation()) that defines how the light level decreases over distance and a range. The range (use IND_Light::SetRange()) is the maximum distance the light will travel. Methods you can use for changing the attributes of these type of lights:

- IND_Light::SetPosition()

- IND_Light::SetRange()

- IND_Light::SetColor()

- IND_Light::SetAttenuation()

Spot light

Spot light is is the most complex light and is the most expensive type in terms of frame rate so use sparingly. A spot light has a position in space and a direction. It emits a cone of light with two degrees of intensity. It has a central brightly lit section and a surrounding dimly lit section that merges with the surrounding shadow. Only objects within the light cone are illuminated. To set a spot light you need to provide position, direction, cone size and a number of parameters determining the spread of the cone. Theta (use IND_Light::SetTheta()) is the angle that defines the inner cone while Phi (use IND_Light::SetPhi()) defines the outer cone. Falloff (use IND_Light::SetFalloff()) defines the decrease in illumination between the outside of the inner cone and the outside of the outer cone Methods you can use for changing the attributes of these type of lights:

- IND_Light::SetPosition()

- ND_Light::SetDirection()

- IND_Light::SetRange()

- IND_Light::SetColor()

- IND_Light::SetAttenuation()

- IND_Light::SetFalloff()

- IND_Light::SetPhi()

- IND_Light::SetTheta()

Starting with code.

Open file main.cpp in folder "a_10_IND_LIGHT".

First we must set , initalize and set colot of ambient light:

IND_Light mLight0;                                     //Making light0
mI->LightManager->Add (&mLight0, IND_AMBIENT_LIGHT);   //Adding Ambient light and setting it to light0
mLight0.SetColor (1.0f, 1.0f, 1.0f, 1.0f);             //Setting color of light

Next we initiate Directional light:

// Light 1 (Direction light)
IND_Light mLight1;                                         //Making light1
mI->LightManager->Add (&mLight1, IND_DIRECTIONAL_LIGHT);   //Adding Directional light to light1
mLight1.SetColor (1.0f, 1.0f, 1.0f, 1.0f);                 //Setting color of light
mLight1.SetDirection (0.0f, -0.3f, 0.5f);                  //Setting direction of light (now is lighting like a sun)
mLight1.SetRange (1000.0f);                                //Setting range of light

Initating Point light:

// Light 2 (Point light)
IND_Light mLight2;                                   //Making light2
mI->LightManager->Add (&mLight2, IND_POINT_LIGHT);   //Adding Point light to light2
mLight2.SetPosition (3, 3, 3);                       //Setting position of light
mLight2.SetColor (0.4f, 1.0f, 0.4f, 1.0f);           //Setting color of light
mLight2.SetRange (200);                              //Setting range of light
mLight2.SetAttenuation (0.5f);                       //Setting Attenuation

And the last one, Spot light:

   // Light 3 (Spot light)
   IND_Light mLight3;                                  //Making light3
   mI->LightManager->Add (&mLight3, IND_SPOT_LIGHT);   //Adding Spot light to light3
   mLight3.SetPosition (5, 5, 5);                      //Setting position of light
   mLight3.SetColor (1.0f, 1.0f, 1.0f, 1.0f);          //Setting color of light
   mLight3.SetDirection (0.0f, -0.3f, 0.5f);           //Setting Direction
   mLight3.SetRange (1000);                            //Setting range of light
   mLight3.SetAttenuation (0.2f);                      //Setting Attenuation
   mLight3.SetFalloff (1.0f);                          //Setting Fallof
   mLight3.SetPhi (8.0f);                              //Setting Phi
   mLight3.SetTheta (7);                               //Setting Theta

Someone may ask what is Attenuation, Falloff, Phi and Theta. Let me explain:

- Attenuation: defines how the light level decreases over distance and a range. - Theta: is the angle that defines the inner cone. - Phi: is the angle that defines the outer cone. - Falloff: defines the decrease in illumination between the outside of the inner cone and the outside of the outer cone.

Disabling Point light and Spot light:


Switching between types of light:

      // Activate only light 1
      if (mI->Input->IsKeyPressed (IND_1))
      // Activate only light 2
      if (mI->Input->IsKeyPressed (IND_2))
      // Activate only light 3
      if (mI->Input->IsKeyPressed (IND_3))

And now just updating lights:

mI->LightManager->Update ();

Was it hard ? I think not ;).

Now you can check Animated Tile Scrolling Tutorial.

By Armageddon ;).

Template:Category Tutorials

Personal tools