win_skin.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. #define SKIN_WINDOW_CLASS "TPSkin"
  20. HDC skindc;
  21. int skinw_width, skinw_height; // size of the window
  22. float skin_x = 128, skin_y = 128, skin_z = 100;
  23. qboolean skin_lines = false;
  24. char tri_filename[1024];
  25. char skin_filename[1024];
  26. int skin_width, skin_height; // size of the .lbm image
  27. unsigned index_texture[1024*512];
  28. void UpdateTexture (int offset)
  29. {
  30. int x, y;
  31. y = offset / width2;
  32. x = offset % width2;
  33. BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
  34. // glTexImage2D (GL_TEXTURE_2D, 0, 3, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgb);
  35. glTexSubImage2D (GL_TEXTURE_2D, 0, x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgb+offset);
  36. }
  37. /*
  38. ===================================================================
  39. TEXEL MODIFICATION
  40. ===================================================================
  41. */
  42. #define MAX_MODIFY 8192
  43. typedef struct
  44. {
  45. int offset;
  46. int oldvalue;
  47. } modify_t;
  48. int modify_index;
  49. int undo_index;
  50. modify_t modify[MAX_MODIFY];
  51. void SetSkinModified (void)
  52. {
  53. char text[1024];
  54. if (modified && modified_past_autosave)
  55. return;
  56. modified = true;
  57. modified_past_autosave = true;
  58. sprintf (text, "%s *", skin_filename);
  59. SetWindowText (skinwindow, text);
  60. }
  61. void SetSkin (int index, int pixel)
  62. {
  63. modify_t *m;
  64. if (!modified)
  65. SetSkinModified ();
  66. // save undo info
  67. m = &modify[undo_index];
  68. m->offset = index;
  69. m->oldvalue = pic[index];
  70. modify_index = (++undo_index)&(MAX_MODIFY-1);
  71. // modify it
  72. rgb[index] = selected_rgb;
  73. pic[index] = selected_index;
  74. UpdateTexture (index);
  75. InvalidateRect (skinwindow, NULL, false);
  76. InvalidateRect (camerawindow, NULL, false);
  77. }
  78. void Undo (void)
  79. {
  80. modify_t *m;
  81. int temp;
  82. if (!undo_index)
  83. return;
  84. if (!--undo_index)
  85. { // back to unmodified state
  86. modified = false;
  87. SetWindowText (skinwindow, skin_filename);
  88. }
  89. m = &modify[undo_index];
  90. // modify it
  91. temp = pic[m->offset];
  92. pic[m->offset] = m->oldvalue;
  93. rgb[m->offset] = palette[m->oldvalue*3] +
  94. (palette[m->oldvalue*3+1]<<8) + (palette[m->oldvalue*3+2]<<16);
  95. m->oldvalue = temp;
  96. UpdateTexture (m->offset);
  97. InvalidateRect (skinwindow, NULL, false);
  98. InvalidateRect (camerawindow, NULL, false);
  99. }
  100. void Redo (void)
  101. {
  102. modify_t *m;
  103. int temp;
  104. if (undo_index == modify_index)
  105. return;
  106. m = &modify[undo_index];
  107. // modify it
  108. temp = pic[m->offset];
  109. pic[m->offset] = m->oldvalue;
  110. rgb[m->offset] = palette[m->oldvalue*3] +
  111. (palette[m->oldvalue*3+1]<<8) + (palette[m->oldvalue*3+2]<<16);
  112. m->oldvalue = temp;
  113. UpdateTexture (m->offset);
  114. InvalidateRect (skinwindow, NULL, false);
  115. InvalidateRect (camerawindow, NULL, false);
  116. if (!undo_index++)
  117. { // modified again
  118. char text[1024];
  119. modified = true;
  120. sprintf (text, "%s *", skin_filename);
  121. SetWindowText (skinwindow, text);
  122. }
  123. }
  124. //===================================================================
  125. /*
  126. =============
  127. Skin_SaveFile
  128. Load a skin texture and the base.tri from the same directory
  129. =============
  130. */
  131. void Skin_SaveFile (char *name)
  132. {
  133. byte *data;
  134. int i, j;
  135. char backup[1024];
  136. // back up the current file if it exists
  137. sprintf (backup, "%s.bak", name);
  138. remove (backup);
  139. rename (name, backup);
  140. modified = false;
  141. modified_past_autosave = false;
  142. modify_index = undo_index = 0;
  143. SetWindowText (skinwindow, skin_filename);
  144. data = malloc(skin_width*skin_height);
  145. for (i=0 ; i<skin_height ; i++)
  146. memcpy (data + i*skin_width, pic + i*width2, skin_width);
  147. Save256Image (name, data, palette, skin_width, skin_height);
  148. free(data);
  149. }
  150. /*
  151. =============
  152. Expand256Texture
  153. =============
  154. */
  155. void Expand256Texture (void)
  156. {
  157. int i, j;
  158. int p;
  159. memset (rgb, 0, sizeof(rgb));
  160. for (i=0 ; i<skin_height ; i++)
  161. {
  162. for (j=0 ; j<skin_width ; j++)
  163. {
  164. p = pic[i*width2+j];
  165. rgb[i*width2+j] = (palette[p*3+0]<<0) + (palette[p*3+1]<<8) + (palette[p*3+2]<<16);
  166. }
  167. }
  168. BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
  169. glTexImage2D (GL_TEXTURE_2D, 0, 3, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgb);
  170. }
  171. void SetSizes (int width, int height)
  172. {
  173. int i;
  174. if (width < 32)
  175. width = 16;
  176. if (height < 32)
  177. height = 16;
  178. skin_width = width;
  179. skin_height = height;
  180. if (skin_width > 1024 || skin_height > 512)
  181. Sys_Error ("Skin file is too large");
  182. width2 = 1;
  183. height2 = 1;
  184. for (i=0 ; i<12 ; i++)
  185. {
  186. if (width2 < skin_width)
  187. width2<<=1;
  188. if (height2 < skin_height)
  189. height2<<=1;
  190. }
  191. // compatability shit for auto sizing of old skins
  192. if (skin_width != 320 || skin_height != 200)
  193. {
  194. skinwidth = skin_width;
  195. skinheight = skin_height;
  196. }
  197. else
  198. {
  199. skinwidth = 0;
  200. skinheight = 0;
  201. }
  202. }
  203. /*
  204. =============
  205. Skin_LoadFile
  206. Load a skin texture and the base.tri from the same directory
  207. =============
  208. */
  209. void Skin_LoadFile (char *name)
  210. {
  211. int i, j, p;
  212. byte *lbmpic;
  213. byte *lbmpal;
  214. char trifile[1024];
  215. int width, height;
  216. modified = false;
  217. modified_past_autosave = false;
  218. modify_index = undo_index = 0;
  219. strcpy (skin_filename, name);
  220. SetWindowText (skinwindow, skin_filename);
  221. //
  222. // read the texture
  223. //
  224. Load256Image (skin_filename, &lbmpic, &lbmpal, &width, &height);
  225. memcpy (palette, lbmpal, sizeof(palette));
  226. free (lbmpal);
  227. SetSizes (width, height);
  228. memset (pic, 0, sizeof(pic));
  229. for (i=0 ; i<skin_height ; i++)
  230. {
  231. for (j=0 ; j<skin_width ; j++)
  232. {
  233. p = lbmpic[i*skin_width + j];
  234. pic[i*width2+j] = p;
  235. }
  236. }
  237. free (lbmpic);
  238. Expand256Texture ();
  239. InitIndexTexture ();
  240. Pal_SetIndex (selected_index);
  241. //
  242. // read the polfile and
  243. // generate the texture coordinates
  244. //
  245. strcpy (trifile, skin_filename);
  246. StripExtension (trifile);
  247. strcat (trifile, ".tri");
  248. if (FileExists (trifile))
  249. {
  250. LoadTriFile (trifile);
  251. CalcTmCoords ();
  252. }
  253. else
  254. {
  255. ExtractFilePath (name, trifile);
  256. strcat (trifile, "base.tri");
  257. if (FileExists (trifile))
  258. {
  259. LoadTriFile (trifile);
  260. CalcTmCoords ();
  261. }
  262. }
  263. InvalidateRect (palettewindow, NULL, false);
  264. InvalidateRect (skinwindow, NULL, false);
  265. InvalidateRect (camerawindow, NULL, false);
  266. }
  267. /*
  268. =============
  269. Skin_Click
  270. =============
  271. */
  272. int skin_last_index;
  273. void Skin_Click (int x, int y, qboolean shift)
  274. {
  275. int index;
  276. index = 0;
  277. glReadBuffer (GL_BACK);
  278. glReadPixels (x, y, 1,1, GL_RGB, GL_UNSIGNED_BYTE, &index);
  279. index--;
  280. if (index == -1)
  281. return;
  282. if (index >= width2*height2)
  283. return;
  284. if (index == skin_last_index)
  285. return; // in same pixel
  286. skin_last_index = index;
  287. if (shift)
  288. {
  289. Pal_SetIndex (pic[index]);
  290. return;
  291. }
  292. SetSkin (index, selected_index);
  293. UpdateWindow (skinwindow);
  294. }
  295. void DrawModelST (void)
  296. {
  297. int i, j;
  298. glColor4f (1,1,1,1);
  299. glBegin (GL_TRIANGLES);
  300. for (i=0 ; i<numfaces ; i++)
  301. {
  302. for (j=0 ; j<3 ; j++)
  303. {
  304. glVertex2f (tmcoords[i][j][0]*width2, (1-tmcoords[i][j][1])*height2);
  305. }
  306. }
  307. glEnd ();
  308. }
  309. void DrawSkin (void)
  310. {
  311. glBegin (GL_POLYGON);
  312. glTexCoord2f (0,1);
  313. glVertex2f (0,0);
  314. glTexCoord2f (0,0);
  315. glVertex2f (0,height2);
  316. glTexCoord2f (1,0);
  317. glVertex2f (width2,height2);
  318. glTexCoord2f (1,1);
  319. glVertex2f (width2,0);
  320. glEnd ();
  321. }
  322. void Skin_Draw (void)
  323. {
  324. int x, y;
  325. float aspect;
  326. float xs, ys;
  327. int c;
  328. //
  329. // draw it
  330. //
  331. if (skin_z < 20)
  332. skin_z = 20;
  333. glViewport (0,0,skinw_width, skinw_height);
  334. glMatrixMode (GL_PROJECTION);
  335. glLoadIdentity ();
  336. gluPerspective (90, (float)skinw_width/skinw_height, 2, 16384);
  337. glMatrixMode (GL_MODELVIEW);
  338. glLoadIdentity ();
  339. gluLookAt (skin_x, skin_y, skin_z, skin_x, skin_y, skin_z-1, 0, 1, 0);
  340. glClearColor (0.3,0.3,0.3,1);
  341. glClear (GL_COLOR_BUFFER_BIT);
  342. glDisable (GL_DEPTH_TEST);
  343. glDisable (GL_CULL_FACE);
  344. glEnable (GL_TEXTURE_2D);
  345. glColor4f (1,1,1,1);
  346. DrawSkin ();
  347. if (skin_lines)
  348. {
  349. glDisable (GL_TEXTURE_2D);
  350. glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
  351. DrawModelST ();
  352. glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  353. glEnable (GL_TEXTURE_2D);
  354. }
  355. SwapBuffers(skindc);
  356. // now fill the back buffer with the index texture
  357. glClearColor (0,0,0,0);
  358. glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  359. BindTextureEXT (GL_TEXTURE_2D, TEXTURE_INDEX);
  360. DrawSkin ();
  361. BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
  362. }
  363. /*
  364. ============
  365. Skin_WndProc
  366. ============
  367. */
  368. LONG WINAPI Skin_WndProc (
  369. HWND hWnd,
  370. UINT uMsg,
  371. WPARAM wParam,
  372. LPARAM lParam)
  373. {
  374. LONG lRet = 1;
  375. int fwKeys, xPos, yPos;
  376. RECT rect;
  377. GetClientRect(hWnd, &rect);
  378. skinw_width = rect.right-rect.left;
  379. skinw_height = rect.bottom-rect.top;
  380. switch (uMsg)
  381. {
  382. case WM_CREATE:
  383. skindc = GetDC(hWnd);
  384. bSetupPixelFormat(skindc);
  385. break;
  386. case WM_PAINT:
  387. {
  388. PAINTSTRUCT ps;
  389. BeginPaint(hWnd, &ps);
  390. if (!wglMakeCurrent( skindc, baseRC ))
  391. Sys_Error ("wglMakeCurrent failed");
  392. Skin_Draw ();
  393. EndPaint(hWnd, &ps);
  394. }
  395. break;
  396. case WM_LBUTTONDOWN:
  397. skin_last_index = -1;
  398. draw:
  399. if (GetTopWindow(mainwindow) != hWnd)
  400. BringWindowToTop(hWnd);
  401. SetFocus (skinwindow);
  402. SetCapture (skinwindow);
  403. fwKeys = wParam; // key flags
  404. xPos = (short)LOWORD(lParam); // horizontal position of cursor
  405. yPos = (short)HIWORD(lParam); // vertical position of cursor
  406. yPos = (int)rect.bottom - 1 - yPos;
  407. if (!wglMakeCurrent( skindc, baseRC ))
  408. Sys_Error ("wglMakeCurrent failed");
  409. Skin_Click (xPos, yPos, !!(wParam&(MK_SHIFT|MK_CONTROL)) );
  410. break;
  411. case WM_MBUTTONUP:
  412. case WM_RBUTTONUP:
  413. case WM_LBUTTONUP:
  414. fwKeys = wParam; // key flags
  415. if (! (fwKeys & (MK_LBUTTON|MK_RBUTTON|MK_MBUTTON)))
  416. ReleaseCapture ();
  417. break;
  418. case WM_MOUSEMOVE:
  419. {
  420. static int oldx, oldy;
  421. int dx, dy;
  422. POINT pt;
  423. if (wParam & MK_LBUTTON)
  424. goto draw;
  425. GetCursorPos (&pt);
  426. xPos = pt.x;
  427. yPos = pt.y;
  428. if (!(wParam & (MK_RBUTTON|MK_MBUTTON)))
  429. {
  430. oldx = xPos;
  431. oldy = yPos;
  432. break;
  433. }
  434. dx = xPos-oldx;
  435. dy = oldy-yPos;
  436. if (!dx && !dy)
  437. break;
  438. SetCursorPos (oldx, oldy);
  439. if (wParam == (MK_RBUTTON|MK_CONTROL) )
  440. {
  441. if (abs(dx) > abs(dy))
  442. skin_z += 0.25*dx;
  443. else
  444. skin_z += 0.25*dy;
  445. InvalidateRect (skinwindow, NULL, false);
  446. }
  447. if (wParam == MK_RBUTTON)
  448. {
  449. skin_x -= 0.25*dx;
  450. skin_y -= 0.25*dy;
  451. InvalidateRect (skinwindow, NULL, false);
  452. }
  453. }
  454. break;
  455. case WM_SIZE:
  456. InvalidateRect(camerawindow, NULL, false);
  457. break;
  458. case WM_NCCALCSIZE:// don't let windows copy pixels
  459. lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  460. return WVR_REDRAW;
  461. case WM_CLOSE:
  462. DestroyWindow (hWnd);
  463. break;
  464. default:
  465. /* pass all unhandled messages to DefWindowProc */
  466. lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  467. break;
  468. }
  469. /* return 1 if handled message, 0 if not */
  470. return lRet;
  471. }
  472. /*
  473. ==============
  474. WSkin_Create
  475. ==============
  476. */
  477. void WSkin_Create (HINSTANCE hInstance)
  478. {
  479. WNDCLASS wc;
  480. /* Register the camera class */
  481. memset (&wc, 0, sizeof(wc));
  482. wc.style = 0;
  483. wc.lpfnWndProc = (WNDPROC)Skin_WndProc;
  484. wc.cbClsExtra = 0;
  485. wc.cbWndExtra = 0;
  486. wc.hInstance = hInstance;
  487. wc.hIcon = 0;
  488. wc.hCursor = LoadCursor (NULL,IDC_ARROW);
  489. wc.hbrBackground = NULL;
  490. wc.lpszMenuName = 0;
  491. wc.lpszClassName = SKIN_WINDOW_CLASS;
  492. if (!RegisterClass (&wc) )
  493. Sys_Error ("RegisterClass failed");
  494. skinwindow = CreateWindow (SKIN_WINDOW_CLASS ,
  495. "Skin View",
  496. QE3_STYLE,
  497. (int)(screen_width*0.5),
  498. (int)(screen_height*0.2),
  499. (int)(screen_width*0.5),
  500. (int)(screen_height*0.8), // size
  501. mainwindow, // parent window
  502. 0, // no menu
  503. hInstance,
  504. 0);
  505. if (!skinwindow)
  506. Error ("Couldn't create skinwindow");
  507. // RestoreWindowState(palettewindow, "palettewindow");
  508. ShowWindow (skinwindow, SW_SHOWDEFAULT);
  509. }
  510. /*
  511. ===================================================================
  512. SKIN RESAMPLING
  513. ===================================================================
  514. */
  515. HWND resamplewindow;
  516. HDC resampledc;
  517. #define RESAMPLE_WINDOW_CLASS "TPResample"
  518. /*
  519. ============
  520. Resample_WndProc
  521. ============
  522. */
  523. LONG WINAPI Resample_WndProc (
  524. HWND hWnd,
  525. UINT uMsg,
  526. WPARAM wParam,
  527. LPARAM lParam)
  528. {
  529. switch (uMsg)
  530. {
  531. case WM_CREATE:
  532. resampledc = GetDC(hWnd);
  533. bSetupPixelFormat(resampledc);
  534. break;
  535. }
  536. return DefWindowProc (hWnd, uMsg, wParam, lParam);
  537. }
  538. /*
  539. ==============
  540. ResampleWindow
  541. ==============
  542. */
  543. void ResampleWindow (HINSTANCE hInstance)
  544. {
  545. WNDCLASS wc;
  546. static qboolean registered;
  547. if (!registered)
  548. {
  549. registered = true;
  550. /* Register the camera class */
  551. memset (&wc, 0, sizeof(wc));
  552. wc.style = 0;
  553. wc.lpfnWndProc = (WNDPROC)Resample_WndProc;
  554. wc.cbClsExtra = 0;
  555. wc.cbWndExtra = 0;
  556. wc.hInstance = hInstance;
  557. wc.hIcon = 0;
  558. wc.hCursor = LoadCursor (NULL,IDC_ARROW);
  559. wc.hbrBackground = NULL;
  560. wc.lpszMenuName = 0;
  561. wc.lpszClassName = RESAMPLE_WINDOW_CLASS;
  562. if (!RegisterClass (&wc) )
  563. Sys_Error ("RegisterClass failed");
  564. }
  565. resamplewindow = CreateWindow (RESAMPLE_WINDOW_CLASS ,
  566. "ResampleWindow",
  567. WS_OVERLAPPED,
  568. 0, 0, width2+32, height2+32, // size
  569. NULL, // parent window
  570. 0, // no menu
  571. hInstance,
  572. 0);
  573. if (!resamplewindow)
  574. Error ("Couldn't create skinwindow");
  575. ShowWindow (resamplewindow, SW_SHOWDEFAULT);
  576. }
  577. void OutlineTexture (byte *pic)
  578. {
  579. int i, j;
  580. int x, y;
  581. int empty;
  582. byte oldpic[1024*512];
  583. memcpy (oldpic, pic, width2*height2);
  584. empty = oldpic[0];
  585. for (i=0 ; i<height2 ; i++)
  586. {
  587. for (j=0 ; j<width2 ; j++)
  588. {
  589. if (oldpic[i*width2+j] != empty)
  590. continue;
  591. for (x=-1 ; x<=1 ; x++)
  592. {
  593. for (y=-1 ; y<=1 ; y++)
  594. {
  595. if (i+y < 0 || i+y >= height2)
  596. continue;
  597. if (j+x < 0 || j+x >= width2)
  598. continue;
  599. if (oldpic[(i+y)*width2 + j+x] != empty)
  600. {
  601. pic[i*width2+j] = oldpic[(i+y)*width2 + j+x];
  602. goto done;
  603. }
  604. }
  605. }
  606. done: ;
  607. }
  608. }
  609. }
  610. void ResampleSkin (void)
  611. {
  612. int i, j;
  613. static float oldtmcoords[10000][3][2];
  614. static int newindex[1024*512];
  615. static byte oldpic[1024*512];
  616. // open a window of the texture size
  617. ResampleWindow (main_instance);
  618. // get new S/T from current frame
  619. memcpy (oldtmcoords, tmcoords, numfaces*3*2*4);
  620. CalcTmCoords ();
  621. // draw all the triangles with the index texture
  622. if (!wglMakeCurrent( resampledc, baseRC ))
  623. Sys_Error ("wglMakeCurrent failed");
  624. glViewport (0,0,width2, height2);
  625. glClearColor (0,0,0,0);
  626. glClear (GL_COLOR_BUFFER_BIT);
  627. glMatrixMode (GL_PROJECTION);
  628. glLoadIdentity ();
  629. glOrtho (0, width2, 0, height2, -100, 100);
  630. glMatrixMode (GL_MODELVIEW);
  631. glLoadIdentity ();
  632. glColor4f (1,1,1,1);
  633. glDisable (GL_DEPTH_TEST);
  634. glDisable (GL_CULL_FACE);
  635. BindTextureEXT (GL_TEXTURE_2D, TEXTURE_INDEX);
  636. #if 0
  637. glDisable(GL_TEXTURE_2D);
  638. glBegin (GL_LINE_LOOP);
  639. glVertex3f (1,1,10);
  640. glVertex3f (skin_width-1,0,10);
  641. glVertex3f (skin_width-1,skin_height-1,10);
  642. glVertex3f (1,skin_height-1,10);
  643. glEnd ();
  644. glEnable(GL_TEXTURE_2D);
  645. #endif
  646. glBegin (GL_TRIANGLES);
  647. for (i=0 ; i<numfaces ; i++)
  648. {
  649. for (j=0 ; j<3 ; j++)
  650. {
  651. glTexCoord2f (oldtmcoords[i][j][0], oldtmcoords[i][j][1]);
  652. glVertex3f (tmcoords[i][j][0]*width2, tmcoords[i][j][1]*height2, 10);
  653. }
  654. }
  655. glEnd ();
  656. SwapBuffers (resampledc);
  657. // build the new color texture
  658. memcpy (oldpic, pic, width2*height2);
  659. glReadBuffer (GL_FRONT);
  660. glReadPixels (0,0,width2,height2,GL_RGBA,GL_UNSIGNED_BYTE, &newindex);
  661. for (i=0 ; i<height2 ; i++)
  662. for (j=0 ; j<width2 ; j++)
  663. pic[i*width2+j] = oldpic[newindex[i*width2+j]&0xffffff];
  664. // outline it
  665. OutlineTexture (pic);
  666. Expand256Texture ();
  667. InvalidateRect (skinwindow, NULL, false);
  668. InvalidateRect (camerawindow, NULL, false);
  669. // change name
  670. strcpy (skin_filename, tri_filename);
  671. StripExtension (skin_filename);
  672. strcat (skin_filename, ".lbm");
  673. SetSkinModified ();
  674. wglMakeCurrent (NULL, NULL);
  675. DestroyWindow (resamplewindow);
  676. }
  677. /*
  678. ===================================================================
  679. NEW SKIN
  680. ===================================================================
  681. */
  682. BOOL CALLBACK NewSkinDlgProc (
  683. HWND hwndDlg, // handle to dialog box
  684. UINT uMsg, // message
  685. WPARAM wParam, // first message parameter
  686. LPARAM lParam // second message parameter
  687. )
  688. {
  689. char sz[256];
  690. int width, height;
  691. switch (uMsg)
  692. {
  693. case WM_INITDIALOG:
  694. SetWindowText(GetDlgItem(hwndDlg, IDC_WIDTH), "320");
  695. SetWindowText(GetDlgItem(hwndDlg, IDC_HEIGHT), "200");
  696. return TRUE;
  697. case WM_COMMAND:
  698. switch (LOWORD(wParam))
  699. {
  700. case IDOK:
  701. GetWindowText(GetDlgItem(hwndDlg, IDC_WIDTH), sz, 255);
  702. width = atoi(sz);
  703. GetWindowText(GetDlgItem(hwndDlg, IDC_HEIGHT), sz, 255);
  704. height = atoi(sz);
  705. SetSizes (width, height);
  706. EndDialog(hwndDlg, 1);
  707. return TRUE;
  708. case IDCANCEL:
  709. EndDialog(hwndDlg, 0);
  710. return TRUE;
  711. }
  712. }
  713. return FALSE;
  714. }
  715. void NewSkin (void)
  716. {
  717. int i, j;
  718. byte *buf;
  719. if (!DialogBox(main_instance, (char *)IDD_NEWSKIN, mainwindow, NewSkinDlgProc))
  720. return;
  721. // open a window of the texture size
  722. ResampleWindow (main_instance);
  723. // get new S/T from current frame
  724. CalcTmCoords ();
  725. // draw all the triangles
  726. if (!wglMakeCurrent( resampledc, baseRC ))
  727. Sys_Error ("wglMakeCurrent failed");
  728. glViewport (0,0,width2, height2);
  729. glClearColor (0,0,0,0);
  730. glClear (GL_COLOR_BUFFER_BIT);
  731. glMatrixMode (GL_PROJECTION);
  732. glLoadIdentity ();
  733. glOrtho (0, width2, 0, height2, -100, 100);
  734. glMatrixMode (GL_MODELVIEW);
  735. glLoadIdentity ();
  736. glColor4f (1,1,1,1);
  737. glDisable (GL_DEPTH_TEST);
  738. glDisable (GL_CULL_FACE);
  739. glDisable (GL_TEXTURE_2D);
  740. for (i=0 ; i<numfaces ; i++)
  741. {
  742. glColor3f ((i&255)/255.0, (i&255)/255.0, (i&255)/255.0);
  743. glBegin (GL_TRIANGLES);
  744. for (j=0 ; j<3 ; j++)
  745. glVertex3f (tmcoords[i][j][0]*width2, tmcoords[i][j][1]*height2, 10);
  746. glEnd ();
  747. }
  748. SwapBuffers (resampledc);
  749. // build the new color texture
  750. glReadBuffer (GL_FRONT);
  751. buf = malloc(width2*height2*4);
  752. glReadPixels (0,0,width2,height2,GL_RGBA,GL_UNSIGNED_BYTE, buf);
  753. for (i=0 ; i<width2*height2 ; i++)
  754. pic[i] = buf[i*4];
  755. free (buf);
  756. // outline it
  757. OutlineTexture (pic);
  758. Expand256Texture ();
  759. InitIndexTexture ();
  760. InvalidateRect (skinwindow, NULL, false);
  761. InvalidateRect (camerawindow, NULL, false);
  762. // change name
  763. strcpy (skin_filename, tri_filename);
  764. StripExtension (skin_filename);
  765. strcat (skin_filename, ".lbm");
  766. SetSkinModified ();
  767. wglMakeCurrent (NULL, NULL);
  768. DestroyWindow (resamplewindow);
  769. }