Main sections
X_POPMATRIX
X_POPMATRIX
X_POPMATRIX allows you to retrieve the stored 3D matrix. The 3D matrix stores movement, scaling and rotation details for 3D objects.
The X_PUSHMATRIX/X_POPMATRIX commands are useful for drawing several objects as one unit - push the matrix, draw the objects in their positions relative to one another, then pop the original matrix back.
// --------------------------------- //
// Project: PushPopMatrix
// Start: Thursday, November 03, 2005
// IDE Version: 2.50920
donut = 0
sphere = 1
// Create a donut
CreateTorus(donut, 12, 24, 12, 9, 2, 1)
// Create a sphere
CreateSphere(sphere, 24, 12, RGB(255,255,255))
LOCAL mx, my, b1, b2
WHILE TRUE
// 1000 is far enough here
X_MAKE3D 1, 1000, 45
// one angle for everything
phi = GETTIMERALL() / 150
X_CAMERA 300,300,300, 0,0,0
X_SPOT_LT 0, RGB(0,255,255), 0,0,0, -1,-1,-1, 180
// Ring-a-ring-a-rosies
FOR i=0 TO 5
psi = phi + i/6*360
px = SIN(psi)*100
py = COS(psi)*50
pz = (SIN(psi)+COS(psi)*2)*120
// move and rotate a bit
X_MOVEMENT px, py, pz
X_ROTATION psi*40, 3,4,5
// now relative to the donut the spheres
X_PUSHMATRIX
FOR j=0 TO 5
X_MOVEMENT SIN(j/6*360)*50, 0, COS(j/6*360)*50
X_SCALING .3, .3, .3
X_DRAWOBJ sphere, 0
NEXT
X_POPMATRIX
X_DRAWOBJ donut,0
NEXT
SHOWSCREEN
WEND
// ------------------------------------------------------------- //
// -=# SPHERE #=-
// ------------------------------------------------------------- //
FUNCTION CreateSphere: num, r, n, col
LOCAL i,j, theta1, theta2, theta3, pi
pi = ACOS(0)*2
IF r < 0 THEN r = -r
IF n < 4 THEN n = 4
X_AUTONORMALS 2 // smooth edges
X_OBJSTART num
FOR j=0 TO INTEGER(n/2) -1
theta1 = j * 2*pi / n - pi/2;
theta2 = (j + 1) * 2*pi / n - pi/2;
FOR i=0 TO n
theta3 = i * 2*pi / n;
X_OBJADDVERTEX r*COS(theta2) * COS(theta3), r*SIN(theta2), _
r*COS(theta2) * SIN(theta3), i/n, 2*(j+1)/n, col
X_OBJADDVERTEX r*COS(theta1) * COS(theta3), r*SIN(theta1), _
r*COS(theta1) * SIN(theta3), i/n, 2*j/n, col
NEXT
X_OBJNEWGROUP
NEXT
X_OBJEND
ENDFUNCTION // n
// ------------------------------------------------------------- //
// -=# CREATETORUS #=-
//
// By Samuel R. Buss
// http://math.ucsd.edu/~sbuss/MathCG
// ------------------------------------------------------------- //
FUNCTION CreateTorus: num, MinorRadius, MajorRadius, NumWraps, NumPerWrap, TextureWrapVert, TextureWrapHoriz
// Diese Variablen sind als LOCAL definiert:
// These variables are defined as LOCAL:
// x, y,
// Draw the torus
LOCAL i, di, j, wrapFrac, wrapFracTex, phi, thetaFrac, thetaFracTex, theta
LOCAL x, y, z, r
X_AUTONORMALS 2
X_OBJSTART num
FOR di=0 TO NumWraps-1
FOR j=0 TO NumPerWrap
FOR i=di+1 TO di STEP -1
wrapFrac = MOD(j, NumPerWrap)/NumPerWrap
wrapFracTex = j/NumPerWrap
phi = 360*wrapFrac
thetaFrac = (MOD(i, NumWraps)+wrapFracTex)/NumWraps
thetaFracTex = (i+wrapFracTex)/NumWraps
theta = 360*thetaFrac
r = MajorRadius + MinorRadius*COS(phi)
x = SIN(theta)*r
z = COS(theta)*r
y = MinorRadius*SIN(phi)
X_OBJADDVERTEX x,y,z, thetaFracTex*TextureWrapVert, wrapFracTex*TextureWrapHoriz, _
RGB(255, 255, 255)
NEXT
NEXT
X_OBJNEWGROUP
NEXT
X_OBJEND
ENDFUNCTION