📄 teengineexplorer.cpp
字号:
/*****************************************************************************\ * * TeObject.cpp * * TeObject abstract class implementation * * Author: Martin Havl龛ek (xhavli15 AT stud.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. *\*****************************************************************************/#include <Inventor/nodes/SoSeparator.h>#include <Inventor/nodes/SoPerspectiveCamera.h>#include <Inventor/nodes/SoMaterial.h>#include <Inventor/nodes/SoFont.h>#include <Inventor/nodes/SoTranslation.h>#include <Inventor/nodes/SoRotation.h>#include <Inventor/events/SoKeyboardEvent.h>#include <Inventor/events/SoLocation2Event.h>#include "objects/TeEngineExplorer.h"#include "TeEngine.h"//-----------------------------------------------------------------------------/** * Default constructor. */TeEngineExplorer::TeEngineExplorer() : defaultCamDistance(SbVec3f(0.0f, 1.1f, 3.5f)){ camDistance = defaultCamDistance;}//-----------------------------------------------------------------------------/** * Creates HUD scene graph in engine's HUDRoot. */void TeEngineExplorer::createHUD(){ SoSeparator *tmp = engine->getHUDRoot(); // centered HUD id SoFont *font1 = new SoFont; font1->name.setValue("Arial"); font1->size.setValue(20.0); SoMaterial *material = new SoMaterial; material->diffuseColor.setValue(SbVec3f(1.0f, 1.0f, 1.0f)); SoTranslation *tr1 = new SoTranslation; tr1->translation.setValue(0.0f, 0.37f, 0.0f); SoText2 *txt1 = new SoText2; txt1->string.setValue("...TerrainEngine Explorer..."); txt1->justification = SoText2::CENTER; tmp->addChild(material); tmp->addChild(font1); tmp->addChild(tr1); tmp->addChild(txt1); // controls SoFont *font2 = new SoFont; font2->name.setValue("Arial"); font2->size.setValue(15.0); SoTranslation *tr2 = new SoTranslation; tr2->translation.setValue(0.3f, -0.41f, 0.0f); SoText2 *txt2 = new SoText2; txt2->string.set1Value(0,"Usage:"); txt2->string.set1Value(1,"Arrows to change speed"); txt2->string.set1Value(2,"8, 6, 2, 4 on NumPad,"); txt2->string.set1Value(3," PdUp and PgDown to"); txt2->string.set1Value(4," change camera settings"); txt2->string.set1Value(5,"ESC to quit"); txt2->justification = SoText2::LEFT; tmp->addChild(font2); tmp->addChild(tr2); tmp->addChild(txt2); // position echo SoTranslation *tr4 = new SoTranslation; tr4->translation.setValue(-0.75f, -0.24f, 0.0f); SoText2 *txt4 = new SoText2; posText = txt4; txt4->justification = SoText2::LEFT; tmp->addChild(tr4); tmp->addChild(txt4); // speed echo SoTranslation *tr5 = new SoTranslation; tr5->translation.setValue(0.0f, 0.03f, 0.0f); SoText2 *txt5 = new SoText2; speedText = txt5; txt5->justification = SoText2::LEFT; tmp->addChild(tr5); tmp->addChild(txt5); // FPS echo SoTranslation *tr6 = new SoTranslation; tr6->translation.setValue(0.0f, 0.06f, 0.0f); SoText2 *txt6 = new SoText2; fpsText = txt6; txt6->justification = SoText2::LEFT; tmp->addChild(tr6); tmp->addChild(txt6); // Tasks in queue echo SoTranslation *tr7 = new SoTranslation; tr7->translation.setValue(0.0f, 0.06f, 0.0f); SoText2 *txt7 = new SoText2; tasksInQueueText = txt7; txt7->justification = SoText2::LEFT; tmp->addChild(tr7); tmp->addChild(txt7); // Root level echo SoTranslation *tr9 = new SoTranslation; tr9->translation.setValue(0.0f, 0.06f, 0.0f); SoText2 *txt9 = new SoText2; rootLevelText = txt9; txt9->justification = SoText2::LEFT; tmp->addChild(tr9); tmp->addChild(txt9); // Total number of patches echo SoTranslation *tr8 = new SoTranslation; tr8->translation.setValue(0.0f, 0.03f, 0.0f); SoText2 *txt8 = new SoText2; patchNumText = txt8; txt8->justification = SoText2::LEFT; tmp->addChild(tr8); tmp->addChild(txt8);}//-----------------------------------------------------------------------------/** * Updates the HUD scene graph. * * In this function all nodes that reflect actual state should be updated. */void TeEngineExplorer::updateHUD(){ char buffer[50]; sprintf(buffer, "Position: %0.2f, %0.2f", this->getPosition()[0], this->getPosition()[1]); posText->string.setValue(buffer); sprintf(buffer, "Speed: %0.2f, %0.2f, %0.2f", this->speed.getValue()[0], this->speed.getValue()[1], this->speed.getValue()[2]); speedText->string.setValue(buffer); sprintf(buffer, "FPS: %0.2f", this->engine->fps); fpsText->string.setValue(buffer); sprintf(buffer, "Tasks in queue: %d", this->engine->tasksInQueue); tasksInQueueText->string.setValue(buffer); sprintf(buffer, "DB tree depth: %d", this->engine->getRootLevel()); rootLevelText->string.setValue(buffer); sprintf(buffer, "Generated patches: %d", this->engine->patchesNum); patchNumText->string.setValue(buffer);}//-----------------------------------------------------------------------------/** * Handles the user interaction. * * This method defines controls of the object and should represent the * physics of the object by correctly adjusted control responses. * * \param event Event invoked by user interaction. */void TeEngineExplorer::handleEvent(const SoEvent *event){ if (event->isOfType(SoLocation2Event::getClassTypeId())) { const SbVec2s &delta = event->getPosition(); SbRotation rotChange(SbRotation(SbVec3f(0.f,-1.f,0.f), 0.03f*delta[0]) * SbRotation(SbVec3f(-1.f,0.f,0.f), 0.03f*delta[1])); this->setOrientation(rotChange * this->getOrientation()); // rotate the speed vector too const SbRotation &orientation = this->getOrientation(); orientation.inverse().multVec(speed,speed); rotChange.multVec(speed,speed); orientation.multVec(speed,speed); return; } // basic orientation vectors are present in the matrix: // vector pointing forward, to the right, and up. SbMatrix m; this->getOrientation().getValue(m); const float *f = m.operator float*(); // get pointer to matrix floats // arrows to change speed vector if (SO_KEY_PRESS_EVENT(event, UP_ARROW)) speed += -2.f * SbVec3f(f[8],f[9],f[10]); else if (SO_KEY_PRESS_EVENT(event, DOWN_ARROW)) speed += 2.0f * SbVec3f(f[8],f[9],f[10]); else if (SO_KEY_PRESS_EVENT(event, LEFT_ARROW)) speed += -2.0f * SbVec3f(f[0],f[1],f[2]); else if (SO_KEY_PRESS_EVENT(event, RIGHT_ARROW)) speed += 2.0f * SbVec3f(f[0],f[1],f[2]); // pad arrows, PgUp and PgDown to change camera distance else if (SO_KEY_PRESS_EVENT(event, PAD_8)) camDistance[1] += 0.05f; else if (SO_KEY_PRESS_EVENT(event, PAD_6)) camDistance[0] += 0.05f; else if (SO_KEY_PRESS_EVENT(event, PAD_2)) camDistance[1] -= 0.05f; else if (SO_KEY_PRESS_EVENT(event, PAD_4)) camDistance[0] -= 0.05f; else if (SO_KEY_PRESS_EVENT(event, PAGE_UP)) camDistance[2] += 0.05f; else if (SO_KEY_PRESS_EVENT(event, PAGE_DOWN)) camDistance[2] -= 0.05f; // pad 5 to default camera distance else if (SO_KEY_PRESS_EVENT(event, PAD_5)) camDistance = defaultCamDistance; // ESC to quit else if (SO_KEY_PRESS_EVENT(event, ESCAPE)) exit(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -