home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Amos / amproe2x.dms / in.adf / Compiler_Examples / AMOS_versions / 3Dcube3.AMOS / 3Dcube3.amosSourceCode
Encoding:
AMOS Source Code  |  1993-06-16  |  5.5 KB  |  268 lines

  1. ' ---------------------------------------    
  2. '
  3. ' AMOSPro Compiler Example 
  4. '
  5. ' Precalulated surface lit 3D cuboid   
  6. '
  7. ' By Jean-Baptiste BOLCATO 
  8. '
  9. ' (c) 1993 Europress Software Ltd. 
  10. '
  11. ' ---------------------------------------    
  12. '
  13. '
  14. ' -------------------------------------------- 
  15. '   remark:  A fast surface lit 3D filled cube!  
  16. '            (precalulated version!) 
  17. '
  18. '            Average Acceleration:  400 %
  19. '
  20. '            Test configuration: A1200, 6Mb  
  21. '            Original AMOS Compiler:  250 %  
  22. ' -----------------------------------------------
  23.  
  24.  
  25. ' ---- Variables Init ---- 
  26.  
  27. ' Ntsc test
  28. YMID=128-28*(Ntsc=True)
  29.  
  30. ' Lens paramenter
  31. DIST=800 : ZOM=350
  32.  
  33. ' Number of loops
  34. STP=1 : NL=500
  35.  
  36. ' Init coords  
  37. Restore _DATA_POINTS
  38. Read NP
  39. Dim XP(NP),YP(NP),ZP(NP)
  40. Dim XP2(NP),YP2(NP),ZP2(NP)
  41. Dim X(4),Y(4),Z(4)
  42. For I=1 To NP
  43.    Read XP(I),YP(I),ZP(I)
  44. Next I
  45.  
  46. Restore _DATA_FACES
  47. Read NF
  48. Dim P(NF,4)
  49. For I=1 To NF
  50.    For J=1 To 4
  51.       Read P(I,J)
  52.    Next J
  53. Next I
  54.  
  55. ' ---- Precalculation ---- 
  56.  
  57. Screen Open 0,320,32,2,Lowres
  58. Flash Off : Curs Off : Hide : Palette 0,$FFF
  59. Cls 0 : Paper 0 : Pen 1
  60. Print "Precalculating..."
  61.  
  62. Reserve As Work 10,360*6*(8+1)*2
  63. ADR0=Start(10)
  64. ADR=ADR0
  65.  
  66. Degree 
  67.  
  68. XMIN=320 : XMAX=0
  69. YMIN=256 : YMAX=0
  70.  
  71. Timer=0
  72.  
  73. For TETA=0 To 359 Step STP
  74.    
  75.    Locate 20,0 : Print(100*TETA)/359;"% complete";
  76.    
  77.    ' Increment rotation angles
  78.    Add PHI,3,0 To 359
  79.    
  80.    ' Precalculation of COSs and SINs  
  81.    ' ( *256 to stay in integer mode (faster!))    
  82.    CPHI=Cos(PHI)*256
  83.    SPHI=Sin(PHI)*256
  84.    CTETA=Cos(TETA)*256
  85.    STETA=Sin(TETA)*256
  86.    
  87.    
  88.    ' Calculation of rotated 3D coords of A,B,C,D of all points  
  89.    ' ( /256 to bring back COS and SIN beetween 0->1)    
  90.    ' Rotation (X-Z with PHI) & rotation (X-Y with TETA) 
  91.    For I=1 To NP
  92.       XP2(I)=(XP(I)*CPHI+ZP(I)*SPHI)/256
  93.       ZP2(I)=(ZP(I)*CPHI-XP(I)*SPHI)/256
  94.       YP2(I)=(YP(I)*CTETA-XP2(I)*STETA)/256
  95.       XP2(I)=(XP2(I)*CTETA+YP(I)*STETA)/256
  96.    Next I
  97.    
  98.    For I=1 To NF
  99.       
  100.       ' Calculation of AB vector 
  101.       X1=XP2(P(I,2))-XP2(P(I,1))
  102.       Y1=YP2(P(I,2))-YP2(P(I,1))
  103.       Z1=ZP2(P(I,2))-ZP2(P(I,1))
  104.       
  105.       ' Calculation of AC vector 
  106.       X2=XP2(P(I,3))-XP2(P(I,1))
  107.       Y2=YP2(P(I,3))-YP2(P(I,1))
  108.       Z2=ZP2(P(I,3))-ZP2(P(I,1))
  109.       
  110.       ' Calculation of PV vector = AB^AC   
  111.       PVX=Y1*Z2-Z1*Y2
  112.       PVY=Z1*X2-X1*Z2
  113.       PVZ=X1*Y2-Y1*X2
  114.       
  115.       ' Calculation of ENL scalair product PV.AB   
  116.       ' (proportionnal with Enlightment of a Face) 
  117.       ENL=PVX*XP2(P(I,1))+PVY*YP2(P(I,1))+PVZ*(ZP2(P(I,1))+DIST)
  118.       
  119.       ' Positive ENL means that the face is hidden 
  120.       If ENL<0
  121.          
  122.          ' Bring back ENL from 0 to 15 for use with Ink 
  123.          ENL=-ENL*16
  124.          ENL=ENL/(Sqr(PVX^2+PVY^2+PVZ^2))
  125.          ENL=ENL/(Sqr(X(1)^2+Y(1)^2+(Z(1)+DIST)^2))
  126.          If ENL>=15 : ENL=15 : End If 
  127.          
  128.          ' Fill bank with lit color 
  129.          Doke ADR,ENL : Add ADR,2
  130.          
  131.          ' Calculation of screen coords of A,B,C,D and fill up bank   
  132.          For J=1 To 4
  133.             X=160+(XP2(P(I,J))*ZOM)/(DIST+ZP2(P(I,J)))
  134.             Doke ADR,X : Add ADR,2
  135.             If X<XMIN : XMIN=X : End If 
  136.             If X>XMAX : XMAX=X+2 : End If 
  137.             
  138.             Y=YMID+(YP2(P(I,J))*ZOM)/(DIST+ZP2(P(I,J)))
  139.             Doke ADR,Y : Add ADR,2
  140.             If Y<YMIN : YMIN=Y : End If 
  141.             If Y>YMAX : YMAX=Y+2 : End If 
  142.          Next J
  143.          
  144.       End If 
  145.    Next I
  146.    
  147.    ' Flag end of cube frame 
  148.    Doke ADR,65535 : Add ADR,2
  149.    
  150. Next TETA
  151.  
  152. ' Flag end rotation
  153. Doke ADR,65281
  154.  
  155. ' ---- Status Report ----  
  156.  
  157. T#=Timer
  158. Cls 0 : Paper 0 : Pen 1
  159. Home : Print "Takes";T#/50;"s for the whole calculation."
  160. Print 
  161. Print "    Press mouse key to continue"
  162. Repeat 
  163.    Multi Wait 
  164. Until Mouse Key or(Inkey$<>"")
  165.  
  166. ' ---- Screen Init ----
  167.  
  168. Screen Open 0,320,YMID*2,16,Lowres
  169. Flash Off : Curs Off : Hide : Cls 0
  170. For I=0 To 15 : Colour I,I*$111 : Next I
  171.  
  172. Double Buffer 
  173. Autoback 0
  174.  
  175. ' ---- Main Loop ----
  176.  
  177. Timer=0
  178.  
  179. ADR=ADR0
  180.  
  181. ' Pop up first face color  
  182. ENL=Deek(ADR) : Add ADR,2
  183.  
  184. For N=1 To NL
  185.    
  186.    Cls 0,XMIN,YMIN To XMAX,YMAX
  187.    
  188.    ' Describe all the cube
  189.    While ENL<>65535
  190.       
  191.       ' Pop up ABCD X,Y from bank  
  192.       For J=1 To 4
  193.          X(J)=Deek(ADR) : Add ADR,2
  194.          Y(J)=Deek(ADR) : Add ADR,2
  195.       Next J
  196.       
  197.       ' Draw Faces!
  198.       Ink ENL
  199.       Polygon X(1),Y(1) To X(2),Y(2) To X(3),Y(3) To X(4),Y(4)
  200.       
  201.       
  202.       ' Pop up next face color 
  203.       ENL=Deek(ADR) : Add ADR,2
  204.    Wend 
  205.    
  206.    ENL=Deek(ADR) : Add ADR,2
  207.    
  208.    ' Pop up back to first face color  
  209.    If ENL=65281
  210.       ADR=ADR0
  211.       ENL=Deek(ADR) : Add ADR,2
  212.    End If 
  213.    
  214.    Screen Swap 
  215.    Wait Vbl 
  216.    
  217. Next N
  218.  
  219. T#=Timer
  220.  
  221. ' --- 2nd Final Report --- 
  222.  
  223. Autoback 1 : Cls 0 : Paper 0 : Pen 15
  224. Print "  Needs";T#/50;" seconds for";NL;" loops."
  225. V#=T#/NL
  226. Print "          ( =";V#;" VBLs )"
  227. Print 
  228. Print "   Press mouse key to end"
  229. Repeat 
  230.    Multi Wait 
  231. Until Mouse Key or(Inkey$<>"")
  232. End 
  233.  
  234. ' ---- Cube Data ----  
  235.  
  236. _DATA_POINTS:
  237. Data 8
  238.  
  239. Data -100,-100,-100
  240. Data 100,-100,-100
  241. Data 100,100,-100
  242. Data -100,100,-100
  243.  
  244. Data -100,-100,100
  245. Data 100,-100,100
  246. Data 100,100,100
  247. Data -100,100,100
  248.  
  249. '       5----------6                     
  250. '      /.         /|             
  251. '     / .        / |                 
  252. '    1----------2  |                         
  253. '    |  .       |  |                 
  254. '    |          |  |               
  255. '    |  8 . . . |. 7               
  256. '    | .        | /                    
  257. '    |.         |/                   
  258. '    4----------3              
  259. '                            
  260.  
  261. _DATA_FACES:
  262. Data 6
  263. Data 1,4,3,2
  264. Data 1,2,6,5
  265. Data 3,4,8,7
  266. Data 5,6,7,8
  267. Data 1,5,8,4
  268. Data 2,3,7,6