Difference between revisions of "Tutorial - Hello World"

From Horde3D Wiki
Jump to: navigation, search
(Updated the references to function names in the tutorial text.)
 
(3 intermediate revisions by 2 users not shown)
Line 9: Line 9:
 
code=
 
code=
 
<source lang="cpp" line="1">
 
<source lang="cpp" line="1">
NodeHandle model = 0, cam = 0;
+
#include <Horde3D.h>
 +
#include <Horde3DUtils.h>
 +
 
 +
H3DNode model = 0, cam = 0;
  
 
void initGame( int winWidth, int winHeight )
 
void initGame( int winWidth, int winHeight )
 
{
 
{
 
     // Initialize engine
 
     // Initialize engine
     Horde3D::init();
+
     h3dInit();
     Horde3D::resize( 0, 0, winWidth, winHeight );
+
     h3dSetupViewport( 0, 0, winWidth, winHeight, true );
 
      
 
      
 
     // Add pipeline resource
 
     // Add pipeline resource
     ResHandle pipeRes = Horde3D::addResource( ResourceTypes::Pipeline, "standard.pipeline.xml", 0 );
+
     H3DRes pipeRes = h3dAddResource( H3DResTypes::Pipeline, "standard.pipeline.xml", 0 );
 
     // Add model resource
 
     // Add model resource
     ResHandle modelRes = Horde3D::addResource( ResourceTypes::SceneGraph, "character.scene.xml", 0 );
+
     H3DRes modelRes = h3dAddResource( H3DResTypes::SceneGraph, "character.scene.xml", 0 );
 
     // Add animation resource
 
     // Add animation resource
     ResHandle animRes = Horde3D::addResource( ResourceTypes::Animation, "walk.anim.xml", 0 );
+
     H3DRes animRes = h3dAddResource( H3DResTypes::Animation, "walk.anim.xml", 0 );
 
     // Load added resources
 
     // Load added resources
     Horde3DUtils::loadResourcesFromDisk( "" );
+
     h3dutLoadResourcesFromDisk( "" );
 
      
 
      
 
     // Add model to scene
 
     // Add model to scene
     model = Horde3D::addNodes( RootNode, modelRes );
+
     model = h3dAddNodes( H3DRootNode, modelRes );
 
     // Apply animation
 
     // Apply animation
     Horde3D::setupModelAnimStage( model, 0, animRes, "" );
+
     h3dSetupModelAnimStage( model, 0, animRes, 0, "", false );
 
      
 
      
 
     // Add light source
 
     // Add light source
     NodeHandle light = Horde3D::addLightNode( RootNode, "Light1", 0, "LIGHTING", "SHADOWMAP" );
+
     H3DNode light = h3dAddLightNode( H3DRootNode, "Light1", 0, "LIGHTING", "SHADOWMAP" );
 
     // Set light position and radius
 
     // Set light position and radius
     Horde3D::setNodeTransform( light, 0, 20, 0, 0, 0, 0, 1, 1, 1 );
+
     h3dSetNodeTransform( light, 0, 20, 0, 0, 0, 0, 1, 1, 1 );
     Horde3D::setLightParam( light, LightNodeParams::Radius, 50.0f );
+
     h3dSetNodeParamF( light, H3DLight::RadiusF, 0, 50.0f );
 
 
 
     // Add camera
 
     // Add camera
     NodeHandle cam = Horde3D::addCameraNode( RootNode, "Camera", pipeRes );
+
     H3DNode cam = h3dAddCameraNode( H3DRootNode, "Camera", pipeRes );
 
}
 
}
 
</source>
 
</source>
 
}}
 
}}
  
The first line of the code above declares two global handles to Horde scene graph nodes. All objects in Horde are accessible via handles, a concept similar to pointers. The first thing we need to do in our initGame function to use Horde3D is initializing the engine. This happens with the function init. After that we tell the engine the size of our rendering window so that it can adjust the viewport.
+
The first line of the code above declares two global handles to Horde scene graph nodes. All objects in Horde are accessible via handles, a concept similar to pointers. The first thing we need to do in our initGame function to use Horde3D is initializing the engine. This happens with the function h3dInit. After that we tell the engine the size of our rendering window so that it can adjust the viewport.
  
