local USEMRT = true

-- load the resources we need
local mtl = material.load("car.sess4.mtl")
local mtlf= mtl:getcontrol("fresnelmul")
local shd = mtl:getshader()

local shdspec    = shader.getparamid(shd,"specctrl")
local shdfresnel = shader.getparamid(shd,"fresnelctrl")

local mrtmtl  = material.load("mrtcombinenpr.sess4.mtl")
local mrtcol = mrtmtl:gettexcontrol("color")
local mrtnormal = mrtmtl:gettexcontrol("normal")
local mrtdepth = mrtmtl:gettexcontrol("depth")
local mrtshd  = mrtmtl:getshader()
local mrtuser = mrtshd:getparamid("userctrl")


-- parameter binding
if (mrtuser) then
	function up(...)
		local args = {...}
		local x,y,z,w = mrtshd:param(mrtuser)
		if (#args < 1) then return x,y,z,w end
		
		mrtshd:param(mrtuser,args[1] or x,args[2] or y,args[3] or z,args[4] or w)
	end
end

if (userf) then
	function fp(...)
		local args = {...}
		local x,y,z,w = shd:param(shdfresnel)
		if (#args < 1) then return x,y,z,w end
		
		shd:param(shdfresnel,args[1] or x,args[2] or y,args[3] or z,args[4] or w)
	end
	
	function sp(...)
		local args = {...}
		local x,y,z,w = shd:param(shdspec)
		if (#args < 1) then return x,y,z,w end
		
		shd:param(shdspec,args[1] or x,args[2] or y,args[3] or z,args[4] or w)
	end
end


-------------------
-- basic scene


if (USEMRT) then
	-- create the textures for render-to-texture
	-- GL_NV_texture_rectangle
	local texcol = texture.createfullwindow("color")
	local texnormal = texture.createfullwindow("normal")
	local texdepth  = texture.createfullwindow("depth",textype.depth())

	-- create the fbo (GL_EXT_framebuffer_object)
	--
	-- glGenFramebuffersEXT(1,&fbo->glID);
	fbo = renderfbo.new(32,32,true)
	
	
	-- customized renderloop for MRT
	view = l3dset.get(0):getdefaultview()
	view:rcmdempty()
	
	-- enable fbo
	--
	-- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fbo->glID);
	local rc = rcmdfbobind.new()
	rc:setup(fbo)
	view:rcmdadd(rc)
	
	-- bind textures to fbo
	--
	-- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_RECTANGLE_NV,tex->glID);
	-- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_RECTANGLE_NV,tex->glID);
	-- glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,  GL_TEXTURE_RECTANGLE_NV,tex->glID);
	local rc = rcmdfbotex.new()
	rc:color(0,texcol)
	rc:color(1,texnormal)
	rc:depth(texdepth)
	view:rcmdadd(rc)
	
	-- set in which colortargets is drawn
	-- GL_ARB_draw_buffers
	--
	-- GLenum targets[2] = {GL_COLOR_ATTACHMENT0_EXT,
	--						GL_COLOR_ATTACHMENT1_EXT};
	-- glDrawBuffersARB(2,targets);
	local rc = rcmdfbodrawto.new()
	rc:setup(0,1)
	view:rcmdadd(rc)
	
	-- fill in default drawing
	view = UtilFunctions.simplerenderqueue(view,true)
	
	-- at the end, disable fbo
	-- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
	view:rcmdadd(rcmdfbooff.new())
	
	-- render fullscreen quad with combine shader
	local rc = rcmddraw2dmesh.new()
	rc:matsurface(mrtmtl)
	rc:moTexcontrol(mrtcol,texcol)
	rc:moTexcontrol(mrtnormal,texnormal)
	rc:moTexcontrol(mrtdepth,texdepth)
	rc:autosize(-1)
	view:rcmdadd(rc)
	
	-- a final error check
	fboerrtype,fboerrmsg = l3dlist.fbotest()

	if (fboerrtype) then
		print(fboerrmsg)
		if (fboerrtype > 0) then
			-- revert
			view = UtilFunctions.simplerenderqueue(view)
		end
	end

else
	view = UtilFunctions.simplerenderqueue() 
end
view.rClear:colorvalue(0.17,0.15,0.10,0)

do
	-- make car
	local carmodel = model.load("car.f3d",false,true)
	
	print("carmodel",carmodel:bbox())
	
	-- load wheels. We have one for each side, so that wheel spinning
	-- is a bit easier. You could however do the same with a single
	-- wheel model and inverting spinning direction for one side.
	local carwheelmodel_r = model.load("car_wheel_r.f3d",false,true)
	local carwheelmodel_l = model.load("car_wheel_l.f3d",false,true)

	-- our main actor for the car
	caractor = actornode.new("car",0,500,0)

	-- create & link l3d equivalents
	-- chassis
	carl3d = l3dmodel.new("car",carmodel,false,false)
	carl3d:linkinterface(caractor)

	-- add wheels to the car. The car model has bone positions
	-- for each wheel.
	wheell3ds = {}
	local function addwheel(bonename,wheelmodel,rimname)
		-- retrieve boneid and get its local matrix
		local bone   = carmodel:boneid(bonename)
		local matrix = bone:matrix()
		
		-- create l3d and link it to chassis
		local l3d = l3dmodel.new(bonename,wheelmodel,false,false)
		l3d:parent(carl3d)
		l3d:localmatrix(matrix)
		
		l3d.mdl    = wheelmodel
		l3d.rimmid = wheelmodel:meshid(rimname)
		table.insert(wheell3ds,l3d)
	end

	addwheel("wheel_joint00",carwheelmodel_l,"rim")
	addwheel("wheel_joint01",carwheelmodel_l,"rim")
	addwheel("wheel_joint02",carwheelmodel_r,"rim02")
	addwheel("wheel_joint03",carwheelmodel_r,"rim02")

	-- a simple drop shadow
	-- set default layer higher, so that its drawn after the ground
	--l3dset.layer(1)
	carshadowmodel = model.load("car_shadow.f3d",false,true)
	carshadowl3d = l3dmodel.new("car_shadow",carshadowmodel,false,false)
	carshadowl3d:linkinterface(caractor)
	
	local function makeshadowblend(l3d)
		l3d:rfNodepthmask(true)
		l3d:rfBlend(true)
		l3d:rsBlendmode(blendmode.modulate())
	end
	makeshadowblend(carshadowl3d)
	
	-- allow fresnel for certain meshes

	for i=1,carmodel:meshcount() do
		local mid = carmodel:meshid(i-1)
		carl3d:matsurface(mid,mtl)
		if (mid:name() == "chassis") then
			carl3d:moControl(mid,mtlf,1,1,1,1)
		end
	end
	carl3d:rfLitSun(true)
	
	-- and the wheels rims
	for i,l3d in ipairs(wheell3ds) do
		for i=1,l3d.mdl:meshcount() do
			local mid = l3d.mdl:meshid(i-1)
			l3d:matsurface(mid,mtl)
			if (mid == l3d.rimmid) then
				l3d:moControl(mid,mtlf,1,1,1,1)
			end
		end
		l3d:rfLitSun(true)
	end

end


sun = actornode.new("sun",1000,-1000,2000)
sun.light = l3dlight.new("sun")
sun.light:diffuse(1.1,0.9,0.8,1)
sun.light:ambient(0.1,0.05,0.0,1)
sun.light:linkinterface(sun)
sun.light:makesun()

cam = l3dcamera.default()
print("cam",cam:frontplane(),cam:backplane())

UtilFunctions.freelook(true)
FreeLook.movemulti = 20

--[[
-- rotation of object
-- A simple user input system for rotation
local cx,cy = 0,0
local pdx,pdy,pdz = 0,0,0
function rotit ()
	local x,y = input.mousepos() -- mouse
	x,y = -y,-x
	
	local l,r = input.ismousedown(0),input.ismousedown(1)
	if not l and not r then -- if no mousebutton is pressed...
		cx,cy = x,y -- set reference coordinate to the mouse position
	end
	local dx,dy,dz = l and (cx-x) or 0, cy-y,r and (cx-x) or 0
	  -- dx,dy,dz is now a direction vector
	dx,dy,dz = dx*.01, dy*.01, dz*.01 -- smaller numbers now
	pdx,pdy,pdz = pdx*.9+dx*.1, pdy*.9+dy*.1, pdz*.9+dz*.1
	dx,dy,dz = pdx,pdy,pdz -- dampen the vector with previous one
	
	
	for i,actor in ipairs(spaceships) do
		actor.rx,actor.ry,actor.rz = -- set rotation info for the actor
			(actor.rx or 0) + dx, (actor.ry or 0)+dy, (actor.rz or 0)+dz
		actor:rotdeg(actor.rx,actor.ry,actor.rz)
		actor:pos(unpack(actor.spos)) -- position/rotation update
	end
	
end

Timer.set("tutorial",rotit,20)
]]
-- rotit is now called every 20 milliseconds
