texwarp.pp 8.9 KB

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