File: Z.CPP

package info (click to toggle)
dhewm3 1.5.1~pre%2Bgit20200905%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 21,664 kB
  • sloc: cpp: 408,868; ansic: 1,188; objc: 1,034; python: 330; sh: 94; makefile: 11
file content (492 lines) | stat: -rw-r--r-- 13,169 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
===========================================================================

Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.

This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").

Doom 3 Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Doom 3 Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.

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.

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.

===========================================================================
*/

#include "tools/edit_gui_common.h"


#include "qe3.h"

#define PAGEFLIPS	2

z_t z;

/*
 =======================================================================================================================
	Z_Init
 =======================================================================================================================
 */
void Z_Init(void) {
	z.origin[0] = 0;
	z.origin[1] = 20;
	z.origin[2] = 46;

	z.scale = 1;
}

/* MOUSE ACTIONS */
static int	cursorx, cursory;

/*
 =======================================================================================================================
	Z_MouseDown
 =======================================================================================================================
 */
void Z_MouseDown(int x, int y, int buttons) {
	idVec3	org, dir, vup, vright;
	brush_t *b;

	Sys_GetCursorPos(&cursorx, &cursory);

	vup[0] = 0;
	vup[1] = 0;
	vup[2] = 1 / z.scale;

	VectorCopy(z.origin, org);
	org[2] += (y - (z.height / 2)) / z.scale;
	org[1] = MIN_WORLD_COORD;

	b = selected_brushes.next;
	if (b != &selected_brushes) {
		org[0] = (b->mins[0] + b->maxs[0]) / 2;
	}

	dir[0] = 0;
	dir[1] = 1;
	dir[2] = 0;

	vright[0] = 0;
	vright[1] = 0;
	vright[2] = 0;


	// new mouse code for ZClip, I'll do this stuff before falling through into the standard ZWindow mouse code...
	//
	if (g_pParentWnd->GetZWnd()->m_pZClip)	// should always be the case I think, but this is safer
	{
		bool bToggle = false;
		bool bSetTop = false;
		bool bSetBot = false;
		bool bReset  = false;

		if (g_PrefsDlg.m_nMouseButtons == 2)
		{
			// 2 button mice...
			//
			bToggle = (GetKeyState(VK_F1) & 0x8000) != 0;
			bSetTop = (GetKeyState(VK_F2) & 0x8000) != 0;
			bSetBot = (GetKeyState(VK_F3) & 0x8000) != 0;
			bReset  = (GetKeyState(VK_F4) & 0x8000) != 0;
		}
		else
		{
			// 3 button mice...
			//
			bToggle = (buttons == (MK_RBUTTON|MK_SHIFT|MK_CONTROL));
			bSetTop = (buttons == (MK_RBUTTON|MK_SHIFT));
			bSetBot = (buttons == (MK_RBUTTON|MK_CONTROL));
			bReset  = (GetKeyState(VK_F4) & 0x8000) != 0;
		}

		if (bToggle)
		{
			g_pParentWnd->GetZWnd()->m_pZClip->Enable(!(g_pParentWnd->GetZWnd()->m_pZClip->IsEnabled()));
			Sys_UpdateWindows (W_ALL);
			return;
		}

		if (bSetTop)
		{
			g_pParentWnd->GetZWnd()->m_pZClip->SetTop(org[2]);
			Sys_UpdateWindows (W_ALL);
			return;
		}

		if (bSetBot)
		{
			g_pParentWnd->GetZWnd()->m_pZClip->SetBottom(org[2]);
			Sys_UpdateWindows (W_ALL);
			return;
		}

		if (bReset)
		{
			g_pParentWnd->GetZWnd()->m_pZClip->Reset();
			Sys_UpdateWindows (W_ALL);
			return;
		}
	}

	//
	// LBUTTON = manipulate selection shift-LBUTTON = select middle button = grab
	// texture ctrl-middle button = set entire brush to texture ctrl-shift-middle
	// button = set single face to texture
	//

	// see code above for these next 3, I just commented them here as well for clarity...
	//
	// ctrl-shift-RIGHT button = toggle ZClip on/off
	//      shift-RIGHT button = set ZClip top marker
	//       ctrl-RIGHT button = set ZClip bottom marker

	int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;
	if
	(
		(buttons == MK_LBUTTON) ||
		(buttons == (MK_LBUTTON | MK_SHIFT)) ||
		(buttons == MK_MBUTTON) // || (buttons == (MK_MBUTTON|MK_CONTROL))
		||
		(buttons == (nMouseButton | MK_SHIFT | MK_CONTROL))
	) {
		Drag_Begin(x, y, buttons, vright, vup, org, dir);
		return;
	}

	// control mbutton = move camera
	if ((buttons == (MK_CONTROL | nMouseButton)) || (buttons == (MK_CONTROL | MK_LBUTTON))) {
		g_pParentWnd->GetCamera()->Camera().origin[2] = org[2];
		Sys_UpdateWindows(W_CAMERA | W_XY_OVERLAY | W_Z);
	}
}

