Header Sep
Java Tips, Tricks & Code
My Rating Score
Login to rate page
September 2004
First JSR-184 MIDlet
 

[Back]

This article will describe how to setup a 3D world using the Mobile 3D graphics API, also known as JSR 184.

In this example of a 3D world we have one active camera that is focused on our only 3D object, a rotating pyramid. A MIDlet with source code to download can be found below.


All objects used in the scene are initialized in the constructor of the M3GCanvas class.

We start by initializing our Graphics3D object that is used to do the rendering to the screen. The World object contains all the 3D object and camera information that is used in the scene.

The setPerspective method used by the camera states that all objects within the range of 0.1 and 50 units in the coordinate system in front of the camera should be visible.

Since both the camera and the pyramid are located at xyz (0.0f, 0.0f, 0.0f), we have to move the pyramid into the screen so that we can see it. This is done by the setTranslation method in the Mesh class.

g3d = Graphics3D.getInstance();
world = new World();

camera = new Camera();
world.addChild(camera); // add the camera to the world.

// The width and height of the canvas.
float w = getWidth();
float h = getHeight();

// Constructs a perspective projection matrix and sets that as the current projection matrix.
camera.setPerspective(60.0f, w / h, 0.1f, 50f);

pyramidMesh = createPyramid(); // create our pyramid.
pyramidMesh.setTranslation(0.0f, 0.0f, -3.0f); // move the pyramid 3 units into the screen.
world.addChild(pyramidMesh); // add the pyramid to the world
world.setActiveCamera(camera);

 

Our main 3D object in this world is the pyramid and the pyramid is created in the createPyramid method. The code in the method is pretty straightforward.


To create the pyramid we first of all have to specify the vertices to use.
Since we use colors in this example we have to specify a color for each vertex of the pyramid. The INDICES array holds the sequence of the POINTS and COLORS. Point 1 will be red, point 2 green, point 3 blue, point 4 violet and point 5 will be cyan.

The nice color shading is set by the setShading(PolygonMode.SHADE_SMOOTH) method.
To only have one color for each side specify SHADE_FLAT.


The pyramid is built of six triangles, one for each side and two for the bottom.
To just create one triangle we have to specify the tree points used by the triangle as in the following code.

// The vertices used by a triangle. x, y, z
short []POINTS = new short[] {-1, -1, 0, // point 1
                               1, -1, 0, // point 2
                               0, 1, 0}; // point 3
                                                           
// The point and color sequence.
int []INDICES = new int[] {0, 1, 2};
byte []COLORS = new byte[] {127, 0, 0, //R
                             0, 127, 0,  //G
                             0, 0, 127};//B
                                                       
// The length of each sequence in the indices array.
int []LENGTH = new int[] {3};

Since we are to create a whole pyramid we have to specify the five points that are used.

// The vertices used by the pyramid. x, y, z
short []POINTS = new short[] {-1, -1, 1, // point 1
                               1, -1, 1, // point 2
                               1, -1, -1, // point 3
                              -1, -1, -1, // point 4
                               0, 1, 0}; // point 5, top
                                                       
                                                           
// The points sequence.
int []INDICES = new int[] {0, 1, 4, 1, 2, 4, 2, 3, 4, 3, 0, 4, 2, 1, 0, 2, 0, 3};
byte []COLORS = new byte[] {127, 0, 0, //R
                            0, 127, 0,  //G
                            0, 0, 127, //B
                            127, 0, 127, //B
                             0, 127, 127};//B
                                                       

// The length of each sequence in the indices array.
int []LENGTH = new int[] {3, 3, 3, 3, 3, 3}; // the pyramid is built by six triangles

The code blow is the same regardless if you choose to create a triangle or a pyramid.

VertexArray POSITION_ARRAY, COLOR_ARRAY;
IndexBuffer INDEX_BUFFER;
       
// Create a VertexArray to be used by the VertexBuffer
POSITION_ARRAY = new VertexArray(POINTS.length / 3, 3, 2);
POSITION_ARRAY.set(0, POINTS.length / 3, POINTS);
COLOR_ARRAY = new VertexArray(COLORS.length / 3, 3, 1);
COLOR_ARRAY.set(0, COLORS.length / 3, COLORS);
INDEX_BUFFER = new TriangleStripArray(INDICES, LENGTH);
       
// VertexBuffer holds references to VertexArrays that contain the positions, colors, normals,
// and texture coordinates for a set of vertices
VertexBuffer vertexBuffer = new VertexBuffer();
vertexBuffer.setPositions(POSITION_ARRAY, 1.0f, null);
vertexBuffer.setColors(COLOR_ARRAY);
       
// Create the 3D object defined as a polygonal surface
Mesh mesh = new Mesh(vertexBuffer, INDEX_BUFFER, null);
       
Appearance appearance = new Appearance(); // A set of component objects that define the rendering attributes of a Mesh
PolygonMode polygonMode = new PolygonMode(); // An Appearance component encapsulating polygon-level attributes
polygonMode.setPerspectiveCorrectionEnable(true);
polygonMode.setCulling(PolygonMode.CULL_NONE); // By using CULL_NONE all faces of the pyramid will be shown.
polygonMode.setShading(PolygonMode.SHADE_SMOOTH); // use a smooth shading of the colors on the pyramid.
appearance.setPolygonMode(polygonMode);
       
mesh.setAppearance(0, appearance); // Set the appearance to the 3D object


Download the complete source code>>

 


 
My Rating Score
Login to rate page