--simple example script that demonstrates most/all avail functions.
--http://www.lua.org/pil/ 	<- lua how2s!
--reddit.com/r/boxengine 	<- for discussion!
--Doc.txt					<- boxEngine documentation!

dofile "data/scripts/lib/objReferences.lua"	--defines names -> geom indices
dofile "data/scripts/lib/bxeGeneral.lua"	--defines names -> geom indices


function Init()
	
	camMode = 1
	mouseX = 0
	mouseY = 0
	AddGeom( "data/geom/core/common.bxo" )
	
	AddTex( "data/textures/noise1bw.png" )
	AddTex( "data/textures/noise3.png", "gr" )
	AddTex( "data/textures/outline2.png", "gr" )
	
	AddShad( "data/shaders/00000.shad", "g" )
	AddShad( "data/shaders/00001.shad", "g" )
	AddShad( "data/shaders/00002.shad" )
	AddShad( "data/shaders/00005.shad" )
	AddShad( "data/shaders/00006.shad" )
end




function KeyHappen( inVal, inDown, inT )	--also registers mouse clicks
	--chVal = string.format( "%c", inVal )
	
	if ( inDown == true ) then
		print ( inVal, inDown )
		if inVal == 321 then
			camMode = 1 - camMode
		end
	end
	
end



function Draw( t, dt )	--time and deltaTime ( time since last frame )
	mouseX, mouseY = GetMouseTotal()
	if ( true ) then
		--Shad( "g", "00001" )
		--BeginRTTFull()
		camDist = 4
		SetCam( 3, 120 )
		LookAt( 0, -camDist, 0, 0, 0, 0 )	--3d view
		Push()
		if ( camMode == 0 ) then
			Move( 0, -camDist, 0 )
		end
		SetAmbientColor( .5, .5, .5 )
		SetLight( 1, 0, 0, 2 )
		SetLight( 0, 0, 0, 2 )
		Rotate( 1, 0, 0, mouseY )
		Rotate( 0, 0, 1, mouseX )
		if ( camMode == 0 ) then
			Move( 0, camDist, 0 )
		end
		Push()
		--Scale( 5 + 2 * math.sin( 0.3 * t ) )	--unfortunately, scaling affects normals & lighting - later there could be a solution to this.
		SetLightColor( 0, 1, 1, 1 )
		SetAmbientColor( 1, 1, 1 )
		Rotate( math.sin( t ), 0, 1, 10 * ( mouseX - mouseY ) )
		
		Tex( "gr", 0 )--int( t ) % 2 )
		
		Color( .1,.1,.1,.902 )	--color doesn't work when lighting is enabled.
		--SetLight( 1, 0, 2 * math.sin( t ), 0 )
		Geom( "cube" )
		--DrawGeom( 256 )	-- internally we use indices, but it is best to use the names in lib/objReferences.lua, as indices will likely change soon.
		--SetLighting( 0 )
		Pop()
		
		
		tv = .5 + .5 * math.sin( t )
		Color( tv, 1 - tv, 1 - tv, .3 )
		for i = 1, 1000 do
			
			
			Push()
			Rotate( 1, 0, 1, t *.1  + i * 0.1 )
			x = math.sin( .1 * i + .2 * t )
			y = math.cos( .07 * i + .2 * t )
			Move( -.5 + x * 2, -.5 + y * 1, -.5 )
			if ( math.mod( i, 3 ) == 0 ) then
				Scale( .2 )
				Shad( -1 )
			end
			Scale( .3, .5, .5 )
			Geom( 'cube' )
			Pop()
		end
		SetLighting( 0 )
		Color( 1 )	--reset color to white
		--Shad( -1 )	--unbind, it is GOOD FOHRM, PETAH PANNIE.
		Pop()
		SetCam( 2, 2 )
		
		Blend( 0 )
		Logic( -1 )
		Scale( .15 + .05 * math.sin( t ) )
		Scale(.9 )
		Geom( 'quad' )
		
		--Shad( 0 )
		SetCam( 2, 0 )
		--EndRTTFull()
		--Scale( .9 )
	else
		SetCam( 2, 1 ) 	--2d view
		SetLighting( 0 )
		Blend( 0 )
		Shad( 4 )
		Tex( "gr", 0 )
		Scale( .3, .3, .3 )
		--Rotate( 0, 0, 1, t )
		Rotate( 0, 1, 0, t )
		Geom( "quad" )
		
	end
	
	

end