/*
 =======================================================================================================================
	Z_MouseUp
 =======================================================================================================================
 */
void Z_MouseUp(int x, int y, int buttons) {
	Drag_MouseUp();
}

/*
 =======================================================================================================================
	Z_MouseMoved
 =======================================================================================================================
 */
void Z_MouseMoved(int x, int y, int buttons) {
	if (!buttons) {
		return;
	}

	if (buttons == MK_LBUTTON) {
		Drag_MouseMoved(x, y, buttons);
		Sys_UpdateWindows(W_Z | W_CAMERA_IFON | W_XY);
		return;
	}

	// rbutton = drag z origin
	if (buttons == MK_RBUTTON) {
		Sys_GetCursorPos(&x, &y);
		if (y != cursory) {
			z.origin[2] += y - cursory;
			Sys_SetCursorPos(cursorx, cursory);
			Sys_UpdateWindows(W_Z);
		}

		return;
	}

	// control mbutton = move camera
	int nMouseButton = g_PrefsDlg.m_nMouseButtons == 2 ? MK_RBUTTON : MK_MBUTTON;
	if ((buttons == (MK_CONTROL | nMouseButton)) || (buttons == (MK_CONTROL | MK_LBUTTON))) {
		g_pParentWnd->GetCamera()->Camera().origin[2] = z.origin[2] + (y - (z.height / 2)) / z.scale;
		Sys_UpdateWindows(W_CAMERA | W_XY_OVERLAY | W_Z);
	}
}

/*
 =======================================================================================================================
	DRAWING 
	Z_DrawGrid
 =======================================================================================================================
 */
void Z_DrawGrid(void) {
	float	zz, zb, ze;
	int		w, h;
	char	text[32];

	w = z.width / 2 / z.scale;
	h = z.height / 2 / z.scale;

	zb = z.origin[2] - h;
	if (zb < region_mins[2]) {
		zb = region_mins[2];
	}

	zb = 64 * floor(zb / 64);

	ze = z.origin[2] + h;
	if (ze > region_maxs[2]) {
		ze = region_maxs[2];
	}

	ze = 64 * ceil(ze / 64);

	// draw major blocks
	qglColor3fv( g_qeglobals.d_savedinfo.colors[COLOR_GRIDMAJOR].ToFloatPtr() );

	qglBegin(GL_LINES);

	qglVertex2f(0, zb);
	qglVertex2f(0, ze);

	for (zz = zb; zz < ze; zz += 64) {
		qglVertex2f(-w, zz);
		qglVertex2f(w, zz);
	}

	qglEnd();

	// draw minor blocks
	if ( g_qeglobals.d_showgrid &&
		g_qeglobals.d_gridsize * z.scale >= 4 &&
		!g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR].Compare( g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK] ) ) {

		qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDMINOR].ToFloatPtr());

		qglBegin(GL_LINES);
		for (zz = zb; zz < ze; zz += g_qeglobals.d_gridsize) {
			if (!((int)zz & 63)) {
				continue;
			}

			qglVertex2f(-w, zz);
			qglVertex2f(w, zz);
		}

		qglEnd();
	}

	// draw coordinate text if needed
	qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_GRIDTEXT].ToFloatPtr());

	for (zz = zb; zz < ze; zz += 64) {
		qglRasterPos2f(-w + 1, zz);
		sprintf(text, "%i", (int)zz);
		qglCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
	}
}

#define CAM_HEIGHT	48	// height of main part
#define CAM_GIZMO	8	// height of the gizmo

/*
 =======================================================================================================================
 =======================================================================================================================
 */
void ZDrawCameraIcon(void) {
	float	x, y;
	int		xCam = z.width / 4;

	x = 0;
	y = g_pParentWnd->GetCamera()->Camera().origin[2];

	qglColor3f(0.0, 0.0, 1.0);
	qglBegin(GL_LINE_STRIP);
	qglVertex3f(x - xCam, y, 0);
	qglVertex3f(x, y + CAM_GIZMO, 0);
	qglVertex3f(x + xCam, y, 0);
	qglVertex3f(x, y - CAM_GIZMO, 0);
	qglVertex3f(x - xCam, y, 0);
	qglVertex3f(x + xCam, y, 0);
	qglVertex3f(x + xCam, y - CAM_HEIGHT, 0);
	qglVertex3f(x - xCam, y - CAM_HEIGHT, 0);
	qglVertex3f(x - xCam, y, 0);
	qglEnd();
}

void ZDrawZClip()
{
	float x,y;

	x = 0;
	y = g_pParentWnd->GetCamera()->Camera().origin[2];

	if (g_pParentWnd->GetZWnd()->m_pZClip)	// should always be the case I think
		g_pParentWnd->GetZWnd()->m_pZClip->Paint();
}


GLbitfield	glbitClear = GL_COLOR_BUFFER_BIT;	// HACK

/*
 =======================================================================================================================
	Z_Draw
 =======================================================================================================================
 */
