Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Horde3DUtils.h>
- #include "glfw.h"
- #include <math.h>
- using namespace std;
- H3DNode gSphereModel, gCam; // references to camera and sphere
- string gContentPath; // contains path to content folder where resources are loaded from
- int gWindowWidth = 800, gWindowHeight = 600; // width and height of window
- bool gRunning;
- float gCamRotation;
- bool gKeys[320];
- inline float degToRad( float f ) { return f * (3.1415926f / 180.0f); }
- void setKeyState( int key, bool state ) { gKeys[key] = state; }
- // trims the app path and navigates to content folder
- string getContentPath( char *fullPath )
- {
- string s( fullPath );
- if( s.find( "/" ) != string::npos ) s = s.substr( 0, s.rfind( "/" ) );
- else if( s.find( "\\" ) != string::npos ) s = s.substr( 0, s.rfind( "\\" ));
- s.append("\\..\\Content"); // Change this when content is located on a different location! Path is always relative
- return s;
- }
- void InitHordeScene(int p_width, int p_height)
- {
- // Initialize engine
- h3dInit();
- h3dSetOption( H3DOptions::LoadTextures, 1 ); // enable automatic texture loading in referenced materials
- h3dSetOption( H3DOptions::TexCompression, 0 ); // disable texture compression
- h3dSetOption( H3DOptions::MaxAnisotropy, 4 ); // set max anisotropic filtering (between 0 and 4)
- h3dSetOption( H3DOptions::ShadowMapSize, 2048 ); // Sets size of shadow map buffer (Values: 128, 256, 512, 1024, 2048; Default: 1024)
- // Add pipeline resource; defines the way the scene is rendered
- H3DRes forwardPipeRes = h3dAddResource( H3DResTypes::Pipeline, "pipelines/forward.pipeline.xml", 0 );
- // Add model resource
- H3DRes sphereRes = h3dAddResource( H3DResTypes::SceneGraph, "models/sphere/sphere.scene.xml", 0 );
- // Skybox resource
- H3DRes skyBoxRes = h3dAddResource( H3DResTypes::SceneGraph, "models/skybox/skybox.scene.xml", 0 );
- // Load added resources
- if (!h3dutLoadResourcesFromDisk(gContentPath.c_str()))
- {
- printf("Failed loading resources! \n");
- }
- // add sphere to scene
- gSphereModel = h3dAddNodes( H3DRootNode, sphereRes );
- // Add skybox to scene
- H3DNode sky = h3dAddNodes( H3DRootNode, skyBoxRes );
- h3dSetNodeTransform( sky, 0, 0, 0, 0, 0, 0, 210, 50, 210 );
- h3dSetNodeFlags( sky, H3DNodeFlags::NoCastShadow, true );
- // 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 );
- h3dSetNodeParamF( light, H3DLight::ColorF3, 0.0f, 2.0f );
- // Add camera
- gCam = h3dAddCameraNode( H3DRootNode, "Camera", forwardPipeRes );
- // Setup viewport and render target sizes
- h3dSetNodeParamI( gCam, H3DCamera::ViewportXI, 0 );
- h3dSetNodeParamI( gCam, H3DCamera::ViewportYI, 0 );
- h3dSetNodeParamI( gCam, H3DCamera::ViewportWidthI, p_width );
- h3dSetNodeParamI( gCam, H3DCamera::ViewportHeightI, p_height );
- h3dSetupCameraView( gCam, 45.0f, (float)p_width / p_height, 0.5f, 2048.0f );
- h3dResizePipelineBuffers( forwardPipeRes, p_width, p_height );
- // move camera so we see the sphere
- h3dSetNodeTransform(gCam,
- 0, 0, 3.0f, // Translation
- 0, 0, 0, // Rotation
- 1, 1, 1 ); // Scale
- }
- void gameLoop( double deltaTime )
- {
- static float t = 0;
- // Increase animation time
- t = t + deltaTime;
- // rotate sphere
- h3dSetNodeTransform(gSphereModel,
- 0, 0, 0, // Translation
- 0, t * 15, 0, // Rotation
- 1, 1, 1 ); // Scale
- // handle keys for camera rotation
- if (gKeys[GLFW_KEY_LEFT]) gCamRotation -= 50.0f * deltaTime;
- if (gKeys[GLFW_KEY_RIGHT]) gCamRotation += 50.0f * deltaTime;
- // update camera position (orbit around sphere)
- float zPos = cos(degToRad(gCamRotation)) * 3.0f;
- float xPos = sin(degToRad(gCamRotation)) * 3.0f;
- // set transform
- h3dSetNodeTransform(gCam,
- xPos, 0, zPos, // Translation
- 0, gCamRotation, 0, // Rotation
- 1, 1, 1 ); // Scale
- // Render scene
- h3dRender( gCam );
- // Finish rendering of frame
- h3dFinalizeFrame();
- }
- int windowCloseListener()
- {
- gRunning = false;
- return 0;
- }
- void keyPressListener( int key, int action )
- {
- if( !gRunning ) return;
- if( action == GLFW_PRESS )
- {
- switch (key)
- {
- case GLFW_KEY_ESC:
- gRunning = false;
- break;
- }
- }
- }
- bool setupWindow( int width, int height)
- {
- if( !glfwOpenWindow( width, height, 8, 8, 8, 8, 24, 8, GLFW_WINDOW ) )
- {
- glfwTerminate();
- return false;
- }
- // Disable vertical synchronization
- glfwSwapInterval( 0 );
- // Set listeners
- glfwSetWindowCloseCallback( windowCloseListener );
- glfwSetKeyCallback( keyPressListener );
- return true;
- }
- int main(int argc, char** argv)
- {
- // get path to executable, and move to the content folder from there
- // note: this must be changed according to where your content folder is located at!
- gContentPath = getContentPath(argv[0]);
- // Initialize GLFW
- glfwInit();
- glfwEnable( GLFW_STICKY_KEYS );
- if( !setupWindow( gWindowWidth, gWindowHeight) ) return -1;
- glfwSetWindowTitle( "Horde 101" );
- // disable mouse cursor
- glfwDisable( GLFW_MOUSE_CURSOR );
- // Initialize application and engine
- InitHordeScene(gWindowWidth, gWindowHeight);
- // intialize variables used in Game loop
- double lastTime = glfwGetTime();
- double time = glfwGetTime();
- double deltaTime = 0;
- gRunning = true;
- // gameloop
- while( gRunning )
- {
- // Calc deltatime
- time = glfwGetTime();
- deltaTime = time - lastTime;
- lastTime = time;
- // Update key states
- for( int i = 0; i < 320; ++i ) setKeyState( i, glfwGetKey( i ) == GLFW_PRESS );
- // do gameloop
- gameLoop(deltaTime);
- glfwSwapBuffers();
- }
- // release mouse
- glfwEnable( GLFW_MOUSE_CURSOR );
- // Releases the Horde3D Engine
- h3dRelease();
- // close window & stop glfw
- glfwTerminate();
- }
Advertisement
Add Comment
Please, Sign In to add comment