Creating a 3D Grid

Blitz3D Forums/Blitz3D Programming/Creating a 3D Grid

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I HAVE FOUND IT!

Edit: Fixed the borders not showing exactly the way they're supposed to ;)
Global GridTexture
Global GridSize#=16
Global GridSpace#=2

InitGraphics(1024,768,"GIA Map Editor")
GridTexture = CreateGridTexture(2+4)

Global DefaultLight = CreateLight()

Global cube[1]

cube[0] = CreateCube()
	EntityTexture cube[0],GridTexture

cube[1] = CreateCube()
	EntityTexture cube[1],GridTexture
	ScaleEntity cube[1],.25,.25,.25
	PositionEntity cube[1],3,0,0,1

Global Pivot = CreatePivot()
	PositionEntity Pivot,0,GridSize*.5,-GridSize*1.5
	
Global Camera = CreateCamera(Pivot)
	CameraClsColor Camera,100,100,100
	CameraRange Camera,.01,100
	RotateEntity Camera,20,0,0

Global Plane = CreatePlane()
	EntityPickMode Plane,2
	EntityAlpha Plane,0

Global Multiplier#=.5
Global TempPoints%
Global Pause%=0
Global Cursor%=1

While Not KeyDown(1)
	If KeyHit(25)
		Pause = Not Pause
		FlushKeys
	EndIf
	
	If MouseHit(1)
		Cursor = Not Cursor
		
		If Cursor = 1 Then ShowPointer
		If Cursor = 0 Then HidePointer
	EndIf
	
	If Pause = 0
		If KeyHit(20) GridSize = GridSize + Multiplier
		If KeyHit(34) GridSize = GridSize - Multiplier
		
		If KeyHit(21) GridSpace = GridSpace + Multiplier
		If KeyHit(35) GridSpace = GridSpace - Multiplier
		
		If GridSize < Multiplier Then GridSize = Multiplier
		If GridSpace < Multiplier Then GridSpace = Multiplier
		
		If KeyDown(31) MoveEntity Pivot,0,0,-.25
		If KeyDown(17) MoveEntity Pivot,0,0,.25
		
		If KeyDown(32) MoveEntity Pivot,.25,0,0
		If KeyDown(30) MoveEntity Pivot,-.25,0,0
		
		If KeyDown(18) TranslateEntity Pivot,0,.25,0
		If KeyDown(16) TranslateEntity Pivot,0,-.25,0
		
		If KeyDown(200) TurnEntity Camera,-4,0,0
		If KeyDown(208) TurnEntity Camera,4,0,0
		
		If KeyDown(203) TurnEntity Pivot,0,4,0
		If KeyDown(205) TurnEntity Pivot,0,-4,0
		
		TempPitch# = EntityPitch(Camera)
		TempYaw# = EntityYaw(Pivot)
		
		RotateEntity Camera,TempPitch,0,0
		RotateEntity Pivot,0,TempYaw,0
		
		DrawWorld()
		
		TempPoints = 0
		
		ProjectLine(-GridSize,0,0,GridSize,0,0,255,0,0,1) ; X Line
		ProjectLine(0,-GridSize,0,0,GridSize,0,0,255,0) ; Y Line
		ProjectLine(0,0,-GridSize,0,0,GridSize,0,0,255,1) ; Z Line
		
		For pos# = 1 To (GridSize*(GridSpace^-1)) ; Instead of doing GridSize/GridSpace which freezes the program if either are 0
												; The smaller the GridSpace, the more points/dots there are
			
			TempSpacing# = GridSpace*pos
			
			If TempSpacing <= GridSize
				
				;X Axis Lines
				ProjectLine(GridSize,0,TempSpacing ,-GridSize,0,TempSpacing ,50,50,50)
				ProjectLine(GridSize,0,-TempSpacing ,-GridSize,0,-TempSpacing ,50,50,50)
				
	;			;YX Axis Lines
	;			ProjectLine(GridSize,TempSpacing,0 ,-GridSize,TempSpacing,0 ,50,50,50)
	;			ProjectLine(GridSize,-TempSpacing,0 ,-GridSize,-TempSpacing,0 ,50,50,50)
	;			
	;			;YZ Axis Lines
	;			ProjectLine(TempSpacing,GridSize,0 ,TempSpacing,-GridSize,0 ,50,50,50)
	;			ProjectLine(-TempSpacing,GridSize,0 ,-TempSpacing,-GridSize,0 ,50,50,50)
				
				;Z Axis Lines
				ProjectLine(TempSpacing,0,GridSize ,TempSpacing,0,-GridSize ,50,50,50)
				ProjectLine(-TempSpacing,0,GridSize ,-TempSpacing,0,-GridSize ,50,50,50)
				
				
				
				ProjectPoint(TempSpacing,0,0,TempSpacing,255,0,0,4)
			EndIf
		Next
		
		;TempGridSize#=Floor(GridSize*(GridSpace^-1))*GridSpace
		
		CameraPick(Camera,MouseX(),MouseY())
		
		If PickedEntity() <> 0
			TempPickedX# = Int(PickedX()/GridSpace)*GridSpace
			TempPickedY# = Int(PickedY()/GridSpace)*GridSpace
			TempPickedZ# = Int(PickedZ()/GridSpace)*GridSpace
			
			ProjectPoint(TempPickedX,TempPickedY,TempPickedZ,"HERE",180,50,2,32)
		EndIf
		
		Color 0,255,0
		
		Text 0,40,"Points = " + TempPoints
		
		Color 0,0,0
		Text 0,80,"Center Cube depth = " + MeshDepth(cube[0])
		Text 0,100,"Smaller Cube X position = " + EntityX(cube[1])
		Text 0,120,"Grid Size " + GridSize
		Text 0,140,"Grid Space " + GridSpace
		Text 0,160,"Press T and G to change the Grid Size"
		Text 0,180,"Press Y and H to change the Grid Space"
		Text 0,200,"Press P to pause the program"
		
		
		Flip
	EndIf
Wend
ClearWorld 1,1,1
End

Function ProjectPoint(x#,y#,z#,value$="",point_red%=0,point_green%=0,point_blue%=0,point_size#=4)
	CameraProject Camera,x,y,z
	
	If ProjectedZ() > 0
		
		RectSize# = Sqr(EntityX(Camera,1)^2+EntityY(Camera,1)^2+EntityZ(Camera,1)^2 + x^2 + y^2 + z^2)
		RectSize = Cos(RectSize)*point_size
		
		If RectSize > 0
			TempX# = ProjectedX()
			TempY# = ProjectedY()
			Color point_red,point_green,point_blue
			Rect TempX-(RectSize*.5),TempY-(RectSize*.5),RectSize,RectSize
			Color 250,250,0
			Text TempX,TempY,value
			TempPoints = TempPoints + 1
		EndIf
	EndIf
	
	Return ProjectedZ()
End Function

Function ProjectLine(x1#,y1#,z1#,x2#,y2#,z2#,line_red%=0,line_green%=0,line_blue%=0,line_thick#=0)
	
	Local TempX#[1]
	Local TempY#[1]
	Local TempZ#[1]

	CameraProject Camera,x1,y1,z1
	
		TempX[0] = ProjectedX()
		TempY[0] = ProjectedY()
		TempZ[0] = ProjectedZ()
		
	CameraProject Camera,x2,y2,z2
	
		TempX[1] = ProjectedX()
		TempY[1] = ProjectedY()
		TempZ[1] = ProjectedZ()
		
	If TempZ[0] > 0
		If TempZ[1] > 0
			Color line_red,line_green,line_blue
			If line_thick = True
				For t# = -1 To 1
					Line TempX[0],TempY[0]+t,TempX[1],TempY[1]+t
				Next
					Else
						Line TempX[0],TempY[0],TempX[1],TempY[1]
			EndIf
			TempPoints = TempPoints + 2
		EndIf
	EndIf
End Function

Function CreateGridTexture(flags%)
	Local gtexture = CreateTexture(32,32,flags)
	
	SetBuffer TextureBuffer(gtexture)
	Color 0,0,0
	Rect 0,0,32,32
	
	Color 90,90,90
	Line 16,0,16,32
	Line 0,16,32,16
	
	SetBuffer BackBuffer()
	
	Return gtexture
End Function

Function DrawWorld()
	UpdateWorld
	RenderWorld
End Function

Function InitGraphics(width = 800, height = 600,title$="Blitz3D Program",exit_message$="Are you sure?")
	Graphics3D width, height, 32, 2
	SetBuffer BackBuffer()
	SeedRnd MilliSecs()
	AppTitle title,exit_message
End Function


...now if only I could find a way so I don't have to make the grid disappear(if I don't, then the lines will appear in odd places)