📄 main.cpp
字号:
//##################################################//# $Id: Main.cpp 36 2005-11-06 04:58:56Z peciva $/*****************************************************************************\ * * Main.cpp * * Test application for Infinite Terrain Engine * * Authors: Martin Havl龛ek (xhavli15 AT stud.fit.vutbr.cz) * PCJohn (peciva AT fit.vutbr.cz) * Contributors: * * ---------------------------------------------------------------------------- * * THIS SOFTWARE IS NOT COPYRIGHTED * * This source code is offered for use in the public domain. * You may use, modify or distribute it freely. * * This source code is distributed in the hope that it will be useful but * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY * DISCLAIMED. This includes but is not limited to warranties of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * If you find the source code useful, authors will kindly welcome * if you give them credit and keep their names with their source code. *\*****************************************************************************/#ifdef _WIN32# include <Inventor/Win/SoWin.h># pragma comment(linker, "/entry:\"mainCRTStartup\"")# pragma comment(linker, "/subsystem:\"console\"")#else# include <Inventor/Qt/SoQt.h>#endif#include <Inventor/nodes/SoSeparator.h>#include <Inventor/nodes/SoDirectionalLight.h>#include <Inventor/nodes/SoPerspectiveCamera.h>#include <Inventor/nodes/SoEventCallback.h>#include <Inventor/nodes/SoFile.h>#include <Inventor/events/SoEvent.h>#include <Inventor/sensors/SoOneShotSensor.h>#include <cstdio> // printf#include <cstdlib> // putenv, exit#include <cmath> // floor#include <ctime> // time#include "TeEngine.h"#include "objects/TeEngineExplorer.h"#include "misc/SoGuiGameViewer.h"#include "misc/SoPerfGraph.h"#define APP_TITLE "TerrainEngine"#define APP_LAST_UPDATE "2005-06-20"#define VIEWER_BGCOLOR SbColor(0.33f, 0.74f, 0.93f)// ??? TEMPORARY:// use "app-object" instead and store all important values in it ???static TeEngine *engine = NULL;static SoPerfGraph *fpsGraph = NULL;static SoPerfGraph *renderTimeGraph = NULL;//----------------------------------------------------------------------------// Time-tick callbackstatic void timeTickCB(void *data, SoSensor *sensor){ // compute delta (time from previous call of this function) static SbTime last = SbTime::zero(); SbTime delta; SbTime current = SbTime::getTimeOfDay(); if (last == SbTime::zero()) delta = SbTime::zero(); else delta = current - last; last = current; // update performance graphs if (fpsGraph && delta.getValue() != 0.) fpsGraph->appendValue(1.f/float(delta.getValue())); if (renderTimeGraph) renderTimeGraph->appendValue(0.f); // call engine's timeTick engine->timeTick(delta); engine->getObject(0)->timeTick(delta); engine->updateGraph(engine->getObject(0)); engine->getObject(1)->timeTick(delta); engine->updateGraph(engine->getObject(1)); sensor->schedule();};//-----------------------------------------------------------------------------// Event callbackstatic void eventCBFunc(void *userData, SoEventCallback *eventCB){ const SoEvent *event = eventCB->getEvent(); engine->getActiveObject()->handleEvent(event); eventCB->setHandled();}//----------------------------------------------------------------------------// mainint main(int argc, char *argv[]){ // SoWin & Coin3D libraries init #ifdef _WIN32 HWND window = SoWin::init(argv[0]); #else QWidget *window = SoQt::init(argv[0]); #endif if (window == NULL) exit(1); // random num. generator init srand((unsigned int)time(NULL)); // Directory with models and textures SoInput::addDirectoryFirst("data/textures/"); SoInput::addDirectoryFirst("data/meshes/"); SoInput::addDirectoryFirst("../data/textures/"); SoInput::addDirectoryFirst("../data/meshes/"); // scene root SoSeparator *root = new SoSeparator; root->ref(); // event callback // (this is placed on the first position bellow the root to handle events quickly) SoEventCallback *eventCallback = new SoEventCallback; eventCallback->addEventCallback(SoEvent::getClassTypeId(), eventCBFunc, NULL); root->addChild(eventCallback); // scene separator SoSeparator *sceneRoot = new SoSeparator; root->addChild(sceneRoot); // scene camera SoPerspectiveCamera *sceneCamera = new SoPerspectiveCamera; sceneCamera->nearDistance = 0.1f; sceneCamera->farDistance = 250.0f; sceneCamera->orientation.setValue( SbRotation(SbVec3f(1.0f, 0.0f, 0.0f), (float)M_PI/180.0f*80.0f) ); sceneRoot->addChild(sceneCamera); // sunlight SoDirectionalLight *sunLight = new SoDirectionalLight; sunLight->direction.setValue(0.75f, 0.5f, -1.0f); sunLight->intensity.setValue(1.0); sceneRoot->addChild(sunLight); // terrain engine engine = new TeEngine(SbVec2s(32,65), SbVec2f(128.0f,256.0f)); engine->faultFormation.maxDelta = 5.0f; engine->setSceneRoot(sceneRoot); // simple engine object, it will fly along +y axis at constant speed TeObject *object1 = new TeEngineExplorer; object1->setModel("Bomber.wrl", TeObject::RENDER); object1->setModel("Bomber-C.wrl", TeObject::COLLISION); object1->setPosition(-1.0f, -27.0f, 210.0f); object1->speed = SbVec3f(0.0f, 10.0f, 0.0f); engine->addObject(object1); // engine explorer - active object // !!! ACTIVE OBJECT SHOULD BE THE LAST ONE IN THE LIST !!! // !!! It will cause remove/add blink if it is not... !!! TeObject *object2 = new TeEngineExplorer; object2->setModel("Bomber.wrl", TeObject::RENDER); object2->setModel("Bomber-C.wrl", TeObject::COLLISION); object2->setPosition(0.0f, -30.0f, 210.0f); object2->speed = SbVec3f(0.0f, 10.0f, 0.0f); engine->addObject(object2); engine->setActiveObject(object2); // HUD separator SoSeparator *HUDRoot = new SoSeparator; root->addChild(HUDRoot); // HUD camera SoPerspectiveCamera *HUDCamera = new SoPerspectiveCamera; HUDCamera->nearDistance = 0.1f; HUDCamera->farDistance = 10.0f; HUDRoot->addChild(HUDCamera); // engine init engine->setHUDRoot(HUDRoot); engine->setSceneCamera(sceneCamera); engine->setHUDCamera(HUDCamera); engine->initialize(); // render area SoGuiGameViewer *ra = new SoGuiGameViewer(window); ra->setMouseMode(SoGuiGameViewer::DELTA_TO_SCENE_GRAPH); ra->setBackgroundColor( VIEWER_BGCOLOR ); ra->setSceneGraph(root); ra->setTitle( APP_TITLE ); ra->show(); // timeTick sensor SoOneShotSensor *timeTickSensor = new SoOneShotSensor(timeTickCB, NULL); timeTickSensor->schedule(); // create FPS performace graph SoPerfGraph::initClass(); fpsGraph = new SoPerfGraph; fpsGraph->horScreenOrigin.setValue(SoPerfGraph::RIGHT); fpsGraph->horAlignment.setValue(SoPerfGraph::RIGHT); fpsGraph->position = SbVec2f(-0.03f, 0.03f); fpsGraph->size = SbVec2f(0.23f,0.11f); root->addChild(fpsGraph); // create render time performace graph renderTimeGraph = new SoPerfGraph; renderTimeGraph->horScreenOrigin.setValue(SoPerfGraph::RIGHT); renderTimeGraph->horAlignment.setValue(SoPerfGraph::RIGHT); renderTimeGraph->position = SbVec2f(-0.03f, 0.23f); renderTimeGraph->size = SbVec2f(0.23f,0.11f); root->addChild(renderTimeGraph); // console echo printf("%s (%s)\n\nRunning...\n\n", APP_TITLE, APP_LAST_UPDATE); printf("Use arrows to change object's speed vector.\nUse 8, 6, 2, 4 on NumPad, PdUp and PgDown to change camera's distance.\n\n"); //ra->setFullScreen(TRUE); // show window and run mainLoop #ifdef _WIN32 SoWin::show(window); SoWin::mainLoop(); #else SoQt::show(window); SoQt::mainLoop(); #endif // free memory delete ra; delete object1; delete object2; root->unref(); // console echo printf("\nDone...\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -