home *** CD-ROM | disk | FTP | other *** search
AmigaBASIC Source Code | 1987-04-02 | 9.1 KB | 480 lines |
- ' Try3D -- An Example of 3D Programming
- ' Initial Amiga implementation
- ' by Jim Meadows 6/1/86 Compuserve [75046,2012]
- '
- ' Refer to article in AMAZING COMPUTING 1986 vol.1 / #7, pages 13-23
-
-
- ' 3D Greeting
- CLS: PRINT
- COLOR 3: PRINT " Try3D";
- COLOR 1: PRINT " -- An Example of 3D Programming"
- COLOR 1: PRINT " by Jim Meadows "
- GOSUB InitVals
- GOSUB SetImage
- ' GOSUB SetImage
- ' GOTO Manual
- ef = -1
- ax = 10
- ay = 5
- px = 180
- py = 70
- GOSUB DrawImage
- LOCATE 18,20: COLOR 3: PRINT "Hello!"
- GOSUB Pause
- ax = 10
- ay = -30
- GOSUB DrawImage
- LOCATE 18,3: COLOR 3: PRINT "Welcome to the World of 3D Graphics!"
- GOSUB Pause
- ax = 30
- ay = 20
- GOSUB DrawImage
- LOCATE 18,15: COLOR 3: PRINT "Hmmmmmm....."
- GOSUB Pause
- ax = -30
- ay = -15
- az = 20
- di = 500
- GOSUB DrawImage
- LOCATE 18,8: COLOR 3: PRINT "Yep, just as I figured..."
- GOSUB Pause
- az = -20
- ay = 15
- GOSUB DrawImage
- LOCATE 18,4: COLOR 3: PRINT "You've got what it takes for 3D!"
- GOSUB Pause
- ax = 10
- ay = -20
- az = 0
- di = 900
- GOSUB DrawImage
- LOCATE 18,15: COLOR 3: PRINT "You have ..."
- GOSUB Pause
- ax = -5
- ay = 0
- di = 700
- GOSUB DrawImage
- LOCATE 18,15: COLOR 3: PRINT "... an AMIGA ! !"
- GOSUB Pause
- FOR i = 1 TO 10000: NEXT
- ef = 0
-
-
- Ri:
- ' Rotating Image
- CLS
- LOCATE 19,13: COLOR 3: PRINT "Delta Wing Fighter"
- GOSUB SetImage
- ' Draw and Undraw rotating image
- ax = -90
- ay = 270
- az = 0
- px = 160
- py = 100
- FOR ii = 1 TO 4
- GOSUB DrawImage
- GOSUB Pause
- ef = 1
- GOSUB DrawImage
- ef = 0
- ax = -20
- ay = ay - 60
- IF ii = 1 THEN ax = 0: ay = 270: az = 0
- NEXT ii
-
-
- Fly:
- ' Animated flight path
- FOR inum = 1 TO 2
- IF inum = 2 THEN GOSUB SetImage
- di = 2400
- ax = 0
- ay = 270
- az = 0
- px = 30
- py = 30
- ef = -1
- GOSUB DrawImage
- px = px + 40
- ax = ax - 5
- di = di - 100
- GOSUB DrawImage
- ax = ax - 5
- di = di - 100
- FOR r = 1 TO 3
- px = px + 40
- ay = ay - 20
- di = di - 100
- GOSUB DrawImage
- NEXT r
- FOR r = 1 TO 3
- px = px + 30
- ay = ay - 20
- az = az - 20
- di = di - 100
- GOSUB DrawImage
- NEXT r
- FOR r = 1 TO 4
- px = px - 20
- py = py + 10
- az = az + 10
- di = di - 80
- GOSUB DrawImage
- NEXT r
- FOR r = 1 TO 8
- px = px - 9 * r
- py = py + 10
- az = az + 5
- di = di - 60
- ax = ax - 5
- GOSUB DrawImage
- NEXT r
- NEXT inum
-
-
- ' Finally allow manual control
- GOSUB SetImage
-
- Manual:
-
- MENU 1,0,1,"Rotate +"
- MENU 1,1,1,"Around X-axis"
- MENU 1,2,1,"Around Y-axis"
- MENU 1,3,1,"Around Z-axis"
-
- MENU 2,0,1,"Rotate -"
- MENU 2,1,1,"Around X-axis"
- MENU 2,2,1,"Around Y-axis"
- MENU 2,3,1,"Around Z-axis"
-
- MENU 3,0,1,"Move"
- MENU 3,1,1,"Closer"
- MENU 3,2,1,"Away"
- MENU 3,3,1,"Right"
- MENU 3,4,1,"Left"
- MENU 3,5,1,"Up"
- MENU 3,6,1,"Down"
-
- MENU 4,0,1,"Reset"
- MENU 4,1,1,"Angles"
- MENU 4,2,1,"Distance"
- MENU 4,3,1,"Position"
- MENU 4,4,1,"Quit"
-
- ON MENU GOSUB Menus
- ON MOUSE GOSUB Mous
-
- m1 = 1: GOSUB Reeset
- m1 = 2: GOSUB Reeset
- m1 = 3: GOSUB Reeset
- act = 1
- ef = -1
-
- MOUSE ON
- MENU ON
-
-
- Loop:
- IF act = 0 THEN inc = 1: GOTO Loop
- GOSUB DrawImage
- GOSUB Vals
- IF MOUSE(0) <> -1 THEN act = 0 :ELSE GOSUB Mous
- GOTO Loop
-
-
- '---------------
- ' Subroutines
- '---------------
- Vals:
- COLOR 1
- LOCATE 1,1: PRINT "Ax,Ay,Az: "ax","ay","az
- LOCATE 2,1: PRINT "Px,Py : "px","py
- LOCATE 3,1: PRINT "Di : "di
- COLOR 3
- LOCATE 4,1: PRINT "Use Menus to Change View"
- LOCATE 5,3: PRINT "eg select AWAY view to zoom out "
- COLOR 2
- LOCATE 6,1: PRINT "(press left button to repeat)"
- LOCATE 7,1: PRINT "(keep it pressed to speed things up a little)"
- RETURN
-
- Menus:
- act = 1
- inc = 1
- m0 = MENU(0)
- m1 = MENU(1)
- ON m0 GOSUB RotateP,RotateM,MoveI,Reeset
- RETURN
-
-
- Mous:
- act = 1
- inc = inc + 0.5
- ON m0 GOSUB RotateP,RotateM,MoveI,Reeset
- RETURN
-
-
- RotateP:
- IF m1 = 1 THEN ax = ax + 10 * inc
- IF m1 = 2 THEN ay = ay + 10 * inc
- IF m1 = 3 THEN az = az + 10 * inc
- RETURN
-
-
- RotateM:
- IF m1 = 1 THEN ax = ax - 10 * inc
- IF m1 = 2 THEN ay = ay - 10 * inc
- IF m1 = 3 THEN az = az - 10 * inc
- RETURN
-
-
- MoveI:
- IF m1 = 1 THEN di = di - 50 * inc
- IF m1 = 2 THEN di = di + 50 * inc
- IF m1 = 3 THEN px = px + 20 * inc
- IF m1 = 4 THEN px = px - 20 * inc
- IF m1 = 5 THEN py = py - 10 * inc
- IF m1 = 6 THEN py = py + 10 * inc
- RETURN
-
-
- Reeset:
- IF m1 = 1 THEN ax = -15: ay = -25: az = 0
- IF m1 = 2 THEN di = 1200
- IF m1 = 3 THEN px = 160: py = 100
- IF m1 = 4 THEN MENU OFF: END
- RETURN
-
-
- Pause:
- FOR i = 1 TO 4000: NEXT
- RETURN
-
-
- ' --------------------------------------
- ' | 3-D Routines |
- ' --------------------------------------
-
- ' ax,ay,az = rotation Angle in degrees
- ' di = distance to image
- ' dw = distance to window (projection plane)
- ' px,py = position of image on screen
- ' sf = screen scaling factor
- ' ef = erase flag (1=erase, 0=draw, -1=cls & draw)
- ' Image data is at end of profram
-
- InitVals:
- ' Define Arrays
- DIM it%(100,3): ' Image Table
- DIM rim%(100,3): ' Rotated Image
- ' Initialize Values
- x = 0
- y = 0
- z = 0
- dw = 4000: ' Distance to Window
- di = 900: ' Distance to image
- sf = 2.35: ' Screen scale factor
- ax = 0: ay = 0: az = 0: ' Angles in degrees
- px = 200: py = 100: ' x,y Image Location
- ef = 0: ' Erase Flag
- f = 57.2958: ' Degrees to Radians factor
- RETURN
-
-
- DrawImage:
- ' Draw the Image
- GOSUB Rotate
- GOSUB DrawIt
- RETURN
-
-
- Rotate:
- ' First get trig values from angles
- sx = SIN(ax / f)
- cx = COS(ax / f)
- sy = SIN(ay / f)
- cy = COS(ay / f)
- sz = SIN(az / f)
- cz = COS(az / f)
- ' Then compute rotation values
- xRx = cy * cz
- yRx = -cy * sz
- zRx = -sy
- xRy = cx * sz - sx * sy * cz
- yRy = cx * cz + sx * sy * sz
- zRy = -sx * cy
- xRz = sx * sz + cx * sy * cz
- yRz = sx * cz - cx * sy * sz
- zRz = cx * cy
- ' Now Rotate Image
- np = 0
-
- Rotate1:
- ' Get next point
- c = it%(np, 0): IF c = -1 THEN RETURN
- x = it%(np, 1)
- y = it%(np, 2)
- z = it%(np, 3)
- ' Compute its new location
- rim%(np, 1) = x * xRx + y * yRx + z * zRx
- rim%(np, 2) = x * xRy + y * yRy + z * zRy
- rim%(np, 3) = x * xRz + y * yRz + z * zRz
- np = np + 1
- GOTO Rotate1
-
-
- DrawIt:
- np = 0
- IF ef = -1 THEN CLS
- DrawIt1:
- ' Check for end of table
- c = it%(np, 0)
- IF c = -1 THEN RETURN
- ' Keep from dividing by zero
- IF (rim%(np, 3) + di) = 0 THEN rim%(np, 3) = rim%(np, 3) + 1
- ' Compute screen x & y
- xw = px + (rim%(np, 1) / (rim%(np, 3) + di)) * dw * sf
- yw = py + (rim%(np, 2) / (rim%(np, 3) + di)) * dw
- ' Draw next line or move to next point
- IF c = 0 THEN GOTO JustMove
- colr = c
- IF ef = 1 THEN colr = 0
- LINE (lx, ly)-(xw, yw),colr
- JustMove: lx = xw: ly = yw
- np = np + 1
- GOTO DrawIt1
-
-
- SetImage:
- ' Routine to insert an image into the table
- n = 0
- itloop:
- READ it%(n, 0)
- IF it%(n, 0) = -1 THEN RETURN
- READ it%(n, 1), it%(n, 2), it%(n, 3)
- n = n + 1
- GOTO itloop
-
-
- ' Greeting Image
- ' Image Data Format: c,x,y,z
- ' (c = color, if = 0 then move w/o drawing)
-
- DATA 0,-50,30,0
- DATA 1,-55,35,10
- DATA 1,-45,0,0
- DATA 1,-20,-60,-30
- DATA 1,20,-60,-30
- DATA 1,20,-60,-30
- DATA 1,45,0,0
- DATA 1,55,35,10
- DATA 1,50,30,0
- DATA 3,30,80,-30
- DATA 3,-30,80,-30
- DATA 3,-50,30,0
- DATA 0,0,22,-30
- DATA 1,0,-4,-36
- DATA 0,-5,0,-30
- DATA 1,0,-4,-36
- DATA 1,5,0,-30
- DATA 0,-20,30,-25
- DATA 1,-35,25,-17
- DATA 1,-20,20,-25
- DATA 1,-5,25,-21
- DATA 1,-20,30,-25
- DATA 2,-20,20,-25
- DATA 0,20,30,-25
- DATA 1,35,25,-17
- DATA 1,20,20,-25
- DATA 1,5,25,-21
- DATA 1,20,30,-25
- DATA 2,20,20,-25
- DATA 0,-20,-26,-22
- DATA 3,0,-34,-30
- DATA 3,20,-26,-22
- DATA 0,-10,-30,-26
- DATA 3,10,-30,-26
- DATA -1
-
- ' Delta Wing Fighter Image
- DATA 0,0,-20,100
- DATA 1,0,20,-100
- DATA 0,50,-20,-100
- DATA 1,0,-20,100
- DATA 1,-50,-20,-100
- DATA 2,0,20,-100
- DATA 2,50,-20,-100
- DATA 2,-50,-20,-100
- DATA 0,-75,0,-100
- DATA 3,0,0,0
- DATA 3,75,0,-100
- DATA 3,-75,0,-100
- DATA -1: ' End of Image
-
-
- ' Chaser Image
- DATA 0,-25,0,-25
- DATA 1,25,0,-25
- DATA 1,25,0,25
- DATA 1,-25,0,25
- DATA 1,-25,0,-25
- DATA 0,-25,25,25
- DATA 3,-25,-25,25
- DATA 3,-25,-25,-25
- DATA 3,-25,25,-25
- DATA 3,-25,25,25
- DATA 0,25,25,25
- DATA 3,25,-25,25
- DATA 3,25,-25,-25
- DATA 3,25,25,-25
- DATA 3,25,25,25
- DATA 0,0,0,-25
- DATA 2,0,0,50
- DATA 2,0,10,25
- DATA 2,0,0,-25
- DATA -1
-
-
- ' XYZ axis Image
- DATA 0,-100,0,0
- DATA 1,100,0,0
- DATA 1,80,-20,0
- DATA 0,100,0,0
- DATA 1,80,20,0
- DATA 0,140,14,0
- DATA 1,170,-16,0
- DATA 0,140,-16,0
- DATA 1,170,14,0
- DATA 0,0,-100,0
- DATA 2,0,100,0
- DATA 2,20,80,0
- DATA 0,0,100,0
- DATA 2,-20,80,0
- DATA 0,0,120,0
- DATA 2,0,134,0
- DATA 2,14,148,0
- DATA 0,0,134,0
- DATA 2,-14,148,0
- DATA 0,0,0,100
- DATA 3,0,0,-100
- DATA 3,0,-20,-80
- DATA 0,0,0,-100
- DATA 3,0,20,-80
- DATA 0,-14,14,-140
- DATA 3,16,14,-140
- DATA 3,-14,-16,-140
- DATA 3,16,-16,-140
- DATA -1
-
-
-
-
-
-
-
-
-
-
-
-