tunnel3d.pp 14 KB

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