Save/Load JSON based Cameras in Proscene

edited June 2016 in Library Questions

https://www.dropbox.com/sh/2riljmpqfywibaa/AADseXkl0ms0WOYcjPSjKow7a?dl=0

^ that's a link to my code

Someone else was apparently able to save/load cameras in proscene from a file: https://forum.processing.org/one/topic/proscene-saving-loading-camera-settings-to-from-file-what-are-the-key-settings-of-a-camera.html

But I'm having trouble implementing their code. I have a feeling it's a basic problem that I just don't understand. When I run this:

import processing.data.JSONObject;

//this is how you save the camera, theoretically;
//save a Proscene scene current camera settings in as a JSON string in a file
void saveCamera(Scene scene, String fileName ) {
  processing.data.JSONObject json = new JSONObject();
  json.setFloat("fov", scene.camera().fieldOfView() );  
  setVector(json, "position", scene.camera().position() );
  setVector(json, "viewdirection", scene.camera().viewDirection() );
  setVector(json, "upvector", scene.camera().upVector() );
  saveJSONObject(json, fileName);
}
//Add a PVector as a string representation of a float array to a JSON object
void setVector(JSONObject json, String attributeName, Vec v) {
  json.setString( attributeName, Arrays.toString( new float[]{ v.x, v.y, v.z} ) );
}

//#####this is how you load the camera, theoretically;
void loadCamera(Scene scene, String fileName) {
  JSONObject json = loadJSONObject(fileName);
  scene.camera().setFieldOfView( json.getFloat("fov") );
  scene.camera().setUpVector( getVector(json, "upvector") );
  scene.camera().setViewDirection( getVector(json, "viewdirection") );
  scene.camera().setPosition( getVector(json, "position") );
}
//Parse a PVector from its string representation as an array of float in a JSON object
PVector getVector(JSONObject json, String attributeName) {
  String o =  json.getString(attributeName);
  String[] arr = o.substring(1, o.length()-1).split(", ");
  float[] f = new float[arr.length];
  int i = 0;
  for (String s : arr) {
    f[i] = Float.parseFloat(s);
    i++;
  }
  return new PVector(f[0], f[1], f[2]);
}

"Arrays" isn't pointing to anything, and I'm not sure what to do about it?

Answers

Sign In or Register to comment.