Rendering a 3D model
Let's take a look at how to render a basic 3D model.
Table of contents
1. Model components
A 3D model consists of the following 3 components:
- Model data ( Figure )
This data builds a basic structure of the 3D model. It is obtained by loading the MBAC data. -
Texture data ( Texture )
This data contains textures to be applied to a model, and the background, etc. It is obtained by loading the BMP ( bitmap ) data. -
Action data ( ActTable )
This data contains information of the model's animation. It is obtained by loading the MTRA data.
2. Model rendering procedure
Let's take a look at how to render a 3D model using the sample code.
2.1. Creating the Figure, Texture, and ActionTable objects
Create the instance of data objects ( Figure, Texture, and ActionTable ) to be used.
Set the data to be loaded in the constructor. Specify the type of texture in the second parameter for Texture; specify true for standard textures, and false for environment mapping.
If you want to apply multiple textures to a single model data, prepare the array containing their Texture objects.
Then associate the created Figure object with Texture, using the setTexture( Texture texture ) method or setTexture( Texture[ ] texArray ) method in the Figure class. When there are multiple textures to be set, pass the array containing these textures as the parameter.
* CanvasEx
* Constructor ( initialization of data loading, etc. )
*/
public CanvasEx(){
try{
figure = new Figure ( "Dragon.mbac" );
/* Loading the action data */
actTable = new ActionTable ( "Dragon.mtra" );
/* Loading the texture data */
texArray [ 0 ] = new Texture ( "Dragon.bmp", true );
texArray [ 1 ] = new Texture ( "DragonBG.bmp", true );
/* Setting the Texture to Figure */
figure.setTexture( texArray );
2.2. Specifying the camera and light settings
Create the transformation matrix that converts the world coordinates to camera coordinates, and the Light object that specifies light setting.
In order to create the transformation matrix to the camera coordinates, use the setViewTrans( Vector3D position, Vector3D direction, Vector3D up ) method in the AffineTrans class.
In this method, position represents the camera position, direction represents the direction of the camera, and up represents which direction the camera's head is facing. The Light object specifies the direction and intensity of directional light, and the intensity of ambient light. To do so, create the Light objects first, and then use the method that specifies settings for these objects, or use the constructor that specifies settings.
vtrans.setViewTrans( new Vector3D( 0, 1024, 8192 ),
new Vector3D( 0, 4096, 0 ));
/* Specifying the light */
light.setAmbIntensity( 4096 );
2.3. Creating the Effect3D object
Specify the settings for various visual effects to be used when rendering 3D graphics.
In MEXA, use the Effect3D object to specify the visual effects settings for lighting, shading, semi-transparency, environment mapping, etc.
The following is the details of each parameter in the Effect3D class constructor, public Effect3D( Light light, int shading, boolean isEnabled, Texture sphereMap ).
| Parameter | Details |
|---|---|
| light | A light object. Set the Light object that you have just created. |
| shading | Specifies the shading method. Specify NORMAL_SHADING for gouraud shading ( standard shading ), and TOON_SHADING for toon shading. |
| isEnabled | Enables/Disables the semi-transparency effect. Specify true to enable, and false to disable. |
| sphereMap | Specifies the environment mapping. Specify the Texture object to be used as the environment map. Specify null for no environment map. |
Also, these settings can be individually set later, using the method provided in the Effect3D class.
effect = new Effect3D( light, Effect3D.NORMAL_SHADING, true, null );
2.4. Creating the FigureLayout object
Specify the rendering layout settings for the model.
In MEXA, use the FigureLayout object to specify settings for the model's rendering position, direction, size, projection methods, etc.
The following is the details of each parameter in the FigureLayout class constructor, FigureLayout( AffineTrans, trans, int x_scale, int y_scale, int cx, int cy ).
| Parameter | Details |
|---|---|
| trans | Sets the affine transformation matrix. |
| x_scale, y_scale | Specifies the scaling for the x-axis and y-axis directions. |
| cx, cy | Specifies the x-coordinate and y-coordinate for the center of the rendering area. |
Also, these settings can be individually set later, using the method provided in the FigureLayout class. Specify the projection method setting in the setParallelSize( ) method for parallel projection, or in the setPerspective( ) method for perspective projection.
/* Specifying the projection method setting */
layout.setPerspective( 1024, 10000, 4096 * 45 / 360 );
/* Specifying the coordinate transformation-related setting */
layout.setAffineTrans( vtrans );
/* Specifying the screen setting */
layout.setCenter( centerX, centerY );
2.5. Specifying the model's action
When an animation is set for the model, specify the model posture settings for the target rendering time.
In order to set the model posture, use the Figure.setPosture( ActionTable actable, int action, int frame ) method and specify the frame to be rendered. Use ActionTable.getNumFrame( int action ) to obtain the maximum number of frames for the action data.
max_frame = actTable.getNumFrame( 0 );
/* Specifying the action setting */
frame += ( 65536 * 3 );
frame %= max_frame;
figure.setPosture( actTable, 0, frame );
2.6. Rendering the model
Render the obtained objects, and transfer each of them to VRAM.
In order to execute rendering, use the renderFigure( Figure figure, int x, int y, FigureLayout layout, Effect3D effect ) method in Graphics3D. The second parameter x, and the third parameter y defines the rendering position. However, this is just a rendering registration at this stage, and the rendering process will not be executed in the frame buffer. Lastly, call the Graphics3D.flush( ) method to execute the actual rendering process of the objects that have been registered.
The Graphcs3D.drawFigure( Figure figure, int x, int y, FigureLayout layout, Effect3D effect ) method executes renderFigure( ) and flush( ). Therefore, instead of executing these two methods, the drawFigure( ) method can be executed for rendering.
After completing the rendering, it is necessary to call the release( Graphics g ) method in Graphics3D, and release the Graphics object from the rendering target.
Also, rendering speed becomes slow if the repaint( ) method is used during rendering; therefore, it is recommended to use the getGraphics( ) method in the GameCanvas class while always retaining the Graphics object, and execute rendering using the paint( Graphics g ) method and the flushGraphics( ) method.
paint( graphics );
flushGraphics();
try{
}
/**
* paint() method
* Clearing the display screen, specifying the clipping area / Clearing, 3D rendering
*
* @param g Graphics instance
*/
public void paint( Graphics g ) {
g.setColor( bgc );
g.fillRect( 0, 0, getWidth(), getHeight() );
/* Specifying the rendering target */
g3d.bind( g );
/* Setting the Figure to Render */
g3d.renderFigure( figure, 0, 0, layout, effect );
/* Transfer to VRAM */
g3d.flush();
/* Releasing from the rendering target */
g3d.release( g );

Sample rendering result
Quick Links
- MIDP + com.mascotcapsule package (MascotCapsule V3 API)
- Basic rendering
- Rendering a 3D model / Semi-transparency using BlendHalf / Additive and subtractive blending effects / Switching the animation data / Switching the pattern using dynamic polygons / Lighting / Shading / Using the environment map / Rendering the primitives / Reducing the texture distortion / Preventing Z-sort rendering mistakes under certain circumstances / Coordinate systems and the camera / Projection methods
- Basic rendering
- Go back to Samples index

