Lab 1: Render a Game Character

Our goal is to render the following game character using the steps mentioned in this article:

image

Create a Game Object

In the engine, open the file Earth.mm.

In the init() method, scroll down to the line that says: CREATE ASTRONAUT HERE

Below this section, implement the code shown in the snippet.

/*----CREATE ASTRONAUT HERE-----*/
//Line 1. Create an instance of U4DGameObject type
myAstronaut=new U4DEngine::U4DGameObject();

In line 1, we provide a name for the game object, myAstronaut, and use the C++ keyword new to create an instance of U4DGameObject.

Load Attributes

The loadModel() method loads the attribute data into the character's data members (line 2). The method requires the name of the 3D model, as specified in Blender 3D. And the file where the attributes are found.

//Line 2. Load attribute (rendering information) into the game entity
// name of character: "astronaut"
// name of attributes file: "astronautAttributes.u4d"
if (myAstronaut->loadModel("astronaut","astronautAttributes.u4d")) {
//Line 3. Load rendering information into the GPU
myAstronaut->loadRenderingInformation();
}

Note that the loadModel() method returns a boolean value. The method will return false if the file was not found, or if the attributes are corrupted.

If the loadModel() method returns true, the next step is to send the attributes to the GPU. This is accomplished with the loadRenderingInformation() method (line 3).

Add to Entity Manager

Finally, the game object is added to the Entity Manager using the addChild() method (line 4).

//Line 4. Add astronaut to the scenegraph
addChild(myAstronaut);

Complete Code Snippet

The complete code to render a 3D Game Character should look as follows:

Code snippet: init() method
void Earth::init(){
/*----DO NOT REMOVE. THIS IS REQUIRED-----*/
//Configures the perspective view, shadows, lights and camera.
setupConfiguration();
//Renders the background models: skybox and island models. Remove if you want to.
setupBackgroundModels();
/*----CREATE ASTRONAUT HERE-----*/
//Line 1. Create an instance of U4DGameObject type
myAstronaut=new U4DEngine::U4DGameObject();
//Line 2. Load attribute (rendering information) into the game entity
if (myAstronaut->loadModel("astronaut","astronautAttributes.u4d")) {
//Line 3. Load rendering information into the GPU
myAstronaut->loadRenderingInformation();
//Line 4. Add astronaut to the scenegraph
addChild(myAstronaut);
}
}

Results

In Xcode, select either the iOS or Mac device and then click on Run.

The game engine should render the Astronaut:

image

Source Code

Download the source game files from our Github Tutorials page.