`jGfx Plugin: GInst Example - Manual Meshlist Assembly
`=============================================================================
`Demonstrates how to setup a GInst's meshlist using manual mesh assembly
Sw = Desktop Width()
Sh = Desktop Height()
Set Display Mode Sw,Sh,32,1
Sync On : Sync Rate 0
Color Backdrop 0
Autocam Off

`Example Settings
InstanceCount = 100		`Number of instances to create




`Setup a source object containing 3 limbs
ObjectID = 1
Make Object Sphere 1, 50, 12, 24		`Limb0

TmpObj=2 : TmpMesh=1
Make Object Cube TmpObj, 50				`Limb1
Offset Limb TmpObj, 0, 0.0, 50.0, 0.0
Make Mesh From Object TmpMesh, TmpObj
Delete Object TmpObj
Add Limb 1,1,TmpMesh
Delete Mesh TmpMesh

Make Object Sphere TmpObj, 50, 12, 24	`Limb2
Offset Limb TmpObj, 0, 0.0, 100.0, 0.0
Make Mesh From Object TmpMesh, TmpObj
Delete Object TmpObj
Add Limb 1,2,TmpMesh
Delete Mesh TmpMesh

Convert Object FVF 1, 0x002 || 0x010 || 0x100 `XYZ + Normal + Tex1
Load Image "Media\bricks.png", 1
Texture Object ObjectID, 0, 1
Exclude Object On ObjectID


`load GInst compatible diffuse effect
Load Effect "Media\GInst_Diffuse.fx", 1, 0



`Create new GInst to use as a source for instance render data
pGInst = GInst_MakeNew()

`Setup the GInst's Render Meshlist by assembling object limbs
GInst_BeginObjectAssembly pGInst
	GInst_AddMesh pGInst, ObjectID, 0	
	//GInst_AddMesh pGInst, ObjectID, 1 `Skip adding limb1 to the meshlist, uncomment to add the cube limb
	GInst_AddMesh pGInst, ObjectID, 2 
GInst_FinishObjectAssembly pGInst

`Apply effect (effect references are no longer obtained by reading limb data)
GInst_SetEffect pGInst, 1




`Create a GInstBuffer to render instances from the GInst
pInstBuffer = GInstBuffer_MakeNew( pGInst, InstanceCount )
`Setup culling (enabled by default)
GInstBuffer_SetCullingEnabled pInstBuffer, 1

`Setup Instances
For i=1 To InstanceCount
	`Set random transforms
	PosX=Rnd(1000)-500
	PosZ=Rnd(1000)-500
	GInstBuffer_SetPosition pInstBuffer, i, PosX, 0.0, PosZ
	GInstBuffer_SetRotation pInstBuffer, i, Rnd(360), Rnd(360), Rnd(360)
	GInstBuffer_SetScale pInstBuffer, i, 100.0, 100.0, 100.0
	`Update the instance's world matrix
	GInstBuffer_UpdateInstance pInstBuffer, i
Next i
  
`Update the instance stream
GInstBuffer_Update pInstBuffer, 1





Position Camera -600, 150, -600
Rotate Camera 25, 45, 0

TimeSpeed# = 0.5
LastTime = Timer()
Do
	Set Cursor 0,0
	Print "FPS = ",Screen Fps()
	Print "Use the mouse and arrowkeys to control the camera"
	
	Now=Timer()
	TimePassed# = (Now-LastTime)*TimeSpeed#
	LastTime = Now

	Move Camera (UpKey()-DownKey())*TimePassed#
	Move Camera Right (RightKey()-LeftKey())*TimePassed#
	Rotate Camera Wrapvalue(Camera Angle X()+MouseMoveY()),  Wrapvalue(Camera Angle Y()+MouseMoveX()), 0

	Sync
Loop
