2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
5 This file is part of Quake 2 Tools source code.
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.
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.
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 ===========================================================================
25 #define CAMERA_WINDOW_CLASS "TPCamera"
30 float pitch, yaw, roll;
31 qboolean model_lines = false;
33 float cam_x, cam_y=-64, cam_z=32;
35 int cam_width, cam_height;
37 BINDTEXFUNCPTR BindTextureEXT;
39 void InitIndexTexture (void)
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);
49 for (i=0 ; i<sizeof(index_texture)/4 ; i++)
50 index_texture[i] = i+1;
52 glTexImage2D (GL_TEXTURE_2D, 0, 3, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, index_texture);
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);
61 void CreateDisplaylist (void)
71 glBegin (GL_TRIANGLES);
72 for (i=0 ; i<numfaces ; i++)
76 glTexCoord2f (tmcoords[i][j][0], tmcoords[i][j][1]);
77 glVertex3fv (faces[i].verts[j]);
89 void Cam_Click (int x, int y, qboolean shift)
93 glReadBuffer (GL_BACK);
94 glReadPixels (x, y, 1,1, GL_RGB, GL_UNSIGNED_BYTE, &index);
99 if (index >= width2*height2)
102 if (index == cam_last_index)
103 return; // in same pixel
104 cam_last_index = index;
107 Pal_SetIndex (pic[index]);
111 SetSkin (index, selected_rgb);
112 UpdateWindow (camerawindow);
116 void Cam_DrawSetup (void)
118 glViewport (0,0,cam_width, cam_height);
119 glMatrixMode (GL_PROJECTION);
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);
124 glRotated (-roll*0.3, 0, 1, 0);
125 glRotated (-pitch*0.3, 1, 0, 0);
126 glRotated (yaw*0.3, 0, 0, 1);
128 glMatrixMode (GL_MODELVIEW);
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);
137 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
142 if (!cam_width || !cam_height)
145 glClearColor (0.3,0.3,0.3,1);
148 BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
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
161 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
162 glEnable (GL_TEXTURE_2D);
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);
173 BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
183 LONG WINAPI WCam_WndProc (
190 int fwKeys, xPos, yPos;
192 static int oldx, oldy;
195 GetClientRect(hWnd, &rect);
196 cam_width = rect.right-rect.left;
197 cam_height = rect.bottom-rect.top;
203 bSetupPixelFormat(camdc);
205 baseRC = wglCreateContext( camdc );
207 Sys_Error ("wglCreateContext failed");
208 if (!wglMakeCurrent( camdc, baseRC ))
209 Sys_Error ("wglMakeCurrent failed");
210 BindTextureEXT = (void *)wglGetProcAddress((LPCSTR) "glBindTextureEXT");
212 Sys_Error ("GetProcAddress for BindTextureEXT failed");
219 BeginPaint(hWnd, &ps);
220 if (!wglMakeCurrent( camdc, baseRC ))
221 Sys_Error ("wglMakeCurrent failed");
229 if (GetTopWindow(mainwindow) != hWnd)
230 BringWindowToTop(hWnd);
232 SetFocus (camerawindow);
233 SetCapture (camerawindow);
244 if (GetTopWindow(mainwindow) != hWnd)
245 BringWindowToTop(hWnd);
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");
256 Cam_Click (xPos, yPos, !!(wParam&(MK_SHIFT|MK_CONTROL)) );
258 // Cam_MouseDown (xPos, yPos, fwKeys);
264 if (! (wParam & (MK_LBUTTON|MK_RBUTTON|MK_MBUTTON)))
272 if (wParam & MK_LBUTTON)
278 if (!(wParam & (MK_RBUTTON|MK_MBUTTON)))
288 SetCursorPos (oldx, oldy);
290 if (wParam == (MK_RBUTTON|MK_CONTROL) )
292 if (abs(dx) > abs(dy))
296 InvalidateRect (camerawindow, NULL, false);
298 if (wParam == MK_RBUTTON)
302 InvalidateRect (camerawindow, NULL, false);
304 if (wParam == (MK_MBUTTON|MK_CONTROL) )
306 if (abs(dx) > abs(dy))
310 InvalidateRect (camerawindow, NULL, false);
312 if (wParam == MK_MBUTTON)
316 InvalidateRect (camerawindow, NULL, false);
324 // camera.width = rect.right;
325 // camera.height = rect.bottom;
326 InvalidateRect(camerawindow, NULL, false);
328 case WM_NCCALCSIZE:// don't let windows copy pixels
329 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
332 /* call destroy window to cleanup and go away */
333 DestroyWindow (hWnd);
341 /* release and free the device context and rendering context */
342 hRC = wglGetCurrentContext();
343 hDC = wglGetCurrentDC();
345 wglMakeCurrent(NULL, NULL);
348 wglDeleteContext(hRC);
350 ReleaseDC(hWnd, hDC);
355 /* pass all unhandled messages to DefWindowProc */
356 lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
360 /* return 1 if handled message, 0 if not */
370 void WCam_Register (HINSTANCE hInstance)
374 /* Register the camera class */
375 memset (&wc, 0, sizeof(wc));
378 wc.lpfnWndProc = (WNDPROC)WCam_WndProc;
381 wc.hInstance = hInstance;
383 wc.hCursor = LoadCursor (NULL,IDC_ARROW);
384 wc.hbrBackground = NULL;
386 wc.lpszClassName = CAMERA_WINDOW_CLASS;
388 if (!RegisterClass (&wc) )
389 Sys_Error ("WCam_Register: failed");
393 void WCam_Create (HINSTANCE hInstance)
395 WCam_Register (hInstance);
397 camerawindow = CreateWindow (CAMERA_WINDOW_CLASS ,
402 (int)(screen_width*0.5),
403 (int)(screen_height-20), // size
405 mainwindow, // parent window
410 Sys_Error ("Couldn't create camerawindow");
412 RestoreWindowState(camerawindow, "camerawindow");
413 ShowWindow (camerawindow, SW_SHOWDEFAULT);