]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/tools/radiant/Z.CPP
Various Mac OS X tweaks to get this to build. Probably breaking things.
[icculus/iodoom3.git] / neo / tools / radiant / Z.CPP
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25
26 ===========================================================================
27 */
28
29 #include "../../idlib/precompiled.h"
30 #pragma hdrstop
31
32 #include "qe3.h"
33
34 #define PAGEFLIPS       2
35
36 z_t z;
37
38 /*
39  =======================================================================================================================
40     Z_Init
41  =======================================================================================================================
42  */
43 void Z_Init(void) {
44         z.origin[0] = 0;
45         z.origin[1] = 20;
46         z.origin[2] = 46;
47
48         z.scale = 1;
49 }
50
51 /* MOUSE ACTIONS */
52 static int      cursorx, cursory;
53
54 /*
55  =======================================================================================================================
56     Z_MouseDown
57  =======================================================================================================================
58  */
59 void Z_MouseDown(int x, int y, int buttons) {
60         idVec3  org, dir, vup, vright;
61         brush_t *b;
62
63         Sys_GetCursorPos(&cursorx, &cursory);
64
65         vup[0] = 0;
66         vup[1] = 0;
67         vup[2] = 1 / z.scale;
68
69         VectorCopy(z.origin, org);
70         org[2] += (y - (z.height / 2)) / z.scale;
71         org[1] = MIN_WORLD_COORD;
72
73         b = selected_brushes.next;
74         if (b != &selected_brushes) {
75                 org[0] = (b->mins[0] + b->maxs[0]) / 2;
76         }
77
78         dir[0] = 0;
79         dir[1] = 1;
80         dir[2] = 0;
81
82         vright[0] = 0;
83         vright[1] = 0;
84         vright[2] = 0;
85
86
87         // new mouse code for ZClip, I'll do this stuff before falling through into the standard ZWindow mouse code...
88         //
89         if (g_pParentWnd->GetZWnd()->m_pZClip)  // should always be the case I think, but this is safer
90         {
91                 bool bToggle = false;
92                 bool bSetTop = false;
93                 bool bSetBot = false;
94                 bool bReset  = false;
95
96                 if (g_PrefsDlg.m_nMouseButtons == 2)
97                 {
98                         // 2 button mice...
99                         //
100                         bToggle = (GetKeyState(VK_F1) & 0x8000) != 0;
101                         bSetTop = (GetKeyState(VK_F2) & 0x8000) != 0;
102                         bSetBot = (GetKeyState(VK_F3) & 0x8000) != 0;
103                         bReset  = (GetKeyState(VK_F4) & 0x8000) != 0;
104                 }
105                 else
106                 {       
107                         // 3 button mice...
108                         //
109                         bToggle = (buttons == (MK_RBUTTON|MK_SHIFT|MK_CONTROL));
110                         bSetTop = (buttons == (MK_RBUTTON|MK_SHIFT));
111                         bSetBot = (buttons == (MK_RBUTTON|MK_CONTROL));
112                         bReset  = (GetKeyState(VK_F4) & 0x8000) != 0;
113                 }                               
114                 
115                 if (bToggle)
116                 {
117                         g_pParentWnd->GetZWnd()->m_pZClip->Enable(!(g_pParentWnd->GetZWnd()->m_pZClip->IsEnabled()));
118                     Sys_UpdateWindows (W_ALL);
119                         return;
120                 }
121
122                 if (bSetTop)
123                 {
124                         g_pParentWnd->GetZWnd()->m_pZClip->SetTop(org[2]);
125                     Sys_UpdateWindows (W_ALL);
126                         return;
127                 }
128                 
129                 if (bSetBot)
130                 {
131                         g_pParentWnd->GetZWnd()->m_pZClip->SetBottom(org[2]);
132                     Sys_UpdateWindows (W_ALL);
133                         return;
134                 }
135
136                 if (bReset)
137                 {
138                         g_pParentWnd->GetZWnd()->m_pZClip->Reset();
139                     Sys_UpdateWindows (W_ALL);
140                         return;
141                 }
142         }
143
144         //
145         // LBUTTON = manipulate selection shift-LBUTTON = select middle button = grab
146         // texture ctrl-middle button = set entire brush to texture ctrl-shift-middle
147         // button = set single face to texture
148         //
149
150         // see code above for these next 3, I just commented them here as well for clarity...
151         //
152         // ctrl-shift-RIGHT button = toggle ZClip on/off
153         //      shift-RIGHT button = set ZClip top marker
154         //       ctrl-RIGHT button = set ZClip bottom marker
155
156         int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;
157         if
158         (
159                 (buttons == MK_LBUTTON) ||
160                 (buttons == (MK_LBUTTON | MK_SHIFT)) ||
161                 (buttons == MK_MBUTTON) // || (buttons == (MK_MBUTTON|MK_CONTROL))
162                 ||
163                 (buttons == (nMouseButton | MK_SHIFT | MK_CONTROL))
164         ) {
165                 Drag_Begin(x, y, buttons, vright, vup, org, dir);
166                 return;
167         }
168
169         // control mbutton = move camera
170         if ((buttons == (MK_CONTROL | nMouseButton)) || (buttons == (MK_CONTROL | MK_LBUTTON))) {
171                 g_pParentWnd->GetCamera()->Camera().origin[2] = org[2];
172                 Sys_UpdateWindows(W_CAMERA | W_XY_OVERLAY | W_Z);
173         }
174 }
175
176 /*
177  =======================================================================================================================
178     Z_MouseUp
179  =======================================================================================================================
180  */
181 void Z_MouseUp(int x, int y, int buttons) {
182         Drag_MouseUp();
183 }
184
185 /*
186  =======================================================================================================================
187     Z_MouseMoved
188  =======================================================================================================================
189  */
190 void Z_MouseMoved(int x, int y, int buttons) {
191         if (!buttons) {
192                 return;
193         }
194
195         if (buttons == MK_LBUTTON) {
196                 Drag_MouseMoved(x, y, buttons);
197                 Sys_UpdateWindows(W_Z | W_CAMERA_IFON | W_XY);
198                 return;
199         }
200
201         // rbutton = drag z origin
202         if (buttons == MK_RBUTTON) {
203                 Sys_GetCursorPos(&x, &y);
204                 if (y != cursory) {
205                         z.origin[2] += y - cursory;
206                         Sys_SetCursorPos(cursorx, cursory);
207                         Sys_UpdateWindows(W_Z);
208                 }
209
210                 return;
211         }
212
213         // control mbutton = move camera
214         int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;
215         if ((buttons == (MK_CONTROL | nMouseButton)) || (buttons == (MK_CONTROL | MK_LBUTTON))) {       
216                 g_pParentWnd->GetCamera()->Camera().origin[2] = z.origin[2] + (y - (z.height / 2)) / z.scale;
217                 Sys_UpdateWindows(W_CAMERA | W_XY_OVERLAY | W_Z);
218         }
219 }
220
221 /*
222  =======================================================================================================================
223     DRAWING £
224     Z_DrawGrid
225  =======================================================================================================================
226  */
227 void Z_DrawGrid(void) {
228         float   zz, zb, ze;
229         int             w, h;
230         char    text[32];
231
232         w = z.width / 2 / z.scale;
233         h = z.height / 2 / z.scale;
234
235         zb = z.origin[2] - h;
236         if (zb < region_mins[2]) {
237                 zb = region_mins[2];
238         }
239
240         zb = 64 * floor(zb / 64);
241
242         ze = z.origin[2] + h;
243         if (ze > region_maxs[2]) {
244                 ze = region_maxs[2];
245         }
246
247         ze = 64 * ceil(ze / 64);
248
249         // draw major blocks
250         qglColor3fv( g_qeglobals.d_savedinfo.colors[COLOR_GRIDMAJOR].ToFloatPtr() );
251
252         qglBegin(GL_LINES);
253
254         qglVertex2f(0, zb);
255         qglVertex2f(0, ze);
256
257         for (zz = zb; zz < ze; zz += 64) {
258                 qglVertex2f(-w, zz);
259                 qglVertex2f(w, zz);
260         }
261
262         qglEnd();
263
264         // draw minor blocks
265         if ( g_qeglobals.d_showgrid &&
266                 g_qeglobals.d_gridsize * z.scale >= 4 &&
267                 !g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR].Compare( g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK] ) ) {
268
269                 qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR].ToFloatPtr());
270
271                 qglBegin(GL_LINES);
272                 for (zz = zb; zz < ze; zz += g_qeglobals.d_gridsize) {
273                         if (!((int)zz & 63)) {
274                                 continue;
275                         }
276
277                         qglVertex2f(-w, zz);
278                         qglVertex2f(w, zz);
279                 }
280
281                 qglEnd();
282         }
283
284         // draw coordinate text if needed
285         qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDTEXT].ToFloatPtr());
286
287         for (zz = zb; zz < ze; zz += 64) {
288                 qglRasterPos2f(-w + 1, zz);
289                 sprintf(text, "%i", (int)zz);
290                 qglCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
291         }
292 }
293
294 #define CAM_HEIGHT      48      // height of main part
295 #define CAM_GIZMO       8       // height of the gizmo
296
297 /*
298  =======================================================================================================================
299  =======================================================================================================================
300  */
301 void ZDrawCameraIcon(void) {
302         float   x, y;
303         int             xCam = z.width / 4;
304
305         x = 0;
306         y = g_pParentWnd->GetCamera()->Camera().origin[2];
307
308         qglColor3f(0.0, 0.0, 1.0);
309         qglBegin(GL_LINE_STRIP);
310         qglVertex3f(x - xCam, y, 0);
311         qglVertex3f(x, y + CAM_GIZMO, 0);
312         qglVertex3f(x + xCam, y, 0);
313         qglVertex3f(x, y - CAM_GIZMO, 0);
314         qglVertex3f(x - xCam, y, 0);
315         qglVertex3f(x + xCam, y, 0);
316         qglVertex3f(x + xCam, y - CAM_HEIGHT, 0);
317         qglVertex3f(x - xCam, y - CAM_HEIGHT, 0);
318         qglVertex3f(x - xCam, y, 0);
319         qglEnd();
320 }
321
322 void ZDrawZClip()
323 {
324         float x,y;
325         
326         x = 0;
327         y = g_pParentWnd->GetCamera()->Camera().origin[2];
328
329         if (g_pParentWnd->GetZWnd()->m_pZClip)  // should always be the case I think
330                 g_pParentWnd->GetZWnd()->m_pZClip->Paint();
331 }
332
333
334 GLbitfield      glbitClear = GL_COLOR_BUFFER_BIT;       // HACK
335
336 /*
337  =======================================================================================================================
338     Z_Draw
339  =======================================================================================================================
340  */
341 void Z_Draw(void) {
342         brush_t         *brush;
343         float           w, h;
344         float           top, bottom;
345         idVec3          org_top, org_bottom, dir_up, dir_down;
346         int                     xCam = z.width / 3;
347
348         if (!active_brushes.next) {
349                 return; // not valid yet
350         }
351
352         // clear
353         qglViewport(0, 0, z.width, z.height);
354         qglScissor(0, 0, z.width, z.height);
355
356         qglClearColor
357         (
358                 g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][0],
359                 g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][1],
360                 g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][2],
361                 0
362         );
363
364         /*
365          * GL Bug £
366          * When not using hw acceleration, gl will fault if we clear the depth buffer bit
367          * on the first pass. The hack fix is to set the GL_DEPTH_BUFFER_BIT only after
368          * Z_Draw() has been called once. Yeah, right. £
369          * qglClear(glbitClear);
370          */
371         qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
372
373         //
374         // glbitClear |= GL_DEPTH_BUFFER_BIT;
375         // qglClear(GL_DEPTH_BUFFER_BIT);
376         //
377         qglMatrixMode(GL_PROJECTION);
378         qglLoadIdentity();
379
380         w = z.width / 2 / z.scale;
381         h = z.height / 2 / z.scale;
382         qglOrtho(-w, w, z.origin[2] - h, z.origin[2] + h, -8, 8);
383
384         globalImages->BindNull();
385         qglDisable(GL_DEPTH_TEST);
386         qglDisable(GL_BLEND);
387
388         // now draw the grid
389         Z_DrawGrid();
390
391         // draw stuff
392         qglDisable(GL_CULL_FACE);
393
394         qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
395
396         globalImages->BindNull();
397
398         // draw filled interiors and edges
399         dir_up[0] = 0;
400         dir_up[1] = 0;
401         dir_up[2] = 1;
402         dir_down[0] = 0;
403         dir_down[1] = 0;
404         dir_down[2] = -1;
405         VectorCopy(z.origin, org_top);
406         org_top[2] = 4096;
407         VectorCopy(z.origin, org_bottom);
408         org_bottom[2] = -4096;
409
410         for (brush = active_brushes.next; brush != &active_brushes; brush = brush->next) {
411                 if
412                 (
413                         brush->mins[0] >= z.origin[0] ||
414                         brush->maxs[0] <= z.origin[0] ||
415                         brush->mins[1] >= z.origin[1] ||
416                         brush->maxs[1] <= z.origin[1]
417                 ) {
418                         continue;
419                 }
420
421                 if (!Brush_Ray(org_top, dir_down, brush, &top)) {
422                         continue;
423                 }
424
425                 top = org_top[2] - top;
426                 if (!Brush_Ray(org_bottom, dir_up, brush, &bottom)) {
427                         continue;
428                 }
429
430                 bottom = org_bottom[2] + bottom;
431
432                 //q = declManager->FindMaterial(brush->brush_faces->texdef.name);
433                 qglColor3f(brush->owner->eclass->color.x, brush->owner->eclass->color.y, brush->owner->eclass->color.z);
434                 qglBegin(GL_QUADS);
435                 qglVertex2f(-xCam, bottom);
436                 qglVertex2f(xCam, bottom);
437                 qglVertex2f(xCam, top);
438                 qglVertex2f(-xCam, top);
439                 qglEnd();
440
441                 qglColor3f(1, 1, 1);
442                 qglBegin(GL_LINE_LOOP);
443                 qglVertex2f(-xCam, bottom);
444                 qglVertex2f(xCam, bottom);
445                 qglVertex2f(xCam, top);
446                 qglVertex2f(-xCam, top);
447                 qglEnd();
448         }
449
450         // now draw selected brushes
451         for (brush = selected_brushes.next; brush != &selected_brushes; brush = brush->next) {
452                 if
453                 (
454                         !(
455                                 brush->mins[0] >= z.origin[0] ||
456                                 brush->maxs[0] <= z.origin[0] ||
457                                 brush->mins[1] >= z.origin[1] ||
458                                 brush->maxs[1] <= z.origin[1]
459                         )
460                 ) {
461                         if (Brush_Ray(org_top, dir_down, brush, &top)) {
462                                 top = org_top[2] - top;
463                                 if (Brush_Ray(org_bottom, dir_up, brush, &bottom)) {
464                                         bottom = org_bottom[2] + bottom;
465
466                                         //q = declManager->FindMaterial(brush->brush_faces->texdef.name);
467                                         qglColor3f(brush->owner->eclass->color.x, brush->owner->eclass->color.y, brush->owner->eclass->color.z);
468                                         qglBegin(GL_QUADS);
469                                         qglVertex2f(-xCam, bottom);
470                                         qglVertex2f(xCam, bottom);
471                                         qglVertex2f(xCam, top);
472                                         qglVertex2f(-xCam, top);
473                                         qglEnd();
474                                 }
475                         }
476                 }
477
478                 qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_SELBRUSHES].ToFloatPtr());
479                 qglBegin(GL_LINE_LOOP);
480                 qglVertex2f(-xCam, brush->mins[2]);
481                 qglVertex2f(xCam, brush->mins[2]);
482                 qglVertex2f(xCam, brush->maxs[2]);
483                 qglVertex2f(-xCam, brush->maxs[2]);
484                 qglEnd();
485         }
486
487         ZDrawCameraIcon();
488         ZDrawZClip();
489
490         qglFinish();
491         QE_CheckOpenGLForErrors();
492 }