mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-05-25 05:48:59 -05:00
Save and restore camera state as lua tables.
This commit is contained in:
@@ -194,9 +194,8 @@ public:
|
||||
void mousePositionCallback(double x, double y);
|
||||
void mouseScrollWheelCallback(double pos);
|
||||
|
||||
void saveCameraPosition(const std::string& filepath = "");
|
||||
void restoreCameraPosition(const std::string& filepath = "");
|
||||
void setCameraState(const ghoul::Dictionary& cameraDict);
|
||||
void saveCameraStateToFile(const std::string& filepath);
|
||||
void restoreCameraStateFromFile(const std::string& filepath);
|
||||
|
||||
private:
|
||||
void setInteractionMode(std::shared_ptr<InteractionMode> interactionMode);
|
||||
|
||||
@@ -737,63 +737,52 @@ void InteractionHandler::keyboardCallback(Key key, KeyModifier modifier, KeyActi
|
||||
}
|
||||
}
|
||||
|
||||
void InteractionHandler::saveCameraPosition(const std::string& filepath) {
|
||||
|
||||
|
||||
void InteractionHandler::saveCameraStateToFile(const std::string& filepath) {
|
||||
if (!filepath.empty()) {
|
||||
auto fullpath = absPath(filepath);
|
||||
LDEBUG("Saving camera position: " << fullpath);
|
||||
LDEBUG("Saving camera position: " << filepath);
|
||||
|
||||
ghoul::Dictionary cameraDict = _camera->getStateDictionary();
|
||||
auto file = ghoul::filesystem::File(fullpath.c_str());
|
||||
|
||||
// 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.
|
||||
|
||||
std::ofstream ofs(fullpath.c_str());
|
||||
_camera->serialize(ofs);
|
||||
|
||||
glm::dvec3 p = _camera->positionVec3();
|
||||
glm::dquat q = _camera->rotationQuaternion();
|
||||
|
||||
ofs << "return {" << std::endl;;
|
||||
ofs << " CameraPosition = {"
|
||||
<< std::to_string(p.x) << ", "
|
||||
<< std::to_string(p.y) << ", "
|
||||
<< std::to_string(p.z) << "}," << std::endl;
|
||||
ofs << " CameraRotation = {"
|
||||
<< std::to_string(q.w) << ", "
|
||||
<< std::to_string(q.x) << ", "
|
||||
<< std::to_string(q.y) << ", "
|
||||
<< std::to_string(q.z) << "}," << std::endl;
|
||||
ofs << "}"<< std::endl;
|
||||
|
||||
ofs.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void InteractionHandler::restoreCameraPosition(const std::string& filepath) {
|
||||
|
||||
/*
|
||||
void InteractionHandler::restoreCameraStateFromFile(const std::string& filepath) {
|
||||
LDEBUG("Reading camera state from file: " << filepath);
|
||||
if (!FileSys.fileExists(filepath))
|
||||
throw ghoul::FileNotFoundError(filepath, "CameraFilePath");
|
||||
|
||||
ghoul::Dictionary cameraDict = _camera->getStateDictionary();
|
||||
ghoul::lua::loadDictionaryFromFile(filepath, cameraDict);
|
||||
|
||||
_camera->setStateFromDictionary(cameraDict);
|
||||
*/
|
||||
|
||||
|
||||
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);
|
||||
ghoul::Dictionary cameraDict;
|
||||
try {
|
||||
ghoul::lua::loadDictionaryFromFile(filepath, cameraDict);
|
||||
_camera->setStateFromDictionary(cameraDict);
|
||||
_cameraUpdatedFromScript = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void InteractionHandler::setCameraState(const ghoul::Dictionary& cameraDict) {
|
||||
glm::dvec3 cameraPosition;
|
||||
glm::dvec4 cameraRotation;
|
||||
cameraDict.getValue("CameraPosition", cameraPosition);
|
||||
cameraDict.getValue("CameraRotation", cameraRotation);
|
||||
catch (ghoul::RuntimeError& e) {
|
||||
LWARNING("Unable to set camera position");
|
||||
LWARNING(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
void InteractionHandler::resetKeyBindings() {
|
||||
@@ -830,16 +819,16 @@ scripting::ScriptEngine::LuaLibrary InteractionHandler::luaLibrary() {
|
||||
"Set the interaction mode for the camera"
|
||||
},
|
||||
{
|
||||
"saveCameraPosition",
|
||||
&luascriptfunctions::saveCameraPosition,
|
||||
"saveCameraStateToFile",
|
||||
&luascriptfunctions::saveCameraStateToFile,
|
||||
"string",
|
||||
"Save the current camera position to file"
|
||||
"Save the current camera state to file"
|
||||
},
|
||||
{
|
||||
"restoreCameraPosition",
|
||||
&luascriptfunctions::restoreCameraPosition,
|
||||
"restoreCameraStateFromFile",
|
||||
&luascriptfunctions::restoreCameraStateFromFile,
|
||||
"string",
|
||||
"Restore the camera position from file"
|
||||
"Restore the camera state from file"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -143,24 +143,24 @@ int setInteractionMode(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int restoreCameraPosition(lua_State* L) {
|
||||
int restoreCameraStateFromFile(lua_State* L) {
|
||||
using ghoul::lua::luaTypeToString;
|
||||
const std::string _loggerCat = "lua.setCameraPosition";
|
||||
const std::string _loggerCat = "lua.restoreCameraStateFromFile";
|
||||
|
||||
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);
|
||||
std::string cameraStateFilePath = luaL_checkstring(L, -1);
|
||||
|
||||
if (cameraPosFilePath.empty())
|
||||
if (cameraStateFilePath.empty())
|
||||
return luaL_error(L, "filepath string is empty");
|
||||
|
||||
OsEng.interactionHandler().restoreCameraPosition(cameraPosFilePath);
|
||||
OsEng.interactionHandler().restoreCameraStateFromFile(cameraStateFilePath);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int saveCameraPosition(lua_State* L) {
|
||||
int saveCameraStateToFile(lua_State* L) {
|
||||
using ghoul::lua::luaTypeToString;
|
||||
const std::string _loggerCat = "lua.setCameraPosition";
|
||||
|
||||
@@ -168,12 +168,12 @@ int saveCameraPosition(lua_State* L) {
|
||||
if (nArguments != 1)
|
||||
return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments);
|
||||
|
||||
std::string cameraPosFilePath = luaL_checkstring(L, -1);
|
||||
std::string cameraStateFilePath = luaL_checkstring(L, -1);
|
||||
|
||||
if (cameraPosFilePath.empty())
|
||||
if (cameraStateFilePath.empty())
|
||||
return luaL_error(L, "filepath string is empty");
|
||||
|
||||
OsEng.interactionHandler().saveCameraPosition(cameraPosFilePath);
|
||||
OsEng.interactionHandler().saveCameraStateToFile(cameraStateFilePath);
|
||||
}
|
||||
|
||||
#ifdef USE_OLD_INTERACTIONHANDLER
|
||||
|
||||
+11
-4
@@ -216,10 +216,17 @@ namespace openspace {
|
||||
|
||||
|
||||
void Camera::setStateFromDictionary(const ghoul::Dictionary& cameraDict) {
|
||||
bool readSuccessful = true;
|
||||
|
||||
glm::dvec3 cameraPosition;
|
||||
glm::dvec4 cameraRotation;
|
||||
cameraDict.getValue("CameraPosition", cameraPosition);
|
||||
cameraDict.getValue("CameraRotation", cameraRotation);
|
||||
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(
|
||||
@@ -233,7 +240,7 @@ namespace openspace {
|
||||
|
||||
cameraPosition = positionVec3();
|
||||
quat = rotationQuaternion();
|
||||
cameraRotation = glm::dvec4(quat.x, quat.y, quat.z, quat.w);
|
||||
cameraRotation = glm::dvec4(quat.w, quat.x, quat.y, quat.z);
|
||||
|
||||
ghoul::Dictionary cameraDict;
|
||||
cameraDict.setValue("CameraPosition", cameraPosition);
|
||||
|
||||
Reference in New Issue
Block a user