Getting to know the Irrlicht 3D engine – Lighting

Google+ Pinterest LinkedIn Tumblr +

DOWNLOAD THE DEMO AND SOURCE CODE

RETURN TO THE TUTORIAL INDEX

In the last tutorial we added a 3D model to the screen. However, because there were no lights set up, we had to disable lighting on the model altogether, otherwise it would have appeared black. In this tutorial we will add a light to the scene.

ApplicationManager.h / ApplicationManager.cpp

The ApplicationManager gains two new variables: light 1 and billboard. The light is a point in the scene that is a source of light. It is not directly visible itself – the light is only visible on the surface of a 3D model. So we can see where the light is in the scene we will create a billboard, which is a flat rectangle that always faces the camera, and attach the light as a child of the billboard.

By attaching the light as a child of the billboard the light will move where ever the billboard moves. This kind of parent / child attachment is quite common in 3D engines, as it allows a group of objects to be modified at once. You could use this to attach a sword to a players hand for example, and then the sword would stick with the player as it moved around.

In the Startup function we make a small modification to the call to the addCameraSceneNodeFPS function. The ninja 3D model is quite small, and the camera moves way too fast in relation to the model. So here we have overridden some of the default parameters to slow the camera down.

We add the ninja 3D model to the scene using the same code as in the last tutorial. The only difference is that we now no longer disable the lighting.

We then create a new billboard by calling ISceneManager addBillboardSceneNode function. Again we override some of the default parameters to make the billboard a bit smaller, and more in line with the size of the ninja 3D model.

We call the setMaterialType function and supply the EMT_TRANSPARENT_ADD_COLOR flag to indicate that the colours in the texture assigned to the billboard will be added to the scene, which effectively makes the black parts of the texture transparent.

The setMaterialTexture function is then used to assign a texture. The first parameter defines the texture layer. We supply 0 because we are assigning this as the first (and only) texture. The second parameter is the texture itself, which in this case is loaded from a bitmap file.

The last thing we do is disable lighting for the billboard, just like we did with the 3D model in the last tutorial. This ensures that the billboard will shown at full brightness.

Now we need to create the light. We do this through the ISceneManager addLightSceneNode function.

The first parameter is the parent scene node of the light. We supply the billboard scene node here, which will have the effect of attaching the light to the billboard.

The second parameter is the lights position. This position is relative to the parent, so by setting it to 0,0,0 we are saying that the light will be positioned at the same place the billboard is, and not in the centre of the scene.

The final parameter is the colour of the light, which we have set to a purple.

We want the light to orbit the ninja so we can see how the light affects it from different angles. To do this we will use an animator. An animator is an object that can be used to modify the properties of another object it is attached to. You can create your own custom animators, but Irrlicht has a built in one, created through the ISceneManager createFlyCircleAnimator function, that will orbit our billboard (and, because it is attached a child to the billboard, the light) around the ninja.

The first parameter defines the centre of the orbit, while the second defines the orbiting speed.

We then assign the animator to the billboard through the ISceneNode addAnimator function.

At this point we have no more use for the animator. Because it was created with a “create” function, we call its drop function. The animator object still exists in memory though, because the billboard has reference to it.

When you run the application you should see the billboard circling the ninja model. And because the light has been attached to the billboard as a child, the light also orbits the model.

4170369299_782eb652b7_o.png

Share.

About Author

Leave A Reply