Code: Select all
'rotation demo
'use FreeBASIC
'FreeBASIC.net
DEFINT A-Z
REM $DYNAMIC
TYPE Point3D
X AS SINGLE 'Normal 3d coords
Y AS SINGLE
Z AS SINGLE
Xr AS SINGLE
Yr AS SINGLE
Zr AS SINGLE
ScrX AS INTEGER 'Translated and projected
ScrY AS INTEGER '2d Coords
END TYPE
TYPE VectorType
V1 AS INTEGER
V2 AS INTEGER
V3 AS INTEGER
Clr AS INTEGER
END TYPE
DECLARE SUB LoadCube (Model() AS Point3D, Vector() AS VectorType)
DECLARE SUB RotateAndProject (Model() AS Point3D, AngleX%, AngleY%, AngleZ%)
DECLARE SUB DrawCube (Model() AS Point3D, Vector() AS VectorType)
CONST LENS = 256 'Z
CONST XCENTER = 160 '??
CONST YCENTER = 100 '??
CONST PI = 3.14151693#
REDIM SHARED CubeModel(1) AS Point3D
REDIM SHARED CubeVector(1) AS VectorType
DIM SHARED MaxVertex, MaxVector
DIM SHARED ThetaX, ThetaY, ThetaZ
DIM SHARED Zcenter, CamX, CamY
LoadCube CubeModel(), CubeVector()
CLS
SCREEN 13,8,2
RANDOMIZE TIMER
ThetaX = 0
ThetaY = 0
ThetaZ = 0
Zcenter = LENS
CamX = 0
CamY = 0
Camdir = -4
DO
SCREENSET 1, 0
CLS
'ThetaX = (ThetaX + 1) MOD 360
'ThetaY = (ThetaY + 1) MOD 360
'ThetaZ = (ThetaZ + 1) MOD 360
if MULTIKEY(&h4B) then ThetaY = (ThetaY - 1)
if MULTIKEY(&h4D) then ThetaY = (ThetaY + 1)
if MULTIKEY(&h48) then ThetaX = (ThetaX - 1)
if MULTIKEY(&h50) then ThetaX = (ThetaX + 1)
RotateAndProject CubeModel(), ThetaX, ThetaY, ThetaZ
DrawCube CubeModel(), CubeVector()
sleep 1
screensync
SCREENCOPY
LOOP WHILE NOT MULTIKEY(&h1)
END
'numPoints
NumPoints:
DATA 8
'vertices of Cube
VertexData:
DATA -50,-50,-50
DATA 50,-50,-50
DATA 50, 50,-50
DATA -50, 50,-50
DATA -50,-50, 50
DATA 50,-50, 50
DATA 50, 50, 50
DATA -50, 50, 50
NumVector:
DATA 12
'Vector Connect data
ConnectData:
DATA 5,1,8
DATA 1,4,8
DATA 6,5,7
DATA 5,8,7
DATA 2,6,3
DATA 6,7,3
DATA 1,2,4
DATA 2,3,4
DATA 4,3,8
DATA 3,7,8
DATA 5,6,1
DATA 6,2,1
REM $STATIC
SUB DrawCube (Model() AS Point3D, Vector() AS VectorType) STATIC
REDIM RotPoints(1 TO MaxVertex, 1 TO 3)
REDIM ConPoints(1 TO MaxVector, 1 TO 3)
FOR I = 1 TO MaxVertex
RotPoints(I, 1) = Model(I).ScrX
RotPoints(I, 2) = Model(I).ScrY
RotPoints(I, 3) = Model(I).Zr
NEXT I
FOR I = 1 TO MaxVector
ConPoints(I, 1) = Vector(I).V1
ConPoints(I, 2) = Vector(I).V2
ConPoints(I, 3) = Vector(I).V3
NEXT I
FOR I = 1 TO MaxVector
x1 = RotPoints(Vector(I).V1, 1)
x2 = RotPoints(Vector(I).V2, 1)
x3 = RotPoints(Vector(I).V3, 1)
y1 = RotPoints(Vector(I).V1, 2)
y2 = RotPoints(Vector(I).V2, 2)
y3 = RotPoints(Vector(I).V3, 2)
Z1 = RotPoints(Vector(I).V1, 3)
Z2 = RotPoints(Vector(I).V2, 3)
Z3 = RotPoints(Vector(I).V3, 3)
Visible = (x3 - x1) * (y2 - y1) - (x2 - x1) * (y3 - y1)
'IF Visible < 0 THEN ' To see the INSIDE, do "Visible > 256" :)
LINE (x1, y1)-(x2, y2), Vector(I).Clr
LINE (x2, y2)-(x3, y3), Vector(I).Clr
LINE (x3, y3)-(x1, y1), Vector(I).Clr
'END IF
NEXT I
END SUB
SUB LoadCube (Model() AS Point3D, Vector() AS VectorType) STATIC
RESTORE NumPoints
READ MaxVertex
REDIM Model(1 TO MaxVertex) AS Point3D
RESTORE VertexData
FOR V = 1 TO MaxVertex
READ Xt, Yt, Zt
Model(V).X = Xt
Model(V).Y = Yt
Model(V).Z = Zt
NEXT V
RESTORE NumVector
READ MaxVector
REDIM Vector(MaxVector) AS VectorType
RESTORE ConnectData
FOR V = 1 TO MaxVector
READ VT1, VT2, VT3
Vector(V).V1 = VT1
Vector(V).V2 = VT2
Vector(V).V3 = VT3
Vector(V).Clr = 50 + INT(RND * 100)
NEXT V
END SUB
SUB RotateAndProject (Model() AS Point3D, AngleX, AngleY, AngleZ) STATIC
dim as single angx, angy, angz
'convert degrees to radians
angx = anglex * PI/180
angy = angley * PI/180
angz = anglez * PI/180
CX! = COS(AngX)
SX! = SIN(AngX)
CY! = COS(AngY)
SY! = SIN(AngY)
CZ! = COS(AngZ)
SZ! = SIN(AngZ)
'Transformation matrix formula
'This is actually 16(or 12) equations but I pared it down to 9
'since TX4=0,TY4=0,TZ4=0,13 to 16th =0,0,0,1 (yes Doom!!!)
TX1! = CY! * CZ!
TX2! = CY! * SZ!
TX3! = -SY!
TY1! = CX! * -SZ! + SX! * SY! * CZ!
TY2! = CX! * CZ! + SX! * SY! * SZ!
TY3! = SX! * CY!
TZ1! = -SX! * -SZ! + CX! * SY! * CZ!
TZ2! = -SX! * CZ! + CZ! * SY! * SZ!
TZ3! = CX! * CY!
FOR I = 1 TO UBOUND(Model)
X! = Model(I).X 'Load Original model
Y! = Model(I).Y
Z! = Model(I).Z
RotX! = (X! * TX1! + Y! * TY1! + Z! * TZ1!)
RotY! = (X! * TX2! + Y! * TY2! + Z! * TZ2!)
RotZ! = (X! * TX3! + Y! * TY3! + Z! * TZ3!)
Model(I).Xr = RotX!
Model(I).Yr = RotY!
Model(I).Zr = RotZ!
'Project
Distance% = (LENS - RotZ!)
IF Distance% THEN
Model(I).ScrX = (Zcenter * RotX! / Distance%) + XCENTER + CamX
Model(I).ScrY = -(Zcenter * RotY! / Distance%) + YCENTER + CamY
ELSE
END IF
NEXT I
END SUB