meshcullradius example

BlitzMax Modules Forums/MiniB3D/meshcullradius example

Hi,
Here is a quick example I put up for version 0.3 of MiniB3D. (get it from the (Latest Version (locked) (sticky)) Topic)

note: I made the meshcull radius 0.75 (smaller than the cubes) so that you can observe the cubes 'popping' in and out. if you set it to 1.0 or more you should not be able to see that.

Also, If you click on a cube it will be picked. I might try a pickcullradius to see if you can gain some speed by only being able to pick objects that are within view


Import "MiniB3D.bmx"

Strict

Local width=640;height=480;depth=16;mode=0

Graphics3D width,height,depth,mode

Local cam:TCamera=CreateCamera()
PositionEntity cam,0,5,0

Local light:TLight=CreateLight()
RotateEntity light,45,0,0

Local marker:TMesh=CreateSphere()
ScaleEntity marker,0.2,0.2,0.2
EntityColor marker,255,0,0

Local mesh:TMesh=Createcube()
Local MeshArray:Tmesh[10000]
Local size:Int = 32
Local z:Int
For Local mm:Int = 0 To size
	For Local x:Int = 0 To size
	
			mesharray[x] = copymesh(mesh)
			EntityPickMode mesharray[x],2
			EntityColor mesharray[x],Rnd(0,255),Rnd(0,150),100+mm

			PositionEntity mesharray[x] ,(-size/2)*4+x*4,0,(-size/2)*4+z*4
			mesharray[x].MeshCullRadius(0.75)

	Next
	z:+1
Next


' used by camera code
Local mxs#=0
Local mys#=0
Local move#=0.5
MouseXSpeed() ' flush
MouseYSpeed() ' flush

' used by fps code
Local old_ms=MilliSecs()
Local renders
Local fps

While Not KeyDown(KEY_ESCAPE)		

	If KeyHit(KEY_ENTER) Then DebugStop

	'' control camera
	
	' mouse look
	
	mxs#=mxs#+(MouseXSpeed()/5.0)
	mys#=mys#+(MouseYSpeed()/5.0)

	RotateEntity cam,mys#,-mxs#,0

	If KeyDown(KEY_SPACE)=False
		MoveMouse width/2,height/2
		MouseXSpeed() ' flush
		MouseYSpeed() ' flush
	EndIf

	' move camera forwards/backwards/left/right with cursor keys
	
	If KeyDown(KEY_W) Or KeyDown(KEY_UP)=True Then MoveEntity cam,0,0,move# ' move camera forward
	If KeyDown(KEY_S) Or KeyDown(KEY_DOWN)=True Then MoveEntity cam,0,0,-move# ' move camera back

	If KeyDown(KEY_A) Or KeyDown(KEY_LEFT)=True Then MoveEntity cam,-move#,0,0 ' move camera left
	If KeyDown(KEY_D) Or KeyDown(KEY_RIGHT)=True Then MoveEntity cam,move#,0,0 ' move camera right
	
	''
	
	'TurnEntity mesh,0,1,0

	' if mousehit then perform camerapick
	If MouseDown(1)
	
		Local pick:TEntity=CameraPick(cam,MouseX(),MouseY())
		
		
		If pick<>Null
			'DebugLog "Picked!"
			'DebugLog "PickedX(): "+String(PickedX())
			'DebugLog "PickedY(): "+String(PickedY())
			'DebugLog "PickedZ(): "+String(PickedZ())
			'DebugLog "PickedNX(): "+String(PickedNX())
			'DebugLog "PickedNY(): "+String(PickedNY())
			'DebugLog "PickedNZ(): "+String(PickedNZ())
			'DebugLog "PickedTime(): "+String(PickedTime())
			'DebugLog "PickedEntity(): "+String(PickedEntity())
			'DebugLog "PickedSurface(): "+String(PickedSurface())
			'DebugLog "PickedTriangle(): "+String(PickedTriangle())
			EntityColor PickedEntity(),255,255,0
			PositionEntity marker,PickedX(),PickedY(),PickedZ()
		Else
			DebugLog "Not Picked"
		EndIf
		
	EndIf

	RenderWorld
	renders=renders+1

	' calculate fps
	If MilliSecs()-old_ms>=1000
		old_ms=MilliSecs()
		fps=renders
		renders=0
	EndIf
	
	DebugText 0,0,"FPS: "+String(fps)

	Flip

Wend
End


Function CreatePlane:TMesh(parent_ent:TEntity=Null,Segments:Int=1)
	
		Local mesh:TMesh=TMesh.CreateMesh(parent_ent)
	
		Local surf:TSurface=mesh.CreateSurface()
			
		surf.AddVertex(-1.0,-1.0,-1.0)
		surf.AddVertex(-1.0, 1.0,-1.0)
		surf.AddVertex( 1.0, 1.0,-1.0)
		surf.AddVertex( 1.0,-1.0,-1.0)
		
		surf.VertexNormal(0,0.0,0.0,-1.0)
		
		surf.VertexTexCoords(0,0.0,1.0)
		
				
		surf.AddTriangle(0,1,2)
		surf.AddTriangle(0,2,3)
		

		Return mesh
	
	End Function