Sorceress

bitrot.bas

Feb 21st, 2020
777
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DEFSNG A-Z
  2. DIM i AS LONG, vecs AS LONG, faces AS LONG
  3.  
  4. vecs = 8
  5.  
  6. DATA -1,1,1
  7. DATA 1,1,1
  8. DATA -1,-1,1
  9. DATA 1,-1,1
  10. DATA -1,1,-1
  11. DATA 1,1,-1
  12. DATA 1,-1,-1
  13. DATA -1,-1,-1
  14.  
  15. faces = 12
  16.  
  17. DATA 0,2,1
  18. DATA 1,2,3
  19. DATA 1,3,6
  20. DATA 1,6,5
  21. DATA 0,1,4
  22. DATA 1,5,4
  23. DATA 2,7,3
  24. DATA 3,7,6
  25. DATA 0,7,2
  26. DATA 0,4,7
  27. DATA 4,5,6
  28. DATA 4,6,7
  29.  
  30. '-------------------------------
  31.  
  32. _TITLE "KAJAM8"
  33. TYPE vec_t
  34.   x AS SINGLE 'in world space
  35.   y AS SINGLE
  36.   z AS SINGLE
  37.   tx AS SINGLE 'in camera space
  38.   ty AS SINGLE
  39.   tz AS SINGLE
  40.   px AS SINGLE 'in screen space
  41.   py AS SINGLE
  42. END TYPE
  43. DIM v(vecs) AS vec_t
  44.  
  45. TYPE face_t
  46.   p1 AS INTEGER
  47.   p2 AS INTEGER
  48.   p3 AS INTEGER
  49. END TYPE
  50. DIM f(faces) AS face_t
  51.  
  52. FOR i = 0 TO vecs - 1: READ v(i).x, v(i).y, v(i).z: NEXT
  53. FOR i = 0 TO faces - 1: READ f(i).p1, f(i).p2, f(i).p3: NEXT
  54.  
  55. DIM zbuf(319, 199) AS SINGLE
  56. SCREEN 13
  57. COLOR 15, 25
  58.  
  59. WHILE INKEY$ <> CHR$(27)
  60.   CLS
  61.   ERASE zbuf
  62.  
  63.   'camera location
  64.   cx = 0: cy = 0: cz = -7.5 + 3 * COS(t)
  65.  
  66.   'project vertices
  67.   FOR i = 0 TO vecs - 1
  68.     x = v(i).x: y = v(i).y: z = v(i).z
  69.     a = rotx: GOSUB rotateX
  70.     a = roty: GOSUB rotateY
  71.     a = rotz: GOSUB rotateZ
  72.     GOSUB project
  73.   NEXT
  74.  
  75.   'draw faces
  76.   FOR i = 0 TO faces - 1
  77.     p1 = f(i).p1: p2 = f(i).p2: p3 = f(i).p3
  78.     'backface culling test
  79.     IF (v(p2).px - v(p1).px) * (v(p3).py - v(p1).py) > (v(p3).px - v(p1).px) * (v(p2).py - v(p1).py) THEN
  80.       GOSUB triangle
  81.     END IF
  82.   NEXT
  83.  
  84.   'update
  85.   rotx = rotx + 0.017
  86.   roty = roty + 0.027
  87.   rotz = rotz + 0.037
  88.   t = t + .022
  89.  
  90.   _DISPLAY
  91.   _LIMIT 60
  92. WEND
  93. SYSTEM
  94.  
  95. '-------------------------------
  96.  
  97. triangle:
  98. IF v(p1).py > v(p2).py THEN temp = p2: p2 = p1: p1 = temp
  99. IF v(p2).py > v(p3).py THEN temp = p2: p2 = p3: p3 = temp
  100. IF v(p1).py > v(p2).py THEN temp = p2: p2 = p1: p1 = temp
  101. iy21 = 1 / (v(p2).py - v(p1).py)
  102. iy31 = 1 / (v(p3).py - v(p1).py)
  103. iy32 = 1 / (v(p3).py - v(p2).py)
  104. x21 = (v(p2).px - v(p1).px) * iy21
  105. x31 = (v(p3).px - v(p1).px) * iy31
  106. x32 = (v(p3).px - v(p2).px) * iy32
  107. z21 = (v(p2).tz - v(p1).tz) * iy21
  108. z31 = (v(p3).tz - v(p1).tz) * iy31
  109. z32 = (v(p3).tz - v(p2).tz) * iy32
  110. sy = v(p1).py: IF sy < 0 THEN sy = 0 'enforce y bounds
  111. ey = v(p3).py: IF ey > 199 THEN ey = 199
  112. FOR y = sy TO ey
  113.   dy = y - v(p1).py
  114.   sx = v(p1).px + dy * x31
  115.   sz = v(p1).tz + dy * z31
  116.   IF y < v(p2).py THEN
  117.     ex = v(p1).px + dy * x21
  118.     ez = v(p1).tz + dy * z21
  119.   ELSE
  120.     dy = y - v(p2).py
  121.     ex = v(p2).px + dy * x32
  122.     ez = v(p2).tz + dy * z32
  123.   END IF
  124.   IF sx > ex THEN
  125.     temp = sx: sx = ex: ex = temp: temp = sz: sz = ez: ez = temp
  126.   END IF
  127.   IF sx < 0 THEN sx = 0 'enforce x bounds
  128.   IF ex > 319 THEN ex = 319
  129.   dzdx = (ez - sz) / (ex - sx): z = sz - 100
  130.   FOR x = sx TO ex 'draw scanline (tight loop, some optimisation)
  131.     IF zbuf(x, y) > z THEN PSET (x, y), i: zbuf(x, y) = z
  132.     z = z + dzdx
  133.   NEXT
  134. NEXT
  135. RETURN
  136.  
  137. bline:
  138. LINE (v(p0).px, v(p0).py)-(v(p1).px, v(p1).py), 15
  139. RETURN
  140.  
  141. project:
  142. x = x - cx: y = y - cy: z = z - cz
  143. zinv = 1 / z
  144. v(i).tx = x: v(i).ty = y: v(i).tz = z
  145. v(i).px = 160 + 400 * x * zinv
  146. v(i).py = 100 - 400 * y * zinv
  147. RETURN
  148.  
  149. rotateX:
  150. ca = COS(a): sa = SIN(a)
  151. y1 = y * ca - z * sa
  152. z1 = y * sa + z * ca
  153. y = y1: z = z1
  154. RETURN
  155.  
  156. rotateY:
  157. ca = COS(a): sa = SIN(a)
  158. x1 = x * ca - z * sa
  159. z1 = x * sa + z * ca
  160. x = x1: z = z1
  161. RETURN
  162.  
  163. rotateZ:
  164. ca = COS(a): sa = SIN(a)
  165. x1 = x * ca - y * sa
  166. y1 = x * sa + y * ca
  167. x = x1: y = y1
  168. RETURN
RAW Paste Data