-- -----------------------------------------------------
-- Orthogonal camera experiment
-- ortho.lua
-- -----------------------------------------------------

if not SimNetwork_INPUT then dofile("input.lua") end

scene = nil
debug = false
viewport = {}

function init_viewport()
    viewport.x, viewport.y, viewport.w, viewport.h = E.get_display_union()

    local width = 512 + 384 * viewport.w / viewport.h

    viewport.a = viewport.h / viewport.w
    viewport.k = viewport.w / width

    viewport.h =  width * viewport.a
    viewport.B = -width * viewport.a / 2
    viewport.T =  width * viewport.a / 2

    viewport.w =  width 
    viewport.L = -width / 2
    viewport.R =  width / 2
end


function create_ortho_scene()

	local scene = E.create_pivot()
	local tile = E.create_object("data/tile.obj")
	local cylender = E.create_object("data/cylender.obj")
	local cylender2 = E.create_clone(cylender)
	local cylender3 = E.create_clone(cylender)
	local cylender4 = E.create_clone(cylender)
	
	local camera = E.create_camera(E.camera_type_orthogonal)
	local light = E.create_light(E.light_type_positional)
	local lightF = E.create_light(E.light_type_positional)
	local lightB = E.create_light(E.light_type_positional)
	
	scene3d = scene
	
	-- set positions
	E.set_entity_scale(tile, 10, 1, 10)
	E.set_entity_position(cylender, 2, 0, 1)
	E.set_entity_position(cylender2, 3, 0, 2)
	E.set_entity_position(cylender3, 4, 0, 5)
	E.set_entity_position(cylender4, -2, 0, -3)

	E.set_entity_position(light, 0, 6, 0)
	
	
	-- E.set_entity_position(camera, -200, -200, -400)
	E.set_entity_position(camera, -viewport.k * viewport.w/2, -viewport.k * viewport.h/2, 0)
	-- configure lights
	E.set_entity_position(lightF, -viewport.w / 2, 0, viewport.h / 2)
	E.set_entity_position(lightB,  viewport.w / 2, 0, viewport.h / 2)
	
	
	-- create scene heirarchy
	E.parent_entity(tile, scene)
	E.parent_entity(cylender, scene)
	E.parent_entity(cylender2, scene)
	E.parent_entity(cylender3, scene)
	E.parent_entity(cylender4, scene)
	
	E.parent_entity(lightF, light)
	E.parent_entity(lightB, lightF)
	E.parent_entity(scene, lightB)
	E.parent_entity(light, camera)

	E.set_entity_scale(scene, 50*viewport.k, 50*viewport.k, 50*viewport.k)
	E.turn_entity(scene, 45, 45, 0)
	RotationX = 45
	RotationY = 45
	zoom = 50*viewport.k
end

init_viewport()
create_ortho_scene()


