//includy definic funkci
include("paramevent.js");
include("loadscene.js");
include("runmodule.js");
include("visibility.js");
include("collisioncallback.js");
include("counters.js");
include("anim.js");
include("inputcallback.js");
include("commandevent.js");

// Test with different renderers, test with different offsets (set from JSScripting module GUI).
// Check especially, whether:
//		1) The background continues from the left window to the right window without any cracks.
//		2) The sphere continues from the left to the right window without any cracks.
//		3) The sphere reflects the background properly. Check for example:
//			- When the sphere is in front of you, it must reflect the path of the scene behind you (check on all windows)
//			- Check whether always (on all windows) things reflected of the left side of the sphere are to the left
//				from the sphere, and so for all other directions.
//				- For example: in the scene, you stand in the middle of some metal sculpture, made of many metal bars.
//					When you set the sphere between any two bars, it should reflect these bars on its corresponding
//					faces, and it should never show both bars on the same side.

var sceneID = loadScene("../data/test_scene/test_envmapping.fhs", 5000);
var scene = Scene.GetScene(sceneID);

// main camera
var camera1ID = renderScene(sceneID, 20000);

var near_plane = 0.1;
var far_plane = 100.0;
var offset = Vector3(0,0,0);
var corner2pa = Vector3(-100, -100,  100);
var corner2pb = Vector3(-100, -100, -100);
var corner2pc = Vector3(-100,  100,  100);
var corner3pa = corner2pb;
var corner3pb = Vector3( 100, -100, -100);
var corner3pc = Vector3(-100,  100, -100);
var eye_axis = Vector3(1, 0, 0);

var window_size = 300;

//---------------------
// Left window: looking left

var camera2ID = renderScene(sceneID, 20000);
scene.TranslateAbs(camera2ID, new Vector3(0, 0, 0));
scene.Move(camera2ID, camera1ID);
var evt = Event.GET_EVT_RENDER_FULLSCREEN_SET(1, 1, 0, 0, window_size, window_size, 0);//windowID=1, fsMode=1, posX, posY, sizeX, sizeY, RGB
evt.PostToKernel();
var evt = Event.GET_EVT_SCENE_CAM_SET_PERSPECTIVE_PARAMS(sceneID, camera2ID, near_plane, far_plane, offset, 0, 0, corner2pa, corner2pb, corner2pc, eye_axis);
evt.PostToKernel();

//---------------------
// Right window: looking forward

var camera3ID = renderScene(sceneID, 20000);
scene.TranslateAbs(camera3ID, new Vector3(0, 0, 0));
scene.Move(camera3ID, camera1ID);
var evt = Event.GET_EVT_RENDER_FULLSCREEN_SET(2, 1, window_size, 0, window_size, window_size, 0);
evt.PostToKernel();
var evt = Event.GET_EVT_SCENE_CAM_SET_PERSPECTIVE_PARAMS(sceneID, camera3ID, near_plane, far_plane, offset, 0, 0, corner3pa, corner3pb, corner3pc, eye_axis);
evt.PostToKernel();


// run scenegraph for testing
runModule("SceneGraph");
updateParamValue("SceneGraph", "sceneID", sceneID);

var defaultLeft = "" + offset.x + "," + offset.y + "," + offset.z;
var paramTextControlLeftID = REGISTER_PARAM_GUI_TEXTCONTROL("Left", defaultLeft, "Camera offset.");
function actionOnSetParam() {

    this.perform = function (evt) {
        if (evt.paramIdentifier == paramTextControlLeftID) {
            LOG("the control " + paramTextControlLeftID + " has new value " + evt.value);
            var values = evt.value.split(",")
            offset = new Vector3(parseFloat(values[0]), parseFloat(values[1]), parseFloat(values[2]));
            var evt = Event.GET_EVT_SCENE_CAM_SET_PERSPECTIVE_PARAMS(sceneID, camera2ID, near_plane, far_plane, offset, 0, 0, corner2pa, corner2pb, corner2pc, eye_axis);
            evt.PostToKernel();
            var evt = Event.GET_EVT_SCENE_CAM_SET_PERSPECTIVE_PARAMS(sceneID, camera3ID, near_plane, far_plane, offset, 0, 0, corner3pa, corner3pb, corner3pc, eye_axis);
            evt.PostToKernel();
        }
    };
}

REGISTER_LISTENER(Event.EVT_PARAM_SET, new actionOnSetParam());