tunnel3d.pp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Tunnel3D demo for OpenPTC 1.0 C++ API
  6. Realtime raytraced tunnel
  7. Copyright (c) 1998 Christian Nentwich ([email protected])
  8. This source code is licensed under the GNU LGPL
  9. And do not just blatantly cut&paste this into your demo :)
  10. }
  11. Program Tunnel3D;
  12. {$MODE objfpc}
  13. Uses
  14. ptc, Math;
  15. Type
  16. PVector = ^TVector;
  17. TVector = Array[0..2] Of Single; { X,Y,Z }
  18. TMatrix = Array[0..3, 0..3] Of Single;{ FIRST = COLUMN
  19. SECOND = ROW
  20. [0, 0] [1, 0] [2, 0]
  21. [0, 1] [1, 1] [2, 1]
  22. [0, 2] [1, 2] [2, 2]
  23. (I know the matrices are the wrong way round, so what, the code is quite
  24. old :) }
  25. TRayTunnel = Class(TObject)
  26. Private
  27. tunneltex : PUint8; { Texture }
  28. pal : PUint8; { Original palette }
  29. lookup : PUint32; { Lookup table for lighting }
  30. sintab, costab : PSingle; { Take a guess }
  31. u_array, v_array, l_array : PInteger; { Raytraced coordinates and light }
  32. norms : PVector;
  33. radius, radius_sqr : Single;
  34. rot : TMatrix;
  35. pos, light : TVector; { Position in the tunnel, pos of }
  36. xa, ya, za : Integer; { lightsource, angles }
  37. lightstatus : Boolean; { Following the viewer ? }
  38. Public
  39. Constructor Create(rad : Single); { Constructor takes the radius }
  40. Destructor Destroy; Override;
  41. Procedure load_texture;
  42. Procedure tilt(x, y, z : Integer); { Rotate relative }
  43. Procedure tilt(x, y, z : Integer; abs : Uint8); { Absolute }
  44. Procedure move(dx, dy, dz : Single); { Relative move }
  45. Procedure move(x, y, z : Single; abs : Uint8); { Absolute }
  46. Procedure movelight(dx, dy, dz : Single);
  47. Procedure movelight(x, y, z : Single; abs : Uint8);
  48. Procedure locklight(lock : Boolean); { Make the light follow the viewer }
  49. Procedure interpolate; { Raytracing }
  50. Procedure draw(dest : PUint32); { Draw the finished tunnel }
  51. End;
  52. { VECTOR ROUTINES }
  53. Procedure vector_normalize(Var v : TVector);
  54. Var
  55. length : Single;
  56. Begin
  57. length := v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
  58. length := sqrt(length);
  59. If length <> 0 Then
  60. Begin
  61. v[0] := v[0] / length;
  62. v[1] := v[1] / length;
  63. v[2] := v[2] / length;
  64. End
  65. Else
  66. Begin
  67. v[0] := 0;
  68. v[1] := 0;
  69. v[2] := 0;
  70. End;
  71. End;
  72. Procedure vector_times_matrix(Const v : TVector; Const m : TMatrix;
  73. Var res : TVector);
  74. Var
  75. i, j : Integer;
  76. Begin
  77. For j := 0 To 2 Do
  78. Begin
  79. res[j] := 0;
  80. For i := 0 To 2 Do
  81. res[j] := res[j] + (m[j, i] * v[i]);
  82. End;
  83. End;
  84. Procedure matrix_idle(Var m : TMatrix);
  85. Begin
  86. FillChar(m, SizeOf(TMatrix), 0);
  87. m[0, 0] := 1;
  88. m[1, 1] := 1;
  89. m[2, 2] := 1;
  90. m[3, 3] := 1;
  91. End;
  92. Procedure matrix_times_matrix(Const m1, m2 : TMatrix; Var res : TMatrix);
  93. Var
  94. i, j, k : Integer;
  95. Begin
  96. For j := 0 To 3 Do
  97. For i := 0 To 3 Do
  98. Begin
  99. res[i, j] := 0;
  100. For k := 0 To 3 Do
  101. res[i, j] := res[i, j] + (m1[k, j] * m2[i, k]);
  102. End;
  103. End;
  104. Procedure matrix_rotate_x(Var m : TMatrix; angle : Integer; sintab, costab : PSingle);
  105. Var
  106. tmp, tmp2 : TMatrix;
  107. Begin
  108. matrix_idle(tmp);
  109. tmp[1, 1] := costab[angle];
  110. tmp[2, 1] := sintab[angle];
  111. tmp[1, 2] := -sintab[angle];
  112. tmp[2, 2] := costab[angle];
  113. matrix_times_matrix(tmp, m, tmp2);
  114. Move(tmp2, m, SizeOf(TMatrix));
  115. End;
  116. Procedure matrix_rotate_y(Var m : TMatrix; angle : Integer; sintab, costab : PSingle);
  117. Var
  118. tmp, tmp2 : TMatrix;
  119. Begin
  120. matrix_idle(tmp);
  121. tmp[0, 0] := costab[angle];
  122. tmp[2, 0] := -sintab[angle];
  123. tmp[0, 2] := sintab[angle];
  124. tmp[2, 2] := costab[angle];
  125. matrix_times_matrix(tmp, m, tmp2);
  126. Move(tmp2, m, SizeOf(TMatrix));
  127. End;
  128. Procedure matrix_rotate_z(Var m : TMatrix; angle : Integer; sintab, costab : PSingle);
  129. Var
  130. tmp, tmp2 : TMatrix;
  131. Begin
  132. matrix_idle(tmp);
  133. tmp[0, 0] := costab[angle];
  134. tmp[1, 0] := sintab[angle];
  135. tmp[0, 1] := -sintab[angle];
  136. tmp[1, 1] := costab[angle];
  137. matrix_times_matrix(tmp, m, tmp2);
  138. Move(tmp2, m, SizeOf(TMatrix));
  139. End;
  140. Constructor TRayTunnel.Create(rad : Single);
  141. Var
  142. x, y : Single;
  143. i, j : Integer;
  144. tmp : TVector;
  145. Begin
  146. tunneltex := Nil;
  147. sintab := Nil;
  148. costab := Nil;
  149. u_array := Nil;
  150. v_array := Nil;
  151. norms := Nil;
  152. lookup := Nil;
  153. pal := Nil;
  154. radius := rad;
  155. radius_sqr := rad * rad;
  156. sintab := GetMem(1024 * SizeOf(Single)); { Set trigonometry and lookups }
  157. costab := GetMem(1024 * SizeOf(Single));
  158. u_array := GetMem(64 * 26 * SizeOf(Integer));
  159. v_array := GetMem(64 * 26 * SizeOf(Integer));
  160. l_array := GetMem(64 * 26 * SizeOf(Integer));
  161. norms := GetMem(64 * 26 * 3 * SizeOf(Single));
  162. lookup := GetMem(65 * 256 * SizeOf(Uint32));
  163. pal := GetMem(768 * SizeOf(Uint8));
  164. For i := 0 To 1023 Do
  165. Begin
  166. sintab[i] := sin(i * pi / 512);
  167. costab[i] := cos(i * pi / 512);
  168. End;
  169. { Generate normal vectors }
  170. y := -100;
  171. For j := 0 To 25 Do
  172. Begin
  173. x := -160;
  174. For i := 0 To 40 Do
  175. Begin
  176. tmp[0] := x;
  177. tmp[1] := y;
  178. tmp[2] := 128;
  179. vector_normalize(tmp);
  180. norms[j * 64 + i] := tmp;
  181. x := x + 8;
  182. End;
  183. y := y + 8;
  184. End;
  185. { Reset tunnel and light position and all angles }
  186. pos[0] := 0; pos[1] := 0; pos[2] := 0;
  187. light[0] := 1; light[1] := 1; light[2] := 0;
  188. xa := 0; ya := 0; za := 0;
  189. lightstatus := False;
  190. { Normalize light vector to length 1.0 }
  191. vector_normalize(light);
  192. End;
  193. Destructor TRayTunnel.Destroy;
  194. Begin
  195. If Assigned(tunneltex) Then
  196. FreeMem(tunneltex);
  197. If Assigned(pal) Then
  198. FreeMem(pal);
  199. If Assigned(lookup) Then
  200. FreeMem(lookup);
  201. If Assigned(norms) Then
  202. FreeMem(norms);
  203. If Assigned(l_array) Then
  204. FreeMem(l_array);
  205. If Assigned(v_array) Then
  206. FreeMem(v_array);
  207. If Assigned(u_array) Then
  208. FreeMem(u_array);
  209. If Assigned(costab) Then
  210. FreeMem(costab);
  211. If Assigned(sintab) Then
  212. FreeMem(sintab);
  213. End;
  214. Procedure TRayTunnel.load_texture;
  215. Var
  216. texfile : File;
  217. tmp : PUint8;
  218. i, j : Uint32;
  219. r, g, b : Uint32;
  220. newoffs : Integer;
  221. Begin
  222. { Allocate tunnel texture 65536+33 bytes too big }
  223. If tunneltex <> Nil Then
  224. Begin
  225. FreeMem(tunneltex);
  226. tunneltex := Nil;
  227. End;
  228. tunneltex := GetMem(2*65536 + 33);
  229. tmp := GetMem(65536);
  230. { Align the texture on a 64k boundary }
  231. While (PtrUInt(tunneltex) And $FFFF) <> 0 Do
  232. Inc(tunneltex);
  233. ASSign(texfile, 'tunnel3d.raw');
  234. Reset(texfile, 1);
  235. BlockRead(texfile, pal^, 768);
  236. BlockRead(texfile, tmp^, 65536);
  237. Close(texfile);
  238. { Generate lookup table for lighting (65 because of possible inaccuracies) }
  239. For j := 0 To 64 Do
  240. For i := 0 To 255 Do
  241. Begin
  242. r := pal[i * 3] Shl 2;
  243. g := pal[i * 3 + 1] Shl 2;
  244. b := pal[i * 3 + 2] Shl 2;
  245. r := (r * j) Shr 6;
  246. g := (g * j) Shr 6;
  247. b := (b * j) Shr 6;
  248. If r > 255 Then
  249. r := 255;
  250. If g > 255 Then
  251. g := 255;
  252. If b > 255 Then
  253. b := 255;
  254. lookup[j * 256 + i] := (r Shl 16) Or (g Shl 8) Or b;
  255. End;
  256. { Arrange texture for cache optimised mapping }
  257. For j := 0 To 255 Do
  258. For i := 0 To 255 Do
  259. Begin
  260. newoffs := ((i Shl 8) And $F800) + (i And $0007) + ((j Shl 3) And $7F8);
  261. (tunneltex + newoffs)^ := (tmp + j * 256 + i)^;
  262. End;
  263. FreeMem(tmp);
  264. End;
  265. Procedure TRayTunnel.interpolate;
  266. Var
  267. ray, intsc, norm, lvec : TVector;
  268. x, y, a, b, c, discr, t, res : Single;
  269. i, j : Integer;
  270. Begin
  271. If lightstatus Then { Lightsource locked to viewpoint }
  272. light := pos;
  273. matrix_idle(rot);
  274. matrix_rotate_x(rot, xa And $3FF, sintab, costab);
  275. matrix_rotate_y(rot, ya And $3FF, sintab, costab);
  276. matrix_rotate_z(rot, za And $3FF, sintab, costab);
  277. { Constant factor }
  278. c := 2 * (pos[0] * pos[0] + pos[1] * pos[1] - radius_sqr);
  279. { Start raytracing }
  280. y := -100;
  281. For j := 0 To 25 Do
  282. Begin
  283. x := -160;
  284. For i := 0 To 40 Do
  285. Begin
  286. vector_times_matrix(norms[(j Shl 6) + i], rot, ray);
  287. a := 2 * (ray[0] * ray[0] + ray[1] * ray[1]);
  288. b := 2 * (pos[0] * ray[0] + pos[1] * ray[1]);
  289. discr := b * b - a * c;
  290. If discr > 0 Then
  291. Begin
  292. discr := sqrt(discr);
  293. t := (- b + discr) / a;
  294. { Calculate intersection point }
  295. intsc[0] := pos[0] + t * ray[0];
  296. intsc[1] := pos[1] + t * ray[1];
  297. intsc[2] := pos[2] + t * ray[2];
  298. { Calculate texture index at intersection point (cylindrical mapping) }
  299. { Try and adjust the 0.2 to stretch/shrink the texture }
  300. u_array[(j Shl 6) + i] := Trunc(intsc[2] * 0.2) Shl 16;
  301. v_array[(j Shl 6) + i] := Trunc(abs(arctan2(intsc[1], intsc[0]) * 256 / pi)) Shl 16;
  302. { Calculate the dotproduct between the normal vector and the vector }
  303. { from the intersection point to the lightsource }
  304. norm[0] := intsc[0] / radius;
  305. norm[1] := intsc[1] / radius;
  306. norm[2] := 0;
  307. lvec[0] := intsc[0] - light[0];
  308. lvec[1] := intsc[1] - light[1];
  309. lvec[2] := intsc[2] - light[2];
  310. vector_normalize(lvec);
  311. res := lvec[0] * norm[0] + lvec[1] * norm[1] + lvec[2] * norm[2];
  312. { Scale the light a bit }
  313. res *= res;
  314. If res < 0 Then
  315. res := 0;
  316. If res > 1 Then
  317. res := 1;
  318. res *= 63;
  319. { Put it into the light array }
  320. l_array[(j Shl 6) + i] := Trunc(res) Shl 16;
  321. End
  322. Else
  323. Begin
  324. u_array[(j Shl 6) + i] := 0;
  325. v_array[(j Shl 6) + i] := 0;
  326. l_array[(j Shl 6) + i] := 0;
  327. End;
  328. x := x + 8;
  329. End;
  330. y := y + 8;
  331. End;
  332. End;
  333. Procedure TRayTunnel.draw(dest : PUint32);
  334. Var
  335. x, y, lu, lv, ru, rv, liu, liv, riu, riv : Integer;
  336. iu, iv, i, j, ll, rl, lil, ril, l, il : Integer;
  337. iadr, adr, til_u, til_v, til_iu, til_iv : DWord;
  338. bla : Uint8;
  339. Begin
  340. For j := 0 To 24 Do
  341. For i := 0 To 39 Do
  342. Begin
  343. iadr := (j Shl 6) + i;
  344. { Set up gradients }
  345. lu := u_array[iadr]; ru := u_array[iadr + 1];
  346. liu := (u_array[iadr + 64] - lu) Shr 3;
  347. riu := (u_array[iadr + 65] - ru) Shr 3;
  348. lv := v_array[iadr]; rv := v_array[iadr + 1];
  349. liv := (v_array[iadr + 64] - lv) Shr 3;
  350. riv := (v_array[iadr + 65] - rv) Shr 3;
  351. ll := l_array[iadr]; rl := l_array[iadr + 1];
  352. lil := (l_array[iadr + 64] - ll) Shr 3;
  353. ril := (l_array[iadr + 65] - rl) Shr 3;
  354. For y := 0 To 7 Do
  355. Begin
  356. iu := (ru - lu) Shr 3;
  357. iv := (rv - lv) Shr 3;
  358. l := ll;
  359. il := (rl - ll) Shr 3;
  360. { Mess up everything for the sake of cache optimised mapping :) }
  361. til_u := DWord(((lu Shl 8) And $F8000000) Or ((lu Shr 1) And $00007FFF) Or (lu And $00070000));
  362. til_v := DWord(((lv Shl 3) And $07F80000) Or ((lv Shr 1) And $00007FFF));
  363. til_iu := DWord((((iu Shl 8) And $F8000000) Or ((iu Shr 1) And $00007FFF) Or
  364. (iu And $00070000)) Or $07F88000);
  365. til_iv := DWord((((iv Shl 3) And $07F80000) Or ((iv Shr 1) And $00007FFF)) Or $F8078000);
  366. adr := til_u + til_v;
  367. For x := 0 To 7 Do
  368. Begin
  369. { Interpolate texture u,v and light }
  370. Inc(til_u, til_iu);
  371. Inc(til_v, til_iv);
  372. Inc(l, il);
  373. adr := adr Shr 16;
  374. til_u := til_u And DWord($F8077FFF);
  375. til_v := til_v And $07F87FFF;
  376. bla := (tunneltex + adr)^;
  377. adr := til_u + til_v;
  378. { Look up the light and write to buffer }
  379. (dest + ((j Shl 3) + y) * 320 + (I Shl 3) + x)^ := lookup[((l And $3F0000) Shr 8) + bla];
  380. End;
  381. Inc(lu, liu); Inc(ru, riu);
  382. Inc(lv, liv); Inc(rv, riv);
  383. Inc(ll, lil); Inc(rl, ril);
  384. End;
  385. End;
  386. End;
  387. { tilt rotates the viewer in the tunnel in a relative / absolute way }
  388. Procedure TRayTunnel.tilt(x, y, z : Integer);
  389. Begin
  390. xa := (xa + x) And $3FF;
  391. ya := (ya + y) And $3FF;
  392. za := (za + z) And $3FF;
  393. End;
  394. Procedure TRayTunnel.tilt(x, y, z : Integer; abs : Uint8);
  395. Begin
  396. xa := x And $3FF;
  397. ya := y And $3FF;
  398. za := z And $3FF;
  399. End;
  400. { Relative / absolute move }
  401. Procedure TRayTunnel.move(dx, dy, dz : Single);
  402. Begin
  403. pos[0] := pos[0] + dx;
  404. pos[1] := pos[1] + dy;
  405. pos[2] := pos[2] + dz;
  406. End;
  407. Procedure TRayTunnel.move(x, y, z : Single; abs : Uint8);
  408. Begin
  409. pos[0] := x;
  410. pos[1] := y;
  411. pos[2] := z;
  412. End;
  413. { Relative / absolute move for the lightsource }
  414. Procedure TRayTunnel.movelight(dx, dy, dz : Single);
  415. Begin
  416. light[0] := light[0] + dx;
  417. light[1] := light[1] + dy;
  418. light[2] := light[2] + dz;
  419. End;
  420. Procedure TRayTunnel.movelight(x, y, z : Single; abs : Uint8);
  421. Begin
  422. light[0] := x;
  423. light[1] := y;
  424. light[2] := z;
  425. End;
  426. { Lock lightsource to the viewer }
  427. Procedure TRayTunnel.locklight(lock : Boolean);
  428. Begin
  429. lightstatus := lock;
  430. End;
  431. Var
  432. console : TPTCConsole;
  433. surface : TPTCSurface;
  434. format : TPTCFormat;
  435. tunnel : TRayTunnel;
  436. posz, phase_x, phase_y : Single;
  437. angle_x, angle_y : Integer;
  438. buffer : PUint32;
  439. Begin
  440. format := Nil;
  441. surface := Nil;
  442. console := Nil;
  443. tunnel := Nil;
  444. Try
  445. Try
  446. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  447. console := TPTCConsole.create;
  448. console.open('Tunnel3D demo', 320, 200, format);
  449. surface := TPTCSurface.create(320, 200, format);
  450. { Create a tunnel, radius=700 }
  451. tunnel := TRayTunnel.Create(700);
  452. tunnel.load_texture;
  453. { Light follows the viewer }
  454. tunnel.locklight(True);
  455. posz := 80; phase_x := 0; phase_y := 0;
  456. angle_x := 6; angle_y := 2;
  457. While Not console.KeyPressed Do
  458. Begin
  459. buffer := surface.lock;
  460. Try
  461. tunnel.interpolate;
  462. { Draw to offscreen buffer }
  463. tunnel.draw(buffer);
  464. Finally
  465. surface.unlock;
  466. End;
  467. { And copy to screen }
  468. surface.copy(console);
  469. console.update;
  470. tunnel.tilt(angle_x, angle_y, 0);
  471. tunnel.move(sin(phase_x), cos(phase_y), posz);
  472. phase_x := phase_x + 0.2;
  473. phase_y := phase_y + 0.1;
  474. End;
  475. Finally
  476. console.close;
  477. console.Free;
  478. surface.Free;
  479. tunnel.Free;
  480. format.Free;
  481. End;
  482. Except
  483. On error : TPTCError Do
  484. error.report;
  485. End;
  486. End.