Guest User

Horde3D 101 App

a guest
Jul 7th, 2013
357
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <Horde3DUtils.h>
  3. #include "glfw.h"
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. H3DNode gSphereModel, gCam;                     // references to camera and sphere
  9. string gContentPath;                            // contains path to content folder where resources are loaded from
  10. int gWindowWidth = 800, gWindowHeight = 600;    // width and height of window
  11. bool gRunning;             
  12. float gCamRotation;
  13. bool gKeys[320];           
  14.  
  15. inline float degToRad( float f ) { return f * (3.1415926f / 180.0f); }
  16. void setKeyState( int key, bool state ) { gKeys[key] = state; }
  17.  
  18. // trims the app path and navigates to content folder
  19. string getContentPath( char *fullPath )
  20. {
  21.     string s( fullPath );
  22.     if( s.find( "/" ) != string::npos ) s = s.substr( 0, s.rfind( "/" ) );
  23.     else if( s.find( "\\" ) != string::npos ) s = s.substr( 0, s.rfind( "\\" ));
  24.     s.append("\\..\\Content");  // Change this when content is located on a different location! Path is always relative
  25.     return s;
  26. }
  27.  
  28. void InitHordeScene(int p_width, int p_height)
  29. {
  30.     // Initialize engine
  31.     h3dInit();
  32.  
  33.     h3dSetOption( H3DOptions::LoadTextures, 1 );        // enable automatic texture loading in referenced materials
  34.     h3dSetOption( H3DOptions::TexCompression, 0 );      // disable texture compression
  35.     h3dSetOption( H3DOptions::MaxAnisotropy, 4 );       // set max anisotropic filtering (between 0 and 4)
  36.     h3dSetOption( H3DOptions::ShadowMapSize, 2048 );    // Sets size of shadow map buffer (Values: 128, 256, 512, 1024, 2048; Default: 1024)
  37.  
  38.     // Add pipeline resource; defines the way the scene is rendered
  39.     H3DRes forwardPipeRes = h3dAddResource( H3DResTypes::Pipeline, "pipelines/forward.pipeline.xml", 0 );
  40.  
  41.     // Add model resource
  42.     H3DRes sphereRes = h3dAddResource( H3DResTypes::SceneGraph, "models/sphere/sphere.scene.xml", 0 );
  43.  
  44.     // Skybox resource
  45.     H3DRes skyBoxRes = h3dAddResource( H3DResTypes::SceneGraph, "models/skybox/skybox.scene.xml", 0 );
  46.  
  47.     // Load added resources            
  48.     if (!h3dutLoadResourcesFromDisk(gContentPath.c_str()))
  49.     {
  50.         printf("Failed loading resources! \n");
  51.     }
  52.  
  53.     // add sphere to scene
  54.     gSphereModel = h3dAddNodes( H3DRootNode, sphereRes );
  55.  
  56.     // Add skybox to scene
  57.     H3DNode sky = h3dAddNodes( H3DRootNode, skyBoxRes );
  58.     h3dSetNodeTransform( sky, 0, 0, 0, 0, 0, 0, 210, 50, 210 );
  59.     h3dSetNodeFlags( sky, H3DNodeFlags::NoCastShadow, true );
  60.  
  61.     // Add light source
  62.     H3DNode light = h3dAddLightNode( H3DRootNode, "Light1", 0, "LIGHTING", "SHADOWMAP" );
  63.  
  64.     // Set light position and radius
  65.     h3dSetNodeTransform( light, 0, 20, 0, 0, 0, 0, 1, 1, 1 );
  66.     h3dSetNodeParamF( light, H3DLight::RadiusF, 0, 50.0f );
  67.     h3dSetNodeParamF( light, H3DLight::ColorF3, 0.0f, 2.0f );
  68.  
  69.     // Add camera
  70.     gCam = h3dAddCameraNode( H3DRootNode, "Camera", forwardPipeRes );
  71.  
  72.     // Setup viewport and render target sizes
  73.     h3dSetNodeParamI( gCam, H3DCamera::ViewportXI, 0 );
  74.     h3dSetNodeParamI( gCam, H3DCamera::ViewportYI, 0 );
  75.     h3dSetNodeParamI( gCam, H3DCamera::ViewportWidthI, p_width );
  76.     h3dSetNodeParamI( gCam, H3DCamera::ViewportHeightI, p_height );
  77.     h3dSetupCameraView( gCam, 45.0f, (float)p_width / p_height, 0.5f, 2048.0f );
  78.     h3dResizePipelineBuffers( forwardPipeRes, p_width, p_height );
  79.  
  80.     // move camera so we see the sphere
  81.     h3dSetNodeTransform(gCam,
  82.                         0,      0,      3.0f,   // Translation
  83.                         0,      0,      0,      // Rotation
  84.                         1,      1,      1 );    // Scale
  85. }
  86.  
  87. void gameLoop( double deltaTime )
  88. {
  89.     static float t = 0;
  90.  
  91.     // Increase animation time
  92.     t = t + deltaTime;
  93.  
  94.     // rotate sphere
  95.     h3dSetNodeTransform(gSphereModel,
  96.                         0,      0,              0,      // Translation
  97.                         0,      t * 15,         0,      // Rotation
  98.                         1,      1,              1 );    // Scale
  99.  
  100.     // handle keys for camera rotation
  101.     if (gKeys[GLFW_KEY_LEFT]) gCamRotation -= 50.0f * deltaTime;
  102.     if (gKeys[GLFW_KEY_RIGHT]) gCamRotation += 50.0f * deltaTime;
  103.  
  104.     // update camera position (orbit around sphere)
  105.     float zPos = cos(degToRad(gCamRotation)) * 3.0f;
  106.     float xPos = sin(degToRad(gCamRotation)) * 3.0f;
  107.  
  108.     // set transform
  109.     h3dSetNodeTransform(gCam,
  110.         xPos,   0,                  zPos,   // Translation
  111.         0,      gCamRotation,       0,      // Rotation
  112.         1,      1,                  1 );    // Scale
  113.  
  114.     // Render scene
  115.     h3dRender( gCam );
  116.  
  117.     // Finish rendering of frame
  118.     h3dFinalizeFrame();
  119. }
  120.  
  121. int windowCloseListener()
  122. {
  123.     gRunning = false;
  124.     return 0;
  125. }
  126.  
  127. void keyPressListener( int key, int action )
  128. {
  129.     if( !gRunning ) return;
  130.  
  131.     if( action == GLFW_PRESS )
  132.     {
  133.         switch (key)
  134.         {
  135.             case GLFW_KEY_ESC:
  136.                 gRunning = false;
  137.                 break;
  138.         }
  139.     }
  140. }
  141.  
  142. bool setupWindow( int width, int height)
  143. {
  144.     if( !glfwOpenWindow( width, height, 8, 8, 8, 8, 24, 8, GLFW_WINDOW ) )
  145.     {
  146.         glfwTerminate();
  147.         return false;
  148.     }
  149.  
  150.     // Disable vertical synchronization
  151.     glfwSwapInterval( 0 );
  152.  
  153.     // Set listeners
  154.     glfwSetWindowCloseCallback( windowCloseListener );
  155.     glfwSetKeyCallback( keyPressListener );
  156.  
  157.     return true;
  158. }
  159.  
  160. int main(int argc, char** argv)
  161. {
  162.     // get path to executable, and move to the content folder from there
  163.     // note: this must be changed according to where your content folder is located at!
  164.     gContentPath =  getContentPath(argv[0]);
  165.  
  166.     // Initialize GLFW
  167.     glfwInit();
  168.     glfwEnable( GLFW_STICKY_KEYS );
  169.     if( !setupWindow( gWindowWidth, gWindowHeight) ) return -1;
  170.     glfwSetWindowTitle( "Horde 101" );
  171.  
  172.     // disable mouse cursor
  173.     glfwDisable( GLFW_MOUSE_CURSOR );
  174.  
  175.     // Initialize application and engine
  176.     InitHordeScene(gWindowWidth, gWindowHeight);
  177.  
  178.     // intialize variables used in Game loop
  179.     double lastTime = glfwGetTime();
  180.     double time = glfwGetTime();
  181.     double deltaTime = 0;
  182.     gRunning = true;
  183.  
  184.     // gameloop
  185.     while( gRunning )
  186.     {  
  187.         // Calc deltatime
  188.         time = glfwGetTime();
  189.         deltaTime = time - lastTime;
  190.         lastTime = time;
  191.  
  192.         // Update key states
  193.         for( int i = 0; i < 320; ++i ) setKeyState( i, glfwGetKey( i ) == GLFW_PRESS );
  194.  
  195.         // do gameloop
  196.         gameLoop(deltaTime);
  197.  
  198.         glfwSwapBuffers();
  199.     }
  200.  
  201.     // release mouse
  202.     glfwEnable( GLFW_MOUSE_CURSOR );
  203.  
  204.     // Releases the Horde3D Engine
  205.     h3dRelease();
  206.  
  207.     // close window & stop glfw
  208.     glfwTerminate();
  209. }
RAW Paste Data