Finalized first draft for the SceneGraph

- Refactored several files
- Implemented PositionInformation subclasses
- Added standard shaders in the shader directory
- Removed SceneGraphLoader, now integrated in the SceneGraph

- TODO: Further testing and improvements to the PowerScale coordinate
system
This commit is contained in:
Jonas Strandstedt
2014-03-13 11:21:30 -04:00
parent 9833c3c8d4
commit b1eab2cf03
29 changed files with 897 additions and 833 deletions
+64 -6
View File
@@ -2,25 +2,81 @@
// open space includes
#include "renderableplanet.h"
#include <ghoul/opengl/texturereader.h>
#include <ghoul/filesystem/filesystem.h>
#include <openspaceengine.h>
#include <sgct.h>
namespace {
std::string _loggerCat = "RenderablePlanet";
}
namespace openspace {
RenderablePlanet::RenderablePlanet(): programObject_(nullptr), texture_(nullptr) {}
RenderablePlanet::RenderablePlanet(): programObject_(nullptr), texture_(nullptr),
planet_(nullptr) {}
RenderablePlanet::~RenderablePlanet() {
delete planet_;
}
bool RenderablePlanet::initialize() {
if (programObject_ == nullptr) {
OsEng.ref().configurationManager().getValue("pscShader", programObject_);
}
if (programObject_ == nullptr) {
return false;
}
return true;
}
bool RenderablePlanet::initializeWithDictionary(ghoul::Dictionary* dictionary) {
if ( ! initialize()) {
return false;
}
double value = 1.0f, exponent= 0.0f;
double segments = 20.0;
if(dictionary->hasKey("Geometry.Radius.1"))
dictionary->getValue("Geometry.Radius.1", value);
if(dictionary->hasKey("Geometry.Radius.2"))
dictionary->getValue("Geometry.Radius.2", exponent);
if(dictionary->hasKey("Geometry.Segments"))
dictionary->getValue("Geometry.Segments", segments);
// create the power scaled scalar
pss planetSize(value, exponent);
setBoundingSphere(planetSize);
// get path if available
std::string path;
dictionary->getValue("Path", path);
if(dictionary->hasKey("Textures.Color")) {
std::string texturePath;
dictionary->getValue("Textures.Color", texturePath);
std::string fullpath = path + "/" + texturePath;
texture_ = ghoul::opengl::loadTexture(fullpath);
if (texture_) {
LDEBUG("Loaded texture from '" << fullpath <<"'");
texture_->uploadTexture();
}
}
planet_ = new PowerScaledSphere(pss(value, exponent), static_cast<int>(segments));
return true;
}
void RenderablePlanet::setProgramObject(ghoul::opengl::ProgramObject *programObject = nullptr) {
assert(programObject) ;
assert(programObject);
programObject_ = programObject;
}
@@ -34,7 +90,7 @@ void RenderablePlanet::render(const Camera *camera, const psc &thisPosition) {
// check so that the shader is set
assert(programObject_);
assert(texture_);
// activate shader
programObject_->activate();
@@ -42,9 +98,11 @@ void RenderablePlanet::render(const Camera *camera, const psc &thisPosition) {
psc currentPosition = thisPosition;
psc campos = camera->getPosition();
glm::mat4 camrot = camera->getViewRotationMatrix();
pss scaling = camera->getScaling();
// scale the planet to appropriate size since the planet is a unit sphere
glm::mat4 transform = glm::mat4(1);
transform = glm::rotate(transform, 4.1f*static_cast<float>(sgct::Engine::instance()->getTime()), glm::vec3(0.0f, 1.0f, 0.0f));
// setup the data to the shader
programObject_->setUniform("ViewProjection", camera->getViewProjectionMatrix());
@@ -52,9 +110,9 @@ void RenderablePlanet::render(const Camera *camera, const psc &thisPosition) {
programObject_->setUniform("campos", campos.getVec4f());
programObject_->setUniform("objpos", currentPosition.getVec4f());
programObject_->setUniform("camrot", camrot);
programObject_->setUniform("scaling", camera->getScaling());
programObject_->setUniform("scaling", scaling.getVec2f());
//// if texture is availible, use it
// Bind texture
glActiveTexture(GL_TEXTURE0);
texture_->bind();
programObject_->setUniform("texture1", 0);
+4 -6
View File
@@ -3,11 +3,11 @@
// open space includes
#include "renderable.h"
#include "util/planet.h"
#include <util/powerscaledsphere.h>
// ghoul includes
#include "ghoul/opengl/programobject.h"
#include "ghoul/opengl/texture.h"
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/texture.h>
namespace openspace {
@@ -30,9 +30,7 @@ public:
private:
ghoul::opengl::ProgramObject *programObject_;
ghoul::opengl::Texture *texture_;
// double rad_;
Planet *planet_;
PowerScaledSphere *planet_;
};
} // namespace openspace
+79 -153
View File
@@ -26,7 +26,6 @@
#include "openspaceengine.h"
#include "scenegraph/scenegraph.h"
#include "scenegraph/scenegraphloader.h"
#include "util/camera.h"
#include "sgct.h"
@@ -44,64 +43,58 @@ RenderEngine::~RenderEngine() {
}
bool RenderEngine::initialize() {
// init camera and set position
// 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)); // about the distance from the sun to our moon, will be overritten by the scenegraphloader
_mainCamera->setPosition(psc(0.0,0.0,1.499823,11.0));
// if master, setup interaction
if (sgct::Engine::instance()->isMaster()) {
OsEng.interactionHandler().setCamera(_mainCamera);
// init interactionhandler and mouse interaction
//keyboardControl_ = new KeyboardExternalControl(RELATIVE_PATH"pyinput/keyboard.py");
//mouseControl_ = new MouseExternalControl(RELATIVE_PATH"pyinput/mouse.py");
//InteractionHandler::ref().addExternalControl(mouseControl_); // the interactionhandler is deallocating the object when it terminates
//InteractionHandler::ref().addExternalControl(keyboardControl_); // the interactionhandler is deallocating the object when it terminates
}
// init scenegraph
// initialize scenegraph
_sceneGraph = new SceneGraph;
_sceneGraph->initialize();
//_sceneGraph = loadSceneGraph(sceneGraph);
return true;
}
bool RenderEngine::initializeGL() {
// GL settings
glEnable (GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// 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::setNearAndFarClippingPlanes(0.1f,10000.0f);
//sgct::Engine::getPtr()->setNearAndFarClippingPlanes(0.1f,10000.0f);
// calculating the maximum field of view for the camera, used to determine visibility of objects in the scene graph
if(sgct::Engine::instance()->getWindowPtr(0)->isUsingFisheyeRendering()) {
#define SGCT_WPTR sgct::Engine::instance()->getWindowPtr(0)
using sgct_core::Viewport;
// 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
// determine visibility of objects in the scene graph
if(SGCT_WPTR->isUsingFisheyeRendering()) {
// fisheye mode, looking upwards to the "dome"
glm::vec4 viewdir(0,1,0,0);
// get the tilt and rotate the view
float tilt = sgct::Engine::instance()->getWindowPtr(0)->getFisheyeTilt();
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(sgct_core::SGCTSettings::Instance()->getFisheyeFOV());
_mainCamera->setMaxFov(sgct::Engine::instance()->getWindowPtr(0)->getFisheyeFOV());
_mainCamera->setMaxFov(SGCT_WPTR->getFisheyeFOV());
} else {
// get corner positions, calculating the forth to easily calculate center
// get corner positions, calculating the forth to easily calculate center
glm::vec3 corners[4];
corners[0] = sgct::Engine::instance()->getWindowPtr(0)->getCurrentViewport()->getViewPlaneCoords(sgct_core::Viewport::LowerLeft);
corners[1] = sgct::Engine::instance()->getWindowPtr(0)->getCurrentViewport()->getViewPlaneCoords(sgct_core::Viewport::UpperLeft);
corners[2] = sgct::Engine::instance()->getWindowPtr(0)->getCurrentViewport()->getViewPlaneCoords(sgct_core::Viewport::UpperRight);
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;
@@ -129,7 +122,7 @@ bool RenderEngine::initializeGL() {
}
_mainCamera->setMaxFov(maxFov);
}
// successful init
return true;
}
@@ -140,52 +133,73 @@ void RenderEngine::postSynchronizationPreDraw() {
_mainCamera->compileViewRotationMatrix();
// update and evaluate the scene starting from the root node
//_sceneGraph->update();
//_sceneGraph->evaluate(_mainCamera);
_sceneGraph->update();
_mainCamera->setCameraDirection(glm::vec3(0,0,-1));
_sceneGraph->evaluate(_mainCamera);
}
void RenderEngine::render() {
// preparing the camera can only be done in the render function
// since the SGCT get matrix functions is only valid in the render function
glm::mat4 projection = sgct::Engine::instance()->getActiveProjectionMatrix();
glm::mat4 view = sgct::Engine::instance()->getActiveViewMatrix();
const glm::vec3 eyePosition = sgct_core::ClusterManager::instance()->getUserPtr()->getPos();
view = glm::translate(view, eyePosition); // make sure the eye is in the center
// SGCT resets certian settings
glEnable (GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
// setup the camera for the current frame
//_mainCamera->setViewProjectionMatrix(projection*view);
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);
// render the scene starting from the root node
//_sceneGraph->render(_mainCamera);
/*
_sceneGraph->render(_mainCamera);
// Print some useful information on the master viewport
if (sgct::Engine::instance()->isMaster()) {
// Apple usually has retina screens
#ifdef __APPLE__
#define FONT_SIZE 18
#else
#define FONT_SIZE 10
#endif
const glm::vec2 scaling = _mainCamera->getScaling();
const glm::vec3 viewdirection = _mainCamera->getViewDirection();
const psc position = _mainCamera->getPosition();
Freetype::print(sgct_text::FontManager::instance()->getFont( "SGCTFont", 10 ), 10, 50,
"Position: (%.5f, %.5f, %.5f, %.5f)", position[0], position[1], position[2], position[3]
);
Freetype::print(sgct_text::FontManager::instance()->getFont( "SGCTFont", 10 ), 10, 35,
"View direction: (%.3f, %.3f, %.3f)", viewdirection[0], viewdirection[1], viewdirection[2]
);
Freetype::print(sgct_text::FontManager::instance()->getFont( "SGCTFont", 10 ), 10, 20,
"Scaling: (%.10f, %.2f)", scaling[0], scaling[1]
);
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]
);
psc campos = _mainCamera->getPosition();
psc origin = OsEng.interactionHandler().getOrigin();
//psc campos = InteractionHandler::ref().getCamera()->getPosition();
//psc origin = InteractionHandler::ref().getOrigin();
psc relative = campos - origin;
pss pssl = relative.length();
//mainCamera_->setScaling(glm::vec2(pssl[0], -pssl[1]+6));
//mainCamera_->setScaling(glm::vec2(3000.0, -11.0f));
Freetype::print(sgct_text::FontManager::instance()->getFont( "SGCTFont", 10 ), 10, 65,
"Distance to origin: (%.15f, %.2f)", pssl[0], pssl[1]
);
}
*/
}
SceneGraph* RenderEngine::sceneGraph() {
@@ -195,92 +209,4 @@ SceneGraph* RenderEngine::sceneGraph() {
}
/*
void RenderEngine::keyboardCallback(int key, int action) {
const double speed = 0.75;
if (key == 'S') {
double dt = InteractionHandler::ref().getDt();
glm::vec3 euler(speed * dt, 0.0, 0.0);
glm::quat rot = glm::quat(euler);
InteractionHandler::ref().orbit(rot);
}
if (key == 'W') {
double dt = InteractionHandler::ref().getDt();
glm::vec3 euler(-speed * dt, 0.0, 0.0);
glm::quat rot = glm::quat(euler);
InteractionHandler::ref().orbit(rot);
}
if (key == 'A') {
double dt = InteractionHandler::ref().getDt();
glm::vec3 euler(0.0, -speed * dt, 0.0);
glm::quat rot = glm::quat(euler);
InteractionHandler::ref().orbit(rot);
}
if (key == 'D') {
double dt = InteractionHandler::ref().getDt();
glm::vec3 euler(0.0, speed * dt, 0.0);
glm::quat rot = glm::quat(euler);
InteractionHandler::ref().orbit(rot);
}
if (key == 262) {
double dt = InteractionHandler::ref().getDt();
glm::vec3 euler(0.0, speed * dt, 0.0);
glm::quat rot = glm::quat(euler);
InteractionHandler::ref().rotate(rot);
}
if (key == 263) {
double dt = InteractionHandler::ref().getDt();
glm::vec3 euler(0.0, -speed * dt, 0.0);
glm::quat rot = glm::quat(euler);
InteractionHandler::ref().rotate(rot);
}
if (key == 264) {
double dt = InteractionHandler::ref().getDt();
glm::vec3 euler(speed * dt, 0.0, 0.0);
glm::quat rot = glm::quat(euler);
InteractionHandler::ref().rotate(rot);
}
if (key == 265) {
double dt = InteractionHandler::ref().getDt();
glm::vec3 euler(-speed * dt, 0.0, 0.0);
glm::quat rot = glm::quat(euler);
InteractionHandler::ref().rotate(rot);
}
if (key == 'R') {
double dt = InteractionHandler::ref().getDt();
pss dist(3 * -speed * dt, 8.0);
InteractionHandler::ref().distance(dist);
}
if (key == 'F') {
double dt = InteractionHandler::ref().getDt();
pss dist(3 * speed * dt, 8.0);
InteractionHandler::ref().distance(dist);
}
if (key == '1') {
SceneGraphNode* earth = sceneGraph_->root()->get("sun");
InteractionHandler::ref().setFocusNode(earth);
InteractionHandler::ref().getCamera()->setPosition(earth->getWorldPosition() + psc(0.0, 0.0, 0.5, 10.0));
InteractionHandler::ref().getCamera()->setCameraDirection(glm::vec3(0.0, 0.0, -1.0));
}
if (key == '2') {
SceneGraphNode* earth = sceneGraph_->root()->get("earth");
InteractionHandler::ref().setFocusNode(earth);
InteractionHandler::ref().getCamera()->setPosition(earth->getWorldPosition() + psc(0.0, 0.0, 1.0, 8.0));
InteractionHandler::ref().getCamera()->setCameraDirection(glm::vec3(0.0, 0.0, -1.0));
}
if (key == '3') {
SceneGraphNode* earth = sceneGraph_->root()->get("moon");
InteractionHandler::ref().setFocusNode(earth);
InteractionHandler::ref().getCamera()->setPosition(earth->getWorldPosition() + psc(0.0, 0.0, 0.5, 8.0));
InteractionHandler::ref().getCamera()->setCameraDirection(glm::vec3(0.0, 0.0, -1.0));
}
}
*/
} // namespace openspace