`jGfx Plugin: GInst Example - LOD
`=============================================
Sw = Desktop Width()
Sh = Desktop Height()
Set Display Mode Sw,Sh,32,1
Sync On : Sync Rate 0
Autocam Off : Color Backdrop 0
Set Camera Range 10, 10000

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


`Create a Source Object which contains 4 levels of detail
ObjectID = 1
Make Object Sphere ObjectID, 50, 12, 24	`LOD0

TmpObj=2
TmpMesh=1
Make Object Sphere TmpObj, 50, 9, 18	`LOD1
Make Mesh From Object TmpMesh, TmpObj
Delete Object TmpObj
Add Limb ObjectID, 1, TmpMesh
Delete Mesh TmpMesh

TmpObj=2
TmpMesh=1
Make Object Sphere TmpObj, 50, 6, 12	`LOD2
Make Mesh From Object TmpMesh, TmpObj
Delete Object TmpObj
Add Limb ObjectID, 2, TmpMesh
Delete Mesh TmpMesh

TmpObj=2
TmpMesh=1
Make Object Sphere TmpObj, 50, 4, 8		`LOD3
Make Mesh From Object TmpMesh, TmpObj
Delete Object TmpObj
Add Limb ObjectID, 3, TmpMesh
Delete Mesh TmpMesh

Convert Object FVF 1, 0x002 || 0x010 || 0x100
Load Image "Media\bricks.png", 1
Texture Object ObjectID, 0, 1 `note: set render mesh textures before applying to GInst

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


`Setup a GInst to use as a source for instance data
pGInst = GInst_MakeNew()
`Setup Meshlist
GInst_SetObject pGInst, ObjectID
`Apply effect
GInst_SetEffect pGInst, 1


`Setup LOD
`Set the number of LOD levels
GInst_SetLODLevels pGInst, 4			`4 Levels (0,1,2 and 3)

`Set LOD level distances
`Note: LOD Distances are calculated using GInstBuffer bounding spheres, meaning smaller buffers will
`cycle through their LOD levels before other buffers with larger bounding spheres.
GInst_SetLODDistance pGInst, 1, 500		`LOD Level 1 begins rendering at 500 units
GInst_SetLODDistance pGInst, 2, 1000	`LOD Level 2 begins rendering at 1000 units
GInst_SetLODDistance pGInst, 3, 1500	`LOD Level 3 begins rendering at 1500 units

`Assign Mesh LOD levels
GInst_SetMeshLOD pGInst, 0, 0			`Mesh0 belongs to LOD level 0
GInst_SetMeshLOD pGInst, 1, 1			`Mesh1 belongs to LOD level 1
GInst_SetMeshLOD pGInst, 2, 2			`Mesh2 belongs to LOD level 2
GInst_SetMeshLOD pGInst, 3, 3			`Mesh3 belongs to LOD level 3


`Don't render beyond 3000 units from the camera
//GInst_SetMaxDrawDistance pGInst, 3000





`Create a GInstBuffer to render instances from the GInst
pInstBuffer = GInstBuffer_MakeNew(pGInst, InstanceCount)
`LOD requires culling to be enabled (this call is redundant, culling is 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 InstBuffer to contain the latest instance data
GInstBuffer_Update pInstBuffer





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 "Draw Primitives = ",GInst_GetDrawPrimitives()
	Print "Use the mouse and arrowkeys to control the camera"
	Print "Press W to toggle wireframe"

	Now=Timer()
	TimePassed# = (Now - LastTime)*TimeSpeed#
	LastTime = Now

	if Keystate(17)
		if WHeld=0
			WHeld=1
			bWireframe = 1-bWireframe
			if bWireframe
				Set effect technique 1,"Wireframe"
			else
				Set effect technique 1,"Diffuse"
			endif
		endif
	Else
		WHeld=0
	endif

	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
