Samples
Rendering a 3D model
Let's take a look at how to render a basic 3D model.
Table of contents
- brewmain.c: This file contains the processes specific to BREW.
- sample3d_v3.c: This file contains the MascotCapsule processes.
1. Model components
A 3D model consists of the following three 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 a texture to be applied to a model, or 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 MascotCapsule V3 interface
Create the MascotCapsule V3 interface using ISHELL_CreateInstance(), so that each method can be called.
The input stream and allocator to be used are initialized here.
pMe -> pIM3D = NULL;
ISHELL_CreateInstance( pMe -> applet.m_pIShell, AEECLSID_IM3D_D31, ( void ** ) &pMe -> pIM3D );
/* Initializing the allocator */
pMe -> allocatorVtbl.alloc = _alloc;
pMe -> allocatorVtbl.free = _free;
pMe -> allocatorVtbl.realloc = _realloc;
/* Initializing the FileIstream */
pMe -> istreamVtbl.readSint8 = _readSint8;
pMe -> istreamVtbl.readSint16 = _readSint16;
pMe -> istreamVtbl.readSint32 = _readSint32;
pMe -> istreamVtbl.readBytes = _readBytes;
pMe -> istreamVtbl.skipBytes = _skipBytes;
pMe -> istreamVtbl.fail = _fail;
2.2. Initializing the Figure, Texture, and ActionTable objects
Initialize the data objects ( Figure, Texture, and ActionTable ) to be used. Associate each object with the allocator as needed.
[ In sample3d_v3.c ]IMICRO3D_Figure_initialize( pMe -> pIM3D, &pMe -> mc_data.figure, pMe -> pAlc );
IMICRO3D_Texture_initialize( pMe -> pIM3D, &pMe -> mc_data.texture [ 0 ] , pMe -> pAlc );
IMICRO3D_Texture_initialize( pMe -> pIM3D, &pMe -> mc_data.texture [ 1 ] , pMe -> pAlc );
IMICRO3D_ActTable_initialize( pMe -> pIM3D, &pMe -> mc_data.act, pMe -> pAlc );
2.3. Loading the data
Load the model data, texture data, and action data in the ( Figure, Texture, and ActionTable ) objects to be used.
Example: Loading the Figure data
STRCPY( pMe -> mc_data.mbactraFile [ 0 ] , "\\data\\v3_sample.mbac" );
/* Initializing the FileIstream object */
FileIstream_initialize( &fis, pMe -> m_pIFileMgr, pMe -> mc_data.mbactraFile [ 0 ] );
/* Loading the model data */
IMICRO3D_Figure_loadMbacData( pMe -> pIM3D, &pMe -> mc_data.figure, FileIstream_getIIstream( &fis ) );
/* Finalization of the FileIstream object */
IFILE_Release( &fis );
2.4. Initializing the camera
Initialize the camera. To do so, specify the position, look direction, up vector, near clipping plane, far clipping plane, and angle of view; and create a transformation matrix to camera coordinates.
[ In sample3d_v3.c, sample3d_v3.h ]#define CAM_NEAR 2048
#define CAM_FAR 32000
#define CAM_ANGLE 228
/* Viewpoint setting */
/* Position */
position.x = 0;
position.y = 1024;
position.z = 12288;
/* look direction */
look.x = 0;
look.y &n = 1365;
look.z = -4096;
/* up vector */
up.x = 0;
up.y = 4096;
up.z = 0;
/* Obtaining the view transformation matrix */
IMICRO3D_Atrans3i_setViewTrans( pMe -> mpMicro3D,
& position,
& look,
& up );
2.5. Specifying the rendering environment
Specify the environment for rendering. Specifically, it contains the following procedures:
- Specifying the setting for the frame buffer as the rendering target
- Specifying the setting for the screen
- Specifying the setting for the projection method
These settings will be retained in Render unless they are changed. When using the same setting, you only have to specify each setting once at the beginning.
Two projection methods, perspective projection and parallel projection are available. In order to specify perspective projection setting, use the following methods: 、
- IMICRO3D_Render_setPerspectiveFov()
- IMICRO3D_Render_setPerspectiveWH(), or IMICOR3D_Render_setPerspectiveW()
In order to specify parallel projection setting, use the following methods:
- IMICRO3D_Render_setOrthographicScale()
- IMICRO3D_Render_setOrthographicWH(), or IMICRO3D_Render_setOrthographicW()
[In sample3d_v3.c]
IMICRO3D_Render_setVram( pMe -> pIM3D,
GetBitmapWidth( pMe ),
GetBitmapHeight( pMe ),
GetBitmapPitch( pMe ),
pMe -> pimage );
/* Perspective projection setting */
IMICRO3D_Render_setPerspectiveFov( pMe -> pIM3D,
  CAM_NEAR,
  CAM_FAR,
  CAM_ANGLE );
/* Screen center setting */
IMICRO3D_Render_setScreenCenter( pMe -> pIM3D,
( GetBitmapWidth( pMe ) >> 1 ),
( GetBitmapHeight( pMe ) >> 1) );
2.6. Specifying the model action
When an animation is set for the model, specify the model posture setting for the target rendering time. In order to set the model posture, use the IMICRO3D_Figure_setPosture() method and specify the frame to be rendered. Use IMICRO3D_ActTable_getNumFrames() to obtain the maximum number of frames for the action data.
[ In sample3d_v3.c ]maxfrm = IMICRO3D_ActTable_getNumFrames( pMe -> pIM3D, &pMe -> mc_data.act, pMe -> mc_data.act_idx );
/* Animation loop setting */
if ( maxfrm != 0 )
{
{
pMe -> mc_data.act_frame -= maxfrm;
}
else
{
/* Specifying the model posture */
IMICRO3D_Figure_setPosture( pMe -> pIM3D,
&pMe -> mc_data.act,
pMe -> mc_data.act_idx,
pMe -> mc_data.act_frame );
/* Specifying the speed */
pMe -> mc_data.act_frame += 32768;
2.7. Rendering the model
Render the obtained objects, and transfer each of them to VRAM. In order to render, use the IMICRO3D_Render_drawFigure() method. 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 IMICRO3D_Render_flush() method to execute the actual rendering process of the objects that have been registered.
[ In sample3d_v3.c ]Util_fillRect( pMe, BG_COLOR, 0, 0, GetBitmapWidth( pMe ), GetBitmapHeight( pMe ) );
/* Rendering registration */
if( HI_TRUE != IMICRO3D_Render_drawFigure( pMe -> pIM3D, & pMe -> mc_data.render, & pMe -> mc_data.figure ) )
{
return HI_FALSE;
/* Rendering to the frame buffer */
IMICRO3D_Render_flush( pMe -> mpMicro3D, &pMe -> mc_data.render );

Sample rendering result
2.8. Finalization
You can release all the objects when exiting the application, etc. To do so, use the following procedures to call finalization processes, and release the resources from all the objects.
- Finalization of Render
- Finalization of all the ActTables
- Finalization of all the Textures, Figures, and Atrans3is
- Calling IMICRO3D_mcext_End
- Calling IMICRO3D_Release
[ In sample3d_v3.c ]
IMICRO3D_Figure_finalize( pMe -> pIM3D, &pMe -> mc_data.figure );
IMICRO3D_Render_finalize( pMe -> pIM3D, &pMe -> mc_data.render );
IMICRO3D_ActTable_finalize( pMe -> pIM3D, &pMe -> mc_data.act );
IMICRO3D_Texture_finalize( pMe -> pIM3D, &pMe -> mc_data.texture [ 0 ] );
IMICRO3D_Texture_finalize( pMe -> pIM3D, &pMe -> mc_data.texture [ 1 ] );
{
IMICRO3D_Release( pMe -> pIM3D );
pMe -> pIM3D = NULL;