The next step is to load the required resources. In Horde resources are data files that are loaded once and can be referenced by several objects for rendering. The function addResource takes the resource type we want to add and the name of the resource (usually the filename) as parameters and returns a handle to the created resource object. In our case we want a model which is represented as a scene graph file in Horde and additionally an animation. Now the resources are created but we still need to load them. Horde supports loading from any sources including encrypted archives or a network but in this case we just want to load our resources from the local hard disk which is done with the utility function loadResourcesFromDisk. Besides our model and animation we also load a pipeline resource. A pipeline defines how the the scene is rendered and can be used to realize post-processing effects or high dynamic range rendering. For the beginning you can just use the files that come with the SDK samples.
+
The next step is to load the required resources. In Horde resources are data files that are loaded once and can be referenced by several objects for rendering. The function h3dAddResource takes the resource type we want to add and the name of the resource (usually the filename) as parameters and returns a handle to the created resource object. In our case we want a model which is represented as a scene graph file in Horde and additionally an animation. Now the resources are created but we still need to load them. Horde supports loading from any sources including encrypted archives or a network but in this case we just want to load our resources from the local hard disk which is done with the utility function h3dutLoadResourcesFromDisk. Besides our model and animation we also load a pipeline resource. A pipeline defines how the the scene is rendered and can be used to realize post-processing effects or high dynamic range rendering. For the beginning you can just use the files that come with the SDK samples.
  
After we have loaded the required resources we can finally build up the scene graph. The scene graph represents the objects in our virtual world in a hierarchical tree structure. First we add the model that we have loaded before. We use the function addNodes for doing this which takes a scene graph resource and a parent node. The parent is the scene object to which the new node is attached, in our case just the root node which is the base of the virtual world. Similar to addResource this function also returns a handle to the created scene graph subtree. After that we assign the loaded animation to our model node with the function setupModelAnimStage. Horde allows you to apply several different animations to a model and makes it possible to blend and mix them but for the beginning one should be enough. Now that adding the model is finished we still need a light source. It would be possible to load another scene graph file which contains the light source but we want to add it manually by using the addLightNode function. This function requires several parameters specifying the shaders used for rendering. More information on this can be found in other sections of the manual. The next step is to set the position and orientation which is done with setNodeTransform. After that we specify the light radius which defines the zone of influence using setLightParam. Finally we still need a camera which represents the viewer. It is added with the function addCameraNode and takes our loaded pipeline resource as parameter.
+
After we have loaded the required resources we can finally build up the scene graph. The scene graph represents the objects in our virtual world in a hierarchical tree structure. First we add the model that we have loaded before. We use the function h3dAddNodes for doing this which takes a scene graph resource and a parent node. The parent is the scene object to which the new node is attached, in our case just the root node which is the base of the virtual world. Similar to h3dAddResource this function also returns a handle to the created scene graph subtree. After that we assign the loaded animation to our model node with the function h3dSetupModelAnimStage. Horde allows you to apply several different animations to a model and makes it possible to blend and mix them but for the beginning one should be enough. Now that adding the model is finished we still need a light source. It would be possible to load another scene graph file which contains the light source but we want to add it manually by using the h3dAddLightNode function. This function requires several parameters specifying the shaders used for rendering. More information on this can be found in other sections of the manual. The next step is to set the position and orientation which is done with h3dSetNodeTransform. After that we specify the light radius which defines the zone of influence using h3dSetNodeParamF. Finally we still need a camera which represents the viewer. It is added with the function h3dAddCameraNode and takes our loaded pipeline resource as parameter.
  
 
{{CppSourceCode|
 
{{CppSourceCode|
Line 61: Line 64:
 
      
 
      
 
     // Play animation
 
     // Play animation
     Horde3D::setModelAnimParams( model, 0, t, 1.0f );
+
     h3dSetModelAnimParams( model, 0, t, 1.0f );
 
      
 
      
 
     // Set new model position
 
     // Set new model position
     Horde3D::setNodeTransform( model, t * 10, 0, 0,     // Translation
+
     h3dSetNodeTransform( model, t * 10, 0, 0, // Translation
                                          0, 0, 0,     // Rotation
+
                        0, 0, 0,             // Rotation
                                          1, 1, 1 );   // Scale
+
                        1, 1, 1 );           // Scale
 
   
 
   
     // Render scene  
+
     // Render scene
     Horde3D::render( cam );
+
     h3dRender( cam );
 +
   
 +
    // Finish rendering of frame
 +
    h3dFinalizeFrame();
 
}
 
}
 +
  
 
void releaseGame()
 
void releaseGame()
 
{
 
{
 
     // Release engine
 
     // Release engine
     Horde3D::release();
+
     h3dRelease();
 
}
 
}
 
</source>
 
</source>
 
}}
 
}}
  
