TEXTURE.C 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * $Source: f:/miner/source/main/editor/rcs/texture.c $
  3. * $Revision: 2.0 $
  4. * $Author: john $
  5. * $Date: 1995/02/27 11:34:50 $
  6. *
  7. * Texture map assignment.
  8. *
  9. * $Log: texture.c $
  10. * Revision 2.0 1995/02/27 11:34:50 john
  11. * Version 2.0! No anonymous unions, Watcom 10.0, with no need
  12. * for bitmaps.tbl.
  13. *
  14. * Revision 1.13 1994/08/04 19:13:15 matt
  15. * Changed a bunch of vecmat calls to use multiple-function routines, and to
  16. * allow the use of C macros for some functions
  17. *
  18. * Revision 1.12 1994/08/03 10:31:56 mike
  19. * Texture map propagation without uv assignment.
  20. *
  21. * Revision 1.11 1994/07/14 19:36:34 yuan
  22. * Tuning texture slides.
  23. *
  24. * Revision 1.10 1994/07/14 19:29:08 yuan
  25. * Fixed sliding.
  26. *
  27. * Revision 1.9 1994/07/14 14:43:06 yuan
  28. * Added 3x rotation.
  29. *
  30. * Revision 1.8 1994/07/14 11:12:42 yuan
  31. * Made sliding 3x more sensitive
  32. *
  33. * Revision 1.7 1994/07/14 10:49:56 yuan
  34. * Made texture rotation 3x finer
  35. *
  36. * Revision 1.6 1994/02/14 12:06:00 mike
  37. * change segment data structure.
  38. *
  39. * Revision 1.5 1993/12/06 13:26:52 mike
  40. * Make rotation and sliding work for triangulated sides.
  41. *
  42. * Revision 1.4 1993/12/04 17:18:46 mike
  43. * Add tiling functions, set_default.
  44. *
  45. * Revision 1.3 1993/12/03 18:39:12 unknown
  46. * Add texture map sliding, allow to work on triangulated sides.
  47. *
  48. * Revision 1.2 1993/11/30 17:06:09 mike
  49. * Texture map functions.
  50. *
  51. * Revision 1.1 1993/11/29 16:00:57 mike
  52. * Initial revision
  53. *
  54. *
  55. */
  56. #pragma off (unreferenced)
  57. static char rcsid[] = "$Id: texture.c 2.0 1995/02/27 11:34:50 john Exp $";
  58. #pragma on (unreferenced)
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <stdarg.h>
  62. #include <math.h>
  63. #include <string.h>
  64. #include "inferno.h"
  65. #include "segment.h"
  66. #include "seguvs.h"
  67. #include "editor.h"
  68. #include "fix.h"
  69. #include "mono.h"
  70. #include "error.h"
  71. #include "kdefs.h"
  72. // -----------------------------------------------------------
  73. int TexFlipX()
  74. {
  75. uvl uvcenter;
  76. fix rotmat[4];
  77. compute_uv_side_center(&uvcenter, Cursegp, Curside);
  78. // Create a rotation matrix
  79. rotmat[0] = -0xffff;
  80. rotmat[1] = 0;
  81. rotmat[2] = 0;
  82. rotmat[3] = 0xffff;
  83. rotate_uv_points_on_side(Cursegp, Curside, rotmat, &uvcenter);
  84. Update_flags |= UF_WORLD_CHANGED;
  85. return 1;
  86. }
  87. // -----------------------------------------------------------
  88. int TexFlipY()
  89. {
  90. uvl uvcenter;
  91. fix rotmat[4];
  92. compute_uv_side_center(&uvcenter, Cursegp, Curside);
  93. // Create a rotation matrix
  94. rotmat[0] = 0xffff;
  95. rotmat[1] = 0;
  96. rotmat[2] = 0;
  97. rotmat[3] = -0xffff;
  98. rotate_uv_points_on_side(Cursegp, Curside, rotmat, &uvcenter);
  99. Update_flags |= UF_WORLD_CHANGED;
  100. return 1;
  101. }
  102. // -----------------------------------------------------------
  103. int DoTexSlideLeft(int value)
  104. {
  105. side *sidep;
  106. uvl duvl03;
  107. fix dist;
  108. byte *vp;
  109. int v;
  110. vp = Side_to_verts[Curside];
  111. sidep = &Cursegp->sides[Curside];
  112. dist = vm_vec_dist(&Vertices[Cursegp->verts[vp[3]]], &Vertices[Cursegp->verts[vp[0]]]);
  113. dist *= value;
  114. if (dist < F1_0/(64*value))
  115. dist = F1_0/(64*value);
  116. duvl03.u = fixdiv(sidep->uvls[3].u - sidep->uvls[0].u,dist);
  117. duvl03.v = fixdiv(sidep->uvls[3].v - sidep->uvls[0].v,dist);
  118. for (v=0; v<4; v++) {
  119. sidep->uvls[v].u -= duvl03.u;
  120. sidep->uvls[v].v -= duvl03.v;
  121. }
  122. Update_flags |= UF_WORLD_CHANGED;
  123. return 1;
  124. }
  125. int TexSlideLeft()
  126. {
  127. return DoTexSlideLeft(3);
  128. }
  129. int TexSlideLeftBig()
  130. {
  131. return DoTexSlideLeft(1);
  132. }
  133. // -----------------------------------------------------------
  134. int DoTexSlideUp(int value)
  135. {
  136. side *sidep;
  137. uvl duvl03;
  138. fix dist;
  139. byte *vp;
  140. int v;
  141. vp = Side_to_verts[Curside];
  142. sidep = &Cursegp->sides[Curside];
  143. dist = vm_vec_dist(&Vertices[Cursegp->verts[vp[1]]], &Vertices[Cursegp->verts[vp[0]]]);
  144. dist *= value;
  145. if (dist < F1_0/(64*value))
  146. dist = F1_0/(64*value);
  147. duvl03.u = fixdiv(sidep->uvls[1].u - sidep->uvls[0].u,dist);
  148. duvl03.v = fixdiv(sidep->uvls[1].v - sidep->uvls[0].v,dist);
  149. for (v=0; v<4; v++) {
  150. sidep->uvls[v].u -= duvl03.u;
  151. sidep->uvls[v].v -= duvl03.v;
  152. }
  153. Update_flags |= UF_WORLD_CHANGED;
  154. return 1;
  155. }
  156. int TexSlideUp()
  157. {
  158. return DoTexSlideUp(3);
  159. }
  160. int TexSlideUpBig()
  161. {
  162. return DoTexSlideUp(1);
  163. }
  164. // -----------------------------------------------------------
  165. int DoTexSlideDown(int value)
  166. {
  167. side *sidep;
  168. uvl duvl03;
  169. fix dist;
  170. byte *vp;
  171. int v;
  172. vp = Side_to_verts[Curside];
  173. sidep = &Cursegp->sides[Curside];
  174. dist = vm_vec_dist(&Vertices[Cursegp->verts[vp[1]]], &Vertices[Cursegp->verts[vp[0]]]);
  175. dist *= value;
  176. if (dist < F1_0/(64*value))
  177. dist = F1_0/(64*value);
  178. duvl03.u = fixdiv(sidep->uvls[1].u - sidep->uvls[0].u,dist);
  179. duvl03.v = fixdiv(sidep->uvls[1].v - sidep->uvls[0].v,dist);
  180. for (v=0; v<4; v++) {
  181. sidep->uvls[v].u += duvl03.u;
  182. sidep->uvls[v].v += duvl03.v;
  183. }
  184. Update_flags |= UF_WORLD_CHANGED;
  185. return 1;
  186. }
  187. int TexSlideDown()
  188. {
  189. return DoTexSlideDown(3);
  190. }
  191. int TexSlideDownBig()
  192. {
  193. return DoTexSlideDown(1);
  194. }
  195. // -----------------------------------------------------------
  196. // Compute the center of the side in u,v coordinates.
  197. void compute_uv_side_center(uvl *uvcenter, segment *segp, int sidenum)
  198. {
  199. int v;
  200. side *sidep = &segp->sides[sidenum];
  201. uvcenter->u = 0;
  202. uvcenter->v = 0;
  203. for (v=0; v<4; v++) {
  204. uvcenter->u += sidep->uvls[v].u;
  205. uvcenter->v += sidep->uvls[v].v;
  206. }
  207. uvcenter->u /= 4;
  208. uvcenter->v /= 4;
  209. }
  210. // -----------------------------------------------------------
  211. // rotate point *uv by matrix rotmat, return *uvrot
  212. void rotate_uv_point(uvl *uvrot, fix *rotmat, uvl *uv, uvl *uvcenter)
  213. {
  214. uvrot->u = fixmul(uv->u - uvcenter->u,rotmat[0]) + fixmul(uv->v - uvcenter->v,rotmat[1]) + uvcenter->u;
  215. uvrot->v = fixmul(uv->u - uvcenter->u,rotmat[2]) + fixmul(uv->v - uvcenter->v,rotmat[3]) + uvcenter->v;
  216. }
  217. // -----------------------------------------------------------
  218. // Compute the center of the side in u,v coordinates.
  219. void rotate_uv_points_on_side(segment *segp, int sidenum, fix *rotmat, uvl *uvcenter)
  220. {
  221. int v;
  222. side *sidep = &segp->sides[sidenum];
  223. uvl tuv;
  224. for (v=0; v<4; v++) {
  225. rotate_uv_point(&tuv, rotmat, &sidep->uvls[v], uvcenter);
  226. sidep->uvls[v] = tuv;
  227. }
  228. }
  229. // -----------------------------------------------------------
  230. // ang is in 0..ffff = 0..359.999 degrees
  231. // rotmat is filled in with 4 fixes
  232. void create_2d_rotation_matrix(fix *rotmat, fix ang)
  233. {
  234. fix sinang, cosang;
  235. fix_sincos(ang, &sinang, &cosang);
  236. rotmat[0] = cosang;
  237. rotmat[1] = sinang;
  238. rotmat[2] = -sinang;
  239. rotmat[3] = cosang;
  240. }
  241. // -----------------------------------------------------------
  242. int DoTexRotateLeft(int value)
  243. {
  244. uvl uvcenter;
  245. fix rotmat[4];
  246. compute_uv_side_center(&uvcenter, Cursegp, Curside);
  247. // Create a rotation matrix
  248. create_2d_rotation_matrix(rotmat, -F1_0/value);
  249. rotate_uv_points_on_side(Cursegp, Curside, rotmat, &uvcenter);
  250. Update_flags |= UF_WORLD_CHANGED;
  251. return 1;
  252. }
  253. int TexRotateLeft()
  254. {
  255. return DoTexRotateLeft(192);
  256. }
  257. int TexRotateLeftBig()
  258. {
  259. return DoTexRotateLeft(64);
  260. }
  261. // -----------------------------------------------------------
  262. int DoTexSlideRight(int value)
  263. {
  264. side *sidep;
  265. uvl duvl03;
  266. fix dist;
  267. byte *vp;
  268. int v;
  269. vp = Side_to_verts[Curside];
  270. sidep = &Cursegp->sides[Curside];
  271. dist = vm_vec_dist(&Vertices[Cursegp->verts[vp[3]]], &Vertices[Cursegp->verts[vp[0]]]);
  272. dist *= value;
  273. if (dist < F1_0/(64*value))
  274. dist = F1_0/(64*value);
  275. duvl03.u = fixdiv(sidep->uvls[3].u - sidep->uvls[0].u,dist);
  276. duvl03.v = fixdiv(sidep->uvls[3].v - sidep->uvls[0].v,dist);
  277. for (v=0; v<4; v++) {
  278. sidep->uvls[v].u += duvl03.u;
  279. sidep->uvls[v].v += duvl03.v;
  280. }
  281. Update_flags |= UF_WORLD_CHANGED;
  282. return 1;
  283. }
  284. int TexSlideRight()
  285. {
  286. return DoTexSlideRight(3);
  287. }
  288. int TexSlideRightBig()
  289. {
  290. return DoTexSlideRight(1);
  291. }
  292. // -----------------------------------------------------------
  293. int DoTexRotateRight(int value)
  294. {
  295. uvl uvcenter;
  296. fix rotmat[4];
  297. compute_uv_side_center(&uvcenter, Cursegp, Curside);
  298. // Create a rotation matrix
  299. create_2d_rotation_matrix(rotmat, F1_0/value);
  300. rotate_uv_points_on_side(Cursegp, Curside, rotmat, &uvcenter);
  301. Update_flags |= UF_WORLD_CHANGED;
  302. return 1;
  303. }
  304. int TexRotateRight()
  305. {
  306. return DoTexRotateRight(192);
  307. }
  308. int TexRotateRightBig()
  309. {
  310. return DoTexRotateRight(64);
  311. }
  312. // -----------------------------------------------------------
  313. int TexSelectActiveEdge()
  314. {
  315. return 1;
  316. }
  317. // -----------------------------------------------------------
  318. int TexRotate90Degrees()
  319. {
  320. uvl uvcenter;
  321. fix rotmat[4];
  322. compute_uv_side_center(&uvcenter, Cursegp, Curside);
  323. // Create a rotation matrix
  324. create_2d_rotation_matrix(rotmat, F1_0/4);
  325. rotate_uv_points_on_side(Cursegp, Curside, rotmat, &uvcenter);
  326. Update_flags |= UF_WORLD_CHANGED;
  327. return 1;
  328. }
  329. // -----------------------------------------------------------
  330. int TexSetDefault()
  331. {
  332. Num_tilings = 1;
  333. Stretch_scale_x = F1_0;
  334. Stretch_scale_y = F1_0;
  335. assign_default_uvs_to_side(Cursegp,Curside);
  336. Update_flags |= UF_GAME_VIEW_CHANGED;
  337. return 1;
  338. }
  339. // -----------------------------------------------------------
  340. int TexIncreaseTiling()
  341. {
  342. Num_tilings++;
  343. assign_default_uvs_to_side(Cursegp, Curside);
  344. Update_flags |= UF_GAME_VIEW_CHANGED;
  345. return 1;
  346. }
  347. // -----------------------------------------------------------
  348. int TexDecreaseTiling()
  349. {
  350. if (--Num_tilings < 1)
  351. Num_tilings = 1;
  352. assign_default_uvs_to_side(Cursegp, Curside);
  353. Update_flags |= UF_GAME_VIEW_CHANGED;
  354. return 1;
  355. }
  356. // direction = -1 or 1 depending on direction
  357. int TexStretchCommon(int direction)
  358. {
  359. fix *sptr;
  360. if ((Curedge == 0) || (Curedge == 2))
  361. sptr = &Stretch_scale_x;
  362. else
  363. sptr = &Stretch_scale_y;
  364. *sptr += direction*F1_0/64;
  365. if (*sptr < F1_0/16)
  366. *sptr = F1_0/16;
  367. if (*sptr > 2*F1_0)
  368. *sptr = 2*F1_0;
  369. stretch_uvs_from_curedge(Cursegp, Curside);
  370. editor_status("Stretch scale = %7.4f, use Set Default to return to 1.0", f2fl(*sptr));
  371. Update_flags |= UF_GAME_VIEW_CHANGED;
  372. return 1;
  373. }
  374. int TexStretchDown(void)
  375. {
  376. return TexStretchCommon(-1);
  377. }
  378. int TexStretchUp(void)
  379. {
  380. return TexStretchCommon(1);
  381. }