//includy definic funkci
include("paramevent.js");
include("loadscene.js");
include("commandevent.js");

command("usemodule RayTracer");

var sceneID;
var matrix = Matrix();
var userCommand = "";





if (computerName() == "SKDATVWG1683") {//Tonda
    updateParamValue("RayTracer", "maxLights", "0");
    updateParamValue("RayTracer", "maxRecursionDepth", "0");
    updateParamValue("RayTracer", "shadows", "0");
    updateParamValue("RayTracer", "trianglesPerLeaf", "1");
    updateParamValue("RayTracer", "optimization", "0,0");
    updateParamValue("RayTracer", "fullPolarization", "0");
    updateParamValue("RayTracer", "drawTimeouts", "50,100");
    
    sceneID = loadScene("../data/renderGl2_testScenes/sponza/sponza.obj", 100000);
    matrix = Matrix(1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 25, 0, 1);//sponza - top
    //matrix = Matrix(0, 0, -1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 15, 2, 0, 1);//sponza - floor
    //
    //sceneID = loadScene("C:/data/Tonda/vrut/trunk/data/renderGl2_testScenes/sponza/sponza.fhs", 3000);
    //sceneID = loadScene("C:/data/Tonda/vrut/trunk/data/Tracks/bal2_skoda_navigace/bal2_skoda_navigace.fhs", 30000);
    //sceneID = loadScene("C:/data/cave/sk26x_odlesky/sk26x_odlesky.fhs", 300000);
    //sceneID = loadScene("C:/data/cave/sk26x_odlesky/sk26x_odlesky_tmp.fhs", 300000);
    //sceneID = loadScene("C:/data/cave/sk48_odlesky/SK48_odlesky_02.fhs", 30000);
    //sceneID = loadScene("C:/data/cave/sk371_sitzkiste/sk371_sitzkiste_test.fhs", 30000);
    //sceneID = loadScene("/home/tonda/workspace/vrut/trunk/data/cave/DataVS/20091204_sk250+motor_VSkola_2.fhs", 30000);
    //sceneID = loadScene("D:/data/vrut/DataVS/20091204_sk250+motor_VSkola_2.fhs", sceneID, 30000);
    //loadInto("D:/data/vrut/DataVS/fabia_lights.fhs", sceneID, 5000);
    //*/
    /*updateParamValue("RayTracer", "shadows", "0");
    updateParamValue("RayTracer", "headlight", "0");
    updateParamValue("RayTracer", "optimization", "10,0");
    updateParamValue("RayTracer", "drawTimeouts", "100,100");
    updateParamValue("RayTracer", "maxLights", "6");
    updateParamValue("RayTracer", "maxRecursionDepth", "4");
    updateParamValue("RayTracer", "fullPolarization", "0");
    var sceneID = loadScene("C:/data/cave/sk371_sitzkiste/sk371_sitzkiste_03.fhs", 30000);//*/

    //matrix = Matrix(0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1380, -330, 920, 1);//sk26x ridic
    //matrix = Matrix(1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 1189.2198, -905.73272, 848.10247, 1);//sk371_sitzkiste_test
    //matrix = Matrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1370000, -1250000, 5600000, 1);//bal2_skoda_navigace
    /*
    updateParamValue("RayTracer", "MaxLights", "1");
    updateParamValue("RayTracer", "MaxRecursionDepth", "0");
    updateParamValue("RayTracer", "Shadows", "0");
    updateParamValue("RayTracer", "initialStep", "1x1");
    updateParamValue("RayTracer", "drawTimeouts", "300,100");
    updateParamValue("RayTracer", "headlight", "0");
    //updateParamValue("Navigation", "nearPlane", "0.01");
    
    //var sceneID = loadScene("../data/test_scene/cornellbox.wrl", 0);
    //var sceneID = loadScene("../data/raytracer/fabia/fabia2b-anim0.obj", 50000);
    sceneID = loadScene("D:/data/vrut/DataVS/20091204_sk250+motor_VSkola_2.fhs", sceneID, 30000);
    loadInto("D:/data/vrut/DataVS/fabia_lights.fhs", sceneID, 5000);

    matrix = Matrix(-0.3, 1, -0.1, 0, -0.3, 0, 0.9, 0, 0.9, 0.3, 0.3, 0, 1360.9, 39.8, 803.6, 1)
    */
}
else if (computerName() == "stroj") {//Tonda home
    updateParamValue("RayTracer", "maxLights", "4");
    updateParamValue("RayTracer", "maxRecursionDepth", "4");
    updateParamValue("RayTracer", "shadows", "0");
    updateParamValue("RayTracer", "trianglesPerLeaf", "1");
    updateParamValue("RayTracer", "optimization", "4,4");
    updateParamValue("RayTracer", "fullPolarization", "0");
    updateParamValue("RayTracer", "drawTimeouts", "50,100");

    //sceneID = loadScene("../data/renderGl2_testScenes/sponza/sponza.obj", 3000);
    //matrix = Matrix(1, 0, 0, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 25, 0, 1);//sponza - top
    //matrix = Matrix(0, 0, -1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 15, 2, 0, 1);//sponza - floor

    //sceneID = loadScene("../data/renderGl2_testScenes/sibenik/sibenik.obj", 3000);
    //matrix = Matrix(0,0,1,0,0,1,0,0,-1,0,0,0,-5,-12,0,1);//sibenik
    
    sceneID = loadScene("../data/renderGl2_testScenes/conference/conference.obj", 3000);
    matrix = Matrix(0,0,1,0,0,1,0,0,-1,0,0,0,-250,300,-130,1);//conference

    //sceneID = loadScene("/home/tonda/workspace/vrut/trunk/data/cave/DataVS/20091204_sk250+motor_VSkola_2.fhs", 30000);
    //matrix = Matrix(0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1380, -330, 920, 1);//sk26x ridic
}
else if (computerName() == "-SKDATVWG1683" || computerName() == "SHIELD") {//Jirka

   if (0) {
    var sceneID = loadScene("../data/girt/scenes/sponza/sponza.obj", 50000);
    loadInto("../data/girt/scenes/sponza/lights2.fhs", sceneID, 0);
		matrix = Matrix(0.0836458, -0.026681576, 0.99611768, 0,
										-0.1718059, 0.98426208, 0.04079254, 0,
										-0.98153992, -0.17455334, 0.077745416, 0,
										-904.27404, 252.90036, -13.286891, 1);
		}
		if (0) {
		    var sceneID = loadScene("../data/Fabia2/fabia2.fhs", 50000);
		    loadInto("../data/Fabia2/fabia_lights.fhs", sceneID, 0);
		    matrix = Matrix(-0.3, 1, -0.1, 0, -0.3, 0, 0.9, 0, 0.9, 0.3, 0.3, 0, 1360.9, 39.8, 803.6, 1)

	 }
		if (0) {
		    var sceneID = loadScene("../data/test_scene/cornell-box-emission.fhs", 50000);
		    //loadInto("../data/girt/scenes/cornellBox/lights.fhs", sceneID);
		    matrix = Matrix(1, 0, 0, 0,
												0, 1, 0, 0,
												0, 0, 1, 0,
												0, 2.2, 7.5, 1)

	 }

    		if (1) {
		    var sceneID = loadScene("../data/Tracks/krizovatka/krizovatka-all.vrut", 50000);


		    matrix = Matrix(0.9731608, 0.22385054, -0.053107264, 0,
				    -0.010532049, 0.27393964, 0.96166728, 0,
				    0.22981542, -0.93530984, 0.26894712, 0,
				    -23605.263, -9936.4472, 2155.0625, 1);

		    
	 }

    if (0) {
		    var sceneID = loadScene("../data/Head/head.OBJ", 50000);
		    loadInto("../data/girt/scenes/cornellBox/lights.fhs", sceneID);
		    matrix = Matrix(1, 0, 0, 0,
												0, 1, 0, 0,
												0, 0, 1, 0,
												0, 0, 1, 1)
	
	 }

			if (0) {
		    var sceneID = loadScene("../data/test_scene/bump-test.fhs", 50000);
		    matrix = Matrix(1, 0, 0, 0,
												0, 1, 0, 0,
												0, 0, 1, 0,
												0, 1, 3, 1)

	 }

    SetRayTracerParams();
    if (1) {
    updateParamValue("RayTracer", "maxLights", "1");
    updateParamValue("RayTracer", "maxRecursionDepth", "3");
    updateParamValue("RayTracer", "shadows", "1");
    updateParamValue("RayTracer", "initialStep", "2x2");
//    updateParamValue("RayTracer", "initialStep", "4x4");
    updateParamValue("RayTracer", "drawTimeouts", "50,100");
    updateParamValue("RayTracer", "headlight", "0");
    updateParamValue("RayTracer", "rayOffset", "0.01");
    updateParamValue("RayTracer", "rayOffset", "0.01");
    updateParamValue("RayTracer", "samplesPerPixel", "1024");
    updateParamValue("RayTracer", "usePathTracing", "true");
   updateParamValue("Navigation", "nearPlane", "100");
	 updateParamValue("RayTracer", "whitePoint", "800");
    updateParamValue("RayTracer", "keyValue", "2000");
	
		}
		
   command("setloglevel debug");


}
else if (computerName() == "Dan") {//Dan
    updateParamValue("RayTracer", "maxLights", "1");
    updateParamValue("RayTracer", "maxRecursionDepth", "0");
    updateParamValue("RayTracer", "shadows", "0");
    updateParamValue("RayTracer", "initialStep", "1x1");
    updateParamValue("RayTracer", "drawTimeouts", "300,100");
    updateParamValue("RayTracer", "headlight", "0");

    //var sceneID = loadScene("../data/test_scene/cornellbox.wrl", 0);
    //var sceneID = loadScene("../data/raytracer/fabia/fabia2b-anim0.obj", 50000);
    var sceneID = loadScene("../data/raytracer/Fabia2/fabia2.fhs", 50000);
    //loadInto("../data/raytracer/Fabia2/fabia_lights.fhs", sceneID, 0);

    matrix = Matrix(-0.3, 1, -0.1, 0, -0.3, 0, 0.9, 0, 0.9, 0.3, 0.3, 0, 1360.9, 39.8, 803.6, 1)

    userCommand = "fullscreen 0 true";
}

var scene = Scene.GetScene(sceneID);

sleep(1000);
var cameraID = renderScene(sceneID, 20000);

var lightId;
var alpha = 0.0;

if (userCommand != "")
    command(userCommand);


//test interaktivity
var paramTextBoxID = REGISTER_PARAM_GUI_TEXTCONTROL("TransformationInterval", "40", "Interval to send camera transformation");
var TransformationInterval = 40;
function actionOnSetParam() {
    this.perform = function (evt) {
        if (evt.paramIdentifier == paramTextBoxID) {
            var prevInterval = TransformationInterval;
            TransformationInterval = parseInt(evt.value);
            LOG("TransformationInterval has new value " + TransformationInterval);
            if (prevInterval==0)
                RegisterTimer(new TransformationTimer(), TransformationInterval);
        }
    };
}

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

function TransformationTimer() {
    this.perform = function () {
        scene.SetTransformation(cameraID, matrix);
        //LOG("JavaScript timer");
        return TransformationInterval;
    }
}

var clusterRunning = false;

function
SetRayTracerParam(param, value)
{
 updateParamValue("RayTracer", param, value);
 if (clusterRunning)
	 ClusterCommand("setparam RayTracer." + param + " " + value);
}

function SetRayTracerParams()
{
	SetRayTracerParam("maxLights", "1");
  SetRayTracerParam( "maxRecursionDepth", "3");
  SetRayTracerParam( "shadows", "1");
  SetRayTracerParam( "initialStep", "2x2");
//    SetRayTracerParam( "initialStep", "4x4");
  SetRayTracerParam( "drawTimeouts", "50,100");
  SetRayTracerParam( "headlight", "0");
  SetRayTracerParam( "rayOffset", "0.01");
  SetRayTracerParam( "rayOffset", "0.01");
  SetRayTracerParam( "samplesPerPixel", "1024");
}

var setupClusterButtonID = REGISTER_PARAM_GUI_BUTTON("SetupCluster", "Send scene to cluster, setup ray tracer cameras and update the cluster ray tracer params.");
REGISTER_LISTENER(Event.EVT_PARAM_SET, new ActionOnSetupCluster(setupClusterButtonID));

function ActionOnSetupCluster(paramID) {
    this.paramID = paramID;
    this.val = 1;

    this.perform = function (evt) {
        if (this.val && (evt.paramIdentifier == this.paramID)) {
            this.val = 0;
            SetupCluster();
        }
    }
}

var usePathTracingID = REGISTER_PARAM_GUI_CHECKBOX("PathTracing", "Use path tracing.");

REGISTER_LISTENER(Event.EVT_PARAM_SET, new ActionOnPathTracing(usePathTracingID));


function ActionOnPathTracing(paramID) {
    this.paramID = paramID;
    this.val = 0;

    this.perform = function (evt) {
        if ((evt.paramIdentifier == this.paramID)) {
						this.val = !this.val;
						SetupPathTracing(this.val);
        }
    }
}

function SetupPathTracing(usePathTracing)
{
 SetRayTracerParam("usePathTracing", usePathTracing);
}

function ClusterCommand(commandString)
{
	updateInstParamValue("Cluster", "clientCommand", "4095", commandString);
}


function SetupCluster()
{
	updateInstParamValue("Cluster", "sceneID", "4095", sceneID);
	updateInstParamValue("Cluster", "sendScene", "4095", "1");
	
	ClusterCommand("setparam RayTracer.sceneID " + sceneID);
	sleep(7000);
	ClusterCommand("setparam RayTracer.cameraID " + cameraID);
	clusterRunning = true;

	SetRayTracerParams();	
	// update the camera far plane 
	updateParamValue("Navigation", "farPlane", "100000");
  updateParamValue("RayTracer", "remoteTiles", "5");
  updateParamValue("RayTracer", "filterRadius", "3");
		
}

function MoveLight() {
    this.perform = function () {

		alpha=3.14159/100.0;
		var mat = Matrix.RotationAxis(new Vector3(0,1,0), alpha);
		
    scene.Transform(lightId, mat);
//    scene.SetTransformation(cameraID, mat);
    //LOG("JavaScript timer");
    return TransformationInterval;
   }
}

// THE SCRIPT EXECUTION CONTINUES HERE

if (computerName() != "SHIELD") {
    RegisterTimer(new TransformationTimer(), TransformationInterval);
}
else {
    scene.SetTransformation(cameraID, matrix);
		lightId = scene.GetNodes("LightPointX000")[0].GetID();
		LOG("Light ID " + lightId);
    RegisterTimer(new MoveLight(), TransformationInterval);
}
