Change camera dictionary state representation to consider position, rotation and focus node.

This commit is contained in:
Kalle Bladin
2016-06-29 15:23:37 -04:00
parent 6cb82f691a
commit 35a095c448
7 changed files with 121 additions and 50 deletions

View File

@@ -14,6 +14,7 @@ end
function postInitialization()
openspace.setInteractionMode('GlobeBrowsingInteractionMode')
--openspace.restoreCameraStateFromFile("camera_lookat_planet.lua")
openspace.setPropertyValue("MilkyWay.renderable.transparency", 0.55)
openspace.setPropertyValue("MilkyWay.renderable.segments", 50)
@@ -27,8 +28,10 @@ return {
CommonFolder = "common",
Camera = {
Focus = "DebugGlobe",
Position = {1, 0, 0, 8},
Position = {41717685.600207, -0.000000, -0.000000},
Rotation = {0.031159, -0.713114, 0.015919, -0.700174},
},
Modules = {
"debugglobe",
"stars",

View File

@@ -160,6 +160,7 @@ public:
void setCamera(Camera* camera);
// Interaction mode setters
void setStateFromDictionary(const ghoul::Dictionary& cameraDict);
void setInteractionModeToOrbital();
void setInteractionModeToGlobeBrowsing();
@@ -175,6 +176,7 @@ public:
void update(double deltaTime);
// Accessors
ghoul::Dictionary getStateDictionary();
SceneGraphNode* const focusNode() const;
Camera* const camera() const;
const InputState& inputState() const;

View File

@@ -127,8 +127,6 @@ namespace openspace {
void serialize(std::ostream& os) const;
void deserialize(std::istream& is);
void setStateFromDictionary(const ghoul::Dictionary& cameraDict);
ghoul::Dictionary getStateDictionary();
/**
Handles SGCT's internal matrices. Also caches a calculated viewProjection
matrix. This is the data that is different for different cameras within

View File

@@ -22,8 +22,6 @@
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <openspace/interaction/interactionhandler.h>
//
#include <openspace/engine/openspaceengine.h>
#include <openspace/interaction/interactionhandler.h>
#include <openspace/query/query.h>
@@ -40,6 +38,10 @@
namespace {
const std::string _loggerCat = "InteractionHandler";
const std::string KeyFocus = "Focus";
const std::string KeyPosition = "Position";
const std::string KeyRotation = "Rotation";
}
#include "interactionhandler_lua.inl"
@@ -673,10 +675,12 @@ void InteractionHandler::setInteractionMode(std::shared_ptr<InteractionMode> int
void InteractionHandler::setInteractionModeToOrbital() {
setInteractionMode(_orbitalInteractionMode);
LINFO("Interaction mode set to 'OrbitalInteractionMode'");
}
void InteractionHandler::setInteractionModeToGlobeBrowsing() {
setInteractionMode(_globebrowsingInteractionMode);
LINFO("Interaction mode set to 'GlobeBrowsingInteractionMode'");
}
void InteractionHandler::lockControls() {
@@ -737,12 +741,58 @@ void InteractionHandler::keyboardCallback(Key key, KeyModifier modifier, KeyActi
}
}
void InteractionHandler::setStateFromDictionary(const ghoul::Dictionary& cameraDict) {
bool readSuccessful = true;
std::string focus;
glm::dvec3 cameraPosition;
glm::dvec4 cameraRotation; // Need to read the quaternion as a vector first.
readSuccessful &= cameraDict.getValue(KeyFocus, focus);
readSuccessful &= cameraDict.getValue(KeyPosition, cameraPosition);
readSuccessful &= cameraDict.getValue(KeyRotation, cameraRotation);
if (!readSuccessful) {
throw ghoul::RuntimeError(
"Position, Rotation and Focus need to be defined for camera dictionary.");
}
SceneGraphNode* node = sceneGraphNode(focus);
if (!node) {
throw ghoul::RuntimeError(
"Could not find a node in scenegraph called '" + focus + "'");
}
// Set state
setFocusNode(node);
_camera->setPositionVec3(cameraPosition);
_camera->setRotation(glm::dquat(
cameraRotation.x, cameraRotation.y, cameraRotation.z, cameraRotation.w));
}
ghoul::Dictionary InteractionHandler::getStateDictionary() {
glm::dvec3 cameraPosition;
glm::dquat quat;
glm::dvec4 cameraRotation;
cameraPosition = _camera->positionVec3();
quat = _camera->rotationQuaternion();
cameraRotation = glm::dvec4(quat.w, quat.x, quat.y, quat.z);
ghoul::Dictionary cameraDict;
cameraDict.setValue(KeyPosition, cameraPosition);
cameraDict.setValue(KeyRotation, cameraRotation);
cameraDict.setValue(KeyFocus, focusNode()->name());
return cameraDict;
}
void InteractionHandler::saveCameraStateToFile(const std::string& filepath) {
if (!filepath.empty()) {
auto fullpath = absPath(filepath);
LDEBUG("Saving camera position: " << filepath);
LINFO("Saving camera position: " << filepath);
ghoul::Dictionary cameraDict = _camera->getStateDictionary();
ghoul::Dictionary cameraDict = getStateDictionary();
// TODO : Should get the camera state as a dictionary and save the dictionary to
// a file in form of a lua state and not use ofstreams here.
@@ -752,12 +802,13 @@ void InteractionHandler::saveCameraStateToFile(const std::string& filepath) {
glm::dvec3 p = _camera->positionVec3();
glm::dquat q = _camera->rotationQuaternion();
ofs << "return {" << std::endl;;
ofs << " CameraPosition = {"
ofs << "return {" << std::endl;
ofs << " " << KeyFocus << " = " << "\"" << focusNode()->name() << "\"" << "," << std::endl;
ofs << " " << KeyPosition << " = {"
<< std::to_string(p.x) << ", "
<< std::to_string(p.y) << ", "
<< std::to_string(p.z) << "}," << std::endl;
ofs << " CameraRotation = {"
ofs << " " << KeyRotation << " = {"
<< std::to_string(q.w) << ", "
<< std::to_string(q.x) << ", "
<< std::to_string(q.y) << ", "
@@ -769,14 +820,14 @@ void InteractionHandler::saveCameraStateToFile(const std::string& filepath) {
}
void InteractionHandler::restoreCameraStateFromFile(const std::string& filepath) {
LDEBUG("Reading camera state from file: " << filepath);
LINFO("Reading camera state from file: " << filepath);
if (!FileSys.fileExists(filepath))
throw ghoul::FileNotFoundError(filepath, "CameraFilePath");
ghoul::Dictionary cameraDict;
try {
ghoul::lua::loadDictionaryFromFile(filepath, cameraDict);
_camera->setStateFromDictionary(cameraDict);
setStateFromDictionary(cameraDict);
_cameraUpdatedFromScript = true;
}
catch (ghoul::RuntimeError& e) {

View File

@@ -25,7 +25,6 @@
namespace openspace {
namespace luascriptfunctions {
/**
* \ingroup LuaScripts
* setOrigin():
@@ -139,7 +138,6 @@ int setInteractionMode(lua_State* L) {
else { // Default
return luaL_error(L, "Unknown interaction mode. default is 'OrbitalInteractionMode'");
}
return 0;
}

View File

@@ -198,6 +198,52 @@ bool Scene::loadSceneInternal(const std::string& sceneDescriptionFilePath) {
_graph.loadFromFile(sceneDescriptionFilePath);
// Initialize all nodes
for (SceneGraphNode* node : _graph.nodes()) {
try {
bool success = node->initialize();
if (success)
LDEBUG(node->name() << " initialized successfully!");
else
LWARNING(node->name() << " not initialized.");
}
catch (const ghoul::RuntimeError& e) {
LERRORC(_loggerCat + "(" + e.component + ")", e.what());
}
}
// update the position of all nodes
// TODO need to check this; unnecessary? (ab)
for (SceneGraphNode* node : _graph.nodes()) {
try {
node->update({ Time::ref().currentTime() });
}
catch (const ghoul::RuntimeError& e) {
LERRORC(e.component, e.message);
}
}
for (auto it = _graph.nodes().rbegin(); it != _graph.nodes().rend(); ++it)
(*it)->calculateBoundingSphere();
// Read the camera dictionary
ghoul::Dictionary cameraDictionary;
if (dictionary.getValue(KeyCamera, cameraDictionary)) {
OsEng.interactionHandler().setStateFromDictionary(cameraDictionary);
}
// explicitly update and sync the camera
Camera* c = OsEng.ref().renderEngine().camera();
c->preSynchronization();
c->postSynchronizationPreDraw();
// HOLY MOLY! This much code to read a camera state? Added the above code with
// functions to set camera state from a dictionary in interactio handler and commented
// away the below code.
// Also, the camera is now defined with a slightly different dictionary (no psc!) //KB
/*
// TODO: Make it less hard-coded and more flexible when nodes are not found
ghoul::Dictionary cameraDictionary;
if (dictionary.getValue(KeyCamera, cameraDictionary)) {
@@ -357,7 +403,7 @@ bool Scene::loadSceneInternal(const std::string& sceneDescriptionFilePath) {
// explicitly update and sync the camera
c->preSynchronization();
c->postSynchronizationPreDraw();
*/
for (SceneGraphNode* node : _graph.nodes()) {
std::vector<properties::Property*> properties = node->propertiesRecursive();

View File

@@ -25,6 +25,10 @@
// open space includes
#include <openspace/util/camera.h>
#include <openspace/util/syncbuffer.h>
#include <openspace/query/query.h>
#include <openspace/engine/openspaceengine.h>
#include <openspace/interaction/interactionhandler.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/vector_angle.hpp>
@@ -35,6 +39,10 @@ namespace openspace {
// CAMERA //
//////////////////////////////////////////////////////////////////////////////////////
namespace {
const std::string _loggerCat = "Camera";
}
const Camera::Vec3 Camera::_VIEW_DIRECTION_CAMERA_SPACE = Camera::Vec3(0, 0, -1);
const Camera::Vec3 Camera::_LOOKUP_VECTOR_CAMERA_SPACE = Camera::Vec3(0, 1, 0);
@@ -213,41 +221,6 @@ namespace openspace {
setPositionVec3(p);
setRotation(q);
}
void Camera::setStateFromDictionary(const ghoul::Dictionary& cameraDict) {
bool readSuccessful = true;
glm::dvec3 cameraPosition;
glm::dvec4 cameraRotation; // Need to read the quaternion as a vector first.
readSuccessful &= cameraDict.getValue("CameraPosition", cameraPosition);
readSuccessful &= cameraDict.getValue("CameraRotation", cameraRotation);
if (!readSuccessful) {
throw ghoul::RuntimeError(
"CameraPosition and/or CameraRotation not defined in dictionary.");
}
setPositionVec3(cameraPosition);
setRotation(glm::dquat(
cameraRotation.x, cameraRotation.y, cameraRotation.z, cameraRotation.w));
}
ghoul::Dictionary Camera::getStateDictionary() {
glm::dvec3 cameraPosition;
glm::dquat quat;
glm::dvec4 cameraRotation;
cameraPosition = positionVec3();
quat = rotationQuaternion();
cameraRotation = glm::dvec4(quat.w, quat.x, quat.y, quat.z);
ghoul::Dictionary cameraDict;
cameraDict.setValue("CameraPosition", cameraPosition);
cameraDict.setValue("CameraRotation", cameraRotation);
return cameraDict;
}
void Camera::preSynchronization() {
std::lock_guard<std::mutex> _lock(_mutex);