Debian Codegraph @

tools/quake2/extra/texpaint/win_cam.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4
5 This file is part of Quake 2 Tools source code.
6
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "texpaint.h"
24
25 #define CAMERA_WINDOW_CLASS "TPCamera"
26
27 HDC camdc;
28 HGLRC baseRC;
29
30 float pitch, yaw, roll;
31 qboolean model_lines = false;
32
33 float cam_x, cam_y=-64, cam_z=32;
34
35 int cam_width, cam_height;
36
37 BINDTEXFUNCPTR BindTextureEXT;
38
39 void InitIndexTexture (void)
40 {
41 int i;
42
43 BindTextureEXT (GL_TEXTURE_2D, TEXTURE_INDEX);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
47 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
48
49 for (i=0 ; i<sizeof(index_texture)/4 ; i++)
50 index_texture[i] = i+1;
51
52 glTexImage2D (GL_TEXTURE_2D, 0, 3, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, index_texture);
53
54 BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
59 }
60
61 void CreateDisplaylist (void)
62 {
63 }
64
65 void DrawModel (void)
66 {
67 int i, j;
68
69 glColor4f (1,1,1,1);
70
71 glBegin (GL_TRIANGLES);
72 for (i=0 ; i<numfaces ; i++)
73 {
74 for (j=0 ; j<3 ; j++)
75 {
76 glTexCoord2f (tmcoords[i][j][0], tmcoords[i][j][1]);
77 glVertex3fv (faces[i].verts[j]);
78 }
79 }
80 glEnd ();
81 }
82
83 /*
84 =============
85 Cam_Click
86 =============
87 */
88 int cam_last_index;
89 void Cam_Click (int x, int y, qboolean shift)
90 {
91 int index;
92 index = 0;
93 glReadBuffer (GL_BACK);
94 glReadPixels (x, y, 1,1, GL_RGB, GL_UNSIGNED_BYTE, &index);
95
96 index--;
97 if (index == -1)
98 return;
99 if (index >= width2*height2)
100 return;
101
102 if (index == cam_last_index)
103 return; // in same pixel
104 cam_last_index = index;
105 if (shift)
106 {
107 Pal_SetIndex (pic[index]);
108 return;
109 }
110
111 SetSkin (index, selected_rgb);
112 UpdateWindow (camerawindow);
113 }
114
115
116 void Cam_DrawSetup (void)
117 {
118 glViewport (0,0,cam_width, cam_height);
119 glMatrixMode (GL_PROJECTION);
120 glLoadIdentity ();
121 gluPerspective (90, (float)cam_width/cam_height, 2, 1024);
122 gluLookAt (cam_x, cam_y, cam_z, cam_x, cam_y+1, cam_z, 0, 0, 1);
123
124 glRotated (-roll*0.3, 0, 1, 0);
125 glRotated (-pitch*0.3, 1, 0, 0);
126 glRotated (yaw*0.3, 0, 0, 1);
127
128 glMatrixMode (GL_MODELVIEW);
129 glLoadIdentity ();
130
131 glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
132 glEnable (GL_DEPTH_TEST);
133 glEnable (GL_CULL_FACE);
134 glEnable (GL_TEXTURE_2D);
135 glCullFace (GL_FRONT);
136
137 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
138 }
139
140 void Cam_Draw (void)
141 {
142 if (!cam_width || !cam_height)
143 return;
144
145 glClearColor (0.3,0.3,0.3,1);
146 Cam_DrawSetup ();
147
148 BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
149
150 DrawModel ();
151
152 if (model_lines)
153 {
154 glDisable (GL_TEXTURE_2D);
155 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
156 glDepthFunc (GL_LEQUAL);
157 glDepthRange (0, 0.999); // nudge depth to avoid dropouts
158 DrawModel ();
159 glDepthRange (0, 1);
160
161 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
162 glEnable (GL_TEXTURE_2D);
163 }
164
165 SwapBuffers(camdc);
166
167 // now fill the back buffer with the index texture
168 glClearColor (0,0,0,0);
169 glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
170 BindTextureEXT (GL_TEXTURE_2D, TEXTURE_INDEX);
171 DrawModel ();
172
173 BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
174 }
175
176
177
178 /*
179 ============
180 CameraWndProc
181 ============
182 */
183 LONG WINAPI WCam_WndProc (
184 HWND hWnd,
185 UINT uMsg,
186 WPARAM wParam,
187 LPARAM lParam)
188 {
189 LONG lRet = 1;
190 int fwKeys, xPos, yPos;
191 RECT rect;
192 static int oldx, oldy;
193 POINT pt;
194
195 GetClientRect(hWnd, &rect);
196 cam_width = rect.right-rect.left;
197 cam_height = rect.bottom-rect.top;
198
199 switch (uMsg)
200 {
201 case WM_CREATE:
202 camdc = GetDC(hWnd);
203 bSetupPixelFormat(camdc);
204
205 baseRC = wglCreateContext( camdc );
206 if (!baseRC)
207 Sys_Error ("wglCreateContext failed");
208 if (!wglMakeCurrent( camdc, baseRC ))
209 Sys_Error ("wglMakeCurrent failed");
210 BindTextureEXT = (void *)wglGetProcAddress((LPCSTR) "glBindTextureEXT");
211 if (!BindTextureEXT)
212 Sys_Error ("GetProcAddress for BindTextureEXT failed");
213
214 break;
215 case WM_PAINT:
216 {
217 PAINTSTRUCT ps;
218
219 BeginPaint(hWnd, &ps);
220 if (!wglMakeCurrent( camdc, baseRC ))
221 Sys_Error ("wglMakeCurrent failed");
222 Cam_Draw ();
223 EndPaint(hWnd, &ps);
224 }
225 break;
226
227 case WM_MBUTTONDOWN:
228 case WM_RBUTTONDOWN:
229 if (GetTopWindow(mainwindow) != hWnd)
230 BringWindowToTop(hWnd);
231
232 SetFocus (camerawindow);
233 SetCapture (camerawindow);
234 GetCursorPos (&pt);
235 xPos = pt.x;
236 yPos = pt.y;
237 oldx = xPos;
238 oldy = yPos;
239 break;
240
241 case WM_LBUTTONDOWN:
242 cam_last_index = -1;
243 draw:
244 if (GetTopWindow(mainwindow) != hWnd)
245 BringWindowToTop(hWnd);
246
247 SetFocus (camerawindow);
248 SetCapture (camerawindow);
249 fwKeys = wParam; // key flags
250 xPos = (short)LOWORD(lParam); // horizontal position of cursor
251 yPos = (short)HIWORD(lParam); // vertical position of cursor
252 yPos = (int)rect.bottom - 1 - yPos;
253 if (!wglMakeCurrent( camdc, baseRC ))
254 Sys_Error ("wglMakeCurrent failed");
255
256 Cam_Click (xPos, yPos, !!(wParam&(MK_SHIFT|MK_CONTROL)) );
257
258 // Cam_MouseDown (xPos, yPos, fwKeys);
259 break;
260
261 case WM_MBUTTONUP:
262 case WM_RBUTTONUP:
263 case WM_LBUTTONUP:
264 if (! (wParam & (MK_LBUTTON|MK_RBUTTON|MK_MBUTTON)))
265 ReleaseCapture ();
266 break;
267
268 case WM_MOUSEMOVE:
269 {
270 int dx, dy;
271
272 if (wParam & MK_LBUTTON)
273 goto draw;
274
275 GetCursorPos (&pt);
276 xPos = pt.x;
277 yPos = pt.y;
278 if (!(wParam & (MK_RBUTTON|MK_MBUTTON)))
279 {
280 oldx = xPos;
281 oldy = yPos;
282 break;
283 }
284 dx = xPos-oldx;
285 dy = oldy-yPos;
286 if (!dx && !dy)
287 break;
288 SetCursorPos (oldx, oldy);
289
290 if (wParam == (MK_RBUTTON|MK_CONTROL) )
291 {
292 if (abs(dx) > abs(dy))
293 cam_y -= 0.1*dx;
294 else
295 cam_y -= 0.1*dy;
296 InvalidateRect (camerawindow, NULL, false);
297 }
298 if (wParam == MK_RBUTTON)
299 {
300 cam_x -= 0.1*dx;
301 cam_z -= 0.1*dy;
302 InvalidateRect (camerawindow, NULL, false);
303 }
304 if (wParam == (MK_MBUTTON|MK_CONTROL) )
305 {
306 if (abs(dx) > abs(dy))
307 roll -= dx;
308 else
309 roll -= dy;
310 InvalidateRect (camerawindow, NULL, false);
311 }
312 if (wParam == MK_MBUTTON)
313 {
314 yaw += dx;
315 pitch += dy;
316 InvalidateRect (camerawindow, NULL, false);
317 }
318 }
319 break;
320
321
322
323 case WM_SIZE:
324 // camera.width = rect.right;
325 // camera.height = rect.bottom;
326 InvalidateRect(camerawindow, NULL, false);
327 break;
328 case WM_NCCALCSIZE:// don't let windows copy pixels
329 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
330 return WVR_REDRAW;
331 case WM_CLOSE:
332 /* call destroy window to cleanup and go away */
333 DestroyWindow (hWnd);
334 break;
335
336 case WM_DESTROY:
337 {
338 HGLRC hRC;
339 HDC hDC;
340
341 /* release and free the device context and rendering context */
342 hRC = wglGetCurrentContext();
343 hDC = wglGetCurrentDC();
344
345 wglMakeCurrent(NULL, NULL);
346
347 if (hRC)
348 wglDeleteContext(hRC);
349 if (hDC)
350 ReleaseDC(hWnd, hDC);
351 }
352 break;
353
354 default:
355 /* pass all unhandled messages to DefWindowProc */
356 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
357 break;
358 }
359
360 /* return 1 if handled message, 0 if not */
361 return lRet;
362 }
363
364
365 /*
366 ==============
367 WCam_Register
368 ==============
369 */
370 void WCam_Register (HINSTANCE hInstance)
371 {
372 WNDCLASS wc;
373
374 /* Register the camera class */
375 memset (&wc, 0, sizeof(wc));
376
377 wc.style = 0;
378 wc.lpfnWndProc = (WNDPROC)WCam_WndProc;
379 wc.cbClsExtra = 0;
380 wc.cbWndExtra = 0;
381 wc.hInstance = hInstance;
382 wc.hIcon = 0;
383 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
384 wc.hbrBackground = NULL;
385 wc.lpszMenuName = 0;
386 wc.lpszClassName = CAMERA_WINDOW_CLASS;
387
388 if (!RegisterClass (&wc) )
389 Sys_Error ("WCam_Register: failed");
390 }
391
392
393 void WCam_Create (HINSTANCE hInstance)
394 {
395 WCam_Register (hInstance);
396
397 camerawindow = CreateWindow (CAMERA_WINDOW_CLASS ,
398 "Camera View",
399 QE3_STYLE,
400 0,
401 0,
402 (int)(screen_width*0.5),
403 (int)(screen_height-20), // size
404
405 mainwindow, // parent window
406 0, // no menu
407 hInstance,
408 0);
409 if (!camerawindow)
410 Sys_Error ("Couldn't create camerawindow");
411
412 RestoreWindowState(camerawindow, "camerawindow");
413 ShowWindow (camerawindow, SW_SHOWDEFAULT);
414 }