texwarp.pp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Texture warp demo for OpenPTC 1.0 C++ API
  6. Copyright (c) 1998 Jonathan Matthew
  7. This source code is licensed under the GNU GPL
  8. }
  9. Program TexWarp;
  10. {$MODE objfpc}
  11. Uses
  12. ptc;
  13. Const
  14. { colour balance values. change these if you don't like the colouring }
  15. { of the texture. }
  16. red_balance : int32 = 2;
  17. green_balance : int32 = 3;
  18. blue_balance : int32 = 1;
  19. Procedure blur(s : TPTCSurface);
  20. Var
  21. d : Pchar8;
  22. pitch : Integer;
  23. spack, r : Integer;
  24. Begin
  25. { lock surface }
  26. d := s.lock;
  27. pitch := s.pitch;
  28. spack := (s.height - 1) * pitch;
  29. { first pixel }
  30. For r := 0 To 3 Do
  31. d[r] := (d[pitch + r] + d[r + 4] + d[spack + r] + d[pitch - 4 + r]) Div 4;
  32. { rest of first line }
  33. For r := 4 To pitch - 1 Do
  34. d[r] := (d[r + pitch] + d[r + 4] + d[r - 4] + d[spack + r]) Div 4;
  35. { rest of surface except last line }
  36. For r := pitch To ((s.height - 1) * pitch) - 1 Do
  37. d[r] := (d[r - pitch] + d[r + pitch] + d[r + 4] + d[r - 4]) Div 4;
  38. { last line except last pixel }
  39. For r := (s.height - 1) * pitch To (s.height * s.pitch) - 5 Do
  40. d[r] := (d[r - pitch] + d[r + 4] + d[r - 4] + d[r - spack]) Div 4;
  41. { last pixel }
  42. For r := (s.height * s.pitch) - 4 To s.height * s.pitch Do
  43. d[r] := (d[r - pitch] + d[r - 4] + d[r - spack] + d[r + 4 - pitch]) Div 4;
  44. s.unlock;
  45. End;
  46. Procedure generate(surface : TPTCSurface);
  47. Var
  48. dest : Pint32;
  49. i : Integer;
  50. x, y : Integer;
  51. d : Pint32;
  52. cv : int32;
  53. r, g, b : char8;
  54. Begin
  55. { draw random dots all over the surface }
  56. dest := surface.lock;
  57. For i := 0 To surface.width * surface.height - 1 Do
  58. Begin
  59. x := Random(surface.width);
  60. y := Random(surface.height);
  61. d := dest + (y * surface.width) + x;
  62. cv := (Random(100) Shl 16) Or (Random(100) Shl 8) Or Random(100);
  63. d^ := cv;
  64. End;
  65. surface.unlock;
  66. { blur the surface }
  67. For i := 1 To 5 Do
  68. blur(surface);
  69. { multiply the color values }
  70. dest := surface.lock;
  71. For i := 0 To surface.width * surface.height - 1 Do
  72. Begin
  73. cv := dest^;
  74. r := (cv Shr 16) And 255;
  75. g := (cv Shr 8) And 255;
  76. b := cv And 255;
  77. r *= red_balance;
  78. g *= green_balance;
  79. b *= blue_balance;
  80. If r > 255 Then
  81. r := 255;
  82. If g > 255 Then
  83. g := 255;
  84. If b > 255 Then
  85. b := 255;
  86. dest^ := (r Shl 16) Or (g Shl 8) Or b;
  87. Inc(dest);
  88. End;
  89. surface.unlock;
  90. End;
  91. Procedure grid_map(grid : Pint32; xbase, ybase, xmove, ymove, amp : Single);
  92. Var
  93. x, y : Integer;
  94. a, b, id : Single;
  95. Begin
  96. a := 0;
  97. For y := 0 To 25 Do
  98. Begin
  99. b := 0;
  100. For x := 0 To 40 Do
  101. Begin
  102. { it should be noted that there is no scientific basis for }
  103. { the following three lines :) }
  104. grid[0] := int32(Trunc((xbase * 14 + x*4 + xmove*sin(b)+sin(cos(a)*sin(amp))*15) * 65536));
  105. grid[1] := int32(Trunc((ybase * 31 + y*3 + ymove*cos(b)*sin(sin(a)*cos(amp))*30) * 65536));
  106. id := (cos(xbase) + sin(ybase) + cos(a*xmove*0.17) + sin(b*ymove*0.11)) * amp * 23;
  107. If id < -127 Then
  108. grid[2] := 0
  109. Else
  110. If id > 127 Then
  111. grid[2] := 255 Shl 16
  112. Else
  113. grid[2] := (128 Shl 16) + Trunc(id * 65536.0);
  114. grid += 3;
  115. b += pi / 30;
  116. End;
  117. a += pi / 34;
  118. End;
  119. End;
  120. Procedure make_light_table(lighttable : Pchar8);
  121. Var
  122. i, j : Integer;
  123. tv : Integer;
  124. Begin
  125. For i := 0 To 255 Do
  126. For j := 0 To 255 Do
  127. Begin
  128. { light table goes from 0 to i*2. }
  129. tv := (i * j) Div 128;
  130. If tv > 255 Then
  131. tv := 255;
  132. lighttable[(j * 256) + i] := tv;
  133. End;
  134. End;
  135. { if you want to see how to do this properly, look at the tunnel3d demo. }
  136. { (not included in this distribution :) }
  137. Procedure texture_warp(dest, grid, texture : Pint32; lighttable : Pchar8);
  138. Var
  139. utl, utr, ubl, ubr : Integer;
  140. vtl, vtr, vbl, vbr : Integer;
  141. itl, itr, ibl, ibr : Integer;
  142. dudx, dvdx, didx, dudy, dvdy, didy, ddudy, ddvdy, ddidy : Integer;
  143. dudx2, dvdx2, didx2 : Integer;
  144. bx, by, px, py : Integer;
  145. uc, vc, ic, ucx, vcx, icx : Integer;
  146. edi : int32;
  147. texel : int32;
  148. cbp, dp : Pint32;
  149. dpix : int32;
  150. ltp : Pchar8;
  151. Begin
  152. cbp := grid;
  153. For by := 0 To 24 Do
  154. Begin
  155. For bx := 0 To 39 Do
  156. Begin
  157. utl := Integer(cbp^);
  158. vtl := Integer((cbp + 1)^);
  159. itl := Integer((cbp + 2)^);
  160. utr := Integer((cbp + (1 * 3))^);
  161. vtr := Integer((cbp + (1 * 3) + 1)^);
  162. itr := Integer((cbp + (1 * 3) + 2)^);
  163. ubl := Integer((cbp + (41 * 3))^);
  164. vbl := Integer((cbp + (41 * 3) + 1)^);
  165. ibl := Integer((cbp + (41 * 3) + 2)^);
  166. ubr := Integer((cbp + (42 * 3))^);
  167. vbr := Integer((cbp + (42 * 3) + 1)^);
  168. ibr := Integer((cbp + (42 * 3) + 2)^);
  169. dudx := (utr - utl) Div 8;
  170. dvdx := (vtr - vtl) Div 8;
  171. didx := (itr - itl) Div 8;
  172. dudx2 := (ubr - ubl) Div 8;
  173. dvdx2 := (vbr - vbl) Div 8;
  174. didx2 := (ibr - ibl) Div 8;
  175. dudy := (ubl - utl) Div 8;
  176. dvdy := (vbl - vtl) Div 8;
  177. didy := (ibl - itl) Div 8;
  178. ddudy := (dudx2 - dudx) Div 8;
  179. ddvdy := (dvdx2 - dvdx) Div 8;
  180. ddidy := (didx2 - didx) Div 8;
  181. uc := utl;
  182. vc := vtl;
  183. ic := itl;
  184. For py := 0 To 7 Do
  185. Begin
  186. ucx := uc;
  187. vcx := vc;
  188. icx := ic;
  189. dp := dest + (((by * 8 + py)*320) + (bx * 8));
  190. For px := 0 To 7 Do
  191. Begin
  192. { get light table pointer for current intensity }
  193. ltp := lighttable + ((icx And $FF0000) Shr 8);
  194. { get texel }
  195. edi := ((ucx And $FF0000) Shr 16) + ((vcx And $FF0000) Shr 8);
  196. texel := texture[edi];
  197. { calculate actual colour }
  198. dpix := ltp[(texel Shr 16) And 255];
  199. dpix := dpix Shl 8;
  200. dpix := dpix Or ltp[(texel Shr 8) And 255];
  201. dpix := dpix Shl 8;
  202. dpix := dpix Or ltp[texel And 255];
  203. dp^ := dpix;
  204. Inc(dp);
  205. ucx += dudx;
  206. vcx += dvdx;
  207. icx += didx;
  208. End;
  209. uc += dudy;
  210. vc += dvdy;
  211. ic += didy;
  212. dudx += ddudy;
  213. dvdx += ddvdy;
  214. didx += ddidy;
  215. End;
  216. cbp += 3;
  217. End;
  218. cbp += 3;
  219. End;
  220. End;
  221. Var
  222. format : TPTCFormat;
  223. texture : TPTCSurface;
  224. surface : TPTCSurface;
  225. console : TPTCConsole;
  226. lighttable : Pchar8;
  227. { texture grid }
  228. grid : Array[0..41*26*3-1] Of int32;
  229. xbase, ybase, xmove, ymove, amp, dct, dxb, dyb, dxm, dym, sa : Single;
  230. p1, p2 : Pint32;
  231. Begin
  232. format := Nil;
  233. texture := Nil;
  234. surface := Nil;
  235. console := Nil;
  236. lighttable := Nil;
  237. Try
  238. Try
  239. { create format }
  240. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  241. { create texture surface }
  242. texture := TPTCSurface.Create(256, 256, format);
  243. { create texture }
  244. generate(texture);
  245. { create lighttable }
  246. lighttable := GetMem(256 * 256);
  247. make_light_table(lighttable);
  248. { create console }
  249. console := TPTCConsole.Create;
  250. { open console }
  251. console.open('Warp demo', 320, 200, format);
  252. { create drawing surface }
  253. surface := TPTCSurface.Create(320, 200, format);
  254. { control values }
  255. xbase := 0;
  256. ybase := 0;
  257. xmove := 0;
  258. ymove := 0;
  259. amp := 0;
  260. dct := 0.024;
  261. dxb := 0.031;
  262. dyb := -0.019;
  263. dxm := 0.015;
  264. dym := -0.0083;
  265. { main loop }
  266. While Not console.KeyPressed Do
  267. Begin
  268. { create texture mapping grid }
  269. grid_map(grid, xbase, ybase, xmove, ymove*3, amp);
  270. p1 := surface.lock;
  271. Try
  272. p2 := texture.lock;
  273. Try
  274. { map texture to drawing surface }
  275. texture_warp(p1, grid, p2, lighttable);
  276. Finally
  277. texture.unlock;
  278. End;
  279. Finally
  280. surface.unlock;
  281. End;
  282. { copy surface to console }
  283. surface.copy(console);
  284. { update console }
  285. console.update;
  286. { move control values (limit them so it doesn't go too far) }
  287. xbase += dxb;
  288. If xbase > pi Then
  289. dxb := -dxb;
  290. If xbase < (-pi) Then
  291. dxb := -dxb;
  292. ybase += dyb;
  293. If ybase > pi Then
  294. dyb := -dyb;
  295. If ybase < (-pi) Then
  296. dyb := -dyb;
  297. xmove += dxm;
  298. If xmove > pi Then
  299. dxm := -dxm;
  300. If xmove < (-pi) Then
  301. dxm := -dxm;
  302. ymove += dym;
  303. If ymove > pi Then
  304. dym := -dym;
  305. If ymove < (-pi) Then
  306. dym := -dym;
  307. amp += dct;
  308. sa := sin(amp);
  309. If (sa > -0.0001) And (sa < 0.0001) Then
  310. Begin
  311. If amp > 8.457547 Then
  312. dct := -dct;
  313. If amp < -5.365735 Then
  314. dct := -dct;
  315. End;
  316. End;
  317. Finally
  318. console.close;
  319. console.Free;
  320. surface.Free;
  321. texture.Free;
  322. format.Free;
  323. If assigned(lighttable) Then
  324. FreeMem(lighttable);
  325. End;
  326. Except
  327. On e : TPTCError Do
  328. e.report;
  329. End;
  330. End.