Loading and rendering an exported 3D model

OK, so now we've got the model data exported, how do we go about loading it into our program and rendering it? It's actually surprisingly easy, as these next sections will show.

Adding the IwGraphics API to a project

Marmalade's 3D model rendering code is part of the IwGraphics API, so before we can draw anything we need to add this library to our project. This is done by adding iwgraphics to the subprojects section of the MKB file.

We then need to add a call to IwGraphicsInit at the start of our program, and IwGraphicsTerminate at the end. This API relies on both IwGx and IwResManager, so we must call the initialization functions for both of these APIs before calling the IwGraphics one.

Loading and accessing an exported 3D model

You've probably already guessed that this is almost trivially easy. The exporter generated a GROUP file, so all we have to do is load it into memory and then dig the model out of the resource group. Here's a block of code which does just that:

CIwResGroup* lpCubeGroup = IwGetResManager()->
LoadGroup("Cube/Cube.group");
CIwModel* lpCube = static_cast<CIwModel*>(lpCubeGroup->
GetResNamed("Cube", "CIwModel"));

Or alternatively you could do the following if you don't want to be bothered with retaining a pointer to the resource group instance:

CIwModel* lpCube = static_cast<CIwModel*>(IwGetResManager()->
GetResNamed("Cube", "CIwModel"));

That's it. The model is now loaded into memory and ready to render.

Rendering an exported 3D model

It is time to render the model on the screen and this too is incredibly easy. All we have to do is set our view and model matrices using IwGxSetViewMatrix and IwGxSetModelMatrix, then execute the following:

lpCube->Render();

The variable lpCube is the pointer to the CIwModel instance that we retrieved from the resource manager in the previous section.

In actual fact the Render method can take two optional parameters. The first parameter is a bool value that tells Marmalade to check a bounding sphere for the model against the clipping planes to see if it actually needs to be rendered. This parameter defaults to true, so the check is done by default. The bounding sphere is generated automatically for us by the model builder code.

The second parameter is a flags field. Aside from one flag that is supposed to have something to do with a 2D screen rotation (I say "supposed" because I can't say it did very much when I tried it), the other flags are only relevant when dealing with animated 3D models that contain normal data, so we will not worry over these for now.

Releasing 3D model data

Since our 3D model data has been loaded into memory using the resource group system, we can make use of the same mechanism of destroying groups to release model data from memory that we no longer need. As a recap, we just do the following if we have a pointer to the CIwResGroup containing the 3D data:

IwGetResManager()->DestroyGroup(lpCubeGroup);

Alternatively we can release a group from memory by destroying it by name, like this:

IwGetResManager()->DestroyGroup("Cube");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.138.104