mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-06 03:29:44 -06:00
Enable basic saving and restoring camera position between runs
This commit is contained in:
@@ -194,9 +194,14 @@ public:
|
||||
void mousePositionCallback(double x, double y);
|
||||
void mouseScrollWheelCallback(double pos);
|
||||
|
||||
void saveCameraPosition(const std::string& filepath = "");
|
||||
void restoreCameraPosition(const std::string& filepath = "");
|
||||
|
||||
private:
|
||||
void setInteractionMode(std::shared_ptr<InteractionMode> interactionMode);
|
||||
|
||||
bool _cameraUpdatedFromScript = false;
|
||||
|
||||
std::multimap<KeyWithModifier, std::string > _keyLua;
|
||||
|
||||
std::unique_ptr<InputState> _inputState;
|
||||
@@ -210,7 +215,6 @@ private:
|
||||
// Properties
|
||||
properties::StringProperty _origin;
|
||||
properties::StringProperty _coordinateSystem;
|
||||
|
||||
};
|
||||
|
||||
#endif // USE_OLD_INTERACTIONHANDLER
|
||||
|
||||
@@ -38,6 +38,8 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace openspace {
|
||||
/**
|
||||
This class still needs some more love. Suggested improvements:
|
||||
@@ -122,6 +124,9 @@ namespace openspace {
|
||||
void serialize(SyncBuffer* syncBuffer);
|
||||
void deserialize(SyncBuffer* syncBuffer);
|
||||
|
||||
void serialize(std::ostream& os) const;
|
||||
void deserialize(std::istream& is);
|
||||
|
||||
/**
|
||||
Handles SGCT's internal matrices. Also caches a calculated viewProjection
|
||||
matrix. This is the data that is different for different cameras within
|
||||
@@ -171,6 +176,7 @@ namespace openspace {
|
||||
const glm::mat4& projectionMatrix() const;
|
||||
[[deprecated("Replaced by Camera::SgctInternal::viewProjectionMatrix()")]]
|
||||
const glm::mat4& viewProjectionMatrix() const;
|
||||
|
||||
private:
|
||||
/**
|
||||
Class encapsulating data that needs to be synched between SGCT nodes.
|
||||
|
||||
@@ -36,6 +36,8 @@
|
||||
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace {
|
||||
const std::string _loggerCat = "InteractionHandler";
|
||||
}
|
||||
@@ -687,7 +689,13 @@ void InteractionHandler::unlockControls() {
|
||||
void InteractionHandler::update(double deltaTime) {
|
||||
ghoul_assert(_inputState != nullptr, "InputState cannot be null!");
|
||||
ghoul_assert(_camera != nullptr, "Camera cannot be null!");
|
||||
_currentInteractionMode->update(*_camera, *_inputState, deltaTime);
|
||||
|
||||
if (_cameraUpdatedFromScript) {
|
||||
_cameraUpdatedFromScript = false;
|
||||
}
|
||||
else {
|
||||
_currentInteractionMode->update(*_camera, *_inputState, deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
SceneGraphNode* const InteractionHandler::focusNode() const {
|
||||
@@ -728,6 +736,38 @@ void InteractionHandler::keyboardCallback(Key key, KeyModifier modifier, KeyActi
|
||||
}
|
||||
}
|
||||
|
||||
void InteractionHandler::saveCameraPosition(const std::string& filepath) {
|
||||
if (!filepath.empty()) {
|
||||
auto fullpath = absPath(filepath);
|
||||
LDEBUG("Saving camera position: " << fullpath);
|
||||
std::ofstream ofs(fullpath.c_str());
|
||||
_camera->serialize(ofs);
|
||||
ofs.close();
|
||||
}
|
||||
}
|
||||
|
||||
void InteractionHandler::restoreCameraPosition(const std::string& filepath) {
|
||||
if (!filepath.empty()) {
|
||||
auto fullpath = absPath(filepath);
|
||||
LDEBUG("Reading camera position: " << fullpath);
|
||||
std::ifstream ifs(fullpath.c_str());
|
||||
Camera c;
|
||||
c.deserialize(ifs);
|
||||
ifs.close();
|
||||
|
||||
// uff, need to do this ...
|
||||
c.preSynchronization();
|
||||
c.postSynchronizationPreDraw();
|
||||
|
||||
auto p = c.positionVec3();
|
||||
auto r = c.rotationQuaternion();
|
||||
|
||||
_camera->setPositionVec3(p);
|
||||
_camera->setRotation(r);
|
||||
_cameraUpdatedFromScript = true;
|
||||
}
|
||||
}
|
||||
|
||||
void InteractionHandler::resetKeyBindings() {
|
||||
_keyLua.clear();
|
||||
}
|
||||
@@ -760,6 +800,18 @@ scripting::ScriptEngine::LuaLibrary InteractionHandler::luaLibrary() {
|
||||
&luascriptfunctions::setInteractionMode,
|
||||
"string",
|
||||
"Set the interaction mode for the camera"
|
||||
},
|
||||
{
|
||||
"saveCameraPosition",
|
||||
&luascriptfunctions::saveCameraPosition,
|
||||
"string",
|
||||
"Save the current camera position to file"
|
||||
},
|
||||
{
|
||||
"restoreCameraPosition",
|
||||
&luascriptfunctions::restoreCameraPosition,
|
||||
"string",
|
||||
"Restore the camera position from file"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -143,6 +143,39 @@ int setInteractionMode(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int restoreCameraPosition(lua_State* L) {
|
||||
using ghoul::lua::luaTypeToString;
|
||||
const std::string _loggerCat = "lua.setCameraPosition";
|
||||
|
||||
int nArguments = lua_gettop(L);
|
||||
if (nArguments != 1)
|
||||
return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments);
|
||||
|
||||
std::string cameraPosFilePath = luaL_checkstring(L, -1);
|
||||
|
||||
if (cameraPosFilePath.empty())
|
||||
return luaL_error(L, "filepath string is empty");
|
||||
|
||||
OsEng.interactionHandler().restoreCameraPosition(cameraPosFilePath);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int saveCameraPosition(lua_State* L) {
|
||||
using ghoul::lua::luaTypeToString;
|
||||
const std::string _loggerCat = "lua.setCameraPosition";
|
||||
|
||||
int nArguments = lua_gettop(L);
|
||||
if (nArguments != 1)
|
||||
return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments);
|
||||
|
||||
std::string cameraPosFilePath = luaL_checkstring(L, -1);
|
||||
|
||||
if (cameraPosFilePath.empty())
|
||||
return luaL_error(L, "filepath string is empty");
|
||||
|
||||
OsEng.interactionHandler().saveCameraPosition(cameraPosFilePath);
|
||||
}
|
||||
|
||||
#ifdef USE_OLD_INTERACTIONHANDLER
|
||||
|
||||
/**
|
||||
|
||||
@@ -190,6 +190,22 @@ namespace openspace {
|
||||
_cachedViewDirection.isDirty = true;
|
||||
}
|
||||
|
||||
void Camera::serialize(std::ostream& os) const {
|
||||
Vec3 p = positionVec3();
|
||||
Quat q = rotationQuaternion();
|
||||
os << p.x << " " << p.y << " " << p.z << std::endl;
|
||||
os << q.x << " " << q.y << " " << q.z << " " << q.w << std::endl;
|
||||
}
|
||||
|
||||
void Camera::deserialize(std::istream& is) {
|
||||
Vec3 p;
|
||||
Quat q;
|
||||
is >> p.x >> p.y >> p.z;
|
||||
is >> q.x >> q.y >> q.z >> q.w;
|
||||
setPositionVec3(p);
|
||||
setRotation(q);
|
||||
}
|
||||
|
||||
void Camera::preSynchronization() {
|
||||
std::lock_guard<std::mutex> _lock(_mutex);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user