`jGfx Plugin: GInst Example - Per-Instance data using Custom Elements
`=============================================================================
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
bUseNormalMapping = 1	`Use Normalmapping shader
bUseCulling = 1			`Enable culling
InstanceCount = 100		`Number of instances to create



`Setup Instance Source Object
ObjectID = 1
Make Object Sphere ObjectID, 50, 12, 24
Convert Object FVF ObjectID, 0x002 || 0x010 || 0x100 //XYZ + Normal + Tex1
Load Image "Media\bricks.png", 1
Texture Object ObjectID, 0, 1

EffectID = 1
If bUseNormalMapping
	`apply normalmap texture
	Load Image "Media\bricks_n.png", 2
	Texture Object ObjectID, 1, 2
	`load GInst compatible normalmapping effect
	Load Effect "Media\GInst_NormalColor.fx", EffectID, 0
Else
	`load GInst compatible diffuse effect
	Load Effect "Media\GInst_DiffuseColor.fx", EffectID, 0
Endif


`Create a GInst, and setup it's render resources
pGInst = GInst_MakeNew()
GInst_SetObject pGInst, ObjectID
GInst_SetEffect pGInst, EffectID



`Custom Element Declaration (uses D3D9 constants contained within GInst_Constants.dba)
`Check device support for UBYTE4N custom elements
dwDTCAPS = Device_GetDeclTypeCaps()
If dwDTCAPS && D3DDTCAPS_UBYTE4N
	bSupportsUBYTE4N = 1
Endif

`Declare a custom element to contain per-instance color values. `Note: The GInst must have mesh and effect resources declaring custom elements.
bUseWorldMat = 1 `Pass instance transforms using the plugin's internal world matrix 
GInst_DeclareCustomElements pGInst, bUseWorldMat 
	`If the device supports it, Pass colors using the UBYTE4N datatype.
    If bSupportsUBYTE4N = 1
		GInst_AddCustomElement pGInst, D3DDECLTYPE_UBYTE4N, D3DDECLUSAGE_TEXCOORD `Custom Element 1
    Else
	`Otherwise fall back to the D3DCOLOR datatype
    	GInst_AddCustomElement pGInst, D3DDECLTYPE_D3DCOLOR, D3DDECLUSAGE_TEXCOORD `Custom Element 1
    Endif
GInst_FinishCustomElements pGInst


`Create a GInstBuffer
pInstBuffer = GInstBuffer_MakeNew(pGInst, InstanceCount)
GInstBuffer_SetCullingEnabled pInstBuffer, bUseCulling

`Setup Instances
For i=1 To InstanceCount
	`set random transforms
	GInstBuffer_SetPosition pInstBuffer, i, Rnd(1000)-500, 0.0, Rnd(1000)-500
	GInstBuffer_SetRotation pInstBuffer, i, Rnd(360), Rnd(360), 0.0
	GInstBuffer_SetScale pInstBuffer, i, 100.0
	GInstBuffer_UpdateInstance pInstBuffer, i

	`set random custom element colors
 	R = Rnd(128)+127
 	G = Rnd(128)+127
 	B = Rnd(128)+127
	A = 255
	`If the device supports UBYTE4N vertex elements
	If bSupportsUBYTE4N = 1
		`pass UBYTE4N colors using RGBA format
		GInstBuffer_SetUByte4 pInstBuffer,i,1, R,G,B,A
	Else
		`pass D3DCOLOR colors using BGRA format
 		GInstBuffer_SetUByte4 pInstBuffer,i,1, B,G,R,A		`Set color bytes individually
 		//GInstBuffer_SetDword pInstBuffer,i,1, RGB(R,G,B)	`Set color bytes from dword
	Endif
Next i

`Update the instance stream to contain the latest instance data
GInstBuffer_Update pInstBuffer, bUseCulling








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
