plot 3d to 2d

Blitz3D Forums/Blitz3D Beginners Area/plot 3d to 2d

hello all,.
i have an array of 3d points(xyz) that i want to plot to the screen(xy) and then rotate the plotted object like those old amiga demos..can anyone help me out with some source to get it working..Thanks.

You might have samles in your /samples/Blitz 2D samples/ directory called 3ddotball.bb and 3dwireframe.bb. They can be easilly modified to get you started...

Andy

gahh..didnt even think to look..thanks

had a look but cant find those files..dont seem to be in the code archives either.any ideas where i can get them.
thanks.

This is the dot ball one ..

; *****************************
; * DEPTH COLORED 3D DOT BALL *
; *****************************
; * CODING BY TRACER          *
; *****************************

dots_in_ball = 1440											; How many dots are in the ball?

; Globals for the 3d.
Global numpoints = dots_in_ball								; Number of points in the point table.
Global distance  = 400										; Needed for perspective.
Global vx#													; X location.
Global vy#													; Y location.
Global vz#													; Z location.

; Arrays used by 3d code.
Dim points(numpoints, 3)									; Holds the point locations of the game over 3d.

Graphics 640,480

SetBuffer BackBuffer()

; make points on the surface of a sphere
; calculation to make a sphere:
; x = cos(theta) * cos(phi)
; y = cos(theta) * sin(phi)
; z = sin(theta)
; where theta = -90 to 90
; and phi = 0 to 360
For t= 1 To dots_in_ball
	xd = Rnd(-90,90)
	x0 = (Cos(xd) * 10) * (Cos(t) * 10)
	y0 = (Cos(xd) * 10) * (Sin(t) * 10)
	z0 = Sin(xd) * 100
	points(t,1) = x0
	points(t,2) = y0
	points(t,3) = z0
Next

; Loop till esc is pressed.
While Not KeyDown(1)
	threed()												; Call the threed thing.
	Color 255,255,255
	Text 0,0,"Depth Colored Real 3D Dot Ball"
	dummy = MouseX()										; Removes the mouse from fullscreen.
	Flip													; Flip the screen.
	Cls														; Clear the screen.
Wend

Function threed()
	vx# = vx# + 0.5											; X rotation speed of ball.
	vy# = vy# + 0.5											; Y rotation speed of ball.
	vz# = vz# + 0.5											; Z rotation speed of ball.

	For n = 1 To numpoints
		x3d = points(n, 1)
		y3d = points(n, 2)
		z3d = points(n, 3)
       
		ty# = ((y3d * Cos(vx#)) - (z3d * Sin(vx#)))
		tz# = ((y3d * Sin(vx#)) + (z3d * Cos(vx#)))
		tx# = ((x3d * Cos(vy#)) - (tz# * Sin(vy#)))
		tz# = ((x3d * Sin(vy#)) + (tz# * Cos(vy#)))
		ox# = tx#
		tx# = ((tx# * Cos(vz#)) - (ty# * Sin(vz#)))
		ty# = ((ox# * Sin(vz#)) + (ty# * Cos(vz#)))
		nx  = Int(512 * (tx#) / (distance - (tz#))) + 320
		ny  = Int(240 - (512 * ty#) / (distance - (tz#)))
      
		setcolor(tz#)
		Rect nx,ny,1,1
	Next
End Function

; This function looks at the z-value of the pixel
; and sets the color accoordingly.
Function setcolor(t#)
	If t# <= 100 And t# >= 75
		Color 250,250,250
	EndIf
	If t# <= 75 And t# >= 50
		Color 225,225,225
	EndIf
	If t# <= 50 And t# >= 25
		Color 200,200,200
	EndIf
	If t# <= 25 And t# >= 0
		Color 175,175,175
	EndIf
	If t# <= 0 And t# >= -25
		Color 150,150,150
	EndIf
	If t# <= -25 And t# >= -50
		Color 125,125,125
	EndIf
	If t# <= -50 And t# >= -75
		Color 100,100,100
	EndIf
	If t# <= -75 And t# >= -100
		Color 50,50,50
	EndIf
End Function



thats nice.should be able to work it out from here.thanks

another question.
vx# = vx# + 0.5 ; X rotation speed of ball.
vy# = vy# + 0.5 ; Y rotation speed of ball.
vz# = vz# + 0.5 ; z rotation speed of ball.

i cant seem to make the ball rotate on the y axis.
if i rem vy,vz the ball spins on x.
if i rem out vx,vy ball spins on z.
but if i rem vx,vz ball is stationary.
I really need to be able to rotate independently on all 3 axes. Is this possible?

This is the other example, which is much better to learn from if you wish to make wireframe graphics...

I actually modified this into a flight sim complete with an aircraft carrier you could land on and buildings, while learning B2D...

I had only heard of Blitz on the darkbasic forum, so when a friend of mine bought it, I went over to have a look. I started playing around with Tracers 3D demos and then I just had to go buy Blitz...

EDIT: In reference to your problem, it works perfectly here!

; *********************************
; * 3D WIREFRAME GRAPHICS EXAMPLE *
; *********************************
; * Coding by: Tracer             *
; *********************************
; * Coded as an example program   *
; * for the Blitz CD.             *
;**********************************

Graphics 640,480
SetBuffer BackBuffer()

Global numpoints = 27										; Number of points in the point table.
Global numconn   = 26										; Number of connections in the connect table.
Global distance  = 400										; Needed for perspective.
Global vx#													; X location.
Global vy#													; Y location.
Global vz#													; Z location.

; Arrays used by 3d code.
Dim points(numpoints, 3)									; Holds the point locations of the game over 3d.
Dim rotated(numpoints, 2)									; Holds rotated points for the 3d.
Dim connect(numconn, 2)										; Holds the connections of the 3d.

; Read data for 3d.
Restore points
For n = 1 To numpoints
	Read x3d, y3d, z3d
	points(n, 1) = x3d
	points(n, 2) = y3d
	points(n, 3) = z3d
Next
Restore conns
For n = 1 To numconn
	Read a3d, b3d
	connect(n, 1) = a3d
	connect(n, 2) = b3d
Next 

While Not KeyDown(1)
	threed()
	Flip
	Cls
Wend

; 3D calculations mayhem.
; Basically it calculates the points in 3d
; and connects these points using the
; connections table.
Function threed()
	vx# = vx# + 0.5
	vy# = vy# + 0.5
	vz# = vz# + 0.5

	For n = 1 To numpoints
		x3d = points(n, 1)
		y3d = points(n, 2)
		z3d = points(n, 3)
       
		ty# = ((y3d * Cos(vx#)) - (z3d * Sin(vx#)))
		tz# = ((y3d * Sin(vx#)) + (z3d * Cos(vx#)))
		tx# = ((x3d * Cos(vy#)) - (tz# * Sin(vy#)))
		tz# = ((x3d * Sin(vy#)) + (tz# * Cos(vy#)))
		ox# = tx#
		tx# = ((tx# * Cos(vz#)) - (ty# * Sin(vz#)))
		ty# = ((ox# * Sin(vz#)) + (ty# * Cos(vz#)))
		nx  = Int(512 * (tx#) / (distance - (tz#))) + 320
		ny  = Int(240 - (512 * ty#) / (distance - (tz#)))
      
		rotated(n, 1) = nx
		rotated(n, 2) = ny
	Next

	For n = 1 To numconn
		Color 255,255,255
		Line rotated(connect(n, 1), 1), rotated(connect(n, 1), 2),rotated(connect(n, 2), 1), rotated(connect(n, 2), 2)
	Next
End Function


; All the data for the 3d game over.. lots of work to make :)

.points
; Points table
;      X,  Y,  Z
Data -100, 50, 50
Data  100, 50, 50
Data  100,-50, 50
Data -100,-50, 50

Data -100, 50,-50
Data  100, 50,-50
Data  100,-50,-50
Data -100,-50,-50

Data   0,-25, 0
Data   0, 25, 0

Data -35,-25, 0
Data -35, 25, 0
Data  -5,-25, 0

Data   5, 25, 0
Data  35, 25, 0
Data  20, 25, 0
Data  20,-25, 0

Data -70, 25, 0
Data -70,-25, 0
Data -50, 25, 0
Data -50,  0, 0
Data -40,  0, 0
Data -40,-25, 0

Data  40, 25, 0
Data  70, 25, 0
Data  40,-25, 0
Data  70,-25, 0

.conns
; Connections  (From,To, From,To, ..)

Data 1,2,2,3,3,4,4,1
Data 5,6,6,7,7,8,8,5

Data 1,5,2,6,3,7,4,8

Data 9,10
Data 11,12,11,13
Data 14,15,16,17
Data 18,19,18,20,20,21,21,22,22,23,23,19
Data 24,25,26,27,25,26