]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/z.cpp
get the basics of a new scons build system together
[xonotic/netradiant.git] / radiant / z.cpp
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 #include "stdafx.h"\r
23 //#include "qe3.h"\r
24 \r
25 #define PAGEFLIPS       2\r
26 \r
27 z_t             z;\r
28 \r
29 /*\r
30 ============\r
31 Z_Init\r
32 ============\r
33 */\r
34 void Z_Init (void)\r
35 {\r
36         z.origin[0] = 0;\r
37         z.origin[1] = 20;\r
38         z.origin[2] = 46;\r
39 \r
40         z.scale = 1;\r
41 }\r
42 \r
43 \r
44 \r
45 /*\r
46 ============================================================================\r
47 \r
48   MOUSE ACTIONS\r
49 \r
50 ============================================================================\r
51 */\r
52 \r
53 static  int     cursorx, cursory;\r
54 \r
55 /*\r
56 ==============\r
57 Z_MouseDown\r
58 ==============\r
59 */\r
60 void Z_MouseDown (int x, int y, int buttons)\r
61 {\r
62         vec3_t  org, dir, vup, vright;\r
63         brush_t *b;\r
64 \r
65         Sys_GetCursorPos (&cursorx, &cursory);\r
66 \r
67         vup[0] = 0; vup[1] = 0; vup[2] = 1/z.scale;\r
68 \r
69         VectorCopy (z.origin, org);\r
70         org[2] += (y - (z.height/2))/z.scale;\r
71         org[1] = g_MinWorldCoord;\r
72 \r
73         b = selected_brushes.next;\r
74         if (b != &selected_brushes)\r
75         {\r
76                 org[0] = (b->mins[0] + b->maxs[0])/2;\r
77         }\r
78 \r
79         dir[0] = 0; dir[1] = 1; dir[2] = 0;\r
80 \r
81         vright[0] = 0; vright[1] = 0; vright[2] = 0;\r
82 \r
83         // LBUTTON = manipulate selection\r
84         // shift-LBUTTON = select\r
85         // middle button = grab texture\r
86         // ctrl-middle button = set entire brush to texture\r
87         // ctrl-shift-middle button = set single face to texture\r
88 \r
89   int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;\r
90         if ( (buttons == MK_LBUTTON)\r
91                 || (buttons == (MK_LBUTTON | MK_SHIFT))\r
92                 || (buttons == MK_MBUTTON)\r
93 //              || (buttons == (MK_MBUTTON|MK_CONTROL))\r
94                 || (buttons == (nMouseButton|MK_SHIFT|MK_CONTROL)) )\r
95         {\r
96                 Drag_Begin (x, y, buttons,\r
97                         vright, vup,\r
98                         org, dir);\r
99                 return;\r
100         }\r
101 \r
102         // control mbutton = move camera\r
103         if ((buttons == (MK_CONTROL|nMouseButton) ) || (buttons == (MK_CONTROL|MK_LBUTTON)))\r
104         {       \r
105                 g_pParentWnd->GetCamWnd()->Camera()->origin[2] = org[2] ;\r
106                 Sys_UpdateWindows (W_CAMERA|W_XY_OVERLAY|W_Z);\r
107         }\r
108 \r
109 \r
110 }\r
111 \r
112 /*\r
113 ==============\r
114 Z_MouseUp\r
115 ==============\r
116 */\r
117 void Z_MouseUp (int x, int y, int buttons)\r
118 {\r
119         Drag_MouseUp ();\r
120 }\r
121 \r
122 /*\r
123 ==============\r
124 Z_MouseMoved\r
125 ==============\r
126 */\r
127 void Z_MouseMoved (int x, int y, int buttons)\r
128 {\r
129         if (!buttons)\r
130                 return;\r
131         if (buttons == MK_LBUTTON)\r
132         {\r
133                 Drag_MouseMoved (x, y, buttons);\r
134                 Sys_UpdateWindows (W_Z|W_CAMERA_IFON|W_XY);\r
135                 return;\r
136         }\r
137         // rbutton = drag z origin\r
138         if (buttons == MK_RBUTTON)\r
139         {\r
140                 Sys_GetCursorPos (&x, &y);\r
141                 if ( y != cursory)\r
142                 {\r
143                         z.origin[2] += y-cursory;\r
144                         Sys_SetCursorPos (cursorx, cursory);\r
145                         Sys_UpdateWindows (W_Z);\r
146                 }\r
147                 return;\r
148         }\r
149                 // control mbutton = move camera\r
150   int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;\r
151         if ((buttons == (MK_CONTROL|nMouseButton) ) || (buttons == (MK_CONTROL|MK_LBUTTON)))\r
152         {       \r
153                 g_pParentWnd->GetCamWnd()->Camera()->origin[2] = (y - (z.height/2))/z.scale;\r
154                 Sys_UpdateWindows (W_CAMERA|W_XY_OVERLAY|W_Z);\r
155         }\r
156 \r
157 }\r
158 \r
159 \r
160 /*\r
161 ============================================================================\r
162 \r
163 DRAWING\r
164 \r
165 ============================================================================\r
166 */\r
167 \r
168 \r
169 /*\r
170 ==============\r
171 Z_DrawGrid\r
172 ==============\r
173 */\r
174 void Z_DrawGrid (void)\r
175 {\r
176         float   zz, zb, ze;\r
177         float   w, h;\r
178         char    text[32];\r
179 \r
180         w = (z.width/2 / z.scale);\r
181         h = (z.height/2 / z.scale);\r
182 \r
183         zb = z.origin[2] - h;\r
184         if (zb < region_mins[2])\r
185                 zb = region_mins[2];\r
186         zb = 64 * floor (zb/64);\r
187 \r
188         ze = z.origin[2] + h;\r
189         if (ze > region_maxs[2])\r
190                 ze = region_maxs[2];\r
191         ze = 64 * ceil (ze/64);\r
192 \r
193         // draw major blocks\r
194 \r
195         qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMAJOR]);\r
196 \r
197         if ( g_qeglobals.d_showgrid )\r
198         {\r
199           if (g_qeglobals.d_gridsize < 128)\r
200           {\r
201             qglBegin (GL_LINES);\r
202 \r
203             qglVertex2f (0, zb);\r
204             qglVertex2f (0, ze);\r
205 \r
206             for (zz=zb ; zz<ze ; zz+=64)\r
207             {\r
208               qglVertex2f (-w, zz);\r
209               qglVertex2f (w, zz);\r
210             }\r
211 \r
212             qglEnd ();\r
213           }\r
214           else\r
215           {\r
216             qglBegin (GL_LINES);\r
217 \r
218             qglVertex2f (0, zb);\r
219             qglVertex2f (0, ze);\r
220 \r
221             for (zz=zb ; zz<ze ; zz+=64)\r
222             {\r
223         // d_gridsize >= 128 .. it's an int for sure\r
224               if ( ((int)zz & ((int)g_qeglobals.d_gridsize-1)) != 0 )\r
225                 continue;\r
226 \r
227               qglVertex2f (-w, zz);\r
228               qglVertex2f (w, zz);\r
229             }\r
230 \r
231             qglEnd ();\r
232           }\r
233         }\r
234 \r
235         // draw minor blocks\r
236         if (g_qeglobals.d_showgrid && g_qeglobals.d_gridsize*z.scale >= 4 &&\r
237       g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR] != g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK])\r
238         {\r
239                 qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR]);\r
240 \r
241                 qglBegin (GL_LINES);\r
242                 for (zz=zb ; zz<ze ; zz+=g_qeglobals.d_gridsize)\r
243                 {\r
244                         if ( ! ((int)zz & 63) )\r
245                                 continue;\r
246                         qglVertex2f (-w, zz);\r
247                         qglVertex2f (w, zz);\r
248                 }\r
249                 qglEnd ();\r
250         }\r
251 \r
252         // draw coordinate text if needed\r
253 \r
254         if ( g_qeglobals.d_savedinfo.show_coordinates)\r
255         {\r
256                 qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDTEXT]);\r
257 \r
258                 int step = (int)(g_qeglobals.d_gridsize > 64 ? g_qeglobals.d_gridsize : 64);\r
259                 zb = z.origin[2] - h;\r
260                 if (zb < region_mins[2])\r
261                         zb = region_mins[2];\r
262                 zb = step * floor (zb/step);\r
263 \r
264                 for (zz=zb ; zz<ze ; zz+=step)\r
265                 {\r
266                         qglRasterPos2f (-w+(1/z.scale), zz);\r
267                         sprintf (text, "%i",(int)zz);\r
268                         gtk_glwidget_print_string(text);\r
269                 }\r
270         }\r
271 }\r
272 \r
273 void ZDrawCameraIcon (void)\r
274 {\r
275         float   x, y;\r
276         float   xCam = z.width/4/z.scale, gizmo = 8 / z.scale, height = 48 / z.scale;\r
277 \r
278         x = 0;\r
279         y = g_pParentWnd->GetCamWnd()->Camera()->origin[2];\r
280 \r
281         qglColor3f (0.0, 0.0, 1.0);\r
282         qglBegin(GL_LINE_STRIP);\r
283         qglVertex3f (x-xCam,y,0);\r
284         qglVertex3f (x,y+gizmo,0);\r
285         qglVertex3f (x+xCam,y,0);\r
286         qglVertex3f (x,y-gizmo,0);\r
287         qglVertex3f (x-xCam,y,0);\r
288         qglVertex3f (x+xCam,y,0);\r
289         qglVertex3f (x+xCam,y-height,0);\r
290         qglVertex3f (x-xCam,y-height,0);\r
291         qglVertex3f (x-xCam,y,0);\r
292         qglEnd ();\r
293 \r
294 }\r
295 \r
296 GLbitfield glbitClear = GL_COLOR_BUFFER_BIT; //HACK\r
297 \r
298 /*\r
299 ==============\r
300 Z_Draw\r
301 ==============\r
302 */\r
303 void Z_Draw (void)\r
304 {\r
305 #ifdef DBG_WINDOWPOS\r
306   CheckWatchit ("Z_Draw");\r
307 #endif\r
308   brush_t       *brush;\r
309         float   w, h;\r
310         double  start, end;\r
311         qtexture_t      *q;\r
312         float   top, bottom;\r
313         vec3_t  org_top, org_bottom, dir_up, dir_down;\r
314         int xCam = z.width/3;\r
315 \r
316         if (!active_brushes.next)\r
317                 return; // not valid yet\r
318 \r
319         if (z.timing)\r
320                 start = Sys_DoubleTime ();\r
321 \r
322         //\r
323         // clear\r
324         //\r
325         qglViewport(0, 0, z.width, z.height);\r
326 \r
327         qglClearColor (\r
328                 g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][0],\r
329                 g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][1],\r
330                 g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][2],\r
331                 0);\r
332 \r
333     /* GL Bug */ \r
334         /* When not using hw acceleration, gl will fault if we clear the depth \r
335         buffer bit on the first pass. The hack fix is to set the GL_DEPTH_BUFFER_BIT\r
336         only after Z_Draw() has been called once. Yeah, right. */\r
337         qglClear(glbitClear); \r
338         glbitClear |= GL_DEPTH_BUFFER_BIT;\r
339         qglMatrixMode(GL_PROJECTION);\r
340 \r
341   qglLoadIdentity ();\r
342         w = z.width/2 / z.scale;\r
343         h = z.height/2 / z.scale;\r
344         qglOrtho (-w, w, z.origin[2]-h, z.origin[2]+h, -8, 8);\r
345 \r
346         qglDisable(GL_TEXTURE_2D);\r
347         qglDisable(GL_TEXTURE_1D);\r
348         qglDisable(GL_DEPTH_TEST);\r
349         qglDisable(GL_BLEND);\r
350 \r
351 \r
352         //\r
353         // now draw the grid\r
354         //\r
355         Z_DrawGrid ();\r
356 \r
357         //\r
358         // draw stuff\r
359         //\r
360 \r
361         qglDisable(GL_CULL_FACE);\r
362 \r
363         qglShadeModel (GL_FLAT);\r
364 \r
365         qglPolygonMode (GL_FRONT_AND_BACK, GL_FILL);\r
366 \r
367         qglDisable(GL_TEXTURE_2D);\r
368         qglDisable(GL_BLEND);\r
369         qglDisable(GL_DEPTH_TEST);\r
370 \r
371 \r
372         // draw filled interiors and edges\r
373         dir_up[0] = 0 ; dir_up[1] = 0; dir_up[2] = 1;\r
374         dir_down[0] = 0 ; dir_down[1] = 0; dir_down[2] = -1;\r
375         VectorCopy (z.origin, org_top);\r
376         org_top[2] = g_MaxWorldCoord;\r
377         VectorCopy (z.origin, org_bottom);\r
378         org_bottom[2] = g_MinWorldCoord;\r
379 \r
380         for (brush = active_brushes.next ; brush != &active_brushes ; brush=brush->next)\r
381         {\r
382                 if (brush->bFiltered)\r
383                         continue;\r
384 \r
385                 if (brush->mins[0] >= z.origin[0]\r
386                         || brush->maxs[0] <= z.origin[0]\r
387                         || brush->mins[1] >= z.origin[1]\r
388                         || brush->maxs[1] <= z.origin[1])\r
389                         continue;\r
390 \r
391                 if (!Brush_Ray (org_top, dir_down, brush, &top))\r
392                         continue;\r
393                 top = org_top[2] - top;\r
394                 if (!Brush_Ray (org_bottom, dir_up, brush, &bottom))\r
395                         continue;\r
396                 bottom = org_bottom[2] + bottom;\r
397 \r
398                 q = brush->brush_faces->pShader->getTexture();\r
399                 qglColor3f (q->color[0], q->color[1], q->color[2]);\r
400                 qglBegin (GL_QUADS);\r
401                 qglVertex2f (-xCam, bottom);\r
402                 qglVertex2f (xCam, bottom);\r
403                 qglVertex2f (xCam, top);\r
404                 qglVertex2f (-xCam, top);\r
405                 qglEnd ();\r
406 \r
407                 qglColor3f (1,1,1);\r
408                 qglBegin (GL_LINE_LOOP);\r
409                 qglVertex2f (-xCam, bottom);\r
410                 qglVertex2f (xCam, bottom);\r
411                 qglVertex2f (xCam, top);\r
412                 qglVertex2f (-xCam, top);\r
413                 qglEnd ();\r
414         }\r
415 \r
416         //\r
417         // now draw selected brushes\r
418         //\r
419         for (brush = selected_brushes.next ; brush != &selected_brushes ; brush=brush->next)\r
420         {\r
421                 if ( !(brush->mins[0] >= z.origin[0]\r
422                         || brush->maxs[0] <= z.origin[0]\r
423                         || brush->mins[1] >= z.origin[1]\r
424                         || brush->maxs[1] <= z.origin[1]) )\r
425                 {\r
426                         if (Brush_Ray (org_top, dir_down, brush, &top))\r
427                         {\r
428                                 top = org_top[2] - top;\r
429                                 if (Brush_Ray (org_bottom, dir_up, brush, &bottom))\r
430                                 {\r
431                                         bottom = org_bottom[2] + bottom;\r
432 \r
433                                         q = brush->brush_faces->pShader->getTexture();\r
434                                         qglColor3f (q->color[0], q->color[1], q->color[2]);\r
435                                         qglBegin (GL_QUADS);\r
436                                         qglVertex2f (-xCam, bottom);\r
437                                         qglVertex2f (xCam, bottom);\r
438                                         qglVertex2f (xCam, top);\r
439                                         qglVertex2f (-xCam, top);\r
440                                         qglEnd ();\r
441                                 }\r
442                         }\r
443                 }\r
444 \r
445           qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_SELBRUSHES]);\r
446                 qglBegin (GL_LINE_LOOP);\r
447                 qglVertex2f (-xCam, brush->mins[2]);\r
448                 qglVertex2f (xCam, brush->mins[2]);\r
449                 qglVertex2f (xCam, brush->maxs[2]);\r
450                 qglVertex2f (-xCam, brush->maxs[2]);\r
451                 qglEnd ();\r
452         }\r
453 \r
454 \r
455         ZDrawCameraIcon ();\r
456 \r
457   qglFinish();\r
458         QE_CheckOpenGLForErrors();\r
459 \r
460         if (z.timing)\r
461         {\r
462                 end = Sys_DoubleTime ();\r
463                 Sys_Printf ("z: %i ms\n", (int)(1000*(end-start)));\r
464         }\r
465 }\r
466 \r