` jGfx Example - Tree It
`-------------------------------------------------------------------------------
Sw=Desktop Width()
Sh=Desktop Height()
Set Display Mode Sw,Sh,32,1
Sync On : Sync Rate 0
Autocam Off : Set camera range 1,15000
Position camera 0,100,-1500
Color backdrop 0


`Setup tree object
	TreeObj=1
	Load Object "Media\Tree.dbo",TreeObj
	
	Load Image "Media\bark01.dds",1		`Trunk textures
	Load Image "Media\bark01_n.dds",2
	Texture limb TreeObj,0,0,1
	Texture limb TreeObj,0,1,2

	Load Image "Media\birch leaf.dds",3	`Leaf texture
	Texture limb TreeObj,1,0,3

	TrunkFx=1 `Trunk shader
	Load Effect "Media\TreeIt_Trunk.fx",TrunkFx,0
	LeafFx=2  `Leaf shader
	Load Effect "Media\TreeIt_Leaf.fx",LeafFx,0


`Create two separate GInsts for rendering, trunks and leaves use different vertex formats and effects.
	pGInst_Trunk = GInst_MakeNew()
	GInst_BeginObjectAssembly pGInst_Trunk
		GInst_AddMesh pGInst_Trunk, TreeObj, 0
	GInst_FinishObjectAssembly pGInst_Trunk
 	GInst_SetEffect pGInst_Trunk, TrunkFx

	pGInst_Leaf = GInst_MakeNew()
 	GInst_BeginObjectAssembly pGInst_Leaf
 		GInst_AddMesh pGInst_Leaf, TreeObj, 1
 	GInst_FinishObjectAssembly pGInst_Leaf
	GInst_SetEffect pGInst_Leaf, LeafFx



`Setup instance buffers
	InstanceCount = 50

	pTrunkBuffer = GInstBuffer_MakeNew(pGInst_Trunk, InstanceCount)
	pLeafBuffer = GInstBuffer_MakeNew(pGInst_Leaf, InstanceCount)
	
`Set random instance transforms
	Randomize 1000
	For i=1 To InstanceCount
		PosX# = rnd(2000)-1000
		PosZ# = rnd(2000)-1000
		Scale# = 100+rnd(50)
		
		GInstBuffer_SetPosition pTrunkBuffer, i, PosX#, 0, PosZ#
		GInstBuffer_SetRotation pTrunkBuffer, i, 0, i*38, 0
		GInstBuffer_SetScale pTrunkBuffer, i, Scale#
		GInstBuffer_UpdateInstance pTrunkBuffer, i

		GInstBuffer_SetPosition pLeafBuffer, i, PosX#, 0, PosZ#
		GInstBuffer_SetRotation pLeafBuffer, i, 0, i*38, 0
		GInstBuffer_SetScale pLeafBuffer, i, Scale#
		GInstBuffer_UpdateInstance pLeafBuffer, i
	Next i
	
	GInstBuffer_Update pTrunkBuffer
	GInstBuffer_Update pLeafBuffer


`Setup tree shader updates
	dim TreeWind(4) as float
	null=Make Vector4(1)
	Global TimerVal as float
	Global TimerFrame as float
	Global TimerStart as float
	Global TimerUpdate as integer
	Global TimerElapsed as integer
	Global TimerTarget as float
	TimerElapsed=10
	TimerTarget=16.66
	TimerStart=Timer()



Do
	Set Cursor 0,0
	Print Screen FPS()
	
	Move Camera (Upkey()-Downkey())*(TimerVal*10)
	Rotate camera 0, Wrapvalue(Camera Angle X(0)+MouseMoveY()), Wrapvalue(Camera Angle Y(0)+MouseMoveX()), 0


`Tree shader update
	Inc TimerUpdate
	If TimerUpdate>TimerElapsed
 		TimerFrame=(TimerFrame*0.5)+((Timer()-TimerStart)*0.5)
 		TimerStart=Timer()
 		TimerVal=(TimerFrame/TimerTarget)/TimerElapsed
 		TimerUpdate=1
	Endif
	Inc TreeWind(1),0.1*TimerVal
	Inc TreeWind(2),1*TimerVal
	Inc TreeWind(3),2*TimerVal
	Inc TreeWind(4),10*TimerVal
	For t=1 to 4
 		TreeWind(t)=Wrapvalue(TreeWind(t))
	Next t
	Set Vector4 1,TreeWind(1)/57.3,TreeWind(2)/57.3,TreeWind(3)/57.3,TreeWind(4)/57.3
	Set Effect Constant Vector TrunkFx,"TreeWindTimer",1
	Set Effect Constant Vector LeafFx,"TreeWindTimer",1

	Sync
Loop







