
|
December 2004 |
|
|
| |
|
Adding light to the scene can make a big difference to the final view of a 3D application, but there's a lot to think about and the results may not always be as expected. This article describes how to apply light to a scene in a 3D application developed with JSR-184, and how to set the material of an object.
Before we start, one thing's important to know: Adding light to a 3D scene can be a very expensive operation in regards to performance, so keep this in mind. There are four different lighting options in JSR-184: Ambient: Ambient light is lighting that has been scattered from the surrounding environment an infinite number of times, making it impossible to determine the light source. The position and direction of ambient lighting doesn't affect the final results. Directional: Directional lighting illustrates real-world sunlight and once it hits a surface, it's scattered equally in all directions. The position of Directional lighting has no effect on the final results. Omnidirectional: Omnidirectional lighting can be referred to as a point light. The light is equally distributed in all directions from the lighting source. You can change the results by changing the position of the lighting source, but changing the direction has no effect. Spot: Spot lighting casts a cone of light in the chosen direction. The cone's width and intensity can be changed. The scene is affected by the position and the direction of spot lighting. When adding light to a 3D scene, there are a few things to remember. All the objects to be lighted must have a material which reflects the light and all polygons (triangles) in the scene must have a corresponding normal. The normal is a vector with a 90º angle relative to the plane.
In JSR-184 we have to enter one normal for every point used in the figure above. The below code shows how this can be done with a pyramid. Keep in mind that normals are entered with values from –127 to 127. // The vertices used by the pyramid. x, y, z // The wall angle of the pyramid is 70º
We also have to set the material to the mesh: Material material = new Material(); The light reflected from the figure is affected by the color of the material. A red material (0xFF0000) will only reflect the red light, even if the light source is pure white, and a blue material will only reflect the blue light. After the normals are set and the material is added, you only have to add the light itself to the scene. light = new Light(); // Create a new light world.addChild(light); // Add the light to the world.
A small MIDlet that uses these techniques can be downloaded from the link below. | |
Copyright © 2001 - 2009 Sony Ericsson Mobile Communications AB. All Rights Reserved.