]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake2/extra/qe4/z.c
Merge branch 'master' into divVerent/farplanedist-sky-fix
[xonotic/netradiant.git] / tools / quake2 / extra / qe4 / z.c
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 "qe3.h"
24
25 #define PAGEFLIPS       2
26
27 z_t             z;
28
29 /*
30 ============
31 Z_Init
32 ============
33 */
34 void Z_Init (void)
35 {
36         z.origin[0] = 0;
37         z.origin[1] = 20;
38         z.origin[2] = 46;
39
40         z.scale = 1;
41 }
42
43
44
45 /*
46 ============================================================================
47
48   MOUSE ACTIONS
49
50 ============================================================================
51 */
52
53 static  int     cursorx, cursory;
54
55 /*
56 ==============
57 Z_MouseDown
58 ==============
59 */
60 void Z_MouseDown (int x, int y, int buttons)
61 {
62         vec3_t  org, dir, vup, vright;
63         brush_t *b;
64
65         Sys_GetCursorPos (&cursorx, &cursory);
66
67         vup[0] = 0; vup[1] = 0; vup[2] = 1/z.scale;
68
69         VectorCopy (z.origin, org);
70         org[2] += (y - (z.height/2))/z.scale;
71         org[1] = -8192;
72
73         b = selected_brushes.next;
74         if (b != &selected_brushes)
75         {
76                 org[0] = (b->mins[0] + b->maxs[0])/2;
77         }
78
79         dir[0] = 0; dir[1] = 1; dir[2] = 0;
80
81         vright[0] = 0; vright[1] = 0; vright[2] = 0;
82
83         // LBUTTON = manipulate selection
84         // shift-LBUTTON = select
85         // middle button = grab texture
86         // ctrl-middle button = set entire brush to texture
87         // ctrl-shift-middle button = set single face to texture
88         if ( (buttons == MK_LBUTTON)
89                 || (buttons == (MK_LBUTTON | MK_SHIFT))
90                 || (buttons == MK_MBUTTON)
91 //              || (buttons == (MK_MBUTTON|MK_CONTROL))
92                 || (buttons == (MK_MBUTTON|MK_SHIFT|MK_CONTROL)) )
93         {
94                 Drag_Begin (x, y, buttons,
95                         vright, vup,
96                         org, dir);
97                 return;
98         }
99
100         // control mbutton = move camera
101         if ((buttons == (MK_CONTROL|MK_MBUTTON) ) || (buttons == (MK_CONTROL|MK_LBUTTON)))
102         {
103                 camera.origin[2] = org[2] ;
104                 Sys_UpdateWindows (W_CAMERA|W_XY_OVERLAY|W_Z);
105         }
106
107
108 }
109
110 /*
111 ==============
112 Z_MouseUp
113 ==============
114 */
115 void Z_MouseUp (int x, int y, int buttons)
116 {
117         Drag_MouseUp ();
118 }
119
120 /*
121 ==============
122 Z_MouseMoved
123 ==============
124 */
125 void Z_MouseMoved (int x, int y, int buttons)
126 {
127         if (!buttons)
128                 return;
129         if (buttons == MK_LBUTTON)
130         {
131                 Drag_MouseMoved (x, y, buttons);
132                 Sys_UpdateWindows (W_Z|W_CAMERA);
133                 return;
134         }
135         // rbutton = drag z origin
136         if (buttons == MK_RBUTTON)
137         {
138                 Sys_GetCursorPos (&x, &y);
139                 if ( y != cursory)
140                 {
141                         z.origin[2] += y-cursory;
142                         Sys_SetCursorPos (cursorx, cursory);
143                         Sys_UpdateWindows (W_Z);
144                 }
145                 return;
146         }
147                 // control mbutton = move camera
148         if ((buttons == (MK_CONTROL|MK_MBUTTON) ) || (buttons == (MK_CONTROL|MK_LBUTTON)))
149         {
150                 camera.origin[2] = (y - (z.height/2))/z.scale;
151                 Sys_UpdateWindows (W_CAMERA|W_XY_OVERLAY|W_Z);
152         }
153
154 }
155
156
157 /*
158 ============================================================================
159
160 DRAWING
161
162 ============================================================================
163 */
164
165
166 /*
167 ==============
168 Z_DrawGrid
169 ==============
170 */
171 void Z_DrawGrid (void)
172 {
173         float   zz, zb, ze;
174         int             w, h;
175         char    text[32];
176
177         w = z.width/2 / z.scale;
178         h = z.height/2 / z.scale;
179
180         zb = z.origin[2] - h;
181         if (zb < region_mins[2])
182                 zb = region_mins[2];
183         zb = 64 * floor (zb/64);
184
185         ze = z.origin[2] + h;
186         if (ze > region_maxs[2])
187                 ze = region_maxs[2];
188         ze = 64 * ceil (ze/64);
189
190         // draw major blocks
191
192         glColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMAJOR]);
193
194         glBegin (GL_LINES);
195
196         glVertex2f (0, zb);
197         glVertex2f (0, ze);
198
199         for (zz=zb ; zz<ze ; zz+=64)
200         {
201                 glVertex2f (-w, zz);
202                 glVertex2f (w, zz);
203         }
204
205         glEnd ();
206
207         // draw minor blocks
208         if (g_qeglobals.d_showgrid && g_qeglobals.d_gridsize*z.scale >= 4)
209         {
210                 glColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR]);
211
212                 glBegin (GL_LINES);
213                 for (zz=zb ; zz<ze ; zz+=g_qeglobals.d_gridsize)
214                 {
215                         if ( ! ((int)zz & 63) )
216                                 continue;
217                         glVertex2f (-w, zz);
218                         glVertex2f (w, zz);
219                 }
220                 glEnd ();
221         }
222
223         // draw coordinate text if needed
224
225         glColor4f(0, 0, 0, 0);
226
227         for (zz=zb ; zz<ze ; zz+=64)
228         {
229                 glRasterPos2f (-w+1, zz);
230                 sprintf (text, "%i",(int)zz);
231                 glCallLists (strlen(text), GL_UNSIGNED_BYTE, text);
232         }
233 }
234
235 #define CAM_HEIGHT              48 // height of main part
236 #define CAM_GIZMO               8       // height of the gizmo
237
238 void ZDrawCameraIcon (void)
239 {
240         float   x, y;
241         int     xCam = z.width/4;
242
243         x = 0;
244         y = camera.origin[2];
245
246         glColor3f (0.0, 0.0, 1.0);
247         glBegin(GL_LINE_STRIP);
248         glVertex3f (x-xCam,y,0);
249         glVertex3f (x,y+CAM_GIZMO,0);
250         glVertex3f (x+xCam,y,0);
251         glVertex3f (x,y-CAM_GIZMO,0);
252         glVertex3f (x-xCam,y,0);
253         glVertex3f (x+xCam,y,0);
254         glVertex3f (x+xCam,y-CAM_HEIGHT,0);
255         glVertex3f (x-xCam,y-CAM_HEIGHT,0);
256         glVertex3f (x-xCam,y,0);
257         glEnd ();
258
259 }
260
261 GLbitfield glbitClear = GL_COLOR_BUFFER_BIT; //HACK
262
263 /*
264 ==============
265 Z_Draw
266 ==============
267 */
268 void Z_Draw (void)
269 {
270     brush_t     *brush;
271         float   w, h;
272         double  start, end;
273         qtexture_t      *q;
274         float   top, bottom;
275         vec3_t  org_top, org_bottom, dir_up, dir_down;
276         int xCam = z.width/3;
277
278         if (!active_brushes.next)
279                 return; // not valid yet
280
281         if (z.timing)
282                 start = Sys_DoubleTime ();
283
284         //
285         // clear
286         //
287         glViewport(0, 0, z.width, z.height);
288
289         glClearColor (
290                 g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][0],
291                 g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][1],
292                 g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][2],
293                 0);
294
295     /* GL Bug */
296         /* When not using hw acceleration, gl will fault if we clear the depth
297         buffer bit on the first pass. The hack fix is to set the GL_DEPTH_BUFFER_BIT
298         only after Z_Draw() has been called once. Yeah, right. */
299         glClear(glbitClear);
300         glbitClear |= GL_DEPTH_BUFFER_BIT;
301
302         glMatrixMode(GL_PROJECTION);
303
304     glLoadIdentity ();
305         w = z.width/2 / z.scale;
306         h = z.height/2 / z.scale;
307         glOrtho (-w, w, z.origin[2]-h, z.origin[2]+h, -8, 8);
308
309         glDisable(GL_TEXTURE_2D);
310         glDisable(GL_TEXTURE_1D);
311         glDisable(GL_DEPTH_TEST);
312         glDisable(GL_BLEND);
313
314
315         //
316         // now draw the grid
317         //
318         Z_DrawGrid ();
319
320         //
321         // draw stuff
322         //
323
324         glDisable(GL_CULL_FACE);
325
326         glShadeModel (GL_FLAT);
327
328         glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
329
330         glDisable(GL_TEXTURE_2D);
331         glDisable(GL_BLEND);
332         glDisable(GL_DEPTH_TEST);
333
334
335         // draw filled interiors and edges
336         dir_up[0] = 0 ; dir_up[1] = 0; dir_up[2] = 1;
337         dir_down[0] = 0 ; dir_down[1] = 0; dir_down[2] = -1;
338         VectorCopy (z.origin, org_top);
339         org_top[2] = 4096;
340         VectorCopy (z.origin, org_bottom);
341         org_bottom[2] = -4096;
342
343         for (brush = active_brushes.next ; brush != &active_brushes ; brush=brush->next)
344         {
345                 if (brush->mins[0] >= z.origin[0]
346                         || brush->maxs[0] <= z.origin[0]
347                         || brush->mins[1] >= z.origin[1]
348                         || brush->maxs[1] <= z.origin[1])
349                         continue;
350
351                 if (!Brush_Ray (org_top, dir_down, brush, &top))
352                         continue;
353                 top = org_top[2] - top;
354                 if (!Brush_Ray (org_bottom, dir_up, brush, &bottom))
355                         continue;
356                 bottom = org_bottom[2] + bottom;
357
358                 q = Texture_ForName (brush->brush_faces->texdef.name);
359                 glColor3f (q->color[0], q->color[1], q->color[2]);
360                 glBegin (GL_QUADS);
361                 glVertex2f (-xCam, bottom);
362                 glVertex2f (xCam, bottom);
363                 glVertex2f (xCam, top);
364                 glVertex2f (-xCam, top);
365                 glEnd ();
366
367                 glColor3f (1,1,1);
368                 glBegin (GL_LINE_LOOP);
369                 glVertex2f (-xCam, bottom);
370                 glVertex2f (xCam, bottom);
371                 glVertex2f (xCam, top);
372                 glVertex2f (-xCam, top);
373                 glEnd ();
374         }
375
376         //
377         // now draw selected brushes
378         //
379         for (brush = selected_brushes.next ; brush != &selected_brushes ; brush=brush->next)
380         {
381                 if ( !(brush->mins[0] >= z.origin[0]
382                         || brush->maxs[0] <= z.origin[0]
383                         || brush->mins[1] >= z.origin[1]
384                         || brush->maxs[1] <= z.origin[1]) )
385                 {
386                         if (Brush_Ray (org_top, dir_down, brush, &top))
387                         {
388                                 top = org_top[2] - top;
389                                 if (Brush_Ray (org_bottom, dir_up, brush, &bottom))
390                                 {
391                                         bottom = org_bottom[2] + bottom;
392
393                                         q = Texture_ForName (brush->brush_faces->texdef.name);
394                                         glColor3f (q->color[0], q->color[1], q->color[2]);
395                                         glBegin (GL_QUADS);
396                                         glVertex2f (-xCam, bottom);
397                                         glVertex2f (xCam, bottom);
398                                         glVertex2f (xCam, top);
399                                         glVertex2f (-xCam, top);
400                                         glEnd ();
401                                 }
402                         }
403                 }
404
405                 glColor3f (1,0,0);
406                 glBegin (GL_LINE_LOOP);
407                 glVertex2f (-xCam, brush->mins[2]);
408                 glVertex2f (xCam, brush->mins[2]);
409                 glVertex2f (xCam, brush->maxs[2]);
410                 glVertex2f (-xCam, brush->maxs[2]);
411                 glEnd ();
412         }
413
414
415         ZDrawCameraIcon ();
416
417     glFinish();
418         QE_CheckOpenGLForErrors();
419
420         if (z.timing)
421         {
422                 end = Sys_DoubleTime ();
423                 Sys_Printf ("z: %i ms\n", (int)(1000*(end-start)));
424         }
425 }
426