Renamed constants for dictionary keys

Created static creation method for Renderables
This commit is contained in:
Alexander Bock
2014-05-04 16:18:35 +02:00
parent fc291bfed0
commit e84c26aeb7
8 changed files with 122 additions and 81 deletions

View File

@@ -36,6 +36,8 @@ namespace openspace {
class Renderable : public properties::PropertyOwner {
public:
static Renderable* createFromDictionary(const ghoul::Dictionary& dictionary);
// constructors & destructor
Renderable(const ghoul::Dictionary& dictionary);
virtual ~Renderable();

View File

@@ -42,61 +42,60 @@ namespace openspace {
class SceneGraphNode {
public:
// constructors & destructor
SceneGraphNode();
~SceneGraphNode();
// constructors & destructor
SceneGraphNode();
~SceneGraphNode();
static SceneGraphNode* createFromDictionary(const ghoul::Dictionary& dictionary);
bool initialize();
bool deinitialize();
// essential
void update();
void evaluate(const Camera *camera, const psc &parentPosition = psc());
void render(const Camera *camera, const psc &parentPosition = psc());
// essential
void update();
void evaluate(const Camera* camera, const psc& parentPosition = psc());
void render(const Camera* camera, const psc& parentPosition = psc());
// set & get
void addNode(SceneGraphNode* child);
void setName(const std::string &name);
void setParent(SceneGraphNode *parent);
const psc& getPosition() const;
psc getWorldPosition() const;
// set & get
void addNode(SceneGraphNode* child);
void setName(const std::string& name);
void setParent(SceneGraphNode* parent);
const psc& getPosition() const;
psc getWorldPosition() const;
std::string nodeName() const;
SceneGraphNode* parent() const;
const std::vector<SceneGraphNode*>& children() const;
// bounding sphere
pss calculateBoundingSphere();
// bounding sphere
pss calculateBoundingSphere();
SceneGraphNode* get(const std::string& name);
void print() const;
// renderable
void setRenderable(Renderable *renderable);
const Renderable * getRenderable() const;
// renderable
void setRenderable(Renderable* renderable);
const Renderable* getRenderable() const;
private:
// essential
std::vector<SceneGraphNode*> _children;
SceneGraphNode* _parent;
std::string _nodeName;
// essential
std::vector<SceneGraphNode*> _children;
SceneGraphNode* _parent;
std::string _nodeName;
PositionInformation* _position;
// renderable
Renderable *_renderable;
bool _renderableVisible;
// bounding sphere
bool _boundingSphereVisible;
pss _boundingSphere;
// private helper methods
bool sphereInsideFrustum(const psc s_pos, const pss & s_rad, const Camera *camera);
// renderable
Renderable* _renderable;
bool _renderableVisible;
// bounding sphere
bool _boundingSphereVisible;
pss _boundingSphere;
// private helper methods
bool sphereInsideFrustum(const psc s_pos, const pss& s_rad, const Camera* camera);
};
} // namespace openspace

View File

@@ -30,27 +30,31 @@
namespace openspace {
namespace constants {
namespace openspaceengine {
const std::string pathKey = "Paths";
const std::string scenePathKey = "Paths.SCENEPATH";
const std::string sgctConfigKey = "SGCTConfig";
const std::string sceneConfigurationKey = "Scene";
const std::string keyPaths = "Paths";
const std::string keyPathScene = "Paths.SCENEPATH";
const std::string keyConfigSgct = "SGCTConfig";
const std::string keyConfigScene = "Scene";
} // namespace openspaceengine
namespace scenegraph {
const std::string modulesKey = "Modules";
const std::string cameraKey = "Camera";
const std::string focusKey = "Focus";
const std::string positionKey = "Position";
const std::string modulePathKey = "ModulePath";
const std::string keyModules = "Modules";
const std::string keyCamera = "Camera";
const std::string keyFocusObject = "Focus";
const std::string keyPositionObject = "Position";
const std::string keyPathModule = "ModulePath";
} // namespace scenegraph
namespace scenegraphnode {
const std::string nameKey = "Name";
const std::string parentKey = "Parent";
const std::string renderableKey = "Renderable";
const std::string ephemerisKey = "Ephemeris";
const std::string keyName = "Name";
const std::string keyParentName = "Parent";
const std::string keyRenderable = "Renderable";
const std::string keyEphemeris = "Ephemeris";
} // namespace scenegraphnode
namespace renderable {
const std::string keyType = "Type";
}
} // namespace constants
} // namespace openspace

View File

@@ -188,15 +188,15 @@ void OpenSpaceEngine::create(int argc, char** argv,
ghoul::Dictionary& configuration = *(_engine->_configurationManager);
ghoul::lua::loadDictionaryFromFile(configurationFilePath, configuration);
if (configuration.hasKey(constants::openspaceengine::pathKey)) {
if (configuration.hasKey(constants::openspaceengine::keyPaths)) {
ghoul::Dictionary pathsDictionary;
if (configuration.getValue(constants::openspaceengine::pathKey, pathsDictionary))
if (configuration.getValue(constants::openspaceengine::keyPaths, pathsDictionary))
OpenSpaceEngine::registerPathsFromDictionary(pathsDictionary);
}
std::string sgctConfigurationPath = _sgctDefaultConfigFile;
if (configuration.hasKey(constants::openspaceengine::sgctConfigKey))
configuration.getValue(constants::openspaceengine::sgctConfigKey, sgctConfigurationPath);
if (configuration.hasKey(constants::openspaceengine::keyConfigSgct))
configuration.getValue(constants::openspaceengine::keyConfigSgct, sgctConfigurationPath);
sgctArguments.push_back(argv[0]);
sgctArguments.push_back("-config");
@@ -247,14 +247,14 @@ bool OpenSpaceEngine::initialize()
std::shared_ptr<SceneGraph> sceneGraph(new SceneGraph);
_renderEngine->setSceneGraph(sceneGraph);
if (!OsEng.configurationManager().hasValue<std::string>(
constants::openspaceengine::sceneConfigurationKey)) {
constants::openspaceengine::keyConfigScene)) {
LFATAL("Configuration needs to point to the scene file");
return false;
}
std::string sceneDescriptionPath;
bool success = _configurationManager->getValue(
constants::openspaceengine::sceneConfigurationKey, sceneDescriptionPath);
constants::openspaceengine::keyConfigScene, sceneDescriptionPath);
if (!FileSys.fileExists(sceneDescriptionPath)) {
LFATAL("Could not find '" << sceneDescriptionPath << "'");
@@ -262,7 +262,7 @@ bool OpenSpaceEngine::initialize()
}
std::string scenePath;
success = _configurationManager->getValue(constants::openspaceengine::scenePathKey, scenePath);
success = _configurationManager->getValue(constants::openspaceengine::keyPathScene, scenePath);
if (!success) {
LFATAL("Could not find SCENEPATH key in configuration file");
return false;

View File

@@ -24,6 +24,12 @@
// open space includes
#include <openspace/rendering/renderable.h>
#include <openspace/util/constants.h>
#include <openspace/util/factorymanager.h>
namespace {
const std::string _loggerCat = "Renderable";
}
namespace openspace {
@@ -31,6 +37,28 @@ namespace openspace {
// : _name("")
//{}
Renderable* Renderable::createFromDictionary(const ghoul::Dictionary& dictionary)
{
if (!dictionary.hasValue<std::string>(constants::renderable::keyType)) {
LERROR("Renderable did not have key '" << constants::renderable::keyType << "'");
return nullptr;
}
std::string renderableType;
dictionary.getValue(constants::renderable::keyType, renderableType);
ghoul::TemplateFactory<Renderable>* factory = FactoryManager::ref().factoryByType<Renderable>();
Renderable* result = factory->create(renderableType, dictionary);
if (result == nullptr) {
LERROR("Failed creating Renderable object of type '" << renderableType << "'");
return nullptr;
}
std::string name;
dictionary.getValue(constants::scenegraphnode::keyName, name);
result->setName(name);
return result;
}
Renderable::Renderable(const ghoul::Dictionary& dictionary)
: _name("")
{

View File

@@ -24,6 +24,7 @@
// open space includes
#include <openspace/rendering/renderableplanet.h>
#include <openspace/util/constants.h>
#include <ghoul/opengl/texturereader.h>
#include <ghoul/filesystem/filesystem.h>
@@ -65,8 +66,8 @@ RenderablePlanet::RenderablePlanet(const ghoul::Dictionary& dictionary)
// get path if available
std::string path = "";
if (dictionary.hasKey("Path")) {
dictionary.getValue("Path", path);
if (dictionary.hasKey(constants::scenegraph::keyPathModule)) {
dictionary.getValue(constants::scenegraph::keyPathModule, path);
path += "/";
}

View File

@@ -242,7 +242,7 @@ bool SceneGraph::loadScene(const std::string& sceneDescriptionFilePath,
Dictionary dictionary;
loadDictionaryFromFile(sceneDescriptionFilePath, dictionary);
Dictionary moduleDictionary;
if (dictionary.getValue(constants::scenegraph::modulesKey, moduleDictionary)) {
if (dictionary.getValue(constants::scenegraph::keyModules, moduleDictionary)) {
std::vector<std::string> keys = moduleDictionary.keys();
std::sort(keys.begin(), keys.end());
for (const std::string& key : keys) {
@@ -254,13 +254,13 @@ bool SceneGraph::loadScene(const std::string& sceneDescriptionFilePath,
// TODO: Make it less hard-coded and more flexible when nodes are not found
Dictionary cameraDictionary;
if (dictionary.getValue(constants::scenegraph::cameraKey, cameraDictionary)) {
if (dictionary.getValue(constants::scenegraph::keyCamera, cameraDictionary)) {
LDEBUG("Camera dictionary found");
std::string focus;
std::string position;
if (cameraDictionary.hasKey(constants::scenegraph::focusKey)
&& cameraDictionary.getValue(constants::scenegraph::focusKey, focus)) {
if (cameraDictionary.hasKey(constants::scenegraph::keyFocusObject)
&& cameraDictionary.getValue(constants::scenegraph::keyFocusObject, focus)) {
auto focusIterator = _allNodes.find(focus);
if (focusIterator != _allNodes.end()) {
_focus = focus;
@@ -269,8 +269,8 @@ bool SceneGraph::loadScene(const std::string& sceneDescriptionFilePath,
else
LERROR("Could not find focus object '" << focus << "'");
}
if (cameraDictionary.hasKey(constants::scenegraph::positionKey)
&& cameraDictionary.getValue(constants::scenegraph::positionKey, position)) {
if (cameraDictionary.hasKey(constants::scenegraph::keyPositionObject)
&& cameraDictionary.getValue(constants::scenegraph::keyPositionObject, position)) {
auto positionIterator = _allNodes.find(position);
if (positionIterator != _allNodes.end()) {
_position = position;
@@ -308,7 +308,7 @@ void SceneGraph::loadModule(const std::string& modulePath)
ghoul::Dictionary element;
moduleDictionary.getValue(key, element);
element.setValue(constants::scenegraph::modulePathKey, modulePath);
element.setValue(constants::scenegraph::keyPathModule, modulePath);
SceneGraphNode* node = SceneGraphNode::createFromDictionary(element);

View File

@@ -74,31 +74,38 @@ bool safeCreationWithDictionary(T** object, const std::string& key,
SceneGraphNode* SceneGraphNode::createFromDictionary(const ghoul::Dictionary& dictionary)
{
using namespace constants::scenegraph;
using namespace constants::scenegraphnode;
SceneGraphNode* result = new SceneGraphNode;
std::string path;
dictionary.getValue(constants::scenegraph::modulePathKey, path);
dictionary.getValue(keyPathModule, path);
if (!dictionary.hasValue<std::string>(constants::scenegraphnode::nameKey)) {
if (!dictionary.hasValue<std::string>(keyName)) {
LERROR("SceneGraphNode in '" << path << "' did not contain a '"
<< constants::scenegraphnode::nameKey << "' key");
<< keyName << "' key");
return nullptr;
}
dictionary.getValue(constants::scenegraphnode::nameKey, result->_nodeName);
dictionary.getValue(keyName, result->_nodeName);
if (dictionary.hasKey(constants::scenegraphnode::renderableKey)) {
if (safeCreationWithDictionary<Renderable>(
&result->_renderable, constants::scenegraphnode::renderableKey,
dictionary, path)) {
LDEBUG(result->_nodeName << ": Successful creation of renderable");
result->_renderable->setName(result->_nodeName);
} else {
LDEBUG(result->_nodeName << ": Failed to create renderable");
if (dictionary.hasValue<ghoul::Dictionary>(keyRenderable)) {
ghoul::Dictionary renderableDictionary;
dictionary.getValue(keyRenderable,
renderableDictionary);
renderableDictionary.setValue(keyPathModule, path);
renderableDictionary.setValue(keyName, result->_nodeName);
result->_renderable = Renderable::createFromDictionary(renderableDictionary);
if (result->_renderable == nullptr) {
LERROR("Failed to create renderable for SceneGraphNode '"
<< result->_nodeName);
return nullptr;
}
}
if (dictionary.hasKey(constants::scenegraphnode::ephemerisKey)) {
if (dictionary.hasKey(keyEphemeris)) {
if (safeCreationWithDictionary<PositionInformation>(
&result->_position, constants::scenegraphnode::ephemerisKey, dictionary,
&result->_position, keyEphemeris, dictionary,
path)) {
LDEBUG(result->_nodeName << ": Successful creation of position");
} else {
@@ -107,7 +114,7 @@ SceneGraphNode* SceneGraphNode::createFromDictionary(const ghoul::Dictionary& di
}
std::string parentName;
if (!dictionary.getValue(constants::scenegraphnode::parentKey, parentName)) {
if (!dictionary.getValue(constants::scenegraphnode::keyParentName, parentName)) {
LWARNING("Could not find 'Parent' key, using 'Root'.");
parentName = "Root";
}