From de8be04754a1cf3e37eead0aecf89d1fa01989dc Mon Sep 17 00:00:00 2001 From: Jonas Strandstedt Date: Tue, 4 Mar 2014 16:40:22 -0500 Subject: [PATCH] Initial new SceneGraph structure - Has support for SceneGraph loading using lua and ghoul dictionary - SceneGraphNode can be initialized using ghoul::Dictionary - Todo: PositionInformation classes - Todo: TemplateFactory singleton --- ext/ghoul | 2 +- openspace-data | 2 +- src/openspaceengine.cpp | 56 +++++---- src/rendering/renderable.cpp | 4 - src/rendering/renderable.h | 6 +- src/rendering/renderablebody.cpp | 3 +- src/rendering/renderablebody.h | 4 +- src/rendering/renderableplanet.cpp | 13 +- src/rendering/renderableplanet.h | 4 +- src/rendering/renderengine.cpp | 10 +- src/rendering/renderengine.h | 2 +- src/scenegraph/scenegraph.cpp | 160 +++++++++++++++++++++-- src/scenegraph/scenegraph.h | 46 +++++-- src/scenegraph/scenegraphnode.cpp | 196 ++++++++++++++++++----------- src/scenegraph/scenegraphnode.h | 75 +++++++---- 15 files changed, 412 insertions(+), 171 deletions(-) diff --git a/ext/ghoul b/ext/ghoul index be4408cc1c..d670b5f62d 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit be4408cc1cb28c365872588db907f267861984c6 +Subproject commit d670b5f62d7ce9b9ef2c31a90ee76284aef3fb7d diff --git a/openspace-data b/openspace-data index 500458eba3..833450690c 160000 --- a/openspace-data +++ b/openspace-data @@ -1 +1 @@ -Subproject commit 500458eba30fef87fbdc924a8918902d27674125 +Subproject commit 833450690c5a5d0397821463c1430899ee3f5076 diff --git a/src/openspaceengine.cpp b/src/openspaceengine.cpp index c59eab9ccf..fbecb38a16 100644 --- a/src/openspaceengine.cpp +++ b/src/openspaceengine.cpp @@ -80,29 +80,8 @@ OpenSpaceEngine& OpenSpaceEngine::ref() { void OpenSpaceEngine::create(int argc, char** argv, int& newArgc, char**& newArgv) { // TODO custom assert (ticket #5) assert(_engine == nullptr); - _engine = new OpenSpaceEngine; - - LogManager::initialize(LogManager::LogLevel::Debug, true); - LogMgr.addLog(new ConsoleLog); - - ghoul::filesystem::FileSystem::initialize(); - -#ifdef __WIN32__ - // Windows: Binary two folders down - FileSys.registerPathToken("${BASE_PATH}", "../.."); -#elif __APPLE__ - // OS X : Binary three folders down - FileSys.registerPathToken("${BASE_PATH}", "../../.."); -#else - // Linux : Binary three folders down - FileSys.registerPathToken("${BASE_PATH}", ".."); -#endif - FileSys.registerPathToken("${SCRIPTS}", "${BASE_PATH}/scripts"); - FileSys.registerPathToken("${OPENSPACE-DATA}", "${BASE_PATH}/openspace-data"); - - _engine->_configurationManager = new ghoul::ConfigurationManager; - _engine->_configurationManager->initialize(); + // set SGCT arguments newArgc = 3; newArgv = new char*[3]; newArgv[0] = "prog"; @@ -118,7 +97,9 @@ void OpenSpaceEngine::create(int argc, char** argv, int& newArgc, char**& newArg newArgv[2] = "../config/single.xml"; #endif - // Create the renderenginge object + // create objects + _engine = new OpenSpaceEngine; + _engine->_configurationManager = new ghoul::ConfigurationManager; _engine->_renderEngine = new RenderEngine; _engine->_interactionHandler = new InteractionHandler; } @@ -128,17 +109,38 @@ void OpenSpaceEngine::destroy() { } bool OpenSpaceEngine::initialize() { + + // initialize ghou logging + LogManager::initialize(LogManager::LogLevel::Debug, true); + LogMgr.addLog(new ConsoleLog); + + // Initialize ghoul filesystem and set path variables + ghoul::filesystem::FileSystem::initialize(); +#ifdef __WIN32__ + // Windows: Binary two folders down + FileSys.registerPathToken("${BASE_PATH}", "../.."); +#elif __APPLE__ + // OS X : Binary three folders down + FileSys.registerPathToken("${BASE_PATH}", "../../.."); +#else + // Linux : Binary three folders down + FileSys.registerPathToken("${BASE_PATH}", ".."); +#endif + FileSys.registerPathToken("${SCRIPTS}", "${BASE_PATH}/scripts"); + FileSys.registerPathToken("${OPENSPACE-DATA}", "${BASE_PATH}/openspace-data"); + FileSys.registerPathToken("${SCENEPATH}", "${OPENSPACE-DATA}/scene"); + // Load the configurationmanager with the default configuration - ghoul::ConfigurationManager configuration; - configuration.initialize(absPath("${SCRIPTS}/DefaultConfig.lua")); - configuration.loadConfiguration(absPath("${SCRIPTS}/ExtraConfigScript.lua"), false); + _engine->_configurationManager->initialize(); + _engine->_configurationManager->loadConfiguration(absPath("${SCRIPTS}/DefaultConfig.lua")); Time::init(); Spice::init(); Spice::ref().loadDefaultKernels(); // TODO add scenegraph file name - _renderEngine->initialize(""); + // initialize the RenderEngine, needs ${SCENEPATH} to be set + _renderEngine->initialize(); DeviceIdentifier::init(); DeviceIdentifier::ref().scanDevices(); diff --git a/src/rendering/renderable.cpp b/src/rendering/renderable.cpp index 0819ce804a..ca2a8c9e44 100644 --- a/src/rendering/renderable.cpp +++ b/src/rendering/renderable.cpp @@ -8,10 +8,6 @@ Renderable::Renderable() { } -Renderable::Renderable(const pss &boundingSphere) { - boundingSphere_ = boundingSphere; -} - Renderable::~Renderable() { } diff --git a/src/rendering/renderable.h b/src/rendering/renderable.h index 389437c4e7..694517ebb1 100644 --- a/src/rendering/renderable.h +++ b/src/rendering/renderable.h @@ -5,6 +5,7 @@ #include "util/psc.h" #include "util/pss.h" #include "util/camera.h" +#include namespace openspace { @@ -13,9 +14,10 @@ public: // constructors & destructor Renderable(); - Renderable(const pss &boundingSphere); - virtual ~Renderable(); + virtual ~Renderable(); + virtual void initialize(ghoul::Dictionary* dictionary) = 0; + void setBoundingSphere(const pss &boundingSphere); const pss &getBoundingSphere(); diff --git a/src/rendering/renderablebody.cpp b/src/rendering/renderablebody.cpp index aa1afda9ed..e61601648f 100644 --- a/src/rendering/renderablebody.cpp +++ b/src/rendering/renderablebody.cpp @@ -3,7 +3,7 @@ #include "renderablebody.h" namespace openspace { - +/* RenderableBody::RenderableBody(const pss &radius):Renderable(radius) { programObject_ = nullptr; texture_ = nullptr; @@ -70,5 +70,6 @@ void RenderableBody::render(const Camera *camera, const psc &thisPosition) { void RenderableBody::update() { } +*/ } // namespace openspace \ No newline at end of file diff --git a/src/rendering/renderablebody.h b/src/rendering/renderablebody.h index 110663de16..4585acd6b2 100644 --- a/src/rendering/renderablebody.h +++ b/src/rendering/renderablebody.h @@ -10,7 +10,7 @@ #include "ghoul/opengl/texture.h" namespace openspace { - +/* class RenderableBody: public Renderable { public: @@ -31,7 +31,7 @@ private: gl4::Sphere *planet_; }; - +*/ } // namespace openspace #endif \ No newline at end of file diff --git a/src/rendering/renderableplanet.cpp b/src/rendering/renderableplanet.cpp index 31a33c98db..e48700d287 100644 --- a/src/rendering/renderableplanet.cpp +++ b/src/rendering/renderableplanet.cpp @@ -4,18 +4,17 @@ namespace openspace { -RenderablePlanet::RenderablePlanet(const pss &radius):Renderable(radius) { - programObject_ = nullptr; - texture_ = nullptr; - - // setup a unit sphere - planet_ = new Planet(radius,30); -} +RenderablePlanet::RenderablePlanet(): programObject_(nullptr), texture_(nullptr) {} RenderablePlanet::~RenderablePlanet() { delete planet_; } + +void RenderablePlanet::initialize(ghoul::Dictionary* dictionary) { + +} + void RenderablePlanet::setProgramObject(ghoul::opengl::ProgramObject *programObject = nullptr) { assert(programObject) ; programObject_ = programObject; diff --git a/src/rendering/renderableplanet.h b/src/rendering/renderableplanet.h index 5bf689788d..94a67a9472 100644 --- a/src/rendering/renderableplanet.h +++ b/src/rendering/renderableplanet.h @@ -15,8 +15,10 @@ class RenderablePlanet: public Renderable { public: // constructors & destructor - RenderablePlanet(const pss &radius); + RenderablePlanet(); ~RenderablePlanet(); + + virtual void initialize(ghoul::Dictionary* dictionary); void setProgramObject(ghoul::opengl::ProgramObject *programObject); void setTexture(ghoul::opengl::Texture *texture); diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index bd35f6c2ee..8ec7a27879 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -36,18 +36,14 @@ namespace { } namespace openspace { -RenderEngine::RenderEngine() - : _mainCamera(nullptr) - , _sceneGraph(nullptr) -{ -} +RenderEngine::RenderEngine() : _mainCamera(nullptr) , _sceneGraph(nullptr) {} RenderEngine::~RenderEngine() { delete _mainCamera; delete _sceneGraph; } -bool RenderEngine::initialize(const std::string& sceneGraph) { +bool RenderEngine::initialize() { // init camera and set position _mainCamera = new Camera(); _mainCamera->setScaling(glm::vec2(1.0, -8.0)); @@ -67,7 +63,7 @@ bool RenderEngine::initialize(const std::string& sceneGraph) { // init scenegraph _sceneGraph = new SceneGraph; - _sceneGraph->init(); + _sceneGraph->initialize(); //_sceneGraph = loadSceneGraph(sceneGraph); return true; diff --git a/src/rendering/renderengine.h b/src/rendering/renderengine.h index c9e16e5648..a3849657ea 100644 --- a/src/rendering/renderengine.h +++ b/src/rendering/renderengine.h @@ -38,7 +38,7 @@ public: RenderEngine(); ~RenderEngine(); - bool initialize(const std::string& sceneGraph); + bool initialize(); SceneGraph* sceneGraph(); diff --git a/src/scenegraph/scenegraph.cpp b/src/scenegraph/scenegraph.cpp index 69e45291d0..a7e65724f9 100644 --- a/src/scenegraph/scenegraph.cpp +++ b/src/scenegraph/scenegraph.cpp @@ -1,8 +1,31 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ // open space includes #include "scenegraph/scenegraph.h" #include "scenegraph/scenegraphloader.h" -#include "rendering/renderablebody.h" +#include "rendering/renderableplanet.h" #include "interaction/interactionhandler.h" #include "util/spice.h" @@ -12,22 +35,35 @@ #include "ghoul/logging/consolelog.h" #include "ghoul/opengl/texturereader.h" #include "ghoul/opengl/texture.h" + #include +#include +#include +#include +#include + +#include +#include + +namespace { + std::string _loggerCat = "SceneGraph"; +} namespace openspace { SceneGraph::SceneGraph() { - root_ = nullptr; + _root = nullptr; } SceneGraph::~SceneGraph() { - // deallocate the scene graph - if(root_) - delete root_; + // deallocate the scene graph. Recursive deallocation will occur + // no need to remove from _nodes and _allNodes. + if(_root) + delete _root; // deallocate shaders, iterate c++11 style - for (auto& shaderTuple: shaders_) { + for (auto& shaderTuple: _shaders) { // the shader is in the maps second position delete shaderTuple.second; @@ -35,9 +71,11 @@ SceneGraph::~SceneGraph() { } -void SceneGraph::init() { +void SceneGraph::initialize() { // logger string std::string _loggerCat = "SceneGraph::init"; + + loadFromModulePath(); // SceneGraphLoader *loader = new SceneGraphLoader(&nodes_, &shaders_); // root_ = loader->loadSceneGraph(absPath("${BASE_PATH}/modules")); @@ -46,18 +84,116 @@ void SceneGraph::init() { } void SceneGraph::update() { - for(int i = 0; i < nodes_.size(); ++i) { - nodes_[i]->update(); - } + for(auto node: _nodes) { + //node->update(); + } } void SceneGraph::evaluate(Camera *camera) { - root_->evaluate(camera); + _root->evaluate(camera); } void SceneGraph::render(Camera *camera) { - root_->render(camera); + _root->render(camera); } +void SceneGraph::loadFromModulePath() { + assert(_root == nullptr); + + ghoul::TemplateFactory renderablefactory; + renderablefactory.registerClass("RenderablePlanet"); + + ghoul::Dictionary dictionary; + lua_State* state = luaL_newstate(); + if (state == nullptr) { + LFATAL("Error creating new Lua state: Memory allocation error"); + return; + } + luaL_openlibs(state); + + _root = new SceneGraphNode; + _root->initialize(); + _nodes.push_back(_root); + _allNodes.insert ( std::make_pair("Root", _root)); + + + ghoul::lua::lua_loadIntoDictionary(state, &dictionary, absPath("${SCENEPATH}/default.scene")); + + ghoul::Dictionary* tmpDictionary; + if(dictionary.getValue("modules", tmpDictionary)) { + auto keys = tmpDictionary->keys(); + std::sort(keys.begin(), keys.end()); + for (auto key: keys) { + std::string moduleFolder; + if(tmpDictionary->getValue(key, moduleFolder)) { + loadModulesFromModulePath(absPath("${SCENEPATH}/"+moduleFolder)); + } + } + } + + // Close the Lua state + lua_close(state); +} + +void SceneGraph::loadModulesFromModulePath(const std::string& modulePath) { + lua_State* state = luaL_newstate(); + if (state == nullptr) { + LFATAL("Error creating new Lua state: Memory allocation error"); + return; + } + luaL_openlibs(state); + + auto pos = modulePath.find_last_of("/"); + if (pos == modulePath.npos) { + LFATAL("Bad format for module path: " << modulePath); + return; + } + + std::string fullModule = modulePath + modulePath.substr(pos) + ".mod"; + LDEBUG("Loading modules from: " << fullModule); + + ghoul::Dictionary moduleDictionary; + ghoul::lua::lua_loadIntoDictionary(state, &moduleDictionary, fullModule); + auto keys = moduleDictionary.keys(); + for (auto key: keys) { + ghoul::Dictionary* singleModuleDictionary; + if(moduleDictionary.getValue(key, singleModuleDictionary)) { + std::string moduleName; + if (singleModuleDictionary->getValue("Name", moduleName)) { + std::string parentName; + if ( ! singleModuleDictionary->getValue("Parent", parentName)) { + LDEBUG("Could not find 'Parent' key, using 'Root'."); + parentName = "Root"; + } + + auto parentIterator = _allNodes.find(parentName); + if (parentIterator == _allNodes.end()) { + LDEBUG("Could not find parent named '"<< parentName << + "' for '" << moduleName << "'." << + " Check module definition order. Skipping module."); + continue; + } + + // allocate SceneGraphNode and initialize with Dictionary + SceneGraphNode* node = new SceneGraphNode; + if(node->initializeWithDictionary(singleModuleDictionary)) { + // add to internal data structures + _allNodes.insert(std::make_pair(moduleName, node)); + _nodes.push_back(node); + + // set child and parent + SceneGraphNode* parentNode = parentIterator->second; + parentNode->addNode(node); + } else { + LFATAL("Unable to initialize module '"<< moduleName <<"' using dictionary."); + delete node; + } + } + } + } + + // Close the Lua state + lua_close(state); +} } // namespace openspace \ No newline at end of file diff --git a/src/scenegraph/scenegraph.h b/src/scenegraph/scenegraph.h index 8e5e8f34d8..e38c2b814f 100644 --- a/src/scenegraph/scenegraph.h +++ b/src/scenegraph/scenegraph.h @@ -1,3 +1,27 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + #ifndef SCENEGRAPH_H #define SCENEGRAPH_H @@ -9,7 +33,8 @@ #include // ghoul includes -#include "ghoul/opengl/programobject.h" +#include +#include namespace openspace { @@ -20,24 +45,29 @@ public: SceneGraph(); ~SceneGraph(); - void init(); + void initialize(); void update(); void evaluate(Camera *camera); void render(Camera *camera); void printChildren() const { - root_->print(); + _root->print(); } - SceneGraphNode* root() const { return root_; } - void setRoot(SceneGraphNode* root) { root_ = root; } + SceneGraphNode* root() const { return _root; } + void setRoot(SceneGraphNode* root) { _root = root; } private: - SceneGraphNode *root_; - std::vector nodes_; - std::map shaders_; + void loadFromModulePath(); + void loadModulesFromModulePath(const std::string& modulePath); + + // actual scenegraph + SceneGraphNode *_root; + std::vector _nodes; + std::map _allNodes; + std::map _shaders; }; diff --git a/src/scenegraph/scenegraphnode.cpp b/src/scenegraph/scenegraphnode.cpp index 3af3cfba86..b458e7e14b 100644 --- a/src/scenegraph/scenegraphnode.cpp +++ b/src/scenegraph/scenegraphnode.cpp @@ -1,57 +1,100 @@ - +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + // open space includes #include "scenegraph/scenegraphnode.h" #include "util/spice.h" // ghoul includes -#include "ghoul/logging/logmanager.h" -#include "ghoul/logging/consolelog.h" +#include +#include + +namespace { + std::string _loggerCat = "SceneGraphNode"; +} namespace openspace { -SceneGraphNode::SceneGraphNode() { - - nodeName_ = ""; - - // init pointers with nullptr - renderable_ = nullptr; - parent_ = nullptr; - boundingSphereVisible_ = false; - renderableVisible_ = false; - - // spice not used when spiceID_ is 0, 0 is the sun barycenter - spiceID_ = 0; - parentSpiceID_ = 0; -} +SceneGraphNode::SceneGraphNode(): _parent(nullptr), _nodeName("Unnamed OpenSpace SceneGraphNode"), + _renderable(nullptr), _renderableVisible(false), + _boundingSphereVisible(false), _spiceName("") {} SceneGraphNode::~SceneGraphNode() { - // logger string - std::string _loggerCat = "SceneGraphNode::~SceneGraphNode()"; - LDEBUG("Deallocating: " << nodeName_); + LDEBUG("Deallocating: " << _nodeName); // deallocate the renderable - if(renderable_) - delete renderable_; + if(_renderable != nullptr) + delete _renderable; // deallocate the child nodes and delete them, iterate c++11 style - for( auto &child: children_) { + for( auto child: _children) delete child; - } +} - // empty the vector - children_.erase (children_.begin(),children_.end()); + +bool SceneGraphNode::initialize() { + return true; +} + +bool SceneGraphNode::initializeWithDictionary(ghoul::Dictionary* dictionary) { + + if( ! initialize()) { + return false; + } + + // set the _nodeName if available + dictionary->getValue("Name", _nodeName); + + // get the renderable + if(dictionary->hasKey("Renderable")) { + ghoul::Dictionary* renderableDictionary; + if(dictionary->getValue("Renderable", renderableDictionary)) { + + } + } + + // get the position + if(dictionary->hasKey("Position")) { + ghoul::Dictionary* positionDictionary; + if(dictionary->getValue("Position", positionDictionary)) { + + } + } + + return true; } // essential void SceneGraphNode::update() { - - if(spiceID_ > 0) { + /* + if(_spiceID > 0) { double state[3]; //double orientation[3][3]; - Spice::ref().spk_getPosition(spiceID_, parentSpiceID_, state); + Spice::ref().spk_getPosition(_spiceID, _parentSpiceID, state); // multiply with factor 1000, spice uses km as standard and Open Space uses m - position_ = psc::CreatePSC(state[0]*1000.0,state[1]*1000.0,state[2]*1000.0); + _position = psc::CreatePSC(state[0]*1000.0,state[1]*1000.0,state[2]*1000.0); // update rotation //if(Spice::ref().spk_getOrientation(spiceName_,orientation)) { @@ -62,43 +105,44 @@ void SceneGraphNode::update() { //} } - if(renderable_) { - renderable_->update(); + if(_renderable) { + _renderable->update(); } + */ } void SceneGraphNode::evaluate(const Camera *camera, const psc & parentPosition) { - const psc thisPosition = parentPosition + position_; + const psc thisPosition = parentPosition + _position; const psc camPos = camera->getPosition(); const psc toCamera = thisPosition - camPos; // init as not visible - boundingSphereVisible_ = true; - renderableVisible_ = false; + _boundingSphereVisible = true; + _renderableVisible = false; // check if camera is outside the node boundingsphere - if(toCamera.length() > boundingSphere_) { + if(toCamera.length() > _boundingSphereVisible) { // check if the boudningsphere is visible before avaluating children - if( ! sphereInsideFrustum(thisPosition, boundingSphere_, camera)) { + if( ! sphereInsideFrustum(thisPosition, _boundingSphere, camera)) { // the node is completely outside of the camera view, stop evaluating this node return; } } - boundingSphereVisible_ = true; + _boundingSphereVisible = true; // inside boudningsphere or parts of the sphere is visible, individual children needs to be evaluated // this node has an renderable - if(renderable_) { + if(_renderable) { // check if the renderable boundingsphere is visible - renderableVisible_ = sphereInsideFrustum(thisPosition, renderable_->getBoundingSphere(), camera); + _renderableVisible = sphereInsideFrustum(thisPosition, _renderable->getBoundingSphere(), camera); } // evaluate all the children, tail-recursive function(?) - for(auto &child: children_) { + for(auto &child: _children) { child->evaluate(camera,thisPosition); } @@ -106,20 +150,20 @@ void SceneGraphNode::evaluate(const Camera *camera, const psc & parentPosition) void SceneGraphNode::render(const Camera *camera, const psc & parentPosition) { - const psc thisPosition = parentPosition + position_; + const psc thisPosition = parentPosition + _position; // check if camera is outside the node boundingsphere - if( ! boundingSphereVisible_) { + if( ! _boundingSphereVisible) { return; } - if(renderableVisible_) { - if (nodeName_ == "earth") - nodeName_ = nodeName_; - renderable_->render(camera,thisPosition); + if(_renderableVisible) { + if (_nodeName == "earth") + _nodeName = _nodeName; + _renderable->render(camera,thisPosition); } // evaluate all the children, tail-recursive function(?) - for(auto &child: children_) { + for(auto &child: _children) { child->render(camera,thisPosition); } @@ -127,53 +171,56 @@ void SceneGraphNode::render(const Camera *camera, const psc & parentPosition) { // set & get void SceneGraphNode::addNode(SceneGraphNode *child) { - // add a child node and set this node to be the parent - child->setParent(this); - children_.push_back(child); + + child->setParent(this); + _children.push_back(child); } void SceneGraphNode::setName(const std::string &name) { - nodeName_ = name; + _nodeName = name; } void SceneGraphNode::setParent(SceneGraphNode *parent) { - parent_ = parent; + _parent = parent; } + void SceneGraphNode::setPosition(const psc &position) { - position_ = position; + _position = position; } void SceneGraphNode::setSpiceID(const int spiceID, const int parentSpiceID) { - spiceID_ = spiceID; - parentSpiceID_ = parentSpiceID; + + _spiceID = spiceID; + _parentSpiceID = parentSpiceID; update(); + } void SceneGraphNode::setSpiceName(const std::string &name) { - spiceName_ = name; + _spiceName = name; } const int SceneGraphNode::getSpiceID() const { - return spiceID_; + return 0;//spiceID_; } const std::string & SceneGraphNode::getSpiceName() { - return spiceName_; + return _spiceName; } const psc &SceneGraphNode::getPosition() const { - return position_; + return _position; } psc SceneGraphNode::getWorldPosition() const { // recursive up the hierarchy if there are parents available - if(parent_) { - return position_ + parent_->getWorldPosition(); + if(_parent) { + return _position + _parent->getWorldPosition(); } else { - return position_; + return _position; } } @@ -181,39 +228,39 @@ psc SceneGraphNode::getWorldPosition() const { pss SceneGraphNode::calculateBoundingSphere() { // set the vounding sphere to 0.0 - boundingSphere_ = 0.0; + _boundingSphere = 0.0; - if(children_.size() > 0) { // node + if(_children.size() > 0) { // node pss maxChild; // loop though all children and find the one furthest away/with the largest bounding sphere - for(size_t i = 0; i < children_.size(); ++i) { + for(size_t i = 0; i < _children.size(); ++i) { // when positions is dynamix, change this part to fins the most distant position - pss child = children_.at(i)->getPosition().length() + children_.at(i)->calculateBoundingSphere(); + pss child = _children.at(i)->getPosition().length() + _children.at(i)->calculateBoundingSphere(); if(child > maxChild) { maxChild = child; } } - boundingSphere_ += maxChild; + _boundingSphere += maxChild; } else { // leaf // if has a renderable, use that boundingsphere - if(renderable_) - boundingSphere_ += renderable_->getBoundingSphere(); + if(_renderable) + _boundingSphere += _renderable->getBoundingSphere(); } - return boundingSphere_; + return _boundingSphere; } // renderable void SceneGraphNode::setRenderable(Renderable *renderable) { - renderable_ = renderable; + _renderable = renderable; update(); } const Renderable * SceneGraphNode::getRenderable() const{ - return renderable_; + return _renderable; } // private helper methods @@ -229,7 +276,7 @@ bool SceneGraphNode::sphereInsideFrustum(const psc s_pos, const pss & s_rad, con psc D = s_pos - U; // check if outside the maximum angle - if (nodeName_ == "earth") { + if (_nodeName == "earth") { //psc tmp = s_pos - camera->getPosition(); //LINFOC("", "Angle: " << psc_camdir.angle(D)); @@ -253,6 +300,5 @@ bool SceneGraphNode::sphereInsideFrustum(const psc s_pos, const pss & s_rad, con } } - } // namespace openspace \ No newline at end of file diff --git a/src/scenegraph/scenegraphnode.h b/src/scenegraph/scenegraphnode.h index efeb8158ab..b6330db4a5 100644 --- a/src/scenegraph/scenegraphnode.h +++ b/src/scenegraph/scenegraphnode.h @@ -1,9 +1,35 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + #ifndef SCENEGRAPHNODE_H #define SCENEGRAPHNODE_H // open space includes #include "rendering/renderable.h" +#include + // std includes #include #include @@ -17,6 +43,11 @@ public: // constructors & destructor SceneGraphNode(); ~SceneGraphNode(); + + bool isInitialized(); + + bool initialize(); + bool initializeWithDictionary(ghoul::Dictionary* dictionary); // essential void update(); @@ -24,36 +55,37 @@ public: void render(const Camera *camera, const psc &parentPosition = psc()); // set & get - void addNode(SceneGraphNode *child); - void setName(const std::string &name); + void addNode(SceneGraphNode* child); + + void setName(const std::string &name); void setParent(SceneGraphNode *parent); - void setPosition(const psc &position); + void setPosition(const psc &position); void setSpiceID(const int spiceID, const int parentSpiceID); void setSpiceName(const std::string &name); const int getSpiceID() const; const std::string & getSpiceName(); const psc& getPosition() const; psc getWorldPosition() const; - std::string nodeName() const { return nodeName_; } + std::string nodeName() const { return _nodeName; } - SceneGraphNode* parent() const { return parent_; } - const std::vector& children() const { return children_; } + SceneGraphNode* parent() const { return _parent; } + const std::vector& children() const { return _children; } // bounding sphere pss calculateBoundingSphere(); SceneGraphNode* get(const std::string& name) { - if (nodeName_ == name) + if (_nodeName == name) return this; else - for (auto it : children_) + for (auto it : _children) return it->get(name); return nullptr; } void print() const { - std::cout << nodeName_ << std::endl; - for (auto it : children_) { + std::cout << _nodeName << std::endl; + for (auto it : _children) { it->print(); } } @@ -65,27 +97,26 @@ public: private: // essential - std::vector children_; - SceneGraphNode *parent_; - psc position_; - std::string nodeName_; + std::vector _children; + SceneGraphNode* _parent; + std::string _nodeName; + psc _position; // renderable - Renderable *renderable_; - bool renderableVisible_; + Renderable *_renderable; + bool _renderableVisible; // bounding sphere - bool boundingSphereVisible_; - pss boundingSphere_; + bool _boundingSphereVisible; + pss _boundingSphere; // spice - std::string spiceName_; - int spiceID_; - int parentSpiceID_; + std::string _spiceName; + int _spiceID; + int _parentSpiceID; // private helper methods bool sphereInsideFrustum(const psc s_pos, const pss & s_rad, const Camera *camera); - }; } // namespace openspace