Samples

Rendering a 3D model

Let's take a look at how to render a basic 3D model.

Rendering a 3D model

Table of contents





In the sample program, please note the following two files that handle main processes:
  • brewmain.c:       This file contains the processes specific to BREW.
  • sample3d_v3.c:  This file contains the MascotCapsule processes.
Please note that the relative filename is indicated in the brackets [] in the sample codes of this document.

1. Model components

A 3D model consists of the following three components:





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.

  [ In brewmain.c ]
/* Creating the interface */
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 ]
/* Initializing the 3D-related object */
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

  [ In sample3d_v3.c ]
/* Specifying the data path */
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 ]
/* Specifying the near clipping plane, far clipping plane, and angle of view */
#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,
        &pMe -> mc_data.view_trans,
        & position,
        & look,
        & up );

2.5. Specifying the rendering environment

Specify the environment for rendering. Specifically, it contains the following procedures:

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: 、

In order to specify parallel projection setting, use the following methods:

  [In sample3d_v3.c]
/* Frame buffer setting */
IMICRO3D_Render_setVram( pMe -> pIM3D,
 pMe -> mc_data.render,
 GetBitmapWidth( pMe ),
 GetBitmapHeight( pMe ),
 GetBitmapPitch( pMe ),
pMe -> pimage );

/* Perspective projection setting */
IMICRO3D_Render_setPerspectiveFov( pMe  -> pIM3D,
  &pMe -> mc_data.render,
  CAM_NEAR,
  CAM_FAR,
  CAM_ANGLE );

/* Screen center setting */
IMICRO3D_Render_setScreenCenter( pMe -> pIM3D,
&pMe -> mc_data.render,
( 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 ]
/* Obtaining the number of frames */
maxfrm = IMICRO3D_ActTable_getNumFrames( pMe -> pIM3D, &pMe -> mc_data.act, pMe -> mc_data.act_idx );

/* Animation loop setting */
if ( maxfrm != 0 )
{
if ( pMe -> mc_data.act_frame > maxfrm )
{
     pMe -> mc_data.act_frame -= maxfrm;
}
}
else
{
     pMe -> mc_data.act_frame = 0;
}

/* Specifying the model posture */
IMICRO3D_Figure_setPosture( pMe  -> pIM3D,
   &pMe -> mc_data.figure,
   &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 ]
/* Clearing the background */
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 ) )
{
DBGPRINTF(" [ ERR ] draw failed");
return HI_FALSE;
}

/* Rendering to the frame buffer */
IMICRO3D_Render_flush( pMe -> mpMicro3D, &pMe -> mc_data.render );
Sample rendering result
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.

  1. Finalization of Render
  2. Finalization of all the ActTables
  3. Finalization of all the Textures, Figures, and Atrans3is
  4. Calling IMICRO3D_mcext_End
  5. Calling IMICRO3D_Release

  [ In sample3d_v3.c ]
/* Finalization of Render, ActTable, Texture, Figure */
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 ] );
  [In brewmain.c]
if ( pMe -> pIM3D != NULL )
{
/* Finalization of Micro3D */
IMICRO3D_Release( pMe -> pIM3D );
pMe -> pIM3D = NULL;
}


Page TopBack