void Z_Draw(void) {
	brush_t		*brush;
	float		w, h;
	float		top, bottom;
	idVec3		org_top, org_bottom, dir_up, dir_down;
	int			xCam = z.width / 3;

	if (!active_brushes.next) {
		return; // not valid yet
	}

	// clear
	qglViewport(0, 0, z.width, z.height);
	qglScissor(0, 0, z.width, z.height);

	qglClearColor
	(
		g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][0],
		g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][1],
		g_qeglobals.d_savedinfo.colors[COLOR_GRIDBACK][2],
		0
	);

	/*
	 * GL Bug 
	 * When not using hw acceleration, gl will fault if we clear the depth buffer bit
	 * on the first pass. The hack fix is to set the GL_DEPTH_BUFFER_BIT only after
	 * Z_Draw() has been called once. Yeah, right. 
	 * qglClear(glbitClear);
	 */
	qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	//
	// glbitClear |= GL_DEPTH_BUFFER_BIT;
	// qglClear(GL_DEPTH_BUFFER_BIT);
	//
	qglMatrixMode(GL_PROJECTION);
	qglLoadIdentity();

	w = z.width / 2 / z.scale;
	h = z.height / 2 / z.scale;
	qglOrtho(-w, w, z.origin[2] - h, z.origin[2] + h, -8, 8);

	globalImages->BindNull();
	qglDisable(GL_DEPTH_TEST);
	qglDisable(GL_BLEND);

	// now draw the grid
	Z_DrawGrid();

	// draw stuff
	qglDisable(GL_CULL_FACE);

	qglPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

	globalImages->BindNull();

	// draw filled interiors and edges
	dir_up[0] = 0;
	dir_up[1] = 0;
	dir_up[2] = 1;
	dir_down[0] = 0;
	dir_down[1] = 0;
	dir_down[2] = -1;
	VectorCopy(z.origin, org_top);
	org_top[2] = 4096;
	VectorCopy(z.origin, org_bottom);
	org_bottom[2] = -4096;

	for (brush = active_brushes.next; brush != &active_brushes; brush = brush->next) {
		if
		(
			brush->mins[0] >= z.origin[0] ||
			brush->maxs[0] <= z.origin[0] ||
			brush->mins[1] >= z.origin[1] ||
			brush->maxs[1] <= z.origin[1]
		) {
			continue;
		}

		if (!Brush_Ray(org_top, dir_down, brush, &top)) {
			continue;
		}

		top = org_top[2] - top;
		if (!Brush_Ray(org_bottom, dir_up, brush, &bottom)) {
			continue;
		}

		bottom = org_bottom[2] + bottom;

		//q = declManager->FindMaterial(brush->brush_faces->texdef.name);
		qglColor3f(brush->owner->eclass->color.x, brush->owner->eclass->color.y, brush->owner->eclass->color.z);
		qglBegin(GL_QUADS);
		qglVertex2f(-xCam, bottom);
		qglVertex2f(xCam, bottom);
		qglVertex2f(xCam, top);
		qglVertex2f(-xCam, top);
		qglEnd();

		qglColor3f(1, 1, 1);
		qglBegin(GL_LINE_LOOP);
		qglVertex2f(-xCam, bottom);
		qglVertex2f(xCam, bottom);
		qglVertex2f(xCam, top);
		qglVertex2f(-xCam, top);
		qglEnd();
	}

	// now draw selected brushes
	for (brush = selected_brushes.next; brush != &selected_brushes; brush = brush->next) {
		if
		(
			!(
				brush->mins[0] >= z.origin[0] ||
				brush->maxs[0] <= z.origin[0] ||
				brush->mins[1] >= z.origin[1] ||
				brush->maxs[1] <= z.origin[1]
			)
		) {
			if (Brush_Ray(org_top, dir_down, brush, &top)) {
				top = org_top[2] - top;
				if (Brush_Ray(org_bottom, dir_up, brush, &bottom)) {
					bottom = org_bottom[2] + bottom;

					//q = declManager->FindMaterial(brush->brush_faces->texdef.name);
					qglColor3f(brush->owner->eclass->color.x, brush->owner->eclass->color.y, brush->owner->eclass->color.z);
					qglBegin(GL_QUADS);
					qglVertex2f(-xCam, bottom);
					qglVertex2f(xCam, bottom);
					qglVertex2f(xCam, top);
					qglVertex2f(-xCam, top);
					qglEnd();
				}
			}
		}

		qglColor3fv(g_qeglobals.d_savedinfo.colors[COLOR_SELBRUSHES].ToFloatPtr());
		qglBegin(GL_LINE_LOOP);
		qglVertex2f(-xCam, brush->mins[2]);
		qglVertex2f(xCam, brush->mins[2]);
		qglVertex2f(xCam, brush->maxs[2]);
		qglVertex2f(-xCam, brush->maxs[2]);
		qglEnd();
	}

	ZDrawCameraIcon();
	ZDrawZClip();

	qglFinish();
	QE_CheckOpenGLForErrors();
}