File: tr_shadowbounds.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 (637 lines) | stat: -rw-r--r-- 14,226 bytes parent folder | download | duplicates (5)
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
===========================================================================

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 "sys/platform.h"
#include "renderer/RenderWorld_local.h"

#include "renderer/tr_local.h"

// Compute conservative shadow bounds as the intersection
// of the object's bounds' shadow volume and the light's bounds.
//
// --cass


template <class T, int N>
struct MyArray
{
	MyArray() : s(0) {}

	MyArray( const MyArray<T,N> & cpy ) : s(cpy.s)
	{
		for(int i=0; i < s; i++)
			v[i] = cpy.v[i];
	}

	void push_back(const T & i) {
		v[s] = i;
		s++;
		//if(s > max_size)
		//	max_size = int(s);
	}

	T & operator[](int i) {
		return v[i];
	}

	const T & operator[](int i) const {
		return v[i];
	}

	unsigned int size() const {
		return s;
	}

	void empty() {
		s = 0;
	}

	T v[N];
	int s;
//	static int max_size;
};

typedef MyArray<int, 4> MyArrayInt;
//int MyArrayInt::max_size = 0;
typedef MyArray<idVec4, 16> MyArrayVec4;
//int MyArrayVec4::max_size = 0;

struct poly
{
	MyArrayInt vi;
	MyArrayInt ni;
	idVec4 plane;
};

typedef MyArray<poly, 9> MyArrayPoly;
//int MyArrayPoly::max_size = 0;

struct edge
{
	int vi[2];
	int pi[2];
};

typedef MyArray<edge, 15> MyArrayEdge;
//int MyArrayEdge::max_size = 0;

MyArrayInt four_ints(int a, int b, int c, int d)
{
	MyArrayInt vi;
	vi.push_back(a);
	vi.push_back(b);
	vi.push_back(c);
	vi.push_back(d);
	return vi;
}

idVec3 homogeneous_difference(idVec4 a, idVec4 b)
{
	idVec3 v;
	v.x = b.x * a.w - a.x * b.w;
	v.y = b.y * a.w - a.y * b.w;
	v.z = b.z * a.w - a.z * b.w;
	return v;
}

// handles positive w only
idVec4 compute_homogeneous_plane(idVec4 a, idVec4 b, idVec4 c)
{
	idVec4 v, t;

	if(a[3] == 0)
	{ t = a; a = b; b = c; c = t; }
	if(a[3] == 0)
	{ t = a; a = b; b = c; c = t; }

	// can't handle 3 infinite points
	if( a[3] == 0 )
		return v;

	idVec3 vb = homogeneous_difference(a, b);
	idVec3 vc = homogeneous_difference(a, c);

	idVec3 n = vb.Cross(vc);
	n.Normalize();

	v.x = n.x;
	v.y = n.y;
	v.z = n.z;

	v.w = - (n * idVec3(a.x, a.y, a.z)) / a.w ;

	return v;
}

struct polyhedron
{
	MyArrayVec4 v;
	MyArrayPoly  p;
	MyArrayEdge  e;

	void add_quad( int va, int vb, int vc, int vd )
	{
		poly pg;
		pg.vi = four_ints(va, vb, vc, vd);
		pg.ni = four_ints(-1, -1, -1, -1);
		pg.plane = compute_homogeneous_plane(v[va], v[vb], v[vc]);
		p.push_back(pg);
	}

	void discard_neighbor_info()
	{
		for(unsigned int i = 0; i < p.size(); i++ )
		{
			MyArrayInt & ni = p[i].ni;
			for(unsigned int j = 0; j < ni.size(); j++)
				ni[j] = -1;
		}
	}

	void compute_neighbors()
	{
		e.empty();

		discard_neighbor_info();

		bool found;
		int P = p.size();
		// for each polygon
		for(int i = 0; i < P-1; i++ )
		{
			const MyArrayInt & vi = p[i].vi;
			MyArrayInt & ni = p[i].ni;
			int Si = vi.size();

			// for each edge of that polygon
			for(int ii=0; ii < Si; ii++)
			{
				int ii0 = ii;
				int ii1 = (ii+1) % Si;

				// continue if we've already found this neighbor
				if(ni[ii] != -1)
					continue;
				found = false;
				// check all remaining polygons
				for(int j = i+1; j < P; j++ )
				{
					const MyArrayInt & vj = p[j].vi;
					MyArrayInt & nj = p[j].ni;
					int Sj = vj.size();

					for( int jj = 0; jj < Sj; jj++ )
					{
						int jj0 = jj;
						int jj1 = (jj+1) % Sj;
						if(vi[ii0] == vj[jj1] && vi[ii1] == vj[jj0])
						{
							edge ed;
							ed.vi[0] = vi[ii0];
							ed.vi[1] = vi[ii1];
							ed.pi[0] = i;
							ed.pi[1] = j;
							e.push_back(ed);
							ni[ii] = j;
							nj[jj] = i;
							found = true;
							break;
						}
						else if ( vi[ii0] == vj[jj0] && vi[ii1] == vj[jj1] )
						{
							fprintf(stderr,"why am I here?\n");
						}
					}
					if( found )
						break;
				}
			}
		}
	}

