gemcube.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. {
  2. Copyright (c) 2017 Karoly Balogh
  3. Rotating 3D cube in a GEM window
  4. Example program for Free Pascal's Atari TOS bindings
  5. This example program is in the Public Domain under the terms of
  6. Unlicense: http://unlicense.org/
  7. **********************************************************************}
  8. {$APPTYPE GUI}
  9. program gemcube;
  10. uses
  11. aes, vdi, xbios;
  12. type
  13. tvertex = record
  14. x: longint;
  15. y: longint;
  16. z: longint;
  17. end;
  18. const
  19. cube: array[0..7] of tvertex = (
  20. ( x: -1; y: -1; z: -1; ), // 0
  21. ( x: 1; y: -1; z: -1; ), // 1
  22. ( x: 1; y: 1; z: -1; ), // 2
  23. ( x: -1; y: 1; z: -1; ), // 3
  24. ( x: -1; y: -1; z: 1; ), // 4
  25. ( x: 1; y: -1; z: 1; ), // 5
  26. ( x: 1; y: 1; z: 1; ), // 6
  27. ( x: -1; y: 1; z: 1; ) // 7
  28. );
  29. type
  30. tface = record
  31. v1, v2, v3: longint;
  32. edge: longint;
  33. end;
  34. const
  35. faces: array[0..11] of tface = (
  36. ( v1: 0; v2: 2; v3: 1; edge: 6), // front
  37. ( v1: 2; v2: 0; v3: 3; edge: 6),
  38. ( v1: 0; v2: 1; v3: 4; edge: 5), // top
  39. ( v1: 1; v2: 5; v3: 4; edge: 3),
  40. ( v1: 3; v2: 0; v3: 7; edge: 5), // left
  41. ( v1: 0; v2: 4; v3: 7; edge: 3),
  42. ( v1: 1; v2: 2; v3: 5; edge: 5), // right
  43. ( v1: 1; v2: 6; v3: 5; edge: 6),
  44. ( v1: 2; v2: 3; v3: 6; edge: 5), // bottom
  45. ( v1: 3; v2: 7; v3: 6; edge: 3),
  46. ( v1: 4; v2: 5; v3: 6; edge: 3), // back
  47. ( v1: 6; v2: 7; v3: 4; edge: 3)
  48. );
  49. const
  50. sincos_table: array[0..255] of longint = (
  51. 0, 1608, 3216, 4821, 6424, 8022, 9616, 11204,
  52. 12785, 14359, 15924, 17479, 19024, 20557, 22078, 23586,
  53. 25079, 26557, 28020, 29465, 30893, 32302, 33692, 35061,
  54. 36409, 37736, 39039, 40319, 41575, 42806, 44011, 45189,
  55. 46340, 47464, 48558, 49624, 50659, 51664, 52638, 53580,
  56. 54490, 55367, 56211, 57021, 57797, 58537, 59243, 59913,
  57. 60546, 61144, 61704, 62227, 62713, 63161, 63571, 63943,
  58. 64276, 64570, 64826, 65042, 65219, 65357, 65456, 65515,
  59. 65535, 65515, 65456, 65357, 65219, 65042, 64826, 64570,
  60. 64276, 63943, 63571, 63161, 62713, 62227, 61704, 61144,
  61. 60546, 59913, 59243, 58537, 57797, 57021, 56211, 55367,
  62. 54490, 53580, 52638, 51664, 50659, 49624, 48558, 47464,
  63. 46340, 45189, 44011, 42806, 41575, 40319, 39039, 37736,
  64. 36409, 35061, 33692, 32302, 30893, 29465, 28020, 26557,
  65. 25079, 23586, 22078, 20557, 19024, 17479, 15924, 14359,
  66. 12785, 11204, 9616, 8022, 6424, 4821, 3216, 1608,
  67. 0, -1608, -3216, -4821, -6424, -8022, -9616,-11204,
  68. -12785,-14359,-15924,-17479,-19024,-20557,-22078,-23586,
  69. -25079,-26557,-28020,-29465,-30893,-32302,-33692,-35061,
  70. -36409,-37736,-39039,-40319,-41575,-42806,-44011,-45189,
  71. -46340,-47464,-48558,-49624,-50659,-51664,-52638,-53580,
  72. -54490,-55367,-56211,-57021,-57797,-58537,-59243,-59913,
  73. -60546,-61144,-61704,-62227,-62713,-63161,-63571,-63943,
  74. -64276,-64570,-64826,-65042,-65219,-65357,-65456,-65515,
  75. -65535,-65515,-65456,-65357,-65219,-65042,-64826,-64570,
  76. -64276,-63943,-63571,-63161,-62713,-62227,-61704,-61144,
  77. -60546,-59913,-59243,-58537,-57797,-57021,-56211,-55367,
  78. -54490,-53580,-52638,-51664,-50659,-49624,-48558,-47464,
  79. -46340,-45189,-44011,-42806,-41575,-40319,-39039,-37736,
  80. -36409,-35061,-33692,-32302,-30893,-29465,-28020,-26557,
  81. -25079,-23586,-22078,-20557,-19024,-17479,-15924,-14359,
  82. -12785,-11204, -9616, -8022, -6424, -4821, -3216, -1608
  83. );
  84. function sin(x: longint): longint; inline;
  85. begin
  86. sin:=sincos_table[x and 255];
  87. end;
  88. function cos(x: longint): longint; inline;
  89. begin
  90. cos:=sincos_table[(x + 64) and 255];
  91. end;
  92. function mulfp(a, b: longint): longint; inline;
  93. begin
  94. mulfp:=sarint64((int64(a) * b),16);
  95. end;
  96. function divfp(a, b: longint): longint;
  97. begin
  98. divfp:=(int64(a) shl 16) div b;
  99. end;
  100. procedure rotate_vertex(const v: tvertex; var vr: tvertex; xa, ya, za: longint);
  101. var
  102. x,y,z: longint;
  103. s,c: longint;
  104. begin
  105. s :=sin(ya);
  106. c :=cos(ya);
  107. x :=mulfp(c,v.x) - mulfp(s,v.z);
  108. z :=mulfp(s,v.x) + mulfp(c,v.z);
  109. if za <> 0 then
  110. begin
  111. vr.x:=mulfp(cos(za),x) + mulfp(sin(za),v.y);
  112. y :=mulfp(cos(za),v.y) - mulfp(sin(za),x);
  113. end
  114. else
  115. begin
  116. vr.x:=x;
  117. y:=v.y;
  118. end;
  119. vr.z:=mulfp(cos(xa),z) - mulfp(sin(xa),y);
  120. vr.y:=mulfp(sin(xa),z) + mulfp(cos(xa),y);
  121. end;
  122. procedure perspective_vertex(const v: tvertex; zc: longint; var xr,yr: longint);
  123. var
  124. rzc: longint;
  125. begin
  126. rzc:=divfp(1 shl 16,(v.z - zc));
  127. xr:=mulfp(mulfp(v.x,zc),rzc);
  128. yr:=mulfp(mulfp(v.y,zc),rzc);
  129. end;
  130. procedure init_cube;
  131. var
  132. i: longint;
  133. begin
  134. for i:=low(cube) to high(cube) do
  135. begin
  136. cube[i].x:=cube[i].x shl 16;
  137. cube[i].y:=cube[i].y shl 16;
  138. cube[i].z:=cube[i].z shl 16;
  139. end;
  140. end;
  141. const
  142. win_info: array[0..63] of char = '';
  143. var
  144. appl_h: smallint;
  145. win_h: smallint;
  146. win_name: pchar;
  147. vdi_h: smallint;
  148. mx, my: smallint;
  149. const
  150. WIN_KIND = NAME or INFO or CLOSER or MOVER or SIZER or FULLER;
  151. function open_vwk: smallint;
  152. var
  153. work_in: array[0..16] of smallint;
  154. work_out: array[0..64] of smallint;
  155. dummy, i: smallint;
  156. handle: smallint;
  157. xyarray: array[0..3] of smallint;
  158. begin
  159. handle:=graf_handle(@dummy,@dummy,@dummy,@dummy);
  160. work_in[0]:=2+xbios_getrez();
  161. for i:=1 to 9 do work_in[i]:=1;
  162. work_in[10]:=2;
  163. v_opnvwk(@work_in, @handle, @work_out);
  164. xyarray[0]:=0;
  165. xyarray[1]:=0;
  166. xyarray[2]:=work_out[0];
  167. xyarray[3]:=work_out[1];
  168. vs_clip(handle,1,@xyarray);
  169. open_vwk:=handle;
  170. end;
  171. function open_win: smallint;
  172. var
  173. handle: smallint;
  174. dim: TGRECT;
  175. begin
  176. handle:=wind_create(WIN_KIND, 0, 0, 0, 0);
  177. win_name:='FPC GEM Cube';
  178. wind_set(handle, WF_NAME, hi(ptruint(win_name)), lo(ptruint(win_name)), 0, 0);
  179. win_info:='Spinning...';
  180. wind_set(handle, WF_INFO, hi(ptruint(@win_info)), lo(ptruint(@win_info)), 0, 0);
  181. wind_get(0, WF_WORKXYWH, @dim.x, @dim.y, @dim.w, @dim.h);
  182. dim.x:=dim.x + (dim.w div 20);
  183. dim.y:=dim.y + (dim.h div 20);
  184. dim.w:=dim.w - (dim.w div 20) * 2;
  185. dim.h:=dim.h - (dim.h div 20) * 2;
  186. wind_open(handle, dim.x, dim.y, dim.w, dim.h);
  187. open_win:=handle;
  188. end;
  189. procedure wind_set_grect(wh: smallint; rect: PGRECT);
  190. var
  191. fsrect: TGRECT;
  192. begin
  193. if rect = nil then
  194. begin
  195. wind_get(0, WF_WORKXYWH, @fsrect.x, @fsrect.y, @fsrect.w, @fsrect.h);
  196. rect:=@fsrect;
  197. end;
  198. wind_set(wh,WF_CURRXYWH,rect^.x,rect^.y,rect^.w,rect^.h);
  199. end;
  200. function min(a, b: smallint): smallint;
  201. begin
  202. if a < b then
  203. min:=a
  204. else
  205. min:=b;
  206. end;
  207. procedure draw_line(x1,y1,x2,y2: smallint);
  208. var
  209. xyarray: array[0..7] of smallint;
  210. begin
  211. xyarray[0]:=x1;
  212. xyarray[1]:=y1;
  213. xyarray[2]:=x2;
  214. xyarray[3]:=y2;
  215. v_pline(vdi_h,2,@xyarray);
  216. end;
  217. procedure wind_redraw(wh: smallint; rect: PGRECT);
  218. var
  219. i,cx,cy,vx,vy: longint;
  220. xyarray: array[0..7] of smallint;
  221. rcube: array[low(cube)..high(cube)] of tvertex;
  222. wrect: TGRECT;
  223. vr: tvertex;
  224. scale: longint;
  225. begin
  226. wind_update(BEG_UPDATE);
  227. wind_get(win_h,WF_WORKXYWH,@wrect.x,@wrect.y,@wrect.w,@wrect.h);
  228. scale:=(min(wrect.h,wrect.w) div 5) shl 16;
  229. cx:=wrect.x + wrect.w div 2;
  230. cy:=wrect.y + wrect.h div 2;
  231. for i:=low(cube) to high(cube) do
  232. begin
  233. rotate_vertex(cube[i],vr,-my,-mx,0);
  234. perspective_vertex(vr,3 shl 16,vx,vy);
  235. rcube[i].x:=cx + sarlongint(mulfp(vx,scale),16);
  236. rcube[i].y:=cy + sarlongint(mulfp(vy,scale),16);
  237. end;
  238. xyarray[0]:=wrect.x;
  239. xyarray[1]:=wrect.y;
  240. xyarray[2]:=wrect.x+wrect.w-1;
  241. xyarray[3]:=wrect.y+wrect.h-1;
  242. v_hide_c(vdi_h);
  243. vsf_color(vdi_h,WHITE);
  244. v_bar(vdi_h,@xyarray);
  245. vsl_color(vdi_h,RED);
  246. for i:=low(faces) to high(faces) do
  247. begin
  248. if (faces[i].edge and 1) > 0 then
  249. draw_line(rcube[faces[i].v1].x,rcube[faces[i].v1].y,
  250. rcube[faces[i].v2].x,rcube[faces[i].v2].y);
  251. if (faces[i].edge and 2) > 0 then
  252. draw_line(rcube[faces[i].v2].x,rcube[faces[i].v2].y,
  253. rcube[faces[i].v3].x,rcube[faces[i].v3].y);
  254. if (faces[i].edge and 4) > 0 then
  255. draw_line(rcube[faces[i].v3].x,rcube[faces[i].v3].y,
  256. rcube[faces[i].v1].x,rcube[faces[i].v1].y);
  257. end;
  258. v_show_c(vdi_h,1);
  259. wind_update(END_UPDATE);
  260. end;
  261. procedure event_loop;
  262. var
  263. msg_buf: array[0..7] of smallint;
  264. sx,sy: string[16];
  265. nmx,nmy: smallint;
  266. dummy: smallint;
  267. e: smallint;
  268. begin
  269. repeat
  270. dummy:=0;
  271. e:=evnt_multi(MU_TIMER or MU_MESAG,dummy,dummy,dummy,
  272. dummy,dummy,dummy,dummy,dummy,
  273. dummy,dummy,dummy,dummy,dummy,
  274. @msg_buf,
  275. 50,0,
  276. @dummy,@dummy,@dummy,@dummy,
  277. @dummy,@dummy);
  278. if e = MU_TIMER then
  279. begin
  280. graf_mkstate(@nmx,@nmy,@dummy,@dummy);
  281. if (nmx <> mx) or (nmy <> my) then
  282. begin
  283. mx:=nmx;
  284. my:=nmy;
  285. str(mx,sx);
  286. str(my,sy);
  287. win_info:='Spinning... X:'+sx+' Y:'+sy;
  288. wind_set(win_h, WF_INFO, hi(ptruint(@win_info)), lo(ptruint(@win_info)), 0, 0);
  289. msg_buf[0]:=WM_REDRAW;
  290. msg_buf[1]:=appl_h;
  291. msg_buf[2]:=0;
  292. msg_buf[3]:=win_h;
  293. msg_buf[4]:=0;
  294. msg_buf[5]:=0;
  295. msg_buf[6]:=0;
  296. msg_buf[7]:=0;
  297. appl_write(appl_h, sizeof(msg_buf), @msg_buf);
  298. end;
  299. end;
  300. if e = MU_MESAG then
  301. case msg_buf[0] of
  302. WM_CLOSED:
  303. break;
  304. WM_REDRAW:
  305. wind_redraw(win_h,PGRECT(@msg_buf[4]));
  306. WM_MOVED,
  307. WM_SIZED:
  308. wind_set_grect(win_h,PGRECT(@msg_buf[4]));
  309. WM_FULLED:
  310. wind_set_grect(win_h,nil);
  311. end;
  312. until false;
  313. end;
  314. begin
  315. appl_h:=appl_init;
  316. init_cube;
  317. vdi_h:=open_vwk;
  318. win_h:=open_win;
  319. event_loop;
  320. wind_close(win_h);
  321. wind_delete(win_h);
  322. v_clsvwk(vdi_h);
  323. appl_exit;
  324. end.