texwarp.pp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 : TPTCSurface);
  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 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 : TPTCSurface);
  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 *= red_balance;
  85. g *= green_balance;
  86. 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. grid += 3;
  124. b += pi / 30;
  125. End;
  126. 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. ucx += dudx;
  215. vcx += dvdx;
  216. icx += didx;
  217. End;
  218. uc += dudy;
  219. vc += dvdy;
  220. ic += didy;
  221. dudx += ddudy;
  222. dvdx += ddvdy;
  223. didx += ddidy;
  224. End;
  225. cbp += 3;
  226. End;
  227. cbp += 3;
  228. End;
  229. End;
  230. Var
  231. format : TPTCFormat;
  232. texture : TPTCSurface;
  233. surface : TPTCSurface;
  234. console : TPTCConsole;
  235. lighttable : PUint8;
  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. format := Nil;
  242. texture := Nil;
  243. surface := Nil;
  244. console := Nil;
  245. lighttable := Nil;
  246. Try
  247. Try
  248. { create format }
  249. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  250. { create texture surface }
  251. texture := TPTCSurface.Create(256, 256, format);
  252. { create texture }
  253. generate(texture);
  254. { create lighttable }
  255. lighttable := GetMem(256 * 256);
  256. make_light_table(lighttable);
  257. { create console }
  258. console := TPTCConsole.Create;
  259. { open console }
  260. console.open('Warp demo', 320, 200, format);
  261. { create drawing surface }
  262. surface := TPTCSurface.Create(320, 200, format);
  263. { control values }
  264. xbase := 0;
  265. ybase := 0;
  266. xmove := 0;
  267. ymove := 0;
  268. amp := 0;
  269. dct := 0.024;
  270. dxb := 0.031;
  271. dyb := -0.019;
  272. dxm := 0.015;
  273. dym := -0.0083;
  274. { main loop }
  275. While Not console.KeyPressed Do
  276. Begin
  277. { create texture mapping grid }
  278. grid_map(grid, xbase, ybase, xmove, ymove*3, amp);
  279. p1 := surface.lock;
  280. Try
  281. p2 := texture.lock;
  282. Try
  283. { map texture to drawing surface }
  284. texture_warp(p1, grid, p2, lighttable);
  285. Finally
  286. texture.unlock;
  287. End;
  288. Finally
  289. surface.unlock;
  290. End;
  291. { copy surface to console }
  292. surface.copy(console);
  293. { update console }
  294. console.update;
  295. { move control values (limit them so it doesn't go too far) }
  296. xbase += dxb;
  297. If xbase > pi Then
  298. dxb := -dxb;
  299. If xbase < (-pi) Then
  300. dxb := -dxb;
  301. ybase += dyb;
  302. If ybase > pi Then
  303. dyb := -dyb;
  304. If ybase < (-pi) Then
  305. dyb := -dyb;
  306. xmove += dxm;
  307. If xmove > pi Then
  308. dxm := -dxm;
  309. If xmove < (-pi) Then
  310. dxm := -dxm;
  311. ymove += dym;
  312. If ymove > pi Then
  313. dym := -dym;
  314. If ymove < (-pi) Then
  315. dym := -dym;
  316. amp += dct;
  317. sa := sin(amp);
  318. If (sa > -0.0001) And (sa < 0.0001) Then
  319. Begin
  320. If amp > 8.457547 Then
  321. dct := -dct;
  322. If amp < -5.365735 Then
  323. dct := -dct;
  324. End;
  325. End;
  326. Finally
  327. console.close;
  328. console.Free;
  329. surface.Free;
  330. texture.Free;
  331. format.Free;
  332. If assigned(lighttable) Then
  333. FreeMem(lighttable);
  334. End;
  335. Except
  336. On e : TPTCError Do
  337. e.report;
  338. End;
  339. End.