Proscene: Saving/Loading camera settings to/from file, what are the key settings of a camera?
in
Contributed Library Questions
•
2 months ago
Hello,
first of all thank you for the excellent Proscene!
I'm working on a project where I would like to set my camera interactively using the mouse and then save it to disk so I can re-load it later
So far I've tried the following:
A. Serializing the camera object : Creating a child class of Camera that implements java.io.Serializable did not work. I don't want to spend too much time on this feature, so I abandoned this option.
B. Saving a few key settings of the camera to a text file (JSON format): I thought saving the orientation quaternion and the position vector and the field of view would be enough, but when I load the camera from the JSON file and plug in the orientation, position and field of view in a new camera I don't get the expected result, which is that I would see the view I had saved from.
This is how the Camera is saved to file at the moment :
- JSONObject json = new JSONObject();
- json.setFloat("fov", camera.fieldOfView() );
- json.setString("orientation", Arrays.toString( new float[]{ camera.orientation().w, camera.orientation().x, camera.orientation().y, camera.orientation().z } ) );
- json.setString("position", Arrays.toString( new float[]{ camera.position().x, camera.position().y, camera.position().z} ) );
- saveJSONObject(json, fileName);
A sample JSON file:
- {
- "position": "[96.02282, -69.49055, 71.95985]",
- "orientation": "[0.88145006, 0.19984764, 0.4224881, -0.06790122]",
- "fov": 0.2617993950843811
- }
And how a Camera is loaded back from file:
- JSONObject json = loadJSONObject(fileName);
- fov = json.getFloat("fov");
- //Parse orientation quaternion
- String o = json.getString("orientation");
- String[] arr = o.substring(1,o.length()-1).split(", ");
- orientation = new float[arr.length];
- int i = 0;
- for(String s : arr){
- orientation[i] = Float.parseFloat(s);
- }
- //Parse position vector
- o = json.getString("position");
- arr = o.substring(1,o.length()-1).split(", ");
- println(arr);
- position = new float[arr.length];
- i = 0;
- for(String s : arr){
- position[i] = Float.parseFloat(s);
- }
- camera = new Camera(scene);
- camera.setFieldOfView(fov);
- camera.setPosition(new PVector(position[0], position[1], position[2]) );
- camera.setOrientation(new Quaternion(orientation, false) );
Obviously I'm missing a few key parameters to re-create the Camera like it was saved. Would anybody know what these parameters would be?
Can you spot any other issue with this approach?
Regards.
1