How to render a 3D Model
Steps to render a 3D character
The steps to render a 3D object is summarized below:
- Load scene data
- Create a Game Object
- Load the attributes
- Add the game object to the scenegraph
Step 1. Load Scene Data
Before you can render a character, you must load the character's attributes. The loading is done through the use of U4DEngine::
Note: Please make sure to read the Untold Engine Toolchain before continuing on with this tutorial.
//Line 1. The U4DResourceLoader is in charge of loading the binary file containing the scene data U4DEngine::U4DResourceLoader *resourceLoader=U4DEngine::U4DResourceLoader::sharedInstance(); //Line 1a.Load binary file with scene data resourceLoader->loadSceneData("spaceScene.u4d"); //Line 1b. Load binary file with texture data resourceLoader->loadTextureData("spaceTextures.u4d");
Step 2. Create a Game Object
In line 2, we provide a name for the game object, myAstronaut, and use the C++ keyword new to create an instance of U4DEngine::
//Line 2. Create an instance of U4DGameObject type U4DEngine::U4DGameObject *myAstronaut=new U4DEngine::U4DGameObject();
Step 3. Load Attributes
The U4DEngine::
//Line 3. Load attribute (rendering information) into the game entity // name of character in blender: "astronaut" if (myAstronaut->loadModel("astronaut")) { //Line 4. 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 U4DEngine::U4DRenderManager::loadRenderingInformation() method (line 4).
Step 4. Add the game object to the scenegraph
Finally, the game object is added to the Entity Manager using the U4DEngine::
//Line 5. Add astronaut to the scenegraph addChild(myAstronaut);
Complete Code Snippet
The complete code to render a 3D Game Character should look as follows:
//Line 1. The U4DResourceLoader is in charge of loading the binary file containing the scene data U4DEngine::U4DResourceLoader *resourceLoader=U4DEngine::U4DResourceLoader::sharedInstance(); //Line 1a.Load binary file with scene data resourceLoader->loadSceneData("spaceScene.u4d"); //Line 1b. Load binary file with texture data resourceLoader->loadTextureData("spaceTextures.u4d"); //Line 2. Create an instance of U4DGameObject type U4DEngine::U4DGameObject *myAstronaut=new U4DEngine::U4DGameObject(); //Line 3. Load attribute (rendering information) into the game entity if (myAstronaut->loadModel("astronaut")) { //Line 4. Load rendering information into the GPU myAstronaut->loadRenderingInformation(); //Line 5. Add astronaut to the scenegraph addChild(myAstronaut); }