	void recompute_planes()
	{
		// for each polygon
		for(unsigned int i = 0; i < p.size(); i++ )
		{
			p[i].plane = compute_homogeneous_plane(v[p[i].vi[0]], v[p[i].vi[1]], v[p[i].vi[2]]);
		}
	}

	void transform(const idMat4 & m)
	{
		for(unsigned int i=0; i < v.size(); i++ )
			v[i] = m * v[i];
		recompute_planes();
	}

};

// make a unit cube
polyhedron PolyhedronFromBounds( const idBounds & b )
{

//       3----------2
//       |\        /|
//       | \      / |
//       |   7--6   |
//       |   |  |   |
//       |   4--5   |
//       |  /    \  |
//       | /      \ |
//       0----------1
//

	static polyhedron p;

	if( p.e.size() == 0 ) {

		p.v.push_back(idVec4( -1, -1,  1, 1));
		p.v.push_back(idVec4(  1, -1,  1, 1));
		p.v.push_back(idVec4(  1,  1,  1, 1));
		p.v.push_back(idVec4( -1,  1,  1, 1));
		p.v.push_back(idVec4( -1, -1, -1, 1));
		p.v.push_back(idVec4(  1, -1, -1, 1));
		p.v.push_back(idVec4(  1,  1, -1, 1));
		p.v.push_back(idVec4( -1,  1, -1, 1));

		p.add_quad( 0, 1, 2, 3 );
		p.add_quad( 7, 6, 5, 4 );
		p.add_quad( 1, 0, 4, 5 );
		p.add_quad( 2, 1, 5, 6 );
		p.add_quad( 3, 2, 6, 7 );
		p.add_quad( 0, 3, 7, 4 );

		p.compute_neighbors();
		p.recompute_planes();
		p.v.empty(); // no need to copy this data since it'll be replaced
	}

	polyhedron p2(p);

	const idVec3 & min = b[0];
	const idVec3 & max = b[1];

	p2.v.empty();
	p2.v.push_back(idVec4( min.x, min.y, max.z, 1));
	p2.v.push_back(idVec4( max.x, min.y, max.z, 1));
	p2.v.push_back(idVec4( max.x, max.y, max.z, 1));
	p2.v.push_back(idVec4( min.x, max.y, max.z, 1));
	p2.v.push_back(idVec4( min.x, min.y, min.z, 1));
	p2.v.push_back(idVec4( max.x, min.y, min.z, 1));
	p2.v.push_back(idVec4( max.x, max.y, min.z, 1));
	p2.v.push_back(idVec4( min.x, max.y, min.z, 1));

	p2.recompute_planes();
	return p2;
}


polyhedron make_sv(const polyhedron & oc, idVec4 light)
{
	static polyhedron lut[64];
	int index = 0;

	for(unsigned int i = 0; i < 6; i++) {
		if( ( oc.p[i].plane * light ) > 0 )
			index |= 1<<i;
	}

	if( lut[index].e.size() == 0 )
	{
		polyhedron & ph = lut[index];
		ph = oc;

		int V = ph.v.size();
		for( int j = 0; j < V; j++ )
		{
			idVec3 proj = homogeneous_difference( light, ph.v[j] );
			ph.v.push_back( idVec4(proj.x, proj.y, proj.z, 0) );
		}

		ph.p.empty();

		for(unsigned int i=0; i < oc.p.size(); i++)
		{
			if( (oc.p[i].plane * light) > 0)
			{
				ph.p.push_back(oc.p[i]);
			}
		}

		if(ph.p.size() == 0)
			return ph = polyhedron();

		ph.compute_neighbors();

		MyArrayPoly vpg;
		int I = ph.p.size();

		for(int i=0; i < I; i++)
		{
			MyArrayInt & vi = ph.p[i].vi;
			MyArrayInt & ni = ph.p[i].ni;
			int S = vi.size();

			for(int j = 0; j < S; j++)
			{
				if( ni[j] == -1 )
				{
					poly pg;
					int a = vi[(j+1)%S];
					int b = vi[j];
					pg.vi = four_ints( a, b, b+V, a+V);
					pg.ni = four_ints(-1, -1, -1, -1);
					vpg.push_back(pg);
				}
			}
		}
		for(unsigned int i = 0; i < vpg.size(); i++)
			ph.p.push_back(vpg[i]);

		ph.compute_neighbors();
		ph.v.empty(); // no need to copy this data since it'll be replaced
	}

	polyhedron ph2 = lut[index];

	// initalize vertices
	ph2.v = oc.v;
	int V = ph2.v.size();
	for( int j = 0; j < V; j++ )
	{
		idVec3 proj = homogeneous_difference( light, ph2.v[j] );
		ph2.v.push_back( idVec4(proj.x, proj.y, proj.z, 0) );
	}

	// need to compute planes for the shadow volume (sv)
	ph2.recompute_planes();

	return ph2;
}

typedef MyArray<idVec4, 36> MySegments;
//int MySegments::max_size = 0;

void polyhedron_edges(polyhedron & a, MySegments & e)
{
	e.empty();
	if(a.e.size() == 0 && a.p.size() != 0)
		a.compute_neighbors();

	for(unsigned int i = 0; i < a.e.size(); i++)
	{
		e.push_back(a.v[a.e[i].vi[0]]);
		e.push_back(a.v[a.e[i].vi[1]]);
	}

}

// clip the segments of e by the planes of polyhedron a.
void clip_segments(const polyhedron & ph, MySegments & is, MySegments & os)
{
	const MyArrayPoly & p = ph.p;

	for(unsigned int i = 0; i < is.size(); i+=2 )
	{
		idVec4 a = is[i  ];
		idVec4 b = is[i+1];
		idVec4 c;

		bool discard = false;

		for(unsigned int j = 0; j < p.size(); j++ )
		{
			float da = a * p[j].plane;
			float db = b * p[j].plane;
			float rdw = 1/(da - db);

			int code = 0;
			if( da > 0 )
				code = 2;
			if( db > 0 )
				code |= 1;


			switch ( code )
			{
			case 3:
				discard = true;
				break;

			case 2:
				c = -db * rdw * a + da * rdw * b;
				a = c;
				break;

			case 1:
				c = -db * rdw * a + da * rdw * b;
				b = c;
				break;

			case 0:
				break;

			default:
				common->Printf("bad clip code!\n");
				break;
			}

			if( discard )
				break;
		}

		if( ! discard )
		{
			os.push_back(a);
			os.push_back(b);
		}
	}

}

idMat4 make_idMat4(const float * m)
{
	return idMat4( m[ 0], m[ 4], m[ 8], m[12],
				   m[ 1], m[ 5], m[ 9], m[13],
				   m[ 2], m[ 6], m[10], m[14],
				   m[ 3], m[ 7], m[11], m[15] );
}

idVec3 v4to3(const idVec4 & v)
{
	return idVec3(v.x/v.w, v.y/v.w, v.z/v.w);
}

void draw_polyhedron( const viewDef_t *viewDef, const polyhedron & p, idVec4 color )
{
	for(unsigned int i = 0; i < p.e.size(); i++)
	{
		viewDef->renderWorld->DebugLine( color, v4to3(p.v[p.e[i].vi[0]]), v4to3(p.v[p.e[i].vi[1]]));
	}
}

void draw_segments( const viewDef_t *viewDef, const MySegments & s, idVec4 color )
{
	for(unsigned int i = 0; i < s.size(); i+=2)
	{
		viewDef->renderWorld->DebugLine( color, v4to3(s[i]), v4to3(s[i+1]));
	}
}

void world_to_hclip( const viewDef_t *viewDef, const idVec4 &global, idVec4 &clip ) {
	int		i;
	idVec4	view;

	for ( i = 0 ; i < 4 ; i ++ ) {
		view[i] =
			global[0] * viewDef->worldSpace.modelViewMatrix[ i + 0 * 4 ] +
			global[1] * viewDef->worldSpace.modelViewMatrix[ i + 1 * 4 ] +
			global[2] * viewDef->worldSpace.modelViewMatrix[ i + 2 * 4 ] +
			global[3] *	viewDef->worldSpace.modelViewMatrix[ i + 3 * 4 ];
	}


	for ( i = 0 ; i < 4 ; i ++ ) {
		clip[i] =
			view[0] * viewDef->projectionMatrix[ i + 0 * 4 ] +
			view[1] * viewDef->projectionMatrix[ i + 1 * 4 ] +
			view[2] * viewDef->projectionMatrix[ i + 2 * 4 ] +
			view[3] * viewDef->projectionMatrix[ i + 3 * 4 ];
	}
}

idScreenRect R_CalcIntersectionScissor( const idRenderLightLocal * lightDef,
										const idRenderEntityLocal * entityDef,
										const viewDef_t * viewDef ) {

	idMat4 omodel = make_idMat4( entityDef->modelMatrix );
	//idMat4 lmodel = make_idMat4( lightDef->modelMatrix );

	// compute light polyhedron
	polyhedron lvol = PolyhedronFromBounds( lightDef->frustumTris->bounds );
	// transform it into world space
	//lvol.transform( lmodel );

	// debug //
	if ( r_useInteractionScissors.GetInteger() == -2 ) {
		draw_polyhedron( viewDef, lvol, colorRed );
	}

	// compute object polyhedron
	polyhedron vol = PolyhedronFromBounds( entityDef->referenceBounds );

	//viewDef->renderWorld->DebugBounds( colorRed, lightDef->frustumTris->bounds );
	//viewDef->renderWorld->DebugBox( colorBlue, idBox( model->Bounds(), entityDef->parms.origin, entityDef->parms.axis ) );

	// transform it into world space
	vol.transform( omodel );

	// debug //
	if ( r_useInteractionScissors.GetInteger() == -2 ) {
		draw_polyhedron( viewDef, vol, colorBlue );
	}

	// transform light position into world space
	idVec4 lightpos = idVec4(lightDef->globalLightOrigin.x,
							 lightDef->globalLightOrigin.y,
							 lightDef->globalLightOrigin.z,
							 1.0f );

	// generate shadow volume "polyhedron"
	polyhedron sv = make_sv(vol, lightpos);

	MySegments in_segs, out_segs;

	// get shadow volume edges
	polyhedron_edges(sv, in_segs);
	// clip them against light bounds planes
	clip_segments(lvol, in_segs, out_segs);

	// get light bounds edges
	polyhedron_edges(lvol, in_segs);
	// clip them by the shadow volume
	clip_segments(sv, in_segs, out_segs);

	// debug //
	if ( r_useInteractionScissors.GetInteger() == -2 ) {
		draw_segments( viewDef, out_segs, colorGreen );
	}

	idBounds outbounds;
	outbounds.Clear();
	for( unsigned int i = 0; i < out_segs.size(); i++ ) {

		idVec4 v;
		world_to_hclip( viewDef, out_segs[i], v );

		if( v.w <= 0.0f ) {
			return lightDef->viewLight->scissorRect;
		}

		idVec3 rv(v.x, v.y, v.z);
		rv /= v.w;

		outbounds.AddPoint( rv );
	}

	// limit the bounds to avoid an inside out scissor rectangle due to floating point to short conversion
	if ( outbounds[0].x < -1.0f ) {
		outbounds[0].x = -1.0f;
	}
	if ( outbounds[1].x > 1.0f ) {
		outbounds[1].x = 1.0f;
	}
	if ( outbounds[0].y < -1.0f ) {
		outbounds[0].y = -1.0f;
	}
	if ( outbounds[1].y > 1.0f ) {
		outbounds[1].y = 1.0f;
	}

	float w2 = ( viewDef->viewport.x2 - viewDef->viewport.x1 + 1 ) / 2.0f;
	float x = viewDef->viewport.x1;
	float h2 = ( viewDef->viewport.y2 - viewDef->viewport.y1 + 1 ) / 2.0f;
	float y = viewDef->viewport.y1;

	idScreenRect rect;
	rect.x1 = outbounds[0].x * w2 + w2 + x;
	rect.x2 = outbounds[1].x * w2 + w2 + x;
	rect.y1 = outbounds[0].y * h2 + h2 + y;
	rect.y2 = outbounds[1].y * h2 + h2 + y;
	rect.Expand();

	rect.Intersect( lightDef->viewLight->scissorRect );

	// debug //
	if ( r_useInteractionScissors.GetInteger() == -2 && !rect.IsEmpty() ) {
		viewDef->renderWorld->DebugScreenRect( colorYellow, rect, viewDef );
	}

	return rect;
}