3d.cmn 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # primitive interactive 3D cube rendered as ASCII
  2. SCREEN_W: 40 .
  3. SCREEN_H: 30 .
  4. FOV: 10 .
  5. N: 16 .
  6. NN: N -1 * .
  7. N2: N 2 * .
  8. SHIFT: 5 .
  9. ~8
  10. ~screen:8000
  11. ~tmp:0
  12. ~0
  13. clearScreen:
  14. SCREEN_W SCREEN_H *
  15. ~8
  16. $screen>tmp
  17. ~0
  18. @'
  19. ~8
  20. " " $:tmp
  21. $>tmp
  22. ~0
  23. --
  24. .
  25. ^
  26. .
  27. printScreen:
  28. ~8
  29. $screen>tmp
  30. ~0
  31. SCREEN_H
  32. @'
  33. SCREEN_W
  34. @'
  35. ~8
  36. $tmp $0 -> ->
  37. $>tmp
  38. ~0
  39. --
  40. .
  41. ^
  42. 10 ->
  43. --
  44. .
  45. ^
  46. .
  47. setPixel: # in: x y
  48. $1 SCREEN_W < $1 SCREEN_H < && ?
  49. SCREEN_W * +
  50. ~8
  51. $screen>tmp
  52. ~0
  53. @'
  54. ~8 $>tmp ~0
  55. --
  56. .
  57. ~8
  58. "." $:tmp
  59. ~0
  60. ^
  61. ;
  62. ^ ^
  63. .
  64. .
  65. perspective: # in: coord dist
  66. >< FOV * >< //
  67. .
  68. drawPoint3D: # in: x y z
  69. $2 $1 perspective SCREEN_W 2 / +
  70. $2 $2 perspective SCREEN_H 2 / +
  71. setPixel
  72. ^ ^ ^
  73. .
  74. drawLine3D: # in: x1 y1 z1 x2 y2 z2 n
  75. $6 $4 + 2 //
  76. $6 $4 + 2 //
  77. $6 $4 + 2 //
  78. $3 ?
  79. $6 $6 $6 $5 $5 $5 $9 -- drawLine3D
  80. $9 $9 $9 $5 $5 $5 $9 -- drawLine3D
  81. .
  82. drawPoint3D
  83. ^
  84. drawPoint3D
  85. drawPoint3D
  86. .
  87. ~offsetX
  88. ~offsetY
  89. PA: N $offsetX + N $offsetY + N .
  90. PB: NN $offsetX + N $offsetY + N .
  91. PC: NN $offsetX + N $offsetY + N2 .
  92. PD: N $offsetX + N $offsetY + N2 .
  93. PE: N $offsetX + NN $offsetY + N .
  94. PF: NN $offsetX + NN $offsetY + N .
  95. PG: NN $offsetX + NN $offsetY + N2 .
  96. PH: N $offsetX + NN $offsetY + N2 .
  97. 0 $:offsetX
  98. 0 $:offsetY
  99. @@
  100. clearScreen
  101. PA PB 4 drawLine3D
  102. PB PC 4 drawLine3D
  103. PC PD 4 drawLine3D
  104. PD PA 4 drawLine3D
  105. PA PE 4 drawLine3D
  106. PB PF 4 drawLine3D
  107. PC PG 4 drawLine3D
  108. PD PH 4 drawLine3D
  109. PE PF 4 drawLine3D
  110. PF PG 4 drawLine3D
  111. PG PH 4 drawLine3D
  112. PH PE 4 drawLine3D
  113. printScreen
  114. 0 10 "enter command (w, s, a, d, q): " -->
  115. <-
  116. $0 "q" = ?
  117. !@
  118. ; $0 "d" = ?
  119. $offsetX SHIFT + $:offsetX
  120. ; $0 "a" = ?
  121. $offsetX SHIFT - $:offsetX
  122. ; $0 "s" = ?
  123. $offsetY SHIFT + $:offsetY
  124. ; $0 "w" = ?
  125. $offsetY SHIFT - $:offsetY
  126. . . . . .
  127. ^
  128. .