The next function in our sample is the game loop which is called once every frame. Here we will animate our character model. For doing this we define a time counter which is increased in every frame. To make the animation speed independent from the framerate, we scale the time step by the inverse of the current frames per second value. After that we tell the engine to update the model animation using the setModelAnimParams function. We could also specify a blend weight for combining animations here but since we have only one animation we don't need that. Now we can displace our character a bit so that it moves through the scene. This is achieved by updating the model node transformation. Finally we need to tell the engine to render the scene and recalculate the model animation. This happens with the function render that expects the camera from which the scene is viewed.
+
The next function in our sample is the game loop which is called once every frame. Here we will animate our character model. For doing this we define a time counter which is increased in every frame. To make the animation speed independent from the framerate, we scale the time step by the inverse of the current frames per second value. After that we tell the engine to update the model animation using the h3dSetModelAnimParams function. We could also specify a blend weight for combining animations here but since we have only one animation we don't need that. Now we can displace our character a bit so that it moves through the scene. This is achieved by updating the model node transformation. Finally we need to tell the engine to render the scene and recalculate the model animation. This happens with the function h3dRender that expects the camera from which the scene is viewed.
  
The last function releaseGame is called when the application is closed. All we have to do here is freeing the engine with the release function.
+
The last function releaseGame is called when the application is closed. All we have to do here is freeing the engine with the h3dRelease function.
  
 
That's it so far with the basic tutorial. You can have a look at the Usage Guide now to learn more details.
 
That's it so far with the basic tutorial. You can have a look at the Usage Guide now to learn more details.

Latest revision as of 15:14, 28 June 2010

In this section we will create a simple application that loads a character and animates it using a walk cycle. You will see how straightforward it is to use the Horde3D API.

As a first step you have to create an application that opens a window which can be used for rendering. This is a pretty standard task and if you are a novice and have problems with that on your platform use Google to find one of the numerous good tutorials on the net or have a look at the samples that come with the Horde3D SDK. The next step is to initialize an OpenGL context for our rendering window. You can do this manually or use the Horde3D Utility Library to make your life easier. After that is done we can finally get to the engine related part.

Simple Setup of the Horde3D renderer.
#include <Horde3D.h>
#include <Horde3DUtils.h>

H3DNode model = 0, cam = 0;

void initGame( int winWidth, int winHeight )
{
    // Initialize engine
    h3dInit();
    h3dSetupViewport( 0, 0, winWidth, winHeight, true );
    
    // Add pipeline resource
    H3DRes pipeRes = h3dAddResource( H3DResTypes::Pipeline, "standard.pipeline.xml", 0 );
    // Add model resource
    H3DRes modelRes = h3dAddResource( H3DResTypes::SceneGraph, "character.scene.xml", 0 );
    // Add animation resource
    H3DRes animRes = h3dAddResource( H3DResTypes::Animation, "walk.anim.xml", 0 );
    // Load added resources
    h3dutLoadResourcesFromDisk( "" );
    
    // Add model to scene
    model = h3dAddNodes( H3DRootNode, modelRes );
    // Apply animation
    h3dSetupModelAnimStage( model, 0, animRes, 0, "", false );
    
    // Add light source
    H3DNode light = h3dAddLightNode( H3DRootNode, "Light1", 0, "LIGHTING", "SHADOWMAP" );
    // Set light position and radius
    h3dSetNodeTransform( light, 0, 20, 0, 0, 0, 0, 1, 1, 1 );
    h3dSetNodeParamF( light, H3DLight::RadiusF, 0, 50.0f );
	
    // Add camera
    H3DNode cam = h3dAddCameraNode( H3DRootNode, "Camera", pipeRes );
}

