📄 teobject.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/SoDB.h>#include <Inventor/nodes/SoSeparator.h>#include <Inventor/nodes/SoTranslation.h>#include <Inventor/nodes/SoRotation.h>#include <Inventor/nodes/SoPerspectiveCamera.h>#include <Inventor/nodes/SoMaterial.h>#include <Inventor/nodes/SoFont.h>#include <Inventor/nodes/SoText2.h>#include <Inventor/events/SoKeyboardEvent.h>#include "objects/TeObject.h"#include "TeEngine.h"const SbVec3f& TeObject::getPosition() const{ return translation->translation.getValue();}void TeObject::setPosition(const SbVec3f &pos){ translation->translation.setValue(pos);}void TeObject::setPosition(const float x, const float y, const float z){ translation->translation.setValue(x,y,z);}const SbRotation& TeObject::getOrientation() const{ return rotation->rotation.getValue();}void TeObject::setOrientation(const SbRotation &orient){ rotation->rotation.setValue(orient);}void TeObject::setOrientation(const float q0, const float q1, const float q2, const float q3){ rotation->rotation.setValue(q0,q1,q2,q3);}//-----------------------------------------------------------------------------/** * Default constructor. * * Sets TeObject::initialized to FALSE, and zeroes TeObject::position and * TeObject::speed. */TeObject::TeObject() : engine(NULL), initialized(FALSE), root(NULL), translation(NULL), rotation(NULL), camDistance(SbVec3f(0.0f, 0.0f, 0.0f)), speed(SbVec3f(0.0f, 0.0f, 0.0f)){ createHiddenScene();}//-----------------------------------------------------------------------------/** * Destructor. * * Releases internal scene and removes self from engine's object list. */TeObject::~TeObject(){ releaseHiddenScene(); engine->removeObject(this);}//-----------------------------------------------------------------------------/** * Creates internal hidden scene that represents the object. * * \sa releaseHiddenScene() */void TeObject::createHiddenScene(){ root = new SoSeparator; root->ref(); translation = new SoTranslation; translation->translation.setValue( SbVec3f(0.0f, 0.0f, 0.0f) ); root->addChild(translation); rotation = new SoRotation; rotation->rotation.setValue( SbRotation( SbVec3f(1.0f, 0.0f, 0.0f), 90.0f * (float)M_PI / 180.0f )); root->addChild(rotation); model = NULL; collisionModel = NULL;}//-----------------------------------------------------------------------------/** * Destroys internal hidden scene that represents the object. * * \sa createHiddenScene() */void TeObject::releaseHiddenScene(){ root->unref(); if (collisionModel) collisionModel->unref();}//-----------------------------------------------------------------------------/** * Creates HUD scene graph in engine's HUDRoot. */void TeObject::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("...TeObject..."); txt1->justification = SoText2::CENTER; tmp->addChild(material); tmp->addChild(font1); tmp->addChild(tr1); tmp->addChild(txt1);}//-----------------------------------------------------------------------------/** * Updates the HUD scene graph. * * This method is empty in this base class, because HUD is static. */void TeObject::updateHUD() {}//-----------------------------------------------------------------------------/** * Updates camera position. * * The camera is positioned at the object's coordinates. */void TeObject::updateCamera(){ SbVec3f cameraShift; getOrientation().multVec(camDistance, cameraShift); (engine->getSceneCamera())->position.setValue(getPosition() + cameraShift); (engine->getSceneCamera())->orientation.setValue(getOrientation());}//-----------------------------------------------------------------------------/** * Time tick function. * * It moves the object according to the speed vector and the time elapsed * since last call, updates scene camera and calls updateHUD() method. * * \param delta Time elapsed since last call. */void TeObject::timeTick(const SbTime delta){ // update position setPosition(getPosition() + speed * (float)delta.getValue()); if (engine->getActiveObject() == this) { this->updateCamera(); // update camera position this->updateHUD(); // update HUD }}//-----------------------------------------------------------------------------/** * Private attribute accessor. * * \return Scene for object model rendering. * * \sa root */SoSeparator *TeObject::getSceneGraph(){ return root;}//-----------------------------------------------------------------------------/** * Private attribute accessor. * * \return Translation node of the internal scene. * * \sa translation */SoTranslation *TeObject::getTranslation(){ return translation;}//-----------------------------------------------------------------------------/** * Private attribute accessor. * * \return Model for object rendering. * * \sa model */SoNode *TeObject::getModel(){ return model;}//-----------------------------------------------------------------------------/** * Private attribute accessor. * * \return Model for collision detection. * * \sa collisionModel */SoNode *TeObject::getCollisionModel(){ return collisionModel;}//-----------------------------------------------------------------------------/** * Sets selected model attribude. * * \param model Pointer to the node that contains the model scene. * \param which Model selector. */void TeObject::setModel(SoNode *model, MODEL_SET which){ if ((which & RENDER) != 0) { if (this->model) root->removeChild(this->model); this->model = model; if (model) root->addChild(model); } if ((which & COLLISION) != 0) { if (this->collisionModel) this->collisionModel->unref(); this->collisionModel = model; if (this->collisionModel) this->collisionModel->ref(); }}//-----------------------------------------------------------------------------/** * Sets selected model attribude. * * \param filename Name of file that contains the model scene. * \param which Model selector. * \return TRUE if the file exists and the model was loaded. */SbBool TeObject::setModel(const char *filename, MODEL_SET which){ if (!which) return TRUE; SoInput in; if (!in.openFile(filename)) return FALSE; SoSeparator *model = SoDB::readAll(&in); model->ref(); setModel(model, which); model->unref(); return TRUE;}//-----------------------------------------------------------------------------/** * Handles the user interaction. * * This method is almost empty in this base class. It only provides chance to * quit the application. * * \param event Event invoked by user interaction. */void TeObject::handleEvent(const SoEvent *event){ if (SO_KEY_PRESS_EVENT(event, ESCAPE)) exit(0);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -