More cleanup

This commit is contained in:
Alexander Bock
2014-05-04 13:57:06 +02:00
parent cb6298b615
commit 2bf71ad9b6
11 changed files with 362 additions and 309 deletions
+2 -1
View File
@@ -25,6 +25,7 @@
#ifndef __QUERY_H__
#define __QUERY_H__
#include <memory>
#include <string>
namespace openspace {
@@ -32,7 +33,7 @@ namespace openspace {
class SceneGraph;
class SceneGraphNode;
SceneGraph* getSceneGraph();
std::shared_ptr<SceneGraph> getSceneGraph();
SceneGraphNode* getSceneGraphNode(const std::string& name);
+4 -2
View File
@@ -27,6 +27,7 @@
#include <openspace/scenegraph/scenegraph.h>
#include <memory>
#include <string>
namespace openspace {
@@ -40,7 +41,8 @@ public:
bool initialize();
SceneGraph* sceneGraph();
void setSceneGraph(std::shared_ptr<SceneGraph> sceneGraph);
std::shared_ptr<SceneGraph> sceneGraph();
// sgct wrapped functions
bool initializeGL();
@@ -54,7 +56,7 @@ public:
private:
Camera* _mainCamera;
SceneGraph* _sceneGraph;
std::shared_ptr<SceneGraph> _sceneGraph;
};
} // namespace openspace
+4 -4
View File
@@ -58,7 +58,10 @@ public:
/*
* Load the scenegraph from the provided folder
*/
bool loadFromModulePath(const std::string& path);
bool loadScene(const std::string& sceneDescriptionFilePath,
const std::string& defaultModulePath);
void loadModule(const std::string& modulePath);
/*
* Updates all SceneGraphNodes relative positions
@@ -86,9 +89,6 @@ public:
SceneGraphNode* root() const;
private:
void loadModulesFromModulePath(const std::string& modulePath);
std::string _focus, _position;
// actual scenegraph
+1
View File
@@ -11,4 +11,5 @@ return {
},
SGCTConfig = "${SGCT}/single.xml",
--sgctConfig = "${SGCT}/single_sbs_stereo.xml",
Scene = "${SCENEPATH}/default.scene"
}
+44 -11
View File
@@ -55,7 +55,10 @@ namespace {
namespace configuration {
const std::string pathKey = "Paths";
const std::string scenePathKey = "Paths.SCENEPATH";
const std::string sgctConfigKey = "SGCTConfig";
const std::string sceneConfigurationKey = "Scene";
}
}
@@ -177,13 +180,20 @@ void OpenSpaceEngine::create(int argc, char** argv,
assert(false);
}
// create objects
_engine = new OpenSpaceEngine;
_engine->_renderEngine = new RenderEngine;
_engine->_interactionHandler = new InteractionHandler;
_engine->_configurationManager = new ghoul::Dictionary;
LDEBUG("Registering base path");
if (!OpenSpaceEngine::registerBasePathFromConfigurationFile(configurationFilePath)) {
LFATAL("Could not register base path");
assert(false);
}
ghoul::Dictionary configuration;
ghoul::Dictionary& configuration = *(_engine->_configurationManager);
ghoul::lua::loadDictionaryFromFile(configurationFilePath, configuration);
if (configuration.hasKey(configuration::pathKey)) {
ghoul::Dictionary pathsDictionary;
@@ -195,15 +205,9 @@ void OpenSpaceEngine::create(int argc, char** argv,
if (configuration.hasKey(configuration::sgctConfigKey))
configuration.getValue(configuration::sgctConfigKey, sgctConfigurationPath);
sgctArguments.emplace_back(argv[0]);
sgctArguments.emplace_back("-config");
sgctArguments.emplace_back(absPath(sgctConfigurationPath));
// create objects
_engine = new OpenSpaceEngine;
_engine->_renderEngine = new RenderEngine;
_engine->_interactionHandler = new InteractionHandler;
_engine->_configurationManager = new ghoul::Dictionary;
sgctArguments.push_back(argv[0]);
sgctArguments.push_back("-config");
sgctArguments.push_back(absPath(sgctConfigurationPath));
}
void OpenSpaceEngine::destroy()
@@ -246,10 +250,39 @@ bool OpenSpaceEngine::initialize()
Spice::ref().loadDefaultKernels();
FactoryManager::initialize();
// TODO add scenegraph file name
// Load scenegraph
std::shared_ptr<SceneGraph> sceneGraph(new SceneGraph);
if (!OsEng.configurationManager().hasValue<std::string>(
configuration::sceneConfigurationKey)) {
LFATAL("Configuration needs to point to the scene file");
return false;
}
std::string sceneDescriptionPath;
bool success = _configurationManager->getValue(
configuration::sceneConfigurationKey, sceneDescriptionPath);
if (!FileSys.fileExists(sceneDescriptionPath)) {
LFATAL("Could not find '" << sceneDescriptionPath << "'");
return false;
}
std::string scenePath;
success = _configurationManager->getValue(configuration::scenePathKey, scenePath);
if (!success) {
LFATAL("Could not find SCENEPATH key in configuration file");
return false;
}
// initialize the RenderEngine, needs ${SCENEPATH} to be set
_renderEngine->initialize();
sceneGraph->loadScene(sceneDescriptionPath, scenePath);
sceneGraph->initialize();
_renderEngine->setSceneGraph(sceneGraph);
// Initialize OpenSpace input devices
DeviceIdentifier::init();
DeviceIdentifier::ref().scanDevices();
+1 -1
View File
@@ -111,7 +111,7 @@ const ghoul::Dictionary& Property::metaData() const {
}
void Property::onChange(std::function<void()> callback) {
_onChangeCallbacks.emplace_back(std::move(callback));
_onChangeCallbacks.push_back(std::move(callback));
}
+2 -2
View File
@@ -45,12 +45,12 @@ namespace {
}
}
SceneGraph* getSceneGraph() {
std::shared_ptr<SceneGraph> getSceneGraph() {
return OsEng.renderEngine().sceneGraph();
}
SceneGraphNode* getSceneGraphNode(const std::string& name) {
SceneGraph* sceneGraph = getSceneGraph();
std::shared_ptr<SceneGraph> sceneGraph = getSceneGraph();
SceneGraphNode* rootNode = sceneGraph->root();
return getSceneGraphNode(rootNode, name);
+3 -3
View File
@@ -122,7 +122,7 @@ void RenderablePlanet::setTexture(ghoul::opengl::Texture *texture) {
}
void RenderablePlanet::render(const Camera *camera, const psc &thisPosition) {
// TODO replace with more robust assert
// check so that the shader is set
assert(_programObject);
assert(_texture);
@@ -180,9 +180,9 @@ void RenderablePlanet::loadTexture() {
delete _texture;
_texture = nullptr;
if (_colorTexturePath.value() != "") {
_texture = ghoul::opengl::loadTexture(_colorTexturePath);
_texture = ghoul::opengl::loadTexture(absPath(_colorTexturePath));
if (_texture) {
LDEBUG("Loaded texture from '" << _colorTexturePath.value() << "'");
LDEBUG("Loaded texture from '" << absPath(_colorTexturePath) << "'");
_texture->uploadTexture();
}
}
+132 -132
View File
@@ -28,136 +28,143 @@
#include <openspace/scenegraph/scenegraph.h>
#include <openspace/util/camera.h>
#include "sgct.h"
//#include "sgct.h"
#include <ghoul/filesystem/filesystem.h>
namespace {
const std::string _loggerCat = "RenderEngine";
const std::string _loggerCat = "RenderEngine";
}
namespace openspace {
RenderEngine::RenderEngine() : _mainCamera(nullptr) , _sceneGraph(nullptr) {}
RenderEngine::~RenderEngine() {
delete _mainCamera;
delete _sceneGraph;
RenderEngine::RenderEngine()
: _mainCamera(nullptr)
, _sceneGraph(nullptr)
{
}
bool RenderEngine::initialize() {
RenderEngine::~RenderEngine()
{
delete _mainCamera;
}
bool RenderEngine::initialize()
{
// init camera and set temporary position and scaling
_mainCamera = new Camera();
_mainCamera->setScaling(glm::vec2(1.0, -8.0));
_mainCamera->setPosition(psc(0.0,0.0,1.499823,11.0));
// if master, setup interaction
if (sgct::Engine::instance()->isMaster()) {
OsEng.interactionHandler().setCamera(_mainCamera);
}
_mainCamera->setPosition(psc(0.0, 0.0, 1.499823, 11.0));
// initialize scenegraph
_sceneGraph = new SceneGraph;
_sceneGraph->loadFromModulePath(absPath("${SCENEPATH}"));
_sceneGraph->initialize();
// if master, setup interaction
if (sgct::Engine::instance()->isMaster())
OsEng.interactionHandler().setCamera(_mainCamera);
return true;
}
bool RenderEngine::initializeGL() {
bool RenderEngine::initializeGL()
{
sgct::SGCTWindow* wPtr = sgct::Engine::instance()->getActiveWindowPtr();
#define SGCT_WPTR sgct::Engine::instance()->getActiveWindowPtr()
using sgct_core::Viewport;
// TODO: Fix the power scaled coordinates in such a way that these values can be set
// TODO: Fix the power scaled coordinates in such a way that these values can be
// set
// to more realistic values
// set the close clip plane and the far clip plane to extreme values while in development
//sgct::Engine::instance()->setNearAndFarClippingPlanes(0.1f,100.0f);
sgct::Engine::instance()->setNearAndFarClippingPlanes(0.00001f,100.0f);
// calculating the maximum field of view for the camera, used to
// set the close clip plane and the far clip plane to extreme values while in
// development
// sgct::Engine::instance()->setNearAndFarClippingPlanes(0.1f,100.0f);
sgct::Engine::instance()->setNearAndFarClippingPlanes(0.00001f, 100.0f);
// calculating the maximum field of view for the camera, used to
// determine visibility of objects in the scene graph
if(SGCT_WPTR->isUsingFisheyeRendering()) {
if (wPtr->isUsingFisheyeRendering()) {
// fisheye mode, looking upwards to the "dome"
glm::vec4 upDirection(0, 1, 0, 0);
// fisheye mode, looking upwards to the "dome"
glm::vec4 viewdir(0,1,0,0);
// get the tilt and rotate the view
const float tilt = wPtr->getFisheyeTilt();
glm::mat4 tiltMatrix
= glm::rotate(glm::mat4(1.0f), tilt, glm::vec3(1.0f, 0.0f, 0.0f));
const glm::vec4 viewdir = tiltMatrix * upDirection;
// get the tilt and rotate the view
float tilt = SGCT_WPTR->getFisheyeTilt();
//tilt = tilt * 0.0174532925; // degrees to radians
glm::mat4 tiltMatrix = glm::rotate(glm::mat4(1.0f), tilt, glm::vec3(1.0f,0.0f,0.0f));
viewdir = tiltMatrix * viewdir;
// set the tilted view and the FOV
_mainCamera->setCameraDirection(glm::vec3(viewdir[0], viewdir[1], viewdir[2]));
_mainCamera->setMaxFov(wPtr->getFisheyeFOV());
} else {
// get corner positions, calculating the forth to easily calculate center
glm::vec3 corners[4];
corners[0] = wPtr->getCurrentViewport()->getViewPlaneCoords(
sgct_core::Viewport::LowerLeft);
corners[1] = wPtr->getCurrentViewport()->getViewPlaneCoords(
sgct_core::Viewport::UpperLeft);
corners[2] = wPtr->getCurrentViewport()->getViewPlaneCoords(
sgct_core::Viewport::UpperRight);
corners[3] = glm::vec3(corners[2][0], corners[0][1], corners[2][2]);
const glm::vec3 center = (corners[0] + corners[1] + corners[2] + corners[3])
/ 4.0f;
// set the tilted view and the FOV
_mainCamera->setCameraDirection(glm::vec3(viewdir[0],viewdir[1],viewdir[2]));
_mainCamera->setMaxFov(SGCT_WPTR->getFisheyeFOV());
} else {
// get corner positions, calculating the forth to easily calculate center
glm::vec3 corners[4];
corners[0] = SGCT_WPTR->getCurrentViewport()->getViewPlaneCoords(Viewport::LowerLeft);
corners[1] = SGCT_WPTR->getCurrentViewport()->getViewPlaneCoords(Viewport::UpperLeft);
corners[2] = SGCT_WPTR->getCurrentViewport()->getViewPlaneCoords(Viewport::UpperRight);
corners[3] = glm::vec3(corners[2][0],corners[0][1],corners[2][2]);
glm::vec3 center = (corners[0] + corners[1] + corners[2] + corners[3]) / 4.0f;
// set the eye position, useful during rendering
const glm::vec3 eyePosition
= sgct_core::ClusterManager::instance()->getUserPtr()->getPos();
// set the eye position, useful during rendering
const glm::vec3 eyePosition = sgct_core::ClusterManager::instance()->getUserPtr()->getPos();
// get viewdirection, stores the direction in the camera, used for culling
const glm::vec3 viewdir = glm::normalize(eyePosition - center);
_mainCamera->setCameraDirection(-viewdir);
_mainCamera->setLookUp(glm::vec3(0.0, 1.0, 0.0));
// get viewdirection, stores the direction in the camera, used for culling
glm::vec3 viewdir = glm::normalize(eyePosition- center);
_mainCamera->setCameraDirection(-viewdir);
_mainCamera->setLookUp(glm::vec3(0.0, 1.0, 0.0));
// set the initial fov to be 0.0 which means everything will be culled
float maxFov = 0.0f;
// set the initial fov to be 0.0 which means everything will be culled
float maxFov = 0.0f;
// for each corner
for (int i = 0; i < 4; ++i) {
// calculate radians to corner
glm::vec3 dir = glm::normalize(eyePosition - corners[i]);
float radsbetween = acos(glm::dot(viewdir, dir))
/ (glm::length(viewdir) * glm::length(dir));
// for each corner
for(int i = 0; i < 4; ++i) {
// the angle to a corner is larger than the current maxima
if (radsbetween > maxFov) {
maxFov = radsbetween;
}
}
_mainCamera->setMaxFov(maxFov);
}
// calculate radians to corner
glm::vec3 dir = glm::normalize(eyePosition- corners[i]);
float radsbetween = acos(glm::dot(viewdir, dir))/(glm::length(viewdir) * glm::length(dir));
// the angle to a corner is larger than the current maxima
if (radsbetween > maxFov) {
maxFov = radsbetween;
}
}
_mainCamera->setMaxFov(maxFov);
}
// successful init
return true;
// successful init
return true;
}
void RenderEngine::postSynchronizationPreDraw() {
// converts the quaternion used to rotation matrices
_mainCamera->compileViewRotationMatrix();
void RenderEngine::postSynchronizationPreDraw()
{
// converts the quaternion used to rotation matrices
_mainCamera->compileViewRotationMatrix();
// update and evaluate the scene starting from the root node
_sceneGraph->update();
_mainCamera->setCameraDirection(glm::vec3(0,0,-1));
_sceneGraph->evaluate(_mainCamera);
// update and evaluate the scene starting from the root node
_sceneGraph->update();
_mainCamera->setCameraDirection(glm::vec3(0, 0, -1));
_sceneGraph->evaluate(_mainCamera);
}
void RenderEngine::render() {
// SGCT resets certian settings
glEnable (GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
// setup the camera for the current frame
const glm::vec3 eyePosition = sgct_core::ClusterManager::instance()->getUserPtr()->getPos();
glm::mat4 view = glm::translate(glm::mat4(1.0), eyePosition); // make sure the eye is in the center
_mainCamera->setViewProjectionMatrix(sgct::Engine::instance()->getActiveModelViewProjectionMatrix()*view);
void RenderEngine::render()
{
// SGCT resets certain settings
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
// setup the camera for the current frame
const glm::vec3 eyePosition
= sgct_core::ClusterManager::instance()->getUserPtr()->getPos();
const glm::mat4 view
= glm::translate(glm::mat4(1.0),
eyePosition); // make sure the eye is in the center
_mainCamera->setViewProjectionMatrix(
sgct::Engine::instance()->getActiveModelViewProjectionMatrix() * view);
// render the scene starting from the root node
_sceneGraph->render(_mainCamera);
// render the scene starting from the root node
_sceneGraph->render(_mainCamera);
// Print some useful information on the master viewport
if (sgct::Engine::instance()->isMaster()) {
if (sgct::Engine::instance()->isMaster()) {
// Apple usually has retina screens
#ifdef __APPLE__
#define FONT_SIZE 18
@@ -165,51 +172,44 @@ void RenderEngine::render() {
#define FONT_SIZE 10
#endif
const glm::vec2 scaling = _mainCamera->getScaling();
const glm::vec3 viewdirection = _mainCamera->getViewDirection();
const psc position = _mainCamera->getPosition();
const glm::vec2 scaling = _mainCamera->getScaling();
const glm::vec3 viewdirection = _mainCamera->getViewDirection();
const psc position = _mainCamera->getPosition();
const psc origin = OsEng.interactionHandler().getOrigin();
const pss pssl = (position - origin).length();
Freetype::print(sgct_text::FontManager::instance()->getFont( "SGCTFont", FONT_SIZE ),
FONT_SIZE,
FONT_SIZE*10,
"Origin: (%.5f, %.5f, %.5f, %.5f)",
origin[0], origin[1], origin[2], origin[3]
);
Freetype::print(sgct_text::FontManager::instance()->getFont( "SGCTFont", FONT_SIZE ),
FONT_SIZE,
FONT_SIZE*8,
"Camera position: (%.5f, %.5f, %.5f, %.5f)",
position[0], position[1], position[2], position[3]
);
Freetype::print(sgct_text::FontManager::instance()->getFont( "SGCTFont", FONT_SIZE ),
FONT_SIZE,
FONT_SIZE*6,
"Distance to origin: (%.15f, %.2f)",
pssl[0], pssl[1]
);
Freetype::print(sgct_text::FontManager::instance()->getFont( "SGCTFont", FONT_SIZE ),
FONT_SIZE,
FONT_SIZE*4,
"View direction: (%.3f, %.3f, %.3f)",
viewdirection[0], viewdirection[1], viewdirection[2]
);
Freetype::print(sgct_text::FontManager::instance()->getFont( "SGCTFont", FONT_SIZE ),
FONT_SIZE,
FONT_SIZE*2,
"Scaling: (%.10f, %.2f)",
scaling[0], scaling[1]
);
const pss pssl = (position - origin).length();
}
Freetype::print(
sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE),
FONT_SIZE, FONT_SIZE * 10, "Origin: (%.5f, %.5f, %.5f, %.5f)", origin[0],
origin[1], origin[2], origin[3]);
Freetype::print(
sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE),
FONT_SIZE, FONT_SIZE * 8, "Camera position: (%.5f, %.5f, %.5f, %.5f)",
position[0], position[1], position[2], position[3]);
Freetype::print(
sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE),
FONT_SIZE, FONT_SIZE * 6, "Distance to origin: (%.15f, %.2f)", pssl[0],
pssl[1]);
Freetype::print(
sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE),
FONT_SIZE, FONT_SIZE * 4, "View direction: (%.3f, %.3f, %.3f)",
viewdirection[0], viewdirection[1], viewdirection[2]);
Freetype::print(
sgct_text::FontManager::instance()->getFont("SGCTFont", FONT_SIZE),
FONT_SIZE, FONT_SIZE * 2, "Scaling: (%.10f, %.2f)", scaling[0], scaling[1]);
}
}
SceneGraph* RenderEngine::sceneGraph() {
std::shared_ptr<SceneGraph> RenderEngine::sceneGraph()
{
// TODO custom assert (ticket #5)
assert(_sceneGraph);
return _sceneGraph;
}
void RenderEngine::setSceneGraph(std::shared_ptr<SceneGraph> sceneGraph)
{
_sceneGraph = sceneGraph;
}
} // namespace openspace
} // namespace openspace
+168 -152
View File
@@ -46,276 +46,290 @@
#include <string>
namespace {
std::string _loggerCat = "SceneGraph";
const std::string _loggerCat = "SceneGraph";
const std::string _rootNodeName = "Root";
const std::string _moduleExtension = ".mod";
namespace configuration {
const std::string modulesKey = "Modules";
const std::string cameraKey = "Camera";
const std::string nameKey = "Name";
const std::string parentKey = "Parent";
}
}
namespace openspace {
void printTree(SceneGraphNode* node, std::string pre = "") {
void printTree(SceneGraphNode* node, std::string pre = "")
{
LDEBUGC("Tree", pre << node->nodeName());
auto children = node->children();
for(auto child: children) {
const std::vector<SceneGraphNode*>& children = node->children();
for (SceneGraphNode* child : children)
printTree(child, pre + " ");
}
}
SceneGraph::SceneGraph(): _focus("Root"), _position("Root"), _root(nullptr) {}
SceneGraph::~SceneGraph() {
deinitialize();
}
bool SceneGraph::initialize() {
SceneGraph::SceneGraph()
: _focus("Root")
, _position("Root")
, _root(nullptr)
{
}
SceneGraph::~SceneGraph()
{
deinitialize();
}
bool SceneGraph::initialize()
{
LDEBUG("Initializing SceneGraph");
using ghoul::opengl::ShaderObject;
using ghoul::opengl::ProgramObject;
ProgramObject* po = nullptr;
if ( OsEng.ref().configurationManager().hasKey("pscShader") &&
OsEng.ref().configurationManager().getValue("pscShader", po)) {
if (OsEng.ref().configurationManager().hasKey("pscShader")
&& OsEng.ref().configurationManager().getValue("pscShader", po)) {
LWARNING("pscShader already in ConfigurationManager, deleting.");
delete po;
po = nullptr;
}
ShaderObject* powerscale_vs = new ShaderObject(ShaderObject::ShaderType::ShaderTypeVertex,
absPath("${SHADERS}/pscstandard_vs.glsl"),
"PS Vertex"
);
ShaderObject* powerscale_fs = new ShaderObject(ShaderObject::ShaderType::ShaderTypeFragment,
absPath("${SHADERS}/pscstandard_fs.glsl"),
"PS Fragment"
);
ShaderObject* powerscale_vs
= new ShaderObject(ShaderObject::ShaderType::ShaderTypeVertex,
absPath("${SHADERS}/pscstandard_vs.glsl"), "PS Vertex");
ShaderObject* powerscale_fs
= new ShaderObject(ShaderObject::ShaderType::ShaderTypeFragment,
absPath("${SHADERS}/pscstandard_fs.glsl"), "PS Fragment");
po = new ProgramObject;
po->attachObject(powerscale_vs);
po->attachObject(powerscale_fs);
if( ! po->compileShaderObjects())
if (!po->compileShaderObjects())
return false;
if( ! po->linkProgramObject())
if (!po->linkProgramObject())
return false;
OsEng.ref().configurationManager().setValue("pscShader", po);
ProgramObject* _fboProgram = new ProgramObject("RaycastProgram");
ShaderObject* vertexShader = new ShaderObject(ShaderObject::ShaderTypeVertex,
ShaderObject* vertexShader = new ShaderObject(ShaderObject::ShaderTypeVertex,
absPath("${SHADERS}/exitpoints.vert"));
ShaderObject* fragmentShader = new ShaderObject(ShaderObject::ShaderTypeFragment,
absPath("${SHADERS}/exitpoints.frag"));
_fboProgram->attachObject(vertexShader);
_fboProgram->attachObject(fragmentShader);
_fboProgram->compileShaderObjects();
_fboProgram->linkProgramObject();
ProgramObject* _twopassProgram = new ProgramObject("TwoPassProgram");
vertexShader = new ShaderObject(ShaderObject::ShaderTypeVertex,
ShaderObject* fragmentShader = new ShaderObject(
ShaderObject::ShaderTypeFragment, absPath("${SHADERS}/exitpoints.frag"));
_fboProgram->attachObject(vertexShader);
_fboProgram->attachObject(fragmentShader);
_fboProgram->compileShaderObjects();
_fboProgram->linkProgramObject();
ProgramObject* _twopassProgram = new ProgramObject("TwoPassProgram");
vertexShader = new ShaderObject(ShaderObject::ShaderTypeVertex,
absPath("${SHADERS}/twopassraycaster.vert"));
fragmentShader = new ShaderObject(ShaderObject::ShaderTypeFragment,
fragmentShader = new ShaderObject(ShaderObject::ShaderTypeFragment,
absPath("${SHADERS}/twopassraycaster.frag"));
_twopassProgram->attachObject(vertexShader);
_twopassProgram->attachObject(fragmentShader);
_twopassProgram->compileShaderObjects();
_twopassProgram->linkProgramObject();
_twopassProgram->setUniform("texBack", 0);
_twopassProgram->setUniform("texFront", 1);
_twopassProgram->setUniform("texVolume", 2);
_twopassProgram->attachObject(vertexShader);
_twopassProgram->attachObject(fragmentShader);
_twopassProgram->compileShaderObjects();
_twopassProgram->linkProgramObject();
_twopassProgram->setUniform("texBack", 0);
_twopassProgram->setUniform("texFront", 1);
_twopassProgram->setUniform("texVolume", 2);
ProgramObject* quad = new ProgramObject("Quad");
ShaderObject* quadv = new ShaderObject(ShaderObject::ShaderTypeVertex,
absPath("${SHADERS}/quadVert.glsl"));
ShaderObject* quadf = new ShaderObject(ShaderObject::ShaderTypeFragment,
absPath("${SHADERS}/quadFrag.glsl"));
quad->attachObject(quadv);
quad->attachObject(quadf);
quad->compileShaderObjects();
quad->linkProgramObject();
quad->setUniform("quadTex", 0);
ShaderObject* quadv = new ShaderObject(ShaderObject::ShaderTypeVertex,
absPath("${SHADERS}/quadVert.glsl"));
ShaderObject* quadf = new ShaderObject(ShaderObject::ShaderTypeFragment,
absPath("${SHADERS}/quadFrag.glsl"));
quad->attachObject(quadv);
quad->attachObject(quadf);
quad->compileShaderObjects();
quad->linkProgramObject();
quad->setUniform("quadTex", 0);
OsEng.ref().configurationManager().setValue("RaycastProgram", _fboProgram);
OsEng.ref().configurationManager().setValue("TwoPassProgram", _twopassProgram);
OsEng.ref().configurationManager().setValue("Quad", quad);
// Initialize all nodes
for(auto node: _nodes) {
for (auto node : _nodes) {
bool success = node->initialize();
if (success) {
if (success)
LDEBUG(node->nodeName() << " initialized successfully!");
} else {
else
LWARNING(node->nodeName() << " not initialized.");
}
}
// update the position of all nodes
update();
// Calculate the bounding sphere for the scenegraph
_root->calculateBoundingSphere();
_root->calculateBoundingSphere();
// set the camera position
auto focusIterator = _allNodes.find(_focus);
auto positionIterator = _allNodes.find(_position);
if(focusIterator != _allNodes.end() && positionIterator != _allNodes.end()) {
LDEBUG("Camera position is '"<< _position <<"', camera focus is '" << _focus << "'");
if (focusIterator != _allNodes.end() && positionIterator != _allNodes.end()) {
LDEBUG("Camera position is '" << _position << "', camera focus is '" << _focus
<< "'");
SceneGraphNode* focusNode = focusIterator->second;
SceneGraphNode* positionNode = positionIterator->second;
Camera* c = OsEng.interactionHandler().getCamera();
// TODO: Make distance depend on radius
// TODO: Set distance and camera direction in some more smart way
// TODO: Set scaling dependent on the position and distance
// set position for camera
psc cameraPosition = positionNode->getPosition();
cameraPosition += psc(0.0,0.0,1.0,2.0);
cameraPosition += psc(0.0, 0.0, 1.0, 2.0);
c->setPosition(cameraPosition);
c->setCameraDirection(glm::vec3(0,0,-1));
c->setScaling(glm::vec2(1.0,0.0));
c->setCameraDirection(glm::vec3(0, 0, -1));
c->setScaling(glm::vec2(1.0, 0.0));
// Set the focus node for the interactionhandler
OsEng.interactionHandler().setFocusNode(focusNode);
}
return true;
}
bool SceneGraph::deinitialize() {
bool SceneGraph::deinitialize()
{
// deallocate the scene graph. Recursive deallocation will occur
if(_root)
delete _root;
delete _root;
_root = nullptr;
_nodes.erase(_nodes.begin(), _nodes.end());
_allNodes.erase(_allNodes.begin(), _allNodes.end());
_focus = "";
_position = "";
return true;
}
void SceneGraph::update() {
for(auto node: _nodes) {
void SceneGraph::update()
{
for (auto node : _nodes)
node->update();
}
}
void SceneGraph::evaluate(Camera *camera) {
_root->evaluate(camera);
void SceneGraph::evaluate(Camera* camera)
{
_root->evaluate(camera);
}
void SceneGraph::render(Camera *camera) {
_root->render(camera);
void SceneGraph::render(Camera* camera)
{
_root->render(camera);
}
bool SceneGraph::loadFromModulePath(const std::string& path) {
bool SceneGraph::loadScene(const std::string& sceneDescriptionFilePath,
const std::string& defaultModulePath)
{
using ghoul::Dictionary;
using ghoul::lua::loadDictionaryFromFile;
LDEBUG("Loading scenegraph nodes");
if(_root != nullptr) {
if (_root != nullptr) {
LFATAL("Scenegraph already loaded");
return false;
}
std::string defaultScene = path + "/default.scene";
if( ! FileSys.fileExists(defaultScene)) {
LFATAL("Could not find 'default.scene' in '" << path << "'");
return false;
}
ghoul::Dictionary dictionary;
// initialize the root node
_root = new SceneGraphNode(ghoul::Dictionary());
_root->setName("Root");
_nodes.push_back(_root);
_allNodes.insert ( std::make_pair("Root", _root));
ghoul::lua::loadDictionaryFromFile(defaultScene, dictionary);
ghoul::Dictionary moduleDictionary;
if(dictionary.getValue("Modules", moduleDictionary)) {
auto keys = moduleDictionary.keys();
// initialize the root node
_root = new SceneGraphNode(Dictionary());
_root->setName(_rootNodeName);
_nodes.push_back(_root);
_allNodes.insert(std::make_pair("Root", _root));
Dictionary dictionary;
loadDictionaryFromFile(sceneDescriptionFilePath, dictionary);
Dictionary moduleDictionary;
if (dictionary.getValue(configuration::modulesKey, moduleDictionary)) {
std::vector<std::string> keys = moduleDictionary.keys();
std::sort(keys.begin(), keys.end());
for (auto key: keys) {
for (const std::string& key : keys) {
std::string moduleFolder;
if(moduleDictionary.getValue(key, moduleFolder)) {
loadModulesFromModulePath(path +"/"+moduleFolder);
}
if (moduleDictionary.getValue(key, moduleFolder))
loadModule(defaultModulePath + "/" + moduleFolder);
}
}
// TODO: Make it less hard-coded and more flexible when nodes are not found
ghoul::Dictionary cameraDictionary;
if(dictionary.getValue("Camera", cameraDictionary)) {
LDEBUG("Cameradictionary found");
Dictionary cameraDictionary;
if (dictionary.getValue(configuration::cameraKey, cameraDictionary)) {
LDEBUG("Camera dictionary found");
std::string focus;
std::string position;
if(cameraDictionary.hasKey("Focus") && cameraDictionary.getValue("Focus", focus)) {
if (cameraDictionary.hasKey("Focus")
&& cameraDictionary.getValue("Focus", focus)) {
auto focusIterator = _allNodes.find(focus);
if (focusIterator != _allNodes.end()) {
_focus = focus;
LDEBUG("Setting camera focus to '"<< _focus << "'");
LDEBUG("Setting camera focus to '" << _focus << "'");
}
else
LERROR("Could not find focus object '" << focus << "'");
}
if(cameraDictionary.hasKey("Position") && cameraDictionary.getValue("Position", position)) {
if (cameraDictionary.hasKey("Position")
&& cameraDictionary.getValue("Position", position)) {
auto positionIterator = _allNodes.find(position);
if (positionIterator != _allNodes.end()) {
_position = position;
LDEBUG("Setting camera position to '"<< _position << "'");
LDEBUG("Setting camera position to '" << _position << "'");
}
else
LERROR("Could not find object '" << position << "' to position camera");
}
}
return true;
}
void SceneGraph::loadModulesFromModulePath(const std::string& modulePath) {
void SceneGraph::loadModule(const std::string& modulePath)
{
auto pos = modulePath.find_last_of("/");
if (pos == modulePath.npos) {
LFATAL("Bad format for module path: " << modulePath);
LERROR("Bad format for module path: " << modulePath);
return;
}
std::string fullModule = modulePath + modulePath.substr(pos) + ".mod";
std::string fullModule = modulePath + modulePath.substr(pos) + _moduleExtension;
LDEBUG("Loading modules from: " << fullModule);
ghoul::Dictionary moduleDictionary;
ghoul::lua::loadDictionaryFromFile(fullModule, moduleDictionary);
auto keys = moduleDictionary.keys();
for (auto key: keys) {
std::vector<std::string> keys = moduleDictionary.keys();
for (const std::string& key : keys) {
ghoul::Dictionary singleModuleDictionary;
if(moduleDictionary.getValue(key, singleModuleDictionary)) {
if (moduleDictionary.getValue(key, singleModuleDictionary)) {
std::string moduleName;
if (singleModuleDictionary.getValue("Name", moduleName)) {
if (singleModuleDictionary.getValue(configuration::nameKey, moduleName)) {
std::string parentName;
if ( ! singleModuleDictionary.getValue("Parent", parentName)) {
if (!singleModuleDictionary.getValue(configuration::parentKey, parentName)) {
LWARNING("Could not find 'Parent' key, using 'Root'.");
parentName = "Root";
}
auto parentIterator = _allNodes.find(parentName);
if (parentIterator == _allNodes.end()) {
LFATAL("Could not find parent named '"<< parentName <<
"' for '" << moduleName << "'." <<
" Check module definition order. Skipping module.");
LFATAL("Could not find parent named '"
<< parentName << "' for '" << moduleName << "'."
<< " Check module definition order. Skipping module.");
continue;
}
// allocate SceneGraphNode and initialize with Dictionary
singleModuleDictionary.setValue("Path", modulePath);
SceneGraphNode* node = nullptr;
node = new SceneGraphNode(singleModuleDictionary);
if(node != nullptr) {
if (node != nullptr) {
// 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);
@@ -323,17 +337,19 @@ void SceneGraph::loadModulesFromModulePath(const std::string& modulePath) {
}
}
}
// Print the tree
printTree(_root);
}
void SceneGraph::printChildren() const {
void SceneGraph::printChildren() const
{
_root->print();
}
SceneGraphNode* SceneGraph::root() const {
SceneGraphNode* SceneGraph::root() const
{
return _root;
}
} // namespace openspace
} // namespace openspace