texpaint.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "texpaint.h"
  19. triangle_t *faces;
  20. int numfaces;
  21. int skinwidth, skinheight;
  22. int picwidth, picheight;
  23. int width, height;
  24. int iwidth, iheight;
  25. int width2, height2; // padded to ^2
  26. float tmcoords[10000][3][2];
  27. byte pic[1024*512];
  28. unsigned rgb[1024*512];
  29. float scale;
  30. float s_scale, t_scale;
  31. char filename[1024];
  32. char picfilename[1024];
  33. /*
  34. ================
  35. BoundFaces
  36. ================
  37. */
  38. vec3_t mins, maxs;
  39. void BoundFaces (void)
  40. {
  41. int i,j,k;
  42. triangle_t *pol;
  43. float v;
  44. for (i=0 ; i<3 ; i++)
  45. {
  46. mins[i] = 9999;
  47. maxs[i] = -9999;
  48. }
  49. for (i=0 ; i<numfaces ; i++)
  50. {
  51. pol = &faces[i];
  52. for (j=0 ; j<3 ; j++)
  53. for (k=0 ; k<3 ; k++)
  54. {
  55. v = pol->verts[j][k];
  56. if (v<mins[k])
  57. mins[k] = v;
  58. if (v>maxs[k])
  59. maxs[k] = v;
  60. }
  61. }
  62. for (i=0 ; i<3 ; i++)
  63. {
  64. mins[i] = floor(mins[i]);
  65. maxs[i] = ceil(maxs[i]);
  66. }
  67. width = maxs[0] - mins[0];
  68. height = maxs[2] - mins[2];
  69. printf ("width: %i height: %i\n",width, height);
  70. if (!skinwidth)
  71. { // old way
  72. scale = 8;
  73. if (width*scale >= 150)
  74. scale = 150.0 / width;
  75. if (height*scale >= 190)
  76. scale = 190.0 / height;
  77. s_scale = t_scale = scale;
  78. iwidth = ceil(width*scale) + 4;
  79. iheight = ceil(height*scale) + 4;
  80. }
  81. else
  82. { // new way
  83. s_scale = (skinwidth/2-4)/(float)width;
  84. t_scale = (skinheight-4)/(float)height;
  85. iwidth = skinwidth/2;
  86. iheight = skinheight;
  87. }
  88. printf ("scale: %f\n",scale);
  89. printf ("iwidth: %i iheight: %i\n",iwidth, iheight);
  90. }
  91. /*
  92. ============
  93. AddFace
  94. ============
  95. */
  96. void AddFace (int facenum, triangle_t *f)
  97. {
  98. vec3_t v1, v2, normal;
  99. int basex, basey;
  100. int i, j;
  101. int coords[3][2];
  102. //
  103. // determine which side to map the teture to
  104. //
  105. VectorSubtract (f->verts[0], f->verts[1], v1);
  106. VectorSubtract (f->verts[2], f->verts[1], v2);
  107. CrossProduct (v1, v2, normal);
  108. if (normal[1] > 0)
  109. basex = iwidth + 2;
  110. else
  111. basex = 2;
  112. basey = 2;
  113. for (i=0 ; i<3 ; i++)
  114. {
  115. coords[i][0] = Q_rint((f->verts[i][0] - mins[0])*s_scale + basex);
  116. coords[i][1] = Q_rint( (maxs[2] - f->verts[i][2])*t_scale + basey);
  117. tmcoords[facenum][i][0] = coords[i][0]/(float)width2;
  118. tmcoords[facenum][i][1] = coords[i][1]/(float)height2;
  119. }
  120. }
  121. void CalcTmCoords (void)
  122. {
  123. int j;
  124. BoundFaces ();
  125. for (j=0 ; j<numfaces ; j++)
  126. AddFace (j, &faces[j]);
  127. printf ("numfaces: %i\n",numfaces);
  128. }
  129. //===============================================================================
  130. #define MAX_NUM_ARGVS 32
  131. int argc;
  132. char *argv[MAX_NUM_ARGVS];
  133. /*
  134. ============
  135. ParseCommandLine
  136. ============
  137. */
  138. void ParseCommandLine (char *lpCmdLine)
  139. {
  140. argc = 1;
  141. argv[0] = "programname";
  142. while (*lpCmdLine && (argc < MAX_NUM_ARGVS))
  143. {
  144. while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
  145. lpCmdLine++;
  146. if (*lpCmdLine)
  147. {
  148. argv[argc] = lpCmdLine;
  149. argc++;
  150. while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
  151. lpCmdLine++;
  152. if (*lpCmdLine)
  153. {
  154. *lpCmdLine = 0;
  155. lpCmdLine++;
  156. }
  157. }
  158. }
  159. }
  160. /*
  161. =================
  162. LoadTriFile
  163. =================
  164. */
  165. void LoadTriFile (char *name)
  166. {
  167. strcpy (tri_filename, name);
  168. SetWindowText (camerawindow, tri_filename);
  169. LoadTriangleList (tri_filename, &faces, &numfaces);
  170. InvalidateRect (camerawindow, NULL, false);
  171. }
  172. /*
  173. ==================
  174. TimerProc
  175. ==================
  176. */
  177. int CALLBACK TimerProc(
  178. HWND hwnd, // handle of window for timer messages
  179. UINT uMsg, // WM_TIMER message
  180. UINT idEvent, // timer identifier
  181. DWORD dwTime // current system time
  182. )
  183. {
  184. static int counter;
  185. char name[1024];
  186. if (!skin_filename[0])
  187. return 0;
  188. if (!modified_past_autosave)
  189. {
  190. counter = 0;
  191. return 0;
  192. }
  193. counter++;
  194. if (counter < 3*5)
  195. return 0; // save every five minutes
  196. strcpy (name, skin_filename);
  197. StripExtension (name);
  198. strcat (name, "_autosave.lbm");
  199. Skin_SaveFile (name);
  200. modified_past_autosave = false;
  201. counter = 0;
  202. return 0;
  203. }
  204. /*
  205. ==================
  206. WinMain
  207. ==================
  208. */
  209. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance
  210. ,LPSTR lpCmdLine, int nCmdShow)
  211. {
  212. MSG msg;
  213. HACCEL accelerators;
  214. main_instance = hInstance;
  215. ParseCommandLine (lpCmdLine);
  216. screen_width = GetSystemMetrics (SM_CXFULLSCREEN);
  217. screen_height = GetSystemMetrics (SM_CYFULLSCREEN);
  218. // hack for broken NT 4.0 dual screen
  219. if (screen_width > 2*screen_height)
  220. screen_width /= 2;
  221. accelerators = LoadAccelerators (hInstance
  222. , MAKEINTRESOURCE(IDR_ACCELERATOR1));
  223. if (!accelerators)
  224. Sys_Error ("LoadAccelerators failed");
  225. Main_Create (hInstance);
  226. WCam_Create (hInstance);
  227. WPal_Create (hInstance);
  228. WSkin_Create (hInstance);
  229. if (argc == 2)
  230. Skin_LoadFile (argv[1]);
  231. SetTimer ( mainwindow, 1, 1000*20, TimerProc );
  232. while (1)
  233. {
  234. if (!GetMessage (&msg, mainwindow, 0, 0))
  235. break;
  236. if (!TranslateAccelerator(mainwindow, accelerators, &msg) )
  237. {
  238. TranslateMessage (&msg);
  239. DispatchMessage (&msg);
  240. }
  241. }
  242. /* return success of application */
  243. return TRUE;
  244. }