//includes of help function definitions
include("paramevent.js");
include("loadscene.js");
include("runmodule.js");
include("commandevent.js");

var c3dConnexionDevice = 6
var htcViveDeviceId = 1

var buttonsBody = htcViveDeviceId; //bodyID of device with buttons (Flystick, SpaceMouse, Wiimote)
var navigationBody = htcViveDeviceId; //bodyID of device used to manipulate with pointer (Flystick, SpaceMouse, or bodyID of Wiimote in tracking)

//pre-set near and far planes
updateParamValue("Navigation", "nearPlane", "10");
updateParamValue("Navigation", "farPlane", "1000000");
command("usemodule rendergl3");
updateParamValue("rendergl3", "Render with vertex arrays", "1");
updateParamValue("rendergl3", "Render with VBO", "1");
updateParamValue("rendergl3", "Prefer Shaders", "1");
updateParamValue("rendergl3", "Bind Data As Generic", "1");
//updateParamValue("rendergl3", "Render with display lists", "1");
updateParamValue("rendergl3", "Enable headlight", "0");
updateParamValue("rendergl3", "Use Offscreen FBO", "1");
updateParamValue("rendergl3", "FBO Multisampling Method", "Multi");
updateParamValue("rendergl3", "Color Samples Count", "4");
updateParamValue("rendergl3", "Sorting", "Group by Effect State, than by Effect Data");
updateParamValue("rendergl3", "Show profiling", "0");
updateParamValue("rendergl3", "CreateGeometries", "0");
updateParamValue("rendergl3", "Looping rendering", "1");
updateParamValue("rendergl3", "Stereo", "OpenVR");//*/
/*command("usemodule RayTracer");
updateParamValue("RayTracer", "createGeometries", "0");
updateParamValue("RayTracer", "Stereo", "OpenVR");//*/
/*command("usemodule RenderGl");
updateParamValue("RenderGl", "sortGeometries", "2");
updateParamValue("RenderGl", "antiAliasingMethod", "Multi");
//updateParamValue("RenderGl", "runRenderLoop", "1");
updateParamValue("RenderGl", "headlight", "0");
updateParamValue("RenderGl", "openVR", "1");//*/

////////////////////////////////////////////////////////////////////////////////
/// Prepare scene
////////////////////////////////////////////////////////////////////////////////

//import of file with base scene structure
//var sceneID = loadScene("D:/data/vrut/trunk/data/test_scene/test.fhs", 5000);
//var sceneID = loadScene("D:/data/cave/prostredi/krizovatka_TZV5_lidi/krizovatka_TZV5_lidi2.vrut", 5000);
var sceneID = loadScene("D:/data/cave/Koncept_VRS/Koncept_VRS.vrut", 10000);
//var sceneID = loadScene("E:/VRUT/ondra/Komplet/ENTWICKLUNGSSTAND_VW376_0-432-1-288-Kalkmodel__NWB_EU-pokus.vrut", 20000);
//var sceneID = loadScene("E:/data/cave/VWCave/VW370_DeltaGen.vrut", 10000);

//get scene
var scene = Scene.GetScene(sceneID);
var rootID = scene.GetRootID();

//loadInto("C:/data/cave/prostredi/krizovatka_TZV5_lidi/krizovatka_TZV5_lidi2.vrut", rootID, 10000);

//start of rendering
var cameraID = renderScene(sceneID, 5000);
loadInto("d:/VRUT_data/common/HTCVive/vr_controller_vive_1_5.vrut", sceneID, cameraID, 10000);
var pointerID = getFirstNameNodeID(scene, "vr_controller_vive_1_5");
//scene.Move(nodeHand, cameraID);

//move the camera to zero, correct position will be set by tracking
scene.SetTransformation(cameraID, new Matrix(-0.31133718, 0.95028144, -1.360894e-8, 0, -4.371125e-8, -3.730271e-14, 1, 0, 0.95027912, 0.31133864, 4.15382e-8, 0, -16244.734, -7330.372, 2712.0009, 1));//Koncept_VRS
//scene.SetTransformation(cameraID, new Matrix(0, -1, 0, 0, 0, 0, 1, 0, -1, 0, 0, 0, -1300, 800, 2000, 1));//VW370_DeltaGen

runModule("TrackingManipulator");
var mappingTableCounter = 0;
SetTableRow("TrackingManipulator", "mappingTable", mappingTableCounter++, [sceneID, pointerID, 1, 1]);

LOG("Scene prepared");

////////////////////////////////////////////////////////////////////////////////
/// Prepare 3D menu
////////////////////////////////////////////////////////////////////////////////

var useVive = true;

if (!useVive) {
    var pointerModel = "C:/data/vrut/trunk/modules/C3Dmenu/3DMenuPointer.vrut"
    loadIntoAsync(pointerModel, sceneID, cameraID);
}
var laserModel = "D:/data/vrut/trunk/modules/C3Dmenu/3DMenuLaser.vrut"
var menuModel = "D:/data/vrut/trunk/modules/C3Dmenu/3Dmenu.vrut"

loadIntoAsync(laserModel, sceneID, cameraID);
loadIntoAsync(menuModel, sceneID, cameraID);
//wait until all is loaded
waitUntilLoaded(2, 100000);

var laserID = getFirstNameNodeID(scene, "Laser");
var menuID = getFirstNameNodeID(scene, "3Dmenu");

updateParamValue("C3DMenu", "sceneID", sceneID);
updateParamValue("C3DMenu", "cameraID", cameraID);
updateParamValue("C3DMenu", "pointerNodeID", pointerID);
updateParamValue("C3DMenu", "laserNodeID", laserID);
updateParamValue("C3DMenu", "menuNodeID", menuID);
updateParamValue("C3DMenu", "buttonsDeviceID", buttonsBody);
runModule("C3DMenu");

////////////////////////////////////////////////////////////////////////////////
/// Setup control devices
////////////////////////////////////////////////////////////////////////////////

// Run device module
if (navigationBody == c3dConnexionDevice || buttonsBody == c3dConnexionDevice) {
    runModule("C3Dconnexion");
    updateParamValue("C3Dconnexion", "trackingBodyID", c3dConnexionDevice);
}

if (!useVive)
{
    runModule("TrackingManipulator");

    //connect pointer geometry with tracking
    updateParamValue("TrackingManipulator", "mappingTable", "0,0," + sceneID);
    updateParamValue("TrackingManipulator", "mappingTable", "1,0," + pointerID);
    if (navigationBody == c3dConnexionDevice) {
        //use spacemouse
        updateParamValue("TrackingManipulator", "mappingTable", "2,0," + navigationBody);
        updateParamValue("TrackingManipulator", "mappingTable", "3,0,4");
        updateParamValue("TrackingManipulator", "mappingTable", "5,0," + cameraID);
    } else {
        updateParamValue("TrackingManipulator", "mappingTable", "2,0," + navigationBody);
        updateParamValue("TrackingManipulator", "mappingTable", "3,0,1");
    }
    updateParamValue("TrackingManipulator", "mappingTable", "7,0,1");
}

////////////////////////////////////////////////////////////////////////////////
/// Test helpers
////////////////////////////////////////////////////////////////////////////////

//var axisModel = "C:/data/TestovaciSceny/3Dmenu/Axis.vrut"

//loadIntoAsync(axisModel, sceneID, rootID);
//loadIntoAsync(axisModel, sceneID, cameraID);
//wait until all is loaded
//waitUntilLoaded(2, 100000);