The first line of the code above declares two global handles to Horde scene graph nodes. All objects in Horde are accessible via handles, a concept similar to pointers. The first thing we need to do in our initGame function to use Horde3D is initializing the engine. This happens with the function h3dInit. After that we tell the engine the size of our rendering window so that it can adjust the viewport.

The next step is to load the required resources. In Horde resources are data files that are loaded once and can be referenced by several objects for rendering. The function h3dAddResource takes the resource type we want to add and the name of the resource (usually the filename) as parameters and returns a handle to the created resource object. In our case we want a model which is represented as a scene graph file in Horde and additionally an animation. Now the resources are created but we still need to load them. Horde supports loading from any sources including encrypted archives or a network but in this case we just want to load our resources from the local hard disk which is done with the utility function h3dutLoadResourcesFromDisk. Besides our model and animation we also load a pipeline resource. A pipeline defines how the the scene is rendered and can be used to realize post-processing effects or high dynamic range rendering. For the beginning you can just use the files that come with the SDK samples.

After we have loaded the required resources we can finally build up the scene graph. The scene graph represents the objects in our virtual world in a hierarchical tree structure. First we add the model that we have loaded before. We use the function h3dAddNodes for doing this which takes a scene graph resource and a parent node. The parent is the scene object to which the new node is attached, in our case just the root node which is the base of the virtual world. Similar to h3dAddResource this function also returns a handle to the created scene graph subtree. After that we assign the loaded animation to our model node with the function h3dSetupModelAnimStage. Horde allows you to apply several different animations to a model and makes it possible to blend and mix them but for the beginning one should be enough. Now that adding the model is finished we still need a light source. It would be possible to load another scene graph file which contains the light source but we want to add it manually by using the h3dAddLightNode function. This function requires several parameters specifying the shaders used for rendering. More information on this can be found in other sections of the manual. The next step is to set the position and orientation which is done with h3dSetNodeTransform. After that we specify the light radius which defines the zone of influence using h3dSetNodeParamF. Finally we still need a camera which represents the viewer. It is added with the function h3dAddCameraNode and takes our loaded pipeline resource as parameter.

Game Loop and Destructor
void gameLoop( float fps )
{
    static float t = 0;
    
    // Increase animation time
    t = t + 10.0f * (1 / fps);
    
    // Play animation
    h3dSetModelAnimParams( model, 0, t, 1.0f );
    
    // Set new model position
    h3dSetNodeTransform( model, t * 10, 0, 0,  // Translation
                         0, 0, 0,              // Rotation
                         1, 1, 1 );            // Scale
										   
    // Render scene
    h3dRender( cam );
    
    // Finish rendering of frame
    h3dFinalizeFrame();
}


void releaseGame()
{
    // Release engine
    h3dRelease();
}

The next function in our sample is the game loop which is called once every frame. Here we will animate our character model. For doing this we define a time counter which is increased in every frame. To make the animation speed independent from the framerate, we scale the time step by the inverse of the current frames per second value. After that we tell the engine to update the model animation using the h3dSetModelAnimParams function. We could also specify a blend weight for combining animations here but since we have only one animation we don't need that. Now we can displace our character a bit so that it moves through the scene. This is achieved by updating the model node transformation. Finally we need to tell the engine to render the scene and recalculate the model animation. This happens with the function h3dRender that expects the camera from which the scene is viewed.

The last function releaseGame is called when the application is closed. All we have to do here is freeing the engine with the h3dRelease function.

That's it so far with the basic tutorial. You can have a look at the Usage Guide now to learn more details.

Hello World Tutorial
H3DPlaceHolder.png
This tutorial introduces the basic steps necessary to setup horde3d
Version: 1.0
Compatible with Horde3D: 1.0 beta
Release date: 2008-03-30
Author(s): Nicolas Schulz