`Primatives Creation Program!!!
`By Segan
 
`First, we will need a type that has all of each objects info
Type tObject
	X As Float:	Y As Float:	Z As Float
	Num	
endtype
 
Dim Objects(0) As TObject 
 
`Now we need some globals. As Homer would say: MMMMmmmm.... Glo-nuts
Global D3ClickX As Float
Global D3ClickY As Float
Global D3ClickZ As Float
 
`Now let's create the environment where we will be working
CreateEnvironment()
 
`Screen Stuff... FUN FUN FUN!!!
Sync On: Sync Rate 60
autocam off
backdrop on
 
CameraX As Float: CameraX = 0.0
CameraY As Float: CameraY = 10.0
CameraZ As Float: CameraZ = -5.0
 
PointCameraX = 0
PointCameraY = 0
PointCameraZ = 0
 
Position Camera CameraX,CameraY,CameraZ
Point Camera PointCameraX,PointCameraY,PointCameraZ
 
CreateObject(1,0,0,0)
 
`Now it's time for our main loop!
Do
 
	If MouseClick() = 1
		MouseClick3D("y",Objects(1).Y)
		Objects(1).X = D3ClickX
		Objects(1).Z = D3ClickZ
		`CreateObject(1, D3ClickX, 0.0, D3ClickZ) 
	Endif
	If MouseClick() = 2
		MouseClick3D("z",Objects(1).Z)
		Objects(1).Y = D3ClickY
		`CreateObject(1,D3ClickX,D3ClickY, 0.0)
	Endif
	MoveCamera()
 
	`Draw all of the stuff on the screen
	If Array Count(Objects()) > 0
		For TOB = 1 To Array Count(Objects())
			Position Object Objects(TOB).Num, Objects(TOB).X, Objects(TOB).Y, Objects(TOB).Z	
		next numobject
	Endif
	Sync
 
 
loop
 
 
Function CreateObject(ObjectType,X#,Y#,Z#)
	If ObjectType < 1 Or ObjectType > 1 `NEED TO CHANGE AS MORE OBJECT TYPES ARE ADDED
		cls
		Text 0,0,"ERROR: Object Type Illegal in Create Object Function."
		Text 20,0,"Press any key to continue."
		Sync
		Wait Key
		ExitFunction	
	Endif
 
	Add To Queue Objects(0)
	ArraySpot = Array Count(Objects())
	Objects(ArraySpot).Num = Get_Free_Object_Number()
 
	If ObjectType = 1 Then Make Object Cube Objects(ArraySpot).Num, 1
	`Need to add more objects here as the ability to make more is added
	Position Object Objects(ArraySpot).Num, X#, Y#, Z#
 
	Objects(ArraySpot).X = x#: Objects(ArraySpot).Y = y#: Objects(ArraySpot).Z = z#	
endfunction
 
Function MoveCamera()
	MoveSpeed# = 0.13
	CameraX# = Camera Position X()
	CameraY# = Camera Position Y()
	CameraZ# = Camera Position Z()
 
	If LeftKey() = 1 Then CameraX# = CameraX# - MoveSpeed#
	If RightKey() = 1 Then CameraX# = CameraX# + MoveSpeed#
	If Upkey() = 1 Then CameraZ# = CameraZ# + MoveSpeed#
	If Downkey() = 1 Then CameraZ# = CameraZ# - MoveSpeed#
 
	Position Camera CameraX#,CameraY#,CameraZ#
endfunction
 
 
Function CreateEnvironment()
	cls 0
	Ink RGB(0,0,0),0
	Box 0,0,20,20
	Ink RGB(255,255,255),RGB(255,255,255)
	Line 0,0,20,0
	Line 0,0,0,20
	Line 0,20,20,20
	Line 20,0,20,20
 
	GridImage = Get_Free_Image_Number()
	Get Image GridImage,0,0,20,20
 
	Make Matrix 1, 100,100,100,100
	Prepare Matrix Texture 1, GridImage, 1, 1
	Position Matrix 1, -50,0,-50
 
 
 
endfunction
 
 
 
Function Get_Free_Object_Number()
	Remstart
	This function just finds a free object, and then returns its number.
	This is so that objects can be given variable names rather than numbers!
 
	***Special Thanks to Ric in the Darkbasic Pro forums who I stole this 
	function from.***
	Remend	
 
	Do
		inc ObjectNumber
	 	If Object Exist(ObjectNumber) = 0 Then Exit
	Loop
EndFunction ObjectNumber
 
 
Function Get_Free_Image_Number()
	Remstart
	This function just finds a free image, and then returns its number.
	This is so that images can be given variable names rather than numbers!
 
	***Special Thanks to Ric in the Darkbasic Pro forums who I stole this 
	function from.***
	Remend	
 
 
 
	Do
		inc ImageNumber
		If Image Exist(ImageNumber) = 0 Then Exit
	loop
endfunction ImageNumber
 
Function MouseClick3D(XYorZ$, PlainClicked#)
	Remstart
	This is a function that, using "Pick Screen" and some triangle trigonometry,
	figures out where the mouse is on a 2D plain in 3D space.
 
	XYorZ$ = The plane in question (X returns a position on the YZ plain etc.)
	PlainClicked# = The height of the plain 
						(if XYorZ$ = "y", PlainClicked# = Y co-ordinate of the plain)	
 
	Notes: This function uses the variables "D3ClickX" "D3ClickY" and "D3ClickZ"
			 as return values. 
 
	***Special thanks to CPU on the Darbasic Pro forums for providing a tutorial and
	example code which inspired this function.***
	Remend
 
	`Code that decides which dimension is held constant.
	XYorZ$ = lower$(XYorZ$) 
	If XYorZ$ = "x" Then PlainType = 1
	If XYorZ$ = "y" Then PlainType = 2
	If XYorZ$ = "z" Then PlainType = 3
 
	`Error Checking
	If PlainType = 0: Text 0,0,"Error: Invalid Parameter in XYMouseClick3D Function":Sync: Wait Key: ExitFunction : Endif
 
	`Determine the dimensions of one triangle (with a hypotenus of 1)
	Pick Screen MouseX(),MouseY(),1
	PickX# = Get Pick Vector X()
	PickY# = Get Pick Vector Y()
	PickZ# = Get Pick Vector Z()	
 
	`Calculate the length of the number of times bigger the second triangle is
	If PlainType = 1 Then Raylength# = (Camera Position X()-PlainClicked#)/PickX#: D3ClickX = PlainClicked#
	If PlainType = 2 Then Raylength# = (Camera Position Y()-PlainClicked#)/PickY#: D3ClickY = PlainClicked#
	If PlainType = 3 Then Raylength# = (Camera Position Z()-PlainClicked#)/PickZ#: D3ClickZ = PlainClicked#
 
	`Scale the triangle so that it is the right dimensions
	If PlainType <> 1 Then D3ClickX = Camera Position X() - (PickX#*Raylength#)
	If PlainType <> 2 Then D3ClickY = Camera Position Y() - (PickY#*Raylength#)
	If PlainType <> 3 Then D3ClickZ = Camera Position Z() - (PickZ#*Raylength#) 
EndFunction