Debian Codegraph @

code/renderergl2/tr_main.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena 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 III Arena 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 III Arena 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 // tr_main.c -- main control flow for each frame
23
24 #include "tr_local.h"
25
26 #include <string.h> // memcpy
27
28 trGlobals_t tr;
29
30 static float s_flipMatrix[16] = {
31 // convert from our coordinate system (looking down X)
32 // to OpenGL's coordinate system (looking down -Z)
33 0, 0, -1, 0,
34 -1, 0, 0, 0,
35 0, 1, 0, 0,
36 0, 0, 0, 1
37 };
38
39
40 refimport_t ri;
41
42 // entities that will have procedurally generated surfaces will just
43 // point at this for their sorting surface
44 surfaceType_t entitySurface = SF_ENTITY;
45
46 /*
47 ================
48 R_CompareVert
49 ================
50 */
51 qboolean R_CompareVert(srfVert_t * v1, srfVert_t * v2, qboolean checkST)
52 {
53 int i;
54
55 for(i = 0; i < 3; i++)
56 {
57 if(floor(v1->xyz[i] + 0.1) != floor(v2->xyz[i] + 0.1))
58 {
59 return qfalse;
60 }
61
62 if(checkST && ((v1->st[0] != v2->st[0]) || (v1->st[1] != v2->st[1])))
63 {
64 return qfalse;
65 }
66 }
67
68 return qtrue;
69 }
70
71 /*
72 =============
73 R_CalcTexDirs
74
75 Lengyel, Eric. "Computing Tangent Space Basis Vectors for an Arbitrary Mesh". Terathon Software 3D Graphics Library, 2001. http://www.terathon.com/code/tangent.html
76 =============
77 */
78 void R_CalcTexDirs(vec3_t sdir, vec3_t tdir, const vec3_t v1, const vec3_t v2,
79 const vec3_t v3, const vec2_t w1, const vec2_t w2, const vec2_t w3)
80 {
81 float x1, x2, y1, y2, z1, z2;
82 float s1, s2, t1, t2, r;
83
84 x1 = v2[0] - v1[0];
85 x2 = v3[0] - v1[0];
86 y1 = v2[1] - v1[1];
87 y2 = v3[1] - v1[1];
88 z1 = v2[2] - v1[2];
89 z2 = v3[2] - v1[2];
90
91 s1 = w2[0] - w1[0];
92 s2 = w3[0] - w1[0];
93 t1 = w2[1] - w1[1];
94 t2 = w3[1] - w1[1];
95
96 r = s1 * t2 - s2 * t1;
97 if (r) r = 1.0f / r;
98
99 VectorSet(sdir, (t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
100 VectorSet(tdir, (s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
101 }
102
103 /*
104 =============
105 R_CalcTangentSpace
106
107 Lengyel, Eric. "Computing Tangent Space Basis Vectors for an Arbitrary Mesh". Terathon Software 3D Graphics Library, 2001. http://www.terathon.com/code/tangent.html
108 =============
109 */
110 vec_t R_CalcTangentSpace(vec3_t tangent, vec3_t bitangent, const vec3_t normal, const vec3_t sdir, const vec3_t tdir)
111 {
112 vec3_t n_cross_t;
113 vec_t n_dot_t, handedness;
114
115 // Gram-Schmidt orthogonalize
116 n_dot_t = DotProduct(normal, sdir);
117 VectorMA(sdir, -n_dot_t, normal, tangent);
118 VectorNormalize(tangent);
119
120 // Calculate handedness
121 CrossProduct(normal, sdir, n_cross_t);
122 handedness = (DotProduct(n_cross_t, tdir) < 0.0f) ? -1.0f : 1.0f;
123
124 // Calculate orthogonal bitangent, if necessary
125 if (bitangent)
126 CrossProduct(normal, tangent, bitangent);
127
128 return handedness;
129 }
130
131 qboolean R_CalcTangentVectors(srfVert_t * dv[3])
132 {
133 int i;
134 float bb, s, t;
135 vec3_t bary;
136
137
138 /* calculate barycentric basis for the triangle */
139 bb = (dv[1]->st[0] - dv[0]->st[0]) * (dv[2]->st[1] - dv[0]->st[1]) - (dv[2]->st[0] - dv[0]->st[0]) * (dv[1]->st[1] - dv[0]->st[1]);
140 if(fabs(bb) < 0.00000001f)
141 return qfalse;
142
143 /* do each vertex */
144 for(i = 0; i < 3; i++)
145 {
146 vec4_t tangent;
147 vec3_t normal, bitangent, nxt;
148
149 // calculate s tangent vector
150 s = dv[i]->st[0] + 10.0f;
151 t = dv[i]->st[1];
152 bary[0] = ((dv[1]->st[0] - s) * (dv[2]->st[1] - t) - (dv[2]->st[0] - s) * (dv[1]->st[1] - t)) / bb;
153 bary[1] = ((dv[2]->st[0] - s) * (dv[0]->st[1] - t) - (dv[0]->st[0] - s) * (dv[2]->st[1] - t)) / bb;
154 bary[2] = ((dv[0]->st[0] - s) * (dv[1]->st[1] - t) - (dv[1]->st[0] - s) * (dv[0]->st[1] - t)) / bb;
155
156 tangent[0] = bary[0] * dv[0]->xyz[0] + bary[1] * dv[1]->xyz[0] + bary[2] * dv[2]->xyz[0];
157 tangent[1] = bary[0] * dv[0]->xyz[1] + bary[1] * dv[1]->xyz[1] + bary[2] * dv[2]->xyz[1];
158 tangent[2] = bary[0] * dv[0]->xyz[2] + bary[1] * dv[1]->xyz[2] + bary[2] * dv[2]->xyz[2];
159
160 VectorSubtract(tangent, dv[i]->xyz, tangent);
161 VectorNormalize(tangent);
162
163 // calculate t tangent vector
164 s = dv[i]->st[0];
165 t = dv[i]->st[1] + 10.0f;
166 bary[0] = ((dv[1]->st[0] - s) * (dv[2]->st[1] - t) - (dv[2]->st[0] - s) * (dv[1]->st[1] - t)) / bb;
167 bary[1] = ((dv[2]->st[0] - s) * (dv[0]->st[1] - t) - (dv[0]->st[0] - s) * (dv[2]->st[1] - t)) / bb;
168 bary[2] = ((dv[0]->st[0] - s) * (dv[1]->st[1] - t) - (dv[1]->st[0] - s) * (dv[0]->st[1] - t)) / bb;
169
170 bitangent[0] = bary[0] * dv[0]->xyz[0] + bary[1] * dv[1]->xyz[0] + bary[2] * dv[2]->xyz[0];
171 bitangent[1] = bary[0] * dv[0]->xyz[1] + bary[1] * dv[1]->xyz[1] + bary[2] * dv[2]->xyz[1];
172 bitangent[2] = bary[0] * dv[0]->xyz[2] + bary[1] * dv[1]->xyz[2] + bary[2] * dv[2]->xyz[2];
173
174 VectorSubtract(bitangent, dv[i]->xyz, bitangent);
175 VectorNormalize(bitangent);
176
177 // store bitangent handedness
178 R_VaoUnpackNormal(normal, dv[i]->normal);
179 CrossProduct(normal, tangent, nxt);
180 tangent[3] = (DotProduct(nxt, bitangent) < 0.0f) ? -1.0f : 1.0f;
181
182 R_VaoPackTangent(dv[i]->tangent, tangent);
183
184 // debug code
185 //% Sys_FPrintf( SYS_VRB, "%d S: (%f %f %f) T: (%f %f %f)\n", i,
186 //% stv[ i ][ 0 ], stv[ i ][ 1 ], stv[ i ][ 2 ], ttv[ i ][ 0 ], ttv[ i ][ 1 ], ttv[ i ][ 2 ] );
187 }
188
189 return qtrue;
190 }
191
192
193 /*
194 =================
195 R_CullLocalBox
196
197 Returns CULL_IN, CULL_CLIP, or CULL_OUT
198 =================
199 */
200 int R_CullLocalBox(vec3_t localBounds[2]) {
201 #if 0
202 int i, j;
203 vec3_t transformed[8];
204 float dists[8];
205 vec3_t v;
206 cplane_t *frust;
207 int anyBack;
208 int front, back;
209
210 if ( r_nocull->integer ) {
211 return CULL_CLIP;
212 }
213
214 // transform into world space
215 for (i = 0 ; i < 8 ; i++) {
216 v[0] = bounds[i&1][0];
217 v[1] = bounds[(i>>1)&1][1];
218 v[2] = bounds[(i>>2)&1][2];
219
220 VectorCopy( tr.or.origin, transformed[i] );
221 VectorMA( transformed[i], v[0], tr.or.axis[0], transformed[i] );
222 VectorMA( transformed[i], v[1], tr.or.axis[1], transformed[i] );
223 VectorMA( transformed[i], v[2], tr.or.axis[2], transformed[i] );
224 }
225
226 // check against frustum planes
227 anyBack = 0;
228 for (i = 0 ; i < 4 ; i++) {
229 frust = &tr.viewParms.frustum[i];
230
231 front = back = 0;
232 for (j = 0 ; j < 8 ; j++) {
233 dists[j] = DotProduct(transformed[j], frust->normal);
234 if ( dists[j] > frust->dist ) {
235 front = 1;
236 if ( back ) {
237 break; // a point is in front
238 }
239 } else {
240 back = 1;
241 }
242 }
243 if ( !front ) {
244 // all points were behind one of the planes
245 return CULL_OUT;
246 }
247 anyBack |= back;
248 }
249
250 if ( !anyBack ) {
251 return CULL_IN; // completely inside frustum
252 }
253
254 return CULL_CLIP; // partially clipped
255 #else
256 int j;
257 vec3_t transformed;
258 vec3_t v;
259 vec3_t worldBounds[2];
260
261 if(r_nocull->integer)
262 {
263 return CULL_CLIP;
264 }
265
266 // transform into world space
267 ClearBounds(worldBounds[0], worldBounds[1]);
268
269 for(j = 0; j < 8; j++)
270 {
271 v[0] = localBounds[j & 1][0];
272 v[1] = localBounds[(j >> 1) & 1][1];
273 v[2] = localBounds[(j >> 2) & 1][2];
274
275 R_LocalPointToWorld(v, transformed);
276
277 AddPointToBounds(transformed, worldBounds[0], worldBounds[1]);
278 }
279
280 return R_CullBox(worldBounds);
281 #endif
282 }
283
284 /*
285 =================
286 R_CullBox
287
288 Returns CULL_IN, CULL_CLIP, or CULL_OUT
289 =================
290 */
291 int R_CullBox(vec3_t worldBounds[2]) {
292 int i;
293 cplane_t *frust;
294 qboolean anyClip;
295 int r, numPlanes;
296
297 numPlanes = (tr.viewParms.flags & VPF_FARPLANEFRUSTUM) ? 5 : 4;
298
299 // check against frustum planes
300 anyClip = qfalse;
301 for(i = 0; i < numPlanes; i++)
302 {
303 frust = &tr.viewParms.frustum[i];
304
305 r = BoxOnPlaneSide(worldBounds[0], worldBounds[1], frust);
306
307 if(r == 2)
308 {
309 // completely outside frustum
310 return CULL_OUT;
311 }
312 if(r == 3)
313 {
314 anyClip = qtrue;
315 }
316 }
317
318 if(!anyClip)
319 {
320 // completely inside frustum
321 return CULL_IN;
322 }
323
324 // partially clipped
325 return CULL_CLIP;
326 }
327
328 /*
329 ** R_CullLocalPointAndRadius
330 */
331 int R_CullLocalPointAndRadius( const vec3_t pt, float radius )
332 {
333 vec3_t transformed;
334
335 R_LocalPointToWorld( pt, transformed );
336
337 return R_CullPointAndRadius( transformed, radius );
338 }
339
340 /*
341 ** R_CullPointAndRadius
342 */
343 int R_CullPointAndRadiusEx( const vec3_t pt, float radius, const cplane_t* frustum, int numPlanes )
344 {
345 int i;
346 float dist;
347 const cplane_t *frust;
348 qboolean mightBeClipped = qfalse;
349
350 if ( r_nocull->integer ) {
351 return CULL_CLIP;
352 }
353
354 // check against frustum planes
355 for (i = 0 ; i < numPlanes ; i++)
356 {
357 frust = &frustum[i];
358
359 dist = DotProduct( pt, frust->normal) - frust->dist;
360 if ( dist < -radius )
361 {
362 return CULL_OUT;
363 }
364 else if ( dist <= radius )
365 {
366 mightBeClipped = qtrue;
367 }
368 }
369
370 if ( mightBeClipped )
371 {
372 return CULL_CLIP;
373 }
374
375 return CULL_IN; // completely inside frustum
376 }
377
378 /*
379 ** R_CullPointAndRadius
380 */
381 int R_CullPointAndRadius( const vec3_t pt, float radius )
382 {
383 return R_CullPointAndRadiusEx(pt, radius, tr.viewParms.frustum, (tr.viewParms.flags & VPF_FARPLANEFRUSTUM) ? 5 : 4);
384 }
385
386 /*
387 =================
388 R_LocalNormalToWorld
389
390 =================
391 */
392 void R_LocalNormalToWorld (const vec3_t local, vec3_t world) {
393 world[0] = local[0] * tr.or.axis[0][0] + local[1] * tr.or.axis[1][0] + local[2] * tr.or.axis[2][0];
394 world[1] = local[0] * tr.or.axis[0][1] + local[1] * tr.or.axis[1][1] + local[2] * tr.or.axis[2][1];
395 world[2] = local[0] * tr.or.axis[0][2] + local[1] * tr.or.axis[1][2] + local[2] * tr.or.axis[2][2];
396 }
397
398 /*
399 =================
400 R_LocalPointToWorld
401
402 =================
403 */
404 void R_LocalPointToWorld (const vec3_t local, vec3_t world) {
405 world[0] = local[0] * tr.or.axis[0][0] + local[1] * tr.or.axis[1][0] + local[2] * tr.or.axis[2][0] + tr.or.origin[0];
406 world[1] = local[0] * tr.or.axis[0][1] + local[1] * tr.or.axis[1][1] + local[2] * tr.or.axis[2][1] + tr.or.origin[1];
407 world[2] = local[0] * tr.or.axis[0][2] + local[1] * tr.or.axis[1][2] + local[2] * tr.or.axis[2][2] + tr.or.origin[2];
408 }
409
410 /*
411 =================
412 R_WorldToLocal
413
414 =================
415 */
416 void R_WorldToLocal (const vec3_t world, vec3_t local) {
417 local[0] = DotProduct(world, tr.or.axis[0]);
418 local[1] = DotProduct(world, tr.or.axis[1]);
419 local[2] = DotProduct(world, tr.or.axis[2]);
420 }
421
422 /*
423 ==========================
424 R_TransformModelToClip
425
426 ==========================
427 */
428 void R_TransformModelToClip( const vec3_t src, const float *modelMatrix, const float *projectionMatrix,
429 vec4_t eye, vec4_t dst ) {
430 int i;
431
432 for ( i = 0 ; i < 4 ; i++ ) {
433 eye[i] =
434 src[0] * modelMatrix[ i + 0 * 4 ] +
435 src[1] * modelMatrix[ i + 1 * 4 ] +
436 src[2] * modelMatrix[ i + 2 * 4 ] +
437 1 * modelMatrix[ i + 3 * 4 ];
438 }
439
440 for ( i = 0 ; i < 4 ; i++ ) {
441 dst[i] =
442 eye[0] * projectionMatrix[ i + 0 * 4 ] +
443 eye[1] * projectionMatrix[ i + 1 * 4 ] +
444 eye[2] * projectionMatrix[ i + 2 * 4 ] +
445 eye[3] * projectionMatrix[ i + 3 * 4 ];
446 }
447 }
448
449 /*
450 ==========================
451 R_TransformClipToWindow
452
453 ==========================
454 */
455 void R_TransformClipToWindow( const vec4_t clip, const viewParms_t *view, vec4_t normalized, vec4_t window ) {
456 normalized[0] = clip[0] / clip[3];
457 normalized[1] = clip[1] / clip[3];
458 normalized[2] = ( clip[2] + clip[3] ) / ( 2 * clip[3] );
459
460 window[0] = 0.5f * ( 1.0f + normalized[0] ) * view->viewportWidth;
461 window[1] = 0.5f * ( 1.0f + normalized[1] ) * view->viewportHeight;
462 window[2] = normalized[2];
463
464 window[0] = (int) ( window[0] + 0.5 );
465 window[1] = (int) ( window[1] + 0.5 );
466 }
467
468
469 /*
470 ==========================
471 myGlMultMatrix
472
473 ==========================
474 */
475 void myGlMultMatrix( const float *a, const float *b, float *out ) {
476 int i, j;
477
478 for ( i = 0 ; i < 4 ; i++ ) {
479 for ( j = 0 ; j < 4 ; j++ ) {
480 out[ i * 4 + j ] =
481 a [ i * 4 + 0 ] * b [ 0 * 4 + j ]
482 + a [ i * 4 + 1 ] * b [ 1 * 4 + j ]
483 + a [ i * 4 + 2 ] * b [ 2 * 4 + j ]
484 + a [ i * 4 + 3 ] * b [ 3 * 4 + j ];
485 }
486 }
487 }
488
489 /*
490 =================
491 R_RotateForEntity
492
493 Generates an orientation for an entity and viewParms
494 Does NOT produce any GL calls
495 Called by both the front end and the back end
496 =================
497 */
498 void R_RotateForEntity( const trRefEntity_t *ent, const viewParms_t *viewParms,
499 orientationr_t *or ) {
500 float glMatrix[16];
501 vec3_t delta;
502 float axisLength;
503
504 if ( ent->e.reType != RT_MODEL ) {
505 *or = viewParms->world;
506 return;
507 }
508
509 VectorCopy( ent->e.origin, or->origin );
510
511 VectorCopy( ent->e.axis[0], or->axis[0] );
512 VectorCopy( ent->e.axis[1], or->axis[1] );
513 VectorCopy( ent->e.axis[2], or->axis[2] );
514
515 glMatrix[0] = or->axis[0][0];
516 glMatrix[4] = or->axis[1][0];
517 glMatrix[8] = or->axis[2][0];
518 glMatrix[12] = or->origin[0];
519
520 glMatrix[1] = or->axis[0][1];
521 glMatrix[5] = or->axis[1][1];
522 glMatrix[9] = or->axis[2][1];
523 glMatrix[13] = or->origin[1];
524
525 glMatrix[2] = or->axis[0][2];
526 glMatrix[6] = or->axis[1][2];
527 glMatrix[10] = or->axis[2][2];
528 glMatrix[14] = or->origin[2];
529
530 glMatrix[3] = 0;
531 glMatrix[7] = 0;
532 glMatrix[11] = 0;
533 glMatrix[15] = 1;
534
535 Mat4Copy(glMatrix, or->transformMatrix);
536 myGlMultMatrix( glMatrix, viewParms->world.modelMatrix, or->modelMatrix );
537
538 // calculate the viewer origin in the model's space
539 // needed for fog, specular, and environment mapping
540 VectorSubtract( viewParms->or.origin, or->origin, delta );
541
542 // compensate for scale in the axes if necessary
543 if ( ent->e.nonNormalizedAxes ) {
544 axisLength = VectorLength( ent->e.axis[0] );
545 if ( !axisLength ) {
546 axisLength = 0;
547 } else {
548 axisLength = 1.0f / axisLength;
549 }
550 } else {
551 axisLength = 1.0f;
552 }
553
554 or->viewOrigin[0] = DotProduct( delta, or->axis[0] ) * axisLength;
555 or->viewOrigin[1] = DotProduct( delta, or->axis[1] ) * axisLength;
556 or->viewOrigin[2] = DotProduct( delta, or->axis[2] ) * axisLength;
557 }
558
559 /*
560 =================
561 R_RotateForViewer
562
563 Sets up the modelview matrix for a given viewParm
564 =================
565 */
566 void R_RotateForViewer (void)
567 {
568 float viewerMatrix[16];
569 vec3_t origin;
570
571 Com_Memset (&tr.or, 0, sizeof(tr.or));
572 tr.or.axis[0][0] = 1;
573 tr.or.axis[1][1] = 1;
574 tr.or.axis[2][2] = 1;
575 VectorCopy (tr.viewParms.or.origin, tr.or.viewOrigin);
576
577 // transform by the camera placement
578 VectorCopy( tr.viewParms.or.origin, origin );
579
580 viewerMatrix[0] = tr.viewParms.or.axis[0][0];
581 viewerMatrix[4] = tr.viewParms.or.axis[0][1];
582 viewerMatrix[8] = tr.viewParms.or.axis[0][2];
583 viewerMatrix[12] = -origin[0] * viewerMatrix[0] + -origin[1] * viewerMatrix[4] + -origin[2] * viewerMatrix[8];
584
585 viewerMatrix[1] = tr.viewParms.or.axis[1][0];
586 viewerMatrix[5] = tr.viewParms.or.axis[1][1];
587 viewerMatrix[9] = tr.viewParms.or.axis[1][2];
588 viewerMatrix[13] = -origin[0] * viewerMatrix[1] + -origin[1] * viewerMatrix[5] + -origin[2] * viewerMatrix[9];
589
590 viewerMatrix[2] = tr.viewParms.or.axis[2][0];
591 viewerMatrix[6] = tr.viewParms.or.axis[2][1];
592 viewerMatrix[10] = tr.viewParms.or.axis[2][2];
593 viewerMatrix[14] = -origin[0] * viewerMatrix[2] + -origin[1] * viewerMatrix[6] + -origin[2] * viewerMatrix[10];
594
595 viewerMatrix[3] = 0;
596 viewerMatrix[7] = 0;
597 viewerMatrix[11] = 0;
598 viewerMatrix[15] = 1;
599
600 // convert from our coordinate system (looking down X)
601 // to OpenGL's coordinate system (looking down -Z)
602 myGlMultMatrix( viewerMatrix, s_flipMatrix, tr.or.modelMatrix );
603
604 tr.viewParms.world = tr.or;
605
606 }
607
608 /*
609 ** SetFarClip
610 */
611 static void R_SetFarClip( void )
612 {
613 float farthestCornerDistance = 0;
614 int i;
615
616 // if not rendering the world (icons, menus, etc)
617 // set a 2k far clip plane
618 if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
619 tr.viewParms.zFar = 2048;
620 return;
621 }
622
623 //
624 // set far clipping planes dynamically
625 //
626 farthestCornerDistance = 0;
627 for ( i = 0; i < 8; i++ )
628 {
629 vec3_t v;
630 vec3_t vecTo;
631 float distance;
632
633 if ( i & 1 )
634 {
635 v[0] = tr.viewParms.visBounds[0][0];
636 }
637 else
638 {
639 v[0] = tr.viewParms.visBounds[1][0];
640 }
641
642 if ( i & 2 )
643 {
644 v[1] = tr.viewParms.visBounds[0][1];
645 }
646 else
647 {
648 v[1] = tr.viewParms.visBounds[1][1];
649 }
650
651 if ( i & 4 )
652 {
653 v[2] = tr.viewParms.visBounds[0][2];
654 }
655 else
656 {
657 v[2] = tr.viewParms.visBounds[1][2];
658 }
659
660 VectorSubtract( v, tr.viewParms.or.origin, vecTo );
661
662 distance = vecTo[0] * vecTo[0] + vecTo[1] * vecTo[1] + vecTo[2] * vecTo[2];
663
664 if ( distance > farthestCornerDistance )
665 {
666 farthestCornerDistance = distance;
667 }
668 }
669 tr.viewParms.zFar = sqrt( farthestCornerDistance );
670 }
671
672 /*
673 =================
674 R_SetupFrustum
675
676 Set up the culling frustum planes for the current view using the results we got from computing the first two rows of
677 the projection matrix.
678 =================
679 */
680 void R_SetupFrustum (viewParms_t *dest, float xmin, float xmax, float ymax, float zProj, float zFar, float stereoSep)
681 {
682 vec3_t ofsorigin;
683 float oppleg, adjleg, length;
684 int i;
685
686 if(stereoSep == 0 && xmin == -xmax)
687 {
688 // symmetric case can be simplified
689 VectorCopy(dest->or.origin, ofsorigin);
690
691 length = sqrt(xmax * xmax + zProj * zProj);
692 oppleg = xmax / length;
693 adjleg = zProj / length;
694
695 VectorScale(dest->or.axis[0], oppleg, dest->frustum[0].normal);
696 VectorMA(dest->frustum[0].normal, adjleg, dest->or.axis[1], dest->frustum[0].normal);
697
698 VectorScale(dest->or.axis[0], oppleg, dest->frustum[1].normal);
699 VectorMA(dest->frustum[1].normal, -adjleg, dest->or.axis[1], dest->frustum[1].normal);
700 }
701 else
702 {
703 // In stereo rendering, due to the modification of the projection matrix, dest->or.origin is not the
704 // actual origin that we're rendering so offset the tip of the view pyramid.
705 VectorMA(dest->or.origin, stereoSep, dest->or.axis[1], ofsorigin);
706
707 oppleg = xmax + stereoSep;
708 length = sqrt(oppleg * oppleg + zProj * zProj);
709 VectorScale(dest->or.axis[0], oppleg / length, dest->frustum[0].normal);
710 VectorMA(dest->frustum[0].normal, zProj / length, dest->or.axis[1], dest->frustum[0].normal);
711
712 oppleg = xmin + stereoSep;
713 length = sqrt(oppleg * oppleg + zProj * zProj);
714 VectorScale(dest->or.axis[0], -oppleg / length, dest->frustum[1].normal);
715 VectorMA(dest->frustum[1].normal, -zProj / length, dest->or.axis[1], dest->frustum[1].normal);
716 }
717
718 length = sqrt(ymax * ymax + zProj * zProj);
719 oppleg = ymax / length;
720 adjleg = zProj / length;
721
722 VectorScale(dest->or.axis[0], oppleg, dest->frustum[2].normal);
723 VectorMA(dest->frustum[2].normal, adjleg, dest->or.axis[2], dest->frustum[2].normal);
724
725 VectorScale(dest->or.axis[0], oppleg, dest->frustum[3].normal);
726 VectorMA(dest->frustum[3].normal, -adjleg, dest->or.axis[2], dest->frustum[3].normal);
727
728 for (i=0 ; i<4 ; i++) {
729 dest->frustum[i].type = PLANE_NON_AXIAL;
730 dest->frustum[i].dist = DotProduct (ofsorigin, dest->frustum[i].normal);
731 SetPlaneSignbits( &dest->frustum[i] );
732 }
733
734 if (zFar != 0.0f)
735 {
736 vec3_t farpoint;
737
738 VectorMA(ofsorigin, zFar, dest->or.axis[0], farpoint);
739 VectorScale(dest->or.axis[0], -1.0f, dest->frustum[4].normal);
740
741 dest->frustum[4].type = PLANE_NON_AXIAL;
742 dest->frustum[4].dist = DotProduct (farpoint, dest->frustum[4].normal);
743 SetPlaneSignbits( &dest->frustum[4] );
744 dest->flags |= VPF_FARPLANEFRUSTUM;
745 }
746 }
747
748 /*
749 ===============
750 R_SetupProjection
751 ===============
752 */
753 void R_SetupProjection(viewParms_t *dest, float zProj, float zFar, qboolean computeFrustum)
754 {
755 float xmin, xmax, ymin, ymax;
756 float width, height, stereoSep = r_stereoSeparation->value;
757
758 /*
759 * offset the view origin of the viewer for stereo rendering
760 * by setting the projection matrix appropriately.
761 */
762
763 if(stereoSep != 0)
764 {
765 if(dest->stereoFrame == STEREO_LEFT)
766 stereoSep = zProj / stereoSep;
767 else if(dest->stereoFrame == STEREO_RIGHT)
768 stereoSep = zProj / -stereoSep;
769 else
770 stereoSep = 0;
771 }
772
773 ymax = zProj * tan(dest->fovY * M_PI / 360.0f);
774 ymin = -ymax;
775
776 xmax = zProj * tan(dest->fovX * M_PI / 360.0f);
777 xmin = -xmax;
778
779 width = xmax - xmin;
780 height = ymax - ymin;
781
782 dest->projectionMatrix[0] = 2 * zProj / width;
783 dest->projectionMatrix[4] = 0;
784 dest->projectionMatrix[8] = (xmax + xmin + 2 * stereoSep) / width;
785 dest->projectionMatrix[12] = 2 * zProj * stereoSep / width;
786
787 dest->projectionMatrix[1] = 0;
788 dest->projectionMatrix[5] = 2 * zProj / height;
789 dest->projectionMatrix[9] = ( ymax + ymin ) / height; // normally 0
790 dest->projectionMatrix[13] = 0;
791
792 dest->projectionMatrix[3] = 0;
793 dest->projectionMatrix[7] = 0;
794 dest->projectionMatrix[11] = -1;
795 dest->projectionMatrix[15] = 0;
796
797 // Now that we have all the data for the projection matrix we can also setup the view frustum.
798 if(computeFrustum)
799 R_SetupFrustum(dest, xmin, xmax, ymax, zProj, zFar, stereoSep);
800 }
801
802 /*
803 ===============
804 R_SetupProjectionZ
805
806 Sets the z-component transformation part in the projection matrix
807 ===============
808 */
809 void R_SetupProjectionZ(viewParms_t *dest)
810 {
811 float zNear, zFar, depth;
812
813 zNear = r_znear->value;
814 zFar = dest->zFar;
815
816 depth = zFar - zNear;
817
818 dest->projectionMatrix[2] = 0;
819 dest->projectionMatrix[6] = 0;
820 dest->projectionMatrix[10] = -( zFar + zNear ) / depth;
821 dest->projectionMatrix[14] = -2 * zFar * zNear / depth;
822
823 if (dest->isPortal)
824 {
825 float plane[4];
826 float plane2[4];
827 vec4_t q, c;
828
829 // transform portal plane into camera space
830 plane[0] = dest->portalPlane.normal[0];
831 plane[1] = dest->portalPlane.normal[1];
832 plane[2] = dest->portalPlane.normal[2];
833 plane[3] = dest->portalPlane.dist;
834
835 plane2[0] = -DotProduct (dest->or.axis[1], plane);
836 plane2[1] = DotProduct (dest->or.axis[2], plane);
837 plane2[2] = -DotProduct (dest->or.axis[0], plane);
838 plane2[3] = DotProduct (plane, dest->or.origin) - plane[3];
839
840 // Lengyel, Eric. "Modifying the Projection Matrix to Perform Oblique Near-plane Clipping".
841 // Terathon Software 3D Graphics Library, 2004. http://www.terathon.com/code/oblique.html
842 q[0] = (SGN(plane2[0]) + dest->projectionMatrix[8]) / dest->projectionMatrix[0];
843 q[1] = (SGN(plane2[1]) + dest->projectionMatrix[9]) / dest->projectionMatrix[5];
844 q[2] = -1.0f;
845 q[3] = (1.0f + dest->projectionMatrix[10]) / dest->projectionMatrix[14];
846
847 VectorScale4(plane2, 2.0f / DotProduct4(plane2, q), c);
848
849 dest->projectionMatrix[2] = c[0];
850 dest->projectionMatrix[6] = c[1];
851 dest->projectionMatrix[10] = c[2] + 1.0f;
852 dest->projectionMatrix[14] = c[3];
853
854 }
855
856 }
857
858 /*
859 ===============
860 R_SetupProjectionOrtho
861 ===============
862 */
863 void R_SetupProjectionOrtho(viewParms_t *dest, vec3_t viewBounds[2])
864 {
865 float xmin, xmax, ymin, ymax, znear, zfar;
866 //viewParms_t *dest = &tr.viewParms;
867 int i;
868 vec3_t pop;
869
870 // Quake3: Projection:
871 //
872 // Z X Y Z
873 // | / | /
874 // |/ |/
875 // Y--+ +--X
876
877 xmin = viewBounds[0][1];
878 xmax = viewBounds[1][1];
879 ymin = -viewBounds[1][2];
880 ymax = -viewBounds[0][2];
881 znear = viewBounds[0][0];
882 zfar = viewBounds[1][0];
883
884 dest->projectionMatrix[0] = 2 / (xmax - xmin);
885 dest->projectionMatrix[4] = 0;
886 dest->projectionMatrix[8] = 0;
887 dest->projectionMatrix[12] = (xmax + xmin) / (xmax - xmin);
888
889 dest->projectionMatrix[1] = 0;
890 dest->projectionMatrix[5] = 2 / (ymax - ymin);
891 dest->projectionMatrix[9] = 0;
892 dest->projectionMatrix[13] = (ymax + ymin) / (ymax - ymin);
893
894 dest->projectionMatrix[2] = 0;
895 dest->projectionMatrix[6] = 0;
896 dest->projectionMatrix[10] = -2 / (zfar - znear);
897 dest->projectionMatrix[14] = -(zfar + znear) / (zfar - znear);
898
899 dest->projectionMatrix[3] = 0;
900 dest->projectionMatrix[7] = 0;
901 dest->projectionMatrix[11] = 0;
902 dest->projectionMatrix[15] = 1;
903
904 VectorScale(dest->or.axis[1], 1.0f, dest->frustum[0].normal);
905 VectorMA(dest->or.origin, viewBounds[0][1], dest->frustum[0].normal, pop);
906 dest->frustum[0].dist = DotProduct(pop, dest->frustum[0].normal);
907
908 VectorScale(dest->or.axis[1], -1.0f, dest->frustum[1].normal);
909 VectorMA(dest->or.origin, -viewBounds[1][1], dest->frustum[1].normal, pop);
910 dest->frustum[1].dist = DotProduct(pop, dest->frustum[1].normal);
911
912 VectorScale(dest->or.axis[2], 1.0f, dest->frustum[2].normal);
913 VectorMA(dest->or.origin, viewBounds[0][2], dest->frustum[2].normal, pop);
914 dest->frustum[2].dist = DotProduct(pop, dest->frustum[2].normal);
915
916 VectorScale(dest->or.axis[2], -1.0f, dest->frustum[3].normal);
917 VectorMA(dest->or.origin, -viewBounds[1][2], dest->frustum[3].normal, pop);
918 dest->frustum[3].dist = DotProduct(pop, dest->frustum[3].normal);
919
920 VectorScale(dest->or.axis[0], -1.0f, dest->frustum[4].normal);
921 VectorMA(dest->or.origin, -viewBounds[1][0], dest->frustum[4].normal, pop);
922 dest->frustum[4].dist = DotProduct(pop, dest->frustum[4].normal);
923
924 for (i = 0; i < 5; i++)
925 {
926 dest->frustum[i].type = PLANE_NON_AXIAL;
927 SetPlaneSignbits (&dest->frustum[i]);
928 }
929
930 dest->flags |= VPF_FARPLANEFRUSTUM;
931 }
932
933 /*
934 =================
935 R_MirrorPoint
936 =================
937 */
938 void R_MirrorPoint (vec3_t in, orientation_t *surface, orientation_t *camera, vec3_t out) {
939 int i;
940 vec3_t local;
941 vec3_t transformed;
942 float d;
943
944 VectorSubtract( in, surface->origin, local );
945
946 VectorClear( transformed );
947 for ( i = 0 ; i < 3 ; i++ ) {
948 d = DotProduct(local, surface->axis[i]);
949 VectorMA( transformed, d, camera->axis[i], transformed );
950 }
951
952 VectorAdd( transformed, camera->origin, out );
953 }
954
955 void R_MirrorVector (vec3_t in, orientation_t *surface, orientation_t *camera, vec3_t out) {
956 int i;
957 float d;
958
959 VectorClear( out );
960 for ( i = 0 ; i < 3 ; i++ ) {
961 d = DotProduct(in, surface->axis[i]);
962 VectorMA( out, d, camera->axis[i], out );
963 }
964 }
965
966
967 /*
968 =============
969 R_PlaneForSurface
970 =============
971 */
972 void R_PlaneForSurface (surfaceType_t *surfType, cplane_t *plane) {
973 srfBspSurface_t *tri;
974 srfPoly_t *poly;
975 srfVert_t *v1, *v2, *v3;
976 vec4_t plane4;
977
978 if (!surfType) {
979 Com_Memset (plane, 0, sizeof(*plane));
980 plane->normal[0] = 1;
981 return;
982 }
983 switch (*surfType) {
984 case SF_FACE:
985 *plane = ((srfBspSurface_t *)surfType)->cullPlane;
986 return;
987 case SF_TRIANGLES:
988 tri = (srfBspSurface_t *)surfType;
989 v1 = tri->verts + tri->indexes[0];
990 v2 = tri->verts + tri->indexes[1];
991 v3 = tri->verts + tri->indexes[2];
992 PlaneFromPoints( plane4, v1->xyz, v2->xyz, v3->xyz );
993 VectorCopy( plane4, plane->normal );
994 plane->dist = plane4[3];
995 return;
996 case SF_POLY:
997 poly = (srfPoly_t *)surfType;
998 PlaneFromPoints( plane4, poly->verts[0].xyz, poly->verts[1].xyz, poly->verts[2].xyz );
999 VectorCopy( plane4, plane->normal );
1000 plane->dist = plane4[3];
1001 return;
1002 default:
1003 Com_Memset (plane, 0, sizeof(*plane));
1004 plane->normal[0] = 1;
1005 return;
1006 }
1007 }
1008
1009 /*
1010 =================
1011 R_GetPortalOrientation
1012
1013 entityNum is the entity that the portal surface is a part of, which may
1014 be moving and rotating.
1015
1016 Returns qtrue if it should be mirrored
1017 =================
1018 */
1019 qboolean R_GetPortalOrientations( drawSurf_t *drawSurf, int entityNum,
1020 orientation_t *surface, orientation_t *camera,
1021 vec3_t pvsOrigin, qboolean *mirror ) {
1022 int i;
1023 cplane_t originalPlane, plane;
1024 trRefEntity_t *e;
1025 float d;
1026 vec3_t transformed;
1027
1028 // create plane axis for the portal we are seeing
1029 R_PlaneForSurface( drawSurf->surface, &originalPlane );
1030
1031 // rotate the plane if necessary
1032 if ( entityNum != REFENTITYNUM_WORLD ) {
1033 tr.currentEntityNum = entityNum;
1034 tr.currentEntity = &tr.refdef.entities[entityNum];
1035
1036 // get the orientation of the entity
1037 R_RotateForEntity( tr.currentEntity, &tr.viewParms, &tr.or );
1038
1039 // rotate the plane, but keep the non-rotated version for matching
1040 // against the portalSurface entities
1041 R_LocalNormalToWorld( originalPlane.normal, plane.normal );
1042 plane.dist = originalPlane.dist + DotProduct( plane.normal, tr.or.origin );
1043
1044 // translate the original plane
1045 originalPlane.dist = originalPlane.dist + DotProduct( originalPlane.normal, tr.or.origin );
1046 } else {
1047 plane = originalPlane;
1048 }
1049
1050 VectorCopy( plane.normal, surface->axis[0] );
1051 PerpendicularVector( surface->axis[1], surface->axis[0] );
1052 CrossProduct( surface->axis[0], surface->axis[1], surface->axis[2] );
1053
1054 // locate the portal entity closest to this plane.
1055 // origin will be the origin of the portal, origin2 will be
1056 // the origin of the camera
1057 for ( i = 0 ; i < tr.refdef.num_entities ; i++ ) {
1058 e = &tr.refdef.entities[i];
1059 if ( e->e.reType != RT_PORTALSURFACE ) {
1060 continue;
1061 }
1062
1063 d = DotProduct( e->e.origin, originalPlane.normal ) - originalPlane.dist;
1064 if ( d > 64 || d < -64) {
1065 continue;
1066 }
1067
1068 // get the pvsOrigin from the entity
1069 VectorCopy( e->e.oldorigin, pvsOrigin );
1070
1071 // if the entity is just a mirror, don't use as a camera point
1072 if ( e->e.oldorigin[0] == e->e.origin[0] &&
1073 e->e.oldorigin[1] == e->e.origin[1] &&
1074 e->e.oldorigin[2] == e->e.origin[2] ) {
1075 VectorScale( plane.normal, plane.dist, surface->origin );
1076 VectorCopy( surface->origin, camera->origin );
1077 VectorSubtract( vec3_origin, surface->axis[0], camera->axis[0] );
1078 VectorCopy( surface->axis[1], camera->axis[1] );
1079 VectorCopy( surface->axis[2], camera->axis[2] );
1080
1081 *mirror = qtrue;
1082 return qtrue;
1083 }
1084
1085 // project the origin onto the surface plane to get
1086 // an origin point we can rotate around
1087 d = DotProduct( e->e.origin, plane.normal ) - plane.dist;
1088 VectorMA( e->e.origin, -d, surface->axis[0], surface->origin );
1089
1090 // now get the camera origin and orientation
1091 VectorCopy( e->e.oldorigin, camera->origin );
1092 AxisCopy( e->e.axis, camera->axis );
1093 VectorSubtract( vec3_origin, camera->axis[0], camera->axis[0] );
1094 VectorSubtract( vec3_origin, camera->axis[1], camera->axis[1] );
1095
1096 // optionally rotate
1097 if ( e->e.oldframe ) {
1098 // if a speed is specified
1099 if ( e->e.frame ) {
1100 // continuous rotate
1101 d = (tr.refdef.time/1000.0f) * e->e.frame;
1102 VectorCopy( camera->axis[1], transformed );
1103 RotatePointAroundVector( camera->axis[1], camera->axis[0], transformed, d );
1104 CrossProduct( camera->axis[0], camera->axis[1], camera->axis[2] );
1105 } else {
1106 // bobbing rotate, with skinNum being the rotation offset
1107 d = sin( tr.refdef.time * 0.003f );
1108 d = e->e.skinNum + d * 4;
1109 VectorCopy( camera->axis[1], transformed );
1110 RotatePointAroundVector( camera->axis[1], camera->axis[0], transformed, d );
1111 CrossProduct( camera->axis[0], camera->axis[1], camera->axis[2] );
1112 }
1113 }
1114 else if ( e->e.skinNum ) {
1115 d = e->e.skinNum;
1116 VectorCopy( camera->axis[1], transformed );
1117 RotatePointAroundVector( camera->axis[1], camera->axis[0], transformed, d );
1118 CrossProduct( camera->axis[0], camera->axis[1], camera->axis[2] );
1119 }
1120 *mirror = qfalse;
1121 return qtrue;
1122 }
1123
1124 // if we didn't locate a portal entity, don't render anything.
1125 // We don't want to just treat it as a mirror, because without a
1126 // portal entity the server won't have communicated a proper entity set
1127 // in the snapshot
1128
1129 // unfortunately, with local movement prediction it is easily possible
1130 // to see a surface before the server has communicated the matching
1131 // portal surface entity, so we don't want to print anything here...
1132
1133 //ri.Printf( PRINT_ALL, "Portal surface without a portal entity\n" );
1134
1135 return qfalse;
1136 }
1137
1138 static qboolean IsMirror( const drawSurf_t *drawSurf, int entityNum )
1139 {
1140 int i;
1141 cplane_t originalPlane, plane;
1142 trRefEntity_t *e;
1143 float d;
1144
1145 // create plane axis for the portal we are seeing
1146 R_PlaneForSurface( drawSurf->surface, &originalPlane );
1147
1148 // rotate the plane if necessary
1149 if ( entityNum != REFENTITYNUM_WORLD )
1150 {
1151 tr.currentEntityNum = entityNum;
1152 tr.currentEntity = &tr.refdef.entities[entityNum];
1153
1154 // get the orientation of the entity
1155 R_RotateForEntity( tr.currentEntity, &tr.viewParms, &tr.or );
1156
1157 // rotate the plane, but keep the non-rotated version for matching
1158 // against the portalSurface entities
1159 R_LocalNormalToWorld( originalPlane.normal, plane.normal );
1160 plane.dist = originalPlane.dist + DotProduct( plane.normal, tr.or.origin );
1161
1162 // translate the original plane
1163 originalPlane.dist = originalPlane.dist + DotProduct( originalPlane.normal, tr.or.origin );
1164 }
1165
1166 // locate the portal entity closest to this plane.
1167 // origin will be the origin of the portal, origin2 will be
1168 // the origin of the camera
1169 for ( i = 0 ; i < tr.refdef.num_entities ; i++ )
1170 {
1171 e = &tr.refdef.entities[i];
1172 if ( e->e.reType != RT_PORTALSURFACE ) {
1173 continue;
1174 }
1175
1176 d = DotProduct( e->e.origin, originalPlane.normal ) - originalPlane.dist;
1177 if ( d > 64 || d < -64) {
1178 continue;
1179 }
1180
1181 // if the entity is just a mirror, don't use as a camera point
1182 if ( e->e.oldorigin[0] == e->e.origin[0] &&
1183 e->e.oldorigin[1] == e->e.origin[1] &&
1184 e->e.oldorigin[2] == e->e.origin[2] )
1185 {
1186 return qtrue;
1187 }
1188
1189 return qfalse;
1190 }
1191 return qfalse;
1192 }
1193
1194 /*
1195 ** SurfIsOffscreen
1196 **
1197 ** Determines if a surface is completely offscreen.
1198 */
1199 static qboolean SurfIsOffscreen( const drawSurf_t *drawSurf, vec4_t clipDest[128] ) {
1200 float shortest = 100000000;
1201 int entityNum;
1202 int numTriangles;
1203 shader_t *shader;
1204 int fogNum;
1205 int dlighted;
1206 int pshadowed;
1207 vec4_t clip, eye;
1208 int i;
1209 unsigned int pointAnd = (unsigned int)~0;
1210
1211 R_RotateForViewer();
1212
1213 R_DecomposeSort( drawSurf->sort, &entityNum, &shader, &fogNum, &dlighted, &pshadowed );
1214 RB_BeginSurface( shader, fogNum, drawSurf->cubemapIndex);
1215 rb_surfaceTable[ *drawSurf->surface ]( drawSurf->surface );
1216
1217 assert( tess.numVertexes < 128 );
1218
1219 for ( i = 0; i < tess.numVertexes; i++ )
1220 {
1221 int j;
1222 unsigned int pointFlags = 0;
1223
1224 R_TransformModelToClip( tess.xyz[i], tr.or.modelMatrix, tr.viewParms.projectionMatrix, eye, clip );
1225
1226 for ( j = 0; j < 3; j++ )
1227 {
1228 if ( clip[j] >= clip[3] )
1229 {
1230 pointFlags |= (1 << (j*2));
1231 }
1232 else if ( clip[j] <= -clip[3] )
1233 {
1234 pointFlags |= ( 1 << (j*2+1));
1235 }
1236 }
1237 pointAnd &= pointFlags;
1238 }
1239
1240 // trivially reject
1241 if ( pointAnd )
1242 {
1243 return qtrue;
1244 }
1245
1246 // determine if this surface is backfaced and also determine the distance
1247 // to the nearest vertex so we can cull based on portal range. Culling
1248 // based on vertex distance isn't 100% correct (we should be checking for
1249 // range to the surface), but it's good enough for the types of portals
1250 // we have in the game right now.
1251 numTriangles = tess.numIndexes / 3;
1252
1253 for ( i = 0; i < tess.numIndexes; i += 3 )
1254 {
1255 vec3_t normal, tNormal;
1256
1257 float len;
1258
1259 VectorSubtract( tess.xyz[tess.indexes[i]], tr.viewParms.or.origin, normal );
1260
1261 len = VectorLengthSquared( normal ); // lose the sqrt
1262 if ( len < shortest )
1263 {
1264 shortest = len;
1265 }
1266
1267 R_VaoUnpackNormal(tNormal, tess.normal[tess.indexes[i]]);
1268
1269 if ( DotProduct( normal, tNormal ) >= 0 )
1270 {
1271 numTriangles--;
1272 }
1273 }
1274 if ( !numTriangles )
1275 {
1276 return qtrue;
1277 }
1278
1279 // mirrors can early out at this point, since we don't do a fade over distance
1280 // with them (although we could)
1281 if ( IsMirror( drawSurf, entityNum ) )
1282 {
1283 return qfalse;
1284 }
1285
1286 if ( shortest > (tess.shader->portalRange*tess.shader->portalRange) )
1287 {
1288 return qtrue;
1289 }
1290
1291 return qfalse;
1292 }
1293
1294 /*
1295 ========================
1296 R_MirrorViewBySurface
1297
1298 Returns qtrue if another view has been rendered
1299 ========================
1300 */
1301 qboolean R_MirrorViewBySurface (drawSurf_t *drawSurf, int entityNum) {
1302 vec4_t clipDest[128];
1303 viewParms_t newParms;
1304 viewParms_t oldParms;
1305 orientation_t surface, camera;
1306
1307 // don't recursively mirror
1308 if (tr.viewParms.isPortal) {
1309 ri.Printf( PRINT_DEVELOPER, "WARNING: recursive mirror/portal found\n" );
1310 return qfalse;
1311 }
1312
1313 if ( r_noportals->integer || (r_fastsky->integer == 1) ) {
1314 return qfalse;
1315 }
1316
1317 // trivially reject portal/mirror
1318 if ( SurfIsOffscreen( drawSurf, clipDest ) ) {
1319 return qfalse;
1320 }
1321
1322 // save old viewParms so we can return to it after the mirror view
1323 oldParms = tr.viewParms;
1324
1325 newParms = tr.viewParms;
1326 newParms.isPortal = qtrue;
1327 newParms.zFar = 0.0f;
1328 newParms.flags &= ~VPF_FARPLANEFRUSTUM;
1329 if ( !R_GetPortalOrientations( drawSurf, entityNum, &surface, &camera,
1330 newParms.pvsOrigin, &newParms.isMirror ) ) {
1331 return qfalse; // bad portal, no portalentity
1332 }
1333
1334 // Never draw viewmodels in portal or mirror views.
1335 newParms.flags |= VPF_NOVIEWMODEL;
1336
1337 R_MirrorPoint (oldParms.or.origin, &surface, &camera, newParms.or.origin );
1338
1339 VectorSubtract( vec3_origin, camera.axis[0], newParms.portalPlane.normal );
1340 newParms.portalPlane.dist = DotProduct( camera.origin, newParms.portalPlane.normal );
1341
1342 R_MirrorVector (oldParms.or.axis[0], &surface, &camera, newParms.or.axis[0]);
1343 R_MirrorVector (oldParms.or.axis[1], &surface, &camera, newParms.or.axis[1]);
1344 R_MirrorVector (oldParms.or.axis[2], &surface, &camera, newParms.or.axis[2]);
1345
1346 // OPTIMIZE: restrict the viewport on the mirrored view
1347
1348 // render the mirror view
1349 R_RenderView (&newParms);
1350
1351 tr.viewParms = oldParms;
1352
1353 return qtrue;
1354 }
1355
1356 /*
1357 =================
1358 R_SpriteFogNum
1359
1360 See if a sprite is inside a fog volume
1361 =================
1362 */
1363 int R_SpriteFogNum( trRefEntity_t *ent ) {
1364 int i, j;
1365 fog_t *fog;
1366
1367 if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
1368 return 0;
1369 }
1370
1371 if ( ent->e.renderfx & RF_CROSSHAIR ) {
1372 return 0;
1373 }
1374
1375 for ( i = 1 ; i < tr.world->numfogs ; i++ ) {
1376 fog = &tr.world->fogs[i];
1377 for ( j = 0 ; j < 3 ; j++ ) {
1378 if ( ent->e.origin[j] - ent->e.radius >= fog->bounds[1][j] ) {
1379 break;
1380 }
1381 if ( ent->e.origin[j] + ent->e.radius <= fog->bounds[0][j] ) {
1382 break;
1383 }
1384 }
1385 if ( j == 3 ) {
1386 return i;
1387 }
1388 }
1389
1390 return 0;
1391 }
1392
1393 /*
1394 ==========================================================================================
1395
1396 DRAWSURF SORTING
1397
1398 ==========================================================================================
1399 */
1400
1401 /*
1402 ===============
1403 R_Radix
1404 ===============
1405 */
1406 static ID_INLINE void R_Radix( int offset, int size, drawSurf_t *source, drawSurf_t *dest )
1407 {
1408 int count[ 256 ] = { 0 };
1409 int index[ 256 ];
1410 int i;
1411 unsigned char *sortKey = NULL;
1412 unsigned char *end = NULL;
1413
1414 sortKey = ( (unsigned char *)&source[ 0 ].sort ) + offset;
1415 end = sortKey + ( size * sizeof( drawSurf_t ) );
1416 for( ; sortKey < end; sortKey += sizeof( drawSurf_t ) )
1417 ++count[ *sortKey ];
1418
1419 index[ 0 ] = 0;
1420
1421 for( i = 1; i < 256; ++i )
1422 index[ i ] = index[ i - 1 ] + count[ i - 1 ];
1423
1424 sortKey = ( (unsigned char *)&source[ 0 ].sort ) + offset;
1425 for( i = 0; i < size; ++i, sortKey += sizeof( drawSurf_t ) )
1426 dest[ index[ *sortKey ]++ ] = source[ i ];
1427 }
1428
1429 /*
1430 ===============
1431 R_RadixSort
1432
1433 Radix sort with 4 byte size buckets
1434 ===============
1435 */
1436 static void R_RadixSort( drawSurf_t *source, int size )
1437 {
1438 static drawSurf_t scratch[ MAX_DRAWSURFS ];
1439 #ifdef Q3_LITTLE_ENDIAN
1440 R_Radix( 0, size, source, scratch );
1441 R_Radix( 1, size, scratch, source );
1442 R_Radix( 2, size, source, scratch );
1443 R_Radix( 3, size, scratch, source );
1444 #else
1445 R_Radix( 3, size, source, scratch );
1446 R_Radix( 2, size, scratch, source );
1447 R_Radix( 1, size, source, scratch );
1448 R_Radix( 0, size, scratch, source );
1449 #endif //Q3_LITTLE_ENDIAN
1450 }
1451
1452 //==========================================================================================
1453
1454 /*
1455 =================
1456 R_AddDrawSurf
1457 =================
1458 */
1459 void R_AddDrawSurf( surfaceType_t *surface, shader_t *shader,
1460 int fogIndex, int dlightMap, int pshadowMap, int cubemap ) {
1461 int index;
1462
1463 // instead of checking for overflow, we just mask the index
1464 // so it wraps around
1465 index = tr.refdef.numDrawSurfs & DRAWSURF_MASK;
1466 // the sort data is packed into a single 32 bit value so it can be
1467 // compared quickly during the qsorting process
1468 tr.refdef.drawSurfs[index].sort = (shader->sortedIndex << QSORT_SHADERNUM_SHIFT)
1469 | tr.shiftedEntityNum | ( fogIndex << QSORT_FOGNUM_SHIFT )
1470 | ((int)pshadowMap << QSORT_PSHADOW_SHIFT) | (int)dlightMap;
1471 tr.refdef.drawSurfs[index].cubemapIndex = cubemap;
1472 tr.refdef.drawSurfs[index].surface = surface;
1473 tr.refdef.numDrawSurfs++;
1474 }
1475
1476 /*
1477 =================
1478 R_DecomposeSort
1479 =================
1480 */
1481 void R_DecomposeSort( unsigned sort, int *entityNum, shader_t **shader,
1482 int *fogNum, int *dlightMap, int *pshadowMap ) {
1483 *fogNum = ( sort >> QSORT_FOGNUM_SHIFT ) & 31;
1484 *shader = tr.sortedShaders[ ( sort >> QSORT_SHADERNUM_SHIFT ) & (MAX_SHADERS-1) ];
1485 *entityNum = ( sort >> QSORT_REFENTITYNUM_SHIFT ) & REFENTITYNUM_MASK;
1486 *pshadowMap = (sort >> QSORT_PSHADOW_SHIFT ) & 1;
1487 *dlightMap = sort & 1;
1488 }
1489
1490 /*
1491 =================
1492 R_SortDrawSurfs
1493 =================
1494 */
1495 void R_SortDrawSurfs( drawSurf_t *drawSurfs, int numDrawSurfs ) {
1496 shader_t *shader;
1497 int fogNum;
1498 int entityNum;
1499 int dlighted;
1500 int pshadowed;
1501 int i;
1502
1503 //ri.Printf(PRINT_ALL, "firstDrawSurf %d numDrawSurfs %d\n", (int)(drawSurfs - tr.refdef.drawSurfs), numDrawSurfs);
1504
1505 // it is possible for some views to not have any surfaces
1506 if ( numDrawSurfs < 1 ) {
1507 // we still need to add it for hyperspace cases
1508 R_AddDrawSurfCmd( drawSurfs, numDrawSurfs );
1509 return;
1510 }
1511
1512 // sort the drawsurfs by sort type, then orientation, then shader
1513 R_RadixSort( drawSurfs, numDrawSurfs );
1514
1515 // skip pass through drawing if rendering a shadow map
1516 if (tr.viewParms.flags & (VPF_SHADOWMAP | VPF_DEPTHSHADOW))
1517 {
1518 R_AddDrawSurfCmd( drawSurfs, numDrawSurfs );
1519 return;
1520 }
1521
1522 // check for any pass through drawing, which
1523 // may cause another view to be rendered first
1524 for ( i = 0 ; i < numDrawSurfs ; i++ ) {
1525 R_DecomposeSort( (drawSurfs+i)->sort, &entityNum, &shader, &fogNum, &dlighted, &pshadowed );
1526
1527 if ( shader->sort > SS_PORTAL ) {
1528 break;
1529 }
1530
1531 // no shader should ever have this sort type
1532 if ( shader->sort == SS_BAD ) {
1533 ri.Error (ERR_DROP, "Shader '%s'with sort == SS_BAD", shader->name );
1534 }
1535
1536 // if the mirror was completely clipped away, we may need to check another surface
1537 if ( R_MirrorViewBySurface( (drawSurfs+i), entityNum) ) {
1538 // this is a debug option to see exactly what is being mirrored
1539 if ( r_portalOnly->integer ) {
1540 return;
1541 }
1542 break; // only one mirror view at a time
1543 }
1544 }
1545
1546 R_AddDrawSurfCmd( drawSurfs, numDrawSurfs );
1547 }
1548
1549 static void R_AddEntitySurface (int entityNum)
1550 {
1551 trRefEntity_t *ent;
1552 shader_t *shader;
1553
1554 tr.currentEntityNum = entityNum;
1555
1556 ent = tr.currentEntity = &tr.refdef.entities[tr.currentEntityNum];
1557
1558 ent->needDlights = qfalse;
1559
1560 // preshift the value we are going to OR into the drawsurf sort
1561 tr.shiftedEntityNum = tr.currentEntityNum << QSORT_REFENTITYNUM_SHIFT;
1562
1563 //
1564 // the weapon model must be handled special --
1565 // we don't want the hacked weapon position showing in
1566 // mirrors, because the true body position will already be drawn
1567 //
1568 if ( (ent->e.renderfx & RF_FIRST_PERSON) && (tr.viewParms.flags & VPF_NOVIEWMODEL)) {
1569 return;
1570 }
1571
1572 // simple generated models, like sprites and beams, are not culled
1573 switch ( ent->e.reType ) {
1574 case RT_PORTALSURFACE:
1575 break; // don't draw anything
1576 case RT_SPRITE:
1577 case RT_BEAM:
1578 case RT_LIGHTNING:
1579 case RT_RAIL_CORE:
1580 case RT_RAIL_RINGS:
1581 // self blood sprites, talk balloons, etc should not be drawn in the primary
1582 // view. We can't just do this check for all entities, because md3
1583 // entities may still want to cast shadows from them
1584 if ( (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal) {
1585 return;
1586 }
1587 shader = R_GetShaderByHandle( ent->e.customShader );
1588 R_AddDrawSurf( &entitySurface, shader, R_SpriteFogNum( ent ), 0, 0, 0 /*cubeMap*/ );
1589 break;
1590
1591 case RT_MODEL:
1592 // we must set up parts of tr.or for model culling
1593 R_RotateForEntity( ent, &tr.viewParms, &tr.or );
1594
1595 tr.currentModel = R_GetModelByHandle( ent->e.hModel );
1596 if (!tr.currentModel) {
1597 R_AddDrawSurf( &entitySurface, tr.defaultShader, 0, 0, 0, 0 /*cubeMap*/ );
1598 } else {
1599 switch ( tr.currentModel->type ) {
1600 case MOD_MESH:
1601 R_AddMD3Surfaces( ent );
1602 break;
1603 case MOD_MDR:
1604 R_MDRAddAnimSurfaces( ent );
1605 break;
1606 case MOD_IQM:
1607 R_AddIQMSurfaces( ent );
1608 break;
1609 case MOD_BRUSH:
1610 R_AddBrushModelSurfaces( ent );
1611 break;
1612 case MOD_BAD: // null model axis
1613 if ( (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal) {
1614 break;
1615 }
1616 R_AddDrawSurf( &entitySurface, tr.defaultShader, 0, 0, 0, 0 );
1617 break;
1618 default:
1619 ri.Error( ERR_DROP, "R_AddEntitySurfaces: Bad modeltype" );
1620 break;
1621 }
1622 }
1623 break;
1624 default:
1625 ri.Error( ERR_DROP, "R_AddEntitySurfaces: Bad reType" );
1626 }
1627 }
1628
1629 /*
1630 =============
1631 R_AddEntitySurfaces
1632 =============
1633 */
1634 void R_AddEntitySurfaces (void) {
1635 int i;
1636
1637 if ( !r_drawentities->integer ) {
1638 return;
1639 }
1640
1641 for ( i = 0; i < tr.refdef.num_entities; i++)
1642 R_AddEntitySurface(i);
1643 }
1644
1645
1646 /*
1647 ====================
1648 R_GenerateDrawSurfs
1649 ====================
1650 */
1651 void R_GenerateDrawSurfs( void ) {
1652 R_AddWorldSurfaces ();
1653
1654 R_AddPolygonSurfaces();
1655
1656 // set the projection matrix with the minimum zfar
1657 // now that we have the world bounded
1658 // this needs to be done before entities are
1659 // added, because they use the projection
1660 // matrix for lod calculation
1661
1662 // dynamically compute far clip plane distance
1663 if (!(tr.viewParms.flags & VPF_SHADOWMAP))
1664 {
1665 R_SetFarClip();
1666 }
1667
1668 // we know the size of the clipping volume. Now set the rest of the projection matrix.
1669 R_SetupProjectionZ (&tr.viewParms);
1670
1671 R_AddEntitySurfaces ();
1672 }
1673
1674 /*
1675 ================
1676 R_DebugPolygon
1677 ================
1678 */
1679 void R_DebugPolygon( int color, int numPoints, float *points ) {
1680 // FIXME: implement this
1681 #if 0
1682 int i;
1683
1684 GL_State( GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
1685
1686 // draw solid shade
1687
1688 qglColor3f( color&1, (color>>1)&1, (color>>2)&1 );
1689 qglBegin( GL_POLYGON );
1690 for ( i = 0 ; i < numPoints ; i++ ) {
1691 qglVertex3fv( points + i * 3 );
1692 }
1693 qglEnd();
1694
1695 // draw wireframe outline
1696 GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
1697 qglDepthRange( 0, 0 );
1698 qglColor3f( 1, 1, 1 );
1699 qglBegin( GL_POLYGON );
1700 for ( i = 0 ; i < numPoints ; i++ ) {
1701 qglVertex3fv( points + i * 3 );
1702 }
1703 qglEnd();
1704 qglDepthRange( 0, 1 );
1705 #endif
1706 }
1707
1708 /*
1709 ====================
1710 R_DebugGraphics
1711
1712 Visualization aid for movement clipping debugging
1713 ====================
1714 */
1715 void R_DebugGraphics( void ) {
1716 if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
1717 return;
1718 }
1719 if ( !r_debugSurface->integer ) {
1720 return;
1721 }
1722
1723 R_IssuePendingRenderCommands();
1724
1725 GL_BindToTMU(tr.whiteImage, TB_COLORMAP);
1726 GL_Cull( CT_FRONT_SIDED );
1727 ri.CM_DrawDebugSurface( R_DebugPolygon );
1728 }
1729
1730
1731 /*
1732 ================
1733 R_RenderView
1734
1735 A view may be either the actual camera view,
1736 or a mirror / remote location
1737 ================
1738 */
1739 void R_RenderView (viewParms_t *parms) {
1740 int firstDrawSurf;
1741 int numDrawSurfs;
1742
1743 if ( parms->viewportWidth <= 0 || parms->viewportHeight <= 0 ) {
1744 return;
1745 }
1746
1747 tr.viewCount++;
1748
1749 tr.viewParms = *parms;
1750 tr.viewParms.frameSceneNum = tr.frameSceneNum;
1751 tr.viewParms.frameCount = tr.frameCount;
1752
1753 firstDrawSurf = tr.refdef.numDrawSurfs;
1754
1755 tr.viewCount++;
1756
1757 // set viewParms.world
1758 R_RotateForViewer ();
1759
1760 R_SetupProjection(&tr.viewParms, r_zproj->value, tr.viewParms.zFar, qtrue);
1761
1762 R_GenerateDrawSurfs();
1763
1764 // if we overflowed MAX_DRAWSURFS, the drawsurfs
1765 // wrapped around in the buffer and we will be missing
1766 // the first surfaces, not the last ones
1767 numDrawSurfs = tr.refdef.numDrawSurfs;
1768 if ( numDrawSurfs > MAX_DRAWSURFS ) {
1769 numDrawSurfs = MAX_DRAWSURFS;
1770 }
1771
1772 R_SortDrawSurfs( tr.refdef.drawSurfs + firstDrawSurf, numDrawSurfs - firstDrawSurf );
1773
1774 // draw main system development information (surface outlines, etc)
1775 R_DebugGraphics();
1776 }
1777
1778
1779 void R_RenderDlightCubemaps(const refdef_t *fd)
1780 {
1781 int i;
1782
1783 for (i = 0; i < tr.refdef.num_dlights; i++)
1784 {
1785 viewParms_t shadowParms;
1786 int j;
1787
1788 // use previous frame to determine visible dlights
1789 if ((1 << i) & tr.refdef.dlightMask)
1790 continue;
1791
1792 Com_Memset( &shadowParms, 0, sizeof( shadowParms ) );
1793
1794 shadowParms.viewportX = tr.refdef.x;
1795 shadowParms.viewportY = glConfig.vidHeight - ( tr.refdef.y + PSHADOW_MAP_SIZE );
1796 shadowParms.viewportWidth = PSHADOW_MAP_SIZE;
1797 shadowParms.viewportHeight = PSHADOW_MAP_SIZE;
1798 shadowParms.isPortal = qfalse;
1799 shadowParms.isMirror = qtrue; // because it is
1800
1801 shadowParms.fovX = 90;
1802 shadowParms.fovY = 90;
1803
1804 shadowParms.flags = VPF_SHADOWMAP | VPF_DEPTHSHADOW | VPF_NOVIEWMODEL;
1805 shadowParms.zFar = tr.refdef.dlights[i].radius;
1806
1807 VectorCopy( tr.refdef.dlights[i].origin, shadowParms.or.origin );
1808
1809 for (j = 0; j < 6; j++)
1810 {
1811 switch(j)
1812 {
1813 case 0:
1814 // -X
1815 VectorSet( shadowParms.or.axis[0], -1, 0, 0);
1816 VectorSet( shadowParms.or.axis[1], 0, 0, -1);
1817 VectorSet( shadowParms.or.axis[2], 0, 1, 0);
1818 break;
1819 case 1:
1820 // +X
1821 VectorSet( shadowParms.or.axis[0], 1, 0, 0);
1822 VectorSet( shadowParms.or.axis[1], 0, 0, 1);
1823 VectorSet( shadowParms.or.axis[2], 0, 1, 0);
1824 break;
1825 case 2:
1826 // -Y
1827 VectorSet( shadowParms.or.axis[0], 0, -1, 0);
1828 VectorSet( shadowParms.or.axis[1], 1, 0, 0);
1829 VectorSet( shadowParms.or.axis[2], 0, 0, -1);
1830 break;
1831 case 3:
1832 // +Y
1833 VectorSet( shadowParms.or.axis[0], 0, 1, 0);
1834 VectorSet( shadowParms.or.axis[1], 1, 0, 0);
1835 VectorSet( shadowParms.or.axis[2], 0, 0, 1);
1836 break;
1837 case 4:
1838 // -Z
1839 VectorSet( shadowParms.or.axis[0], 0, 0, -1);
1840 VectorSet( shadowParms.or.axis[1], 1, 0, 0);
1841 VectorSet( shadowParms.or.axis[2], 0, 1, 0);
1842 break;
1843 case 5:
1844 // +Z
1845 VectorSet( shadowParms.or.axis[0], 0, 0, 1);
1846 VectorSet( shadowParms.or.axis[1], -1, 0, 0);
1847 VectorSet( shadowParms.or.axis[2], 0, 1, 0);
1848 break;
1849 }
1850
1851 R_RenderView(&shadowParms);
1852 R_AddCapShadowmapCmd( i, j );
1853 }
1854 }
1855 }
1856
1857
1858 void R_RenderPshadowMaps(const refdef_t *fd)
1859 {
1860 viewParms_t shadowParms;
1861 int i;
1862
1863 // first, make a list of shadows
1864 for ( i = 0; i < tr.refdef.num_entities; i++)
1865 {
1866 trRefEntity_t *ent = &tr.refdef.entities[i];
1867
1868 if((ent->e.renderfx & (RF_FIRST_PERSON | RF_NOSHADOW)))
1869 continue;
1870
1871 //if((ent->e.renderfx & RF_THIRD_PERSON))
1872 //continue;
1873
1874 if (ent->e.reType == RT_MODEL)
1875 {
1876 model_t *model = R_GetModelByHandle( ent->e.hModel );
1877 pshadow_t shadow;
1878 float radius = 0.0f;
1879 float scale = 1.0f;
1880 vec3_t diff;
1881 int j;
1882
1883 if (!model)
1884 continue;
1885
1886 if (ent->e.nonNormalizedAxes)
1887 {
1888 scale = VectorLength( ent->e.axis[0] );
1889 }
1890
1891 switch (model->type)
1892 {
1893 case MOD_MESH:
1894 {
1895 mdvFrame_t *frame = &model->mdv[0]->frames[ent->e.frame];
1896
1897 radius = frame->radius * scale;
1898 }
1899 break;
1900
1901 case MOD_MDR:
1902 {
1903 // FIXME: never actually tested this
1904 mdrHeader_t *header = model->modelData;
1905 int frameSize = (size_t)( &((mdrFrame_t *)0)->bones[ header->numBones ] );
1906 mdrFrame_t *frame = ( mdrFrame_t * ) ( ( byte * ) header + header->ofsFrames + frameSize * ent->e.frame);
1907
1908 radius = frame->radius;
1909 }
1910 break;
1911 case MOD_IQM:
1912 {
1913 // FIXME: never actually tested this
1914 iqmData_t *data = model->modelData;
1915 vec3_t diag;
1916 float *framebounds;
1917
1918 framebounds = data->bounds + 6*ent->e.frame;
1919 VectorSubtract( framebounds+3, framebounds, diag );
1920 radius = 0.5f * VectorLength( diag );
1921 }
1922 break;
1923
1924 default:
1925 break;
1926 }
1927
1928 if (!radius)
1929 continue;
1930
1931 // Cull entities that are behind the viewer by more than lightRadius
1932 VectorSubtract(ent->e.origin, fd->vieworg, diff);
1933 if (DotProduct(diff, fd->viewaxis[0]) < -r_pshadowDist->value)
1934 continue;
1935
1936 memset(&shadow, 0, sizeof(shadow));
1937
1938 shadow.numEntities = 1;
1939 shadow.entityNums[0] = i;
1940 shadow.viewRadius = radius;
1941 shadow.lightRadius = r_pshadowDist->value;
1942 VectorCopy(ent->e.origin, shadow.viewOrigin);
1943 shadow.sort = DotProduct(diff, diff) / (radius * radius);
1944 VectorCopy(ent->e.origin, shadow.entityOrigins[0]);
1945 shadow.entityRadiuses[0] = radius;
1946
1947 for (j = 0; j < MAX_CALC_PSHADOWS; j++)
1948 {
1949 pshadow_t swap;
1950
1951 if (j + 1 > tr.refdef.num_pshadows)
1952 {
1953 tr.refdef.num_pshadows = j + 1;
1954 tr.refdef.pshadows[j] = shadow;
1955 break;
1956 }
1957
1958 // sort shadows by distance from camera divided by radius
1959 // FIXME: sort better
1960 if (tr.refdef.pshadows[j].sort <= shadow.sort)
1961 continue;
1962
1963 swap = tr.refdef.pshadows[j];
1964 tr.refdef.pshadows[j] = shadow;
1965 shadow = swap;
1966 }
1967 }
1968 }
1969
1970 // next, merge touching pshadows
1971 for ( i = 0; i < tr.refdef.num_pshadows; i++)
1972 {
1973 pshadow_t *ps1 = &tr.refdef.pshadows[i];
1974 int j;
1975
1976 for (j = i + 1; j < tr.refdef.num_pshadows; j++)
1977 {
1978 pshadow_t *ps2 = &tr.refdef.pshadows[j];
1979 int k;
1980 qboolean touch;
1981
1982 if (ps1->numEntities == 8)
1983 break;
1984
1985 touch = qfalse;
1986 if (SpheresIntersect(ps1->viewOrigin, ps1->viewRadius, ps2->viewOrigin, ps2->viewRadius))
1987 {
1988 for (k = 0; k < ps1->numEntities; k++)
1989 {
1990 if (SpheresIntersect(ps1->entityOrigins[k], ps1->entityRadiuses[k], ps2->viewOrigin, ps2->viewRadius))
1991 {
1992 touch = qtrue;
1993 break;
1994 }
1995 }
1996 }
1997
1998 if (touch)
1999 {
2000 vec3_t newOrigin;
2001 float newRadius;
2002
2003 BoundingSphereOfSpheres(ps1->viewOrigin, ps1->viewRadius, ps2->viewOrigin, ps2->viewRadius, newOrigin, &newRadius);
2004 VectorCopy(newOrigin, ps1->viewOrigin);
2005 ps1->viewRadius = newRadius;
2006
2007 ps1->entityNums[ps1->numEntities] = ps2->entityNums[0];
2008 VectorCopy(ps2->viewOrigin, ps1->entityOrigins[ps1->numEntities]);
2009 ps1->entityRadiuses[ps1->numEntities] = ps2->viewRadius;
2010
2011 ps1->numEntities++;
2012
2013 for (k = j; k < tr.refdef.num_pshadows - 1; k++)
2014 {
2015 tr.refdef.pshadows[k] = tr.refdef.pshadows[k + 1];
2016 }
2017
2018 j--;
2019 tr.refdef.num_pshadows--;
2020 }
2021 }
2022 }
2023
2024 // cap number of drawn pshadows
2025 if (tr.refdef.num_pshadows > MAX_DRAWN_PSHADOWS)
2026 {
2027 tr.refdef.num_pshadows = MAX_DRAWN_PSHADOWS;
2028 }
2029
2030 // next, fill up the rest of the shadow info
2031 for ( i = 0; i < tr.refdef.num_pshadows; i++)
2032 {
2033 pshadow_t *shadow = &tr.refdef.pshadows[i];
2034 vec3_t up;
2035 vec3_t ambientLight, directedLight, lightDir;
2036
2037 VectorSet(lightDir, 0.57735f, 0.57735f, 0.57735f);
2038 #if 1
2039 R_LightForPoint(shadow->viewOrigin, ambientLight, directedLight, lightDir);
2040
2041 // sometimes there's no light
2042 if (DotProduct(lightDir, lightDir) < 0.9f)
2043 VectorSet(lightDir, 0.0f, 0.0f, 1.0f);
2044 #endif
2045
2046 if (shadow->viewRadius * 3.0f > shadow->lightRadius)
2047 {
2048 shadow->lightRadius = shadow->viewRadius * 3.0f;
2049 }
2050
2051 VectorMA(shadow->viewOrigin, shadow->viewRadius, lightDir, shadow->lightOrigin);
2052
2053 // make up a projection, up doesn't matter
2054 VectorScale(lightDir, -1.0f, shadow->lightViewAxis[0]);
2055 VectorSet(up, 0, 0, -1);
2056
2057 if ( fabsf(DotProduct(up, shadow->lightViewAxis[0])) > 0.9f )
2058 {
2059 VectorSet(up, -1, 0, 0);
2060 }
2061
2062 CrossProduct(shadow->lightViewAxis[0], up, shadow->lightViewAxis[1]);
2063 VectorNormalize(shadow->lightViewAxis[1]);
2064 CrossProduct(shadow->lightViewAxis[0], shadow->lightViewAxis[1], shadow->lightViewAxis[2]);
2065
2066 VectorCopy(shadow->lightViewAxis[0], shadow->cullPlane.normal);
2067 shadow->cullPlane.dist = DotProduct(shadow->cullPlane.normal, shadow->lightOrigin);
2068 shadow->cullPlane.type = PLANE_NON_AXIAL;
2069 SetPlaneSignbits(&shadow->cullPlane);
2070 }
2071
2072 // next, render shadowmaps
2073 for ( i = 0; i < tr.refdef.num_pshadows; i++)
2074 {
2075 int firstDrawSurf;
2076 pshadow_t *shadow = &tr.refdef.pshadows[i];
2077 int j;
2078
2079 Com_Memset( &shadowParms, 0, sizeof( shadowParms ) );
2080
2081 if (glRefConfig.framebufferObject)
2082 {
2083 shadowParms.viewportX = 0;
2084 shadowParms.viewportY = 0;
2085 }
2086 else
2087 {
2088 shadowParms.viewportX = tr.refdef.x;
2089 shadowParms.viewportY = glConfig.vidHeight - ( tr.refdef.y + PSHADOW_MAP_SIZE );
2090 }
2091 shadowParms.viewportWidth = PSHADOW_MAP_SIZE;
2092 shadowParms.viewportHeight = PSHADOW_MAP_SIZE;
2093 shadowParms.isPortal = qfalse;
2094 shadowParms.isMirror = qfalse;
2095
2096 shadowParms.fovX = 90;
2097 shadowParms.fovY = 90;
2098
2099 if (glRefConfig.framebufferObject)
2100 shadowParms.targetFbo = tr.pshadowFbos[i];
2101
2102 shadowParms.flags = VPF_DEPTHSHADOW | VPF_NOVIEWMODEL;
2103 shadowParms.zFar = shadow->lightRadius;
2104
2105 VectorCopy(shadow->lightOrigin, shadowParms.or.origin);
2106
2107 VectorCopy(shadow->lightViewAxis[0], shadowParms.or.axis[0]);
2108 VectorCopy(shadow->lightViewAxis[1], shadowParms.or.axis[1]);
2109 VectorCopy(shadow->lightViewAxis[2], shadowParms.or.axis[2]);
2110
2111 {
2112 tr.viewCount++;
2113
2114 tr.viewParms = shadowParms;
2115 tr.viewParms.frameSceneNum = tr.frameSceneNum;
2116 tr.viewParms.frameCount = tr.frameCount;
2117
2118 firstDrawSurf = tr.refdef.numDrawSurfs;
2119
2120 tr.viewCount++;
2121
2122 // set viewParms.world
2123 R_RotateForViewer ();
2124
2125 {
2126 float xmin, xmax, ymin, ymax, znear, zfar;
2127 viewParms_t *dest = &tr.viewParms;
2128 vec3_t pop;
2129
2130 xmin = ymin = -shadow->viewRadius;
2131 xmax = ymax = shadow->viewRadius;
2132 znear = 0;
2133 zfar = shadow->lightRadius;
2134
2135 dest->projectionMatrix[0] = 2 / (xmax - xmin);
2136 dest->projectionMatrix[4] = 0;
2137 dest->projectionMatrix[8] = (xmax + xmin) / (xmax - xmin);
2138 dest->projectionMatrix[12] =0;
2139
2140 dest->projectionMatrix[1] = 0;
2141 dest->projectionMatrix[5] = 2 / (ymax - ymin);
2142 dest->projectionMatrix[9] = ( ymax + ymin ) / (ymax - ymin); // normally 0
2143 dest->projectionMatrix[13] = 0;
2144
2145 dest->projectionMatrix[2] = 0;
2146 dest->projectionMatrix[6] = 0;
2147 dest->projectionMatrix[10] = 2 / (zfar - znear);
2148 dest->projectionMatrix[14] = 0;
2149
2150 dest->projectionMatrix[3] = 0;
2151 dest->projectionMatrix[7] = 0;
2152 dest->projectionMatrix[11] = 0;
2153 dest->projectionMatrix[15] = 1;
2154
2155 VectorScale(dest->or.axis[1], 1.0f, dest->frustum[0].normal);
2156 VectorMA(dest->or.origin, -shadow->viewRadius, dest->frustum[0].normal, pop);
2157 dest->frustum[0].dist = DotProduct(pop, dest->frustum[0].normal);
2158
2159 VectorScale(dest->or.axis[1], -1.0f, dest->frustum[1].normal);
2160 VectorMA(dest->or.origin, -shadow->viewRadius, dest->frustum[1].normal, pop);
2161 dest->frustum[1].dist = DotProduct(pop, dest->frustum[1].normal);
2162
2163 VectorScale(dest->or.axis[2], 1.0f, dest->frustum[2].normal);
2164 VectorMA(dest->or.origin, -shadow->viewRadius, dest->frustum[2].normal, pop);
2165 dest->frustum[2].dist = DotProduct(pop, dest->frustum[2].normal);
2166
2167 VectorScale(dest->or.axis[2], -1.0f, dest->frustum[3].normal);
2168 VectorMA(dest->or.origin, -shadow->viewRadius, dest->frustum[3].normal, pop);
2169 dest->frustum[3].dist = DotProduct(pop, dest->frustum[3].normal);
2170
2171 VectorScale(dest->or.axis[0], -1.0f, dest->frustum[4].normal);
2172 VectorMA(dest->or.origin, -shadow->lightRadius, dest->frustum[4].normal, pop);
2173 dest->frustum[4].dist = DotProduct(pop, dest->frustum[4].normal);
2174
2175 for (j = 0; j < 5; j++)
2176 {
2177 dest->frustum[j].type = PLANE_NON_AXIAL;
2178 SetPlaneSignbits (&dest->frustum[j]);
2179 }
2180
2181 dest->flags |= VPF_FARPLANEFRUSTUM;
2182 }
2183
2184 for (j = 0; j < shadow->numEntities; j++)
2185 {
2186 R_AddEntitySurface(shadow->entityNums[j]);
2187 }
2188
2189 R_SortDrawSurfs( tr.refdef.drawSurfs + firstDrawSurf, tr.refdef.numDrawSurfs - firstDrawSurf );
2190
2191 if (!glRefConfig.framebufferObject)
2192 R_AddCapShadowmapCmd( i, -1 );
2193 }
2194 }
2195 }
2196
2197 static float CalcSplit(float n, float f, float i, float m)
2198 {
2199 return (n * pow(f / n, i / m) + (f - n) * i / m) / 2.0f;
2200 }
2201
2202
2203 void R_RenderSunShadowMaps(const refdef_t *fd, int level)
2204 {
2205 viewParms_t shadowParms;
2206 vec4_t lightDir, lightCol;
2207 vec3_t lightViewAxis[3];
2208 vec3_t lightOrigin;
2209 float splitZNear, splitZFar, splitBias;
2210 float viewZNear, viewZFar;
2211 vec3_t lightviewBounds[2];
2212 qboolean lightViewIndependentOfCameraView = qfalse;
2213
2214 if (r_forceSun->integer == 2)
2215 {
2216 int scale = 32768;
2217 float angle = (fd->time % scale) / (float)scale * M_PI;
2218 lightDir[0] = cos(angle);
2219 lightDir[1] = sin(35.0f * M_PI / 180.0f);
2220 lightDir[2] = sin(angle) * cos(35.0f * M_PI / 180.0f);
2221 lightDir[3] = 0.0f;
2222
2223 if (1) //((fd->time % (scale * 2)) < scale)
2224 {
2225 lightCol[0] =
2226 lightCol[1] =
2227 lightCol[2] = CLAMP(sin(angle) * 2.0f, 0.0f, 1.0f) * 2.0f;
2228 lightCol[3] = 1.0f;
2229 }
2230 else
2231 {
2232 lightCol[0] =
2233 lightCol[1] =
2234 lightCol[2] = CLAMP(sin(angle) * 2.0f * 0.1f, 0.0f, 0.1f);
2235 lightCol[3] = 1.0f;
2236 }
2237
2238 VectorCopy4(lightDir, tr.refdef.sunDir);
2239 VectorCopy4(lightCol, tr.refdef.sunCol);
2240 VectorScale4(lightCol, 0.2f, tr.refdef.sunAmbCol);
2241 }
2242 else
2243 {
2244 VectorCopy4(tr.refdef.sunDir, lightDir);
2245 }
2246
2247 viewZNear = r_shadowCascadeZNear->value;
2248 viewZFar = r_shadowCascadeZFar->value;
2249 splitBias = r_shadowCascadeZBias->value;
2250
2251 switch(level)
2252 {
2253 case 0:
2254 default:
2255 //splitZNear = r_znear->value;
2256 //splitZFar = 256;
2257 splitZNear = viewZNear;
2258 splitZFar = CalcSplit(viewZNear, viewZFar, 1, 3) + splitBias;
2259 break;
2260 case 1:
2261 splitZNear = CalcSplit(viewZNear, viewZFar, 1, 3) + splitBias;
2262 splitZFar = CalcSplit(viewZNear, viewZFar, 2, 3) + splitBias;
2263 //splitZNear = 256;
2264 //splitZFar = 896;
2265 break;
2266 case 2:
2267 splitZNear = CalcSplit(viewZNear, viewZFar, 2, 3) + splitBias;
2268 splitZFar = viewZFar;
2269 //splitZNear = 896;
2270 //splitZFar = 3072;
2271 break;
2272 }
2273
2274 if (level != 3)
2275 VectorCopy(fd->vieworg, lightOrigin);
2276 else
2277 VectorCopy(tr.world->lightGridOrigin, lightOrigin);
2278
2279 // Make up a projection
2280 VectorScale(lightDir, -1.0f, lightViewAxis[0]);
2281
2282 if (level == 3 || lightViewIndependentOfCameraView)
2283 {
2284 // Use world up as light view up
2285 VectorSet(lightViewAxis[2], 0, 0, 1);
2286 }
2287 else if (level == 0)
2288 {
2289 // Level 0 tries to use a diamond texture orientation relative to camera view
2290 // Use halfway between camera view forward and left for light view up
2291 VectorAdd(fd->viewaxis[0], fd->viewaxis[1], lightViewAxis[2]);
2292 }
2293 else
2294 {
2295 // Use camera view up as light view up
2296 VectorCopy(fd->viewaxis[2], lightViewAxis[2]);
2297 }
2298
2299 // Check if too close to parallel to light direction
2300 if (fabsf(DotProduct(lightViewAxis[2], lightViewAxis[0])) > 0.9f)
2301 {
2302 if (level == 3 || lightViewIndependentOfCameraView)
2303 {
2304 // Use world left as light view up
2305 VectorSet(lightViewAxis[2], 0, 1, 0);
2306 }
2307 else if (level == 0)
2308 {
2309 // Level 0 tries to use a diamond texture orientation relative to camera view
2310 // Use halfway between camera view forward and up for light view up
2311 VectorAdd(fd->viewaxis[0], fd->viewaxis[2], lightViewAxis[2]);
2312 }
2313 else
2314 {
2315 // Use camera view left as light view up
2316 VectorCopy(fd->viewaxis[1], lightViewAxis[2]);
2317 }
2318 }
2319
2320 // clean axes
2321 CrossProduct(lightViewAxis[2], lightViewAxis[0], lightViewAxis[1]);
2322 VectorNormalize(lightViewAxis[1]);
2323 CrossProduct(lightViewAxis[0], lightViewAxis[1], lightViewAxis[2]);
2324
2325 // Create bounds for light projection using slice of view projection
2326 {
2327 mat4_t lightViewMatrix;
2328 vec4_t point, base, lightViewPoint;
2329 float lx, ly;
2330
2331 base[3] = 1;
2332 point[3] = 1;
2333 lightViewPoint[3] = 1;
2334
2335 Mat4View(lightViewAxis, lightOrigin, lightViewMatrix);
2336
2337 ClearBounds(lightviewBounds[0], lightviewBounds[1]);
2338
2339 if (level != 3)
2340 {
2341 // add view near plane
2342 lx = splitZNear * tan(fd->fov_x * M_PI / 360.0f);
2343 ly = splitZNear * tan(fd->fov_y * M_PI / 360.0f);
2344 VectorMA(fd->vieworg, splitZNear, fd->viewaxis[0], base);
2345
2346 VectorMA(base, lx, fd->viewaxis[1], point);
2347 VectorMA(point, ly, fd->viewaxis[2], point);
2348 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2349 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2350
2351 VectorMA(base, -lx, fd->viewaxis[1], point);
2352 VectorMA(point, ly, fd->viewaxis[2], point);
2353 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2354 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2355
2356 VectorMA(base, lx, fd->viewaxis[1], point);
2357 VectorMA(point, -ly, fd->viewaxis[2], point);
2358 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2359 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2360
2361 VectorMA(base, -lx, fd->viewaxis[1], point);
2362 VectorMA(point, -ly, fd->viewaxis[2], point);
2363 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2364 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2365
2366
2367 // add view far plane
2368 lx = splitZFar * tan(fd->fov_x * M_PI / 360.0f);
2369 ly = splitZFar * tan(fd->fov_y * M_PI / 360.0f);
2370 VectorMA(fd->vieworg, splitZFar, fd->viewaxis[0], base);
2371
2372 VectorMA(base, lx, fd->viewaxis[1], point);
2373 VectorMA(point, ly, fd->viewaxis[2], point);
2374 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2375 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2376
2377 VectorMA(base, -lx, fd->viewaxis[1], point);
2378 VectorMA(point, ly, fd->viewaxis[2], point);
2379 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2380 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2381
2382 VectorMA(base, lx, fd->viewaxis[1], point);
2383 VectorMA(point, -ly, fd->viewaxis[2], point);
2384 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2385 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2386
2387 VectorMA(base, -lx, fd->viewaxis[1], point);
2388 VectorMA(point, -ly, fd->viewaxis[2], point);
2389 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2390 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2391 }
2392 else
2393 {
2394 // use light grid size as level size
2395 // FIXME: could be tighter
2396 vec3_t bounds;
2397
2398 bounds[0] = tr.world->lightGridSize[0] * tr.world->lightGridBounds[0];
2399 bounds[1] = tr.world->lightGridSize[1] * tr.world->lightGridBounds[1];
2400 bounds[2] = tr.world->lightGridSize[2] * tr.world->lightGridBounds[2];
2401
2402 point[0] = tr.world->lightGridOrigin[0];
2403 point[1] = tr.world->lightGridOrigin[1];
2404 point[2] = tr.world->lightGridOrigin[2];
2405 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2406 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2407
2408 point[0] = tr.world->lightGridOrigin[0] + bounds[0];
2409 point[1] = tr.world->lightGridOrigin[1];
2410 point[2] = tr.world->lightGridOrigin[2];
2411 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2412 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2413
2414 point[0] = tr.world->lightGridOrigin[0];
2415 point[1] = tr.world->lightGridOrigin[1] + bounds[1];
2416 point[2] = tr.world->lightGridOrigin[2];
2417 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2418 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2419
2420 point[0] = tr.world->lightGridOrigin[0] + bounds[0];
2421 point[1] = tr.world->lightGridOrigin[1] + bounds[1];
2422 point[2] = tr.world->lightGridOrigin[2];
2423 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2424 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2425
2426 point[0] = tr.world->lightGridOrigin[0];
2427 point[1] = tr.world->lightGridOrigin[1];
2428 point[2] = tr.world->lightGridOrigin[2] + bounds[2];
2429 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2430 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2431
2432 point[0] = tr.world->lightGridOrigin[0] + bounds[0];
2433 point[1] = tr.world->lightGridOrigin[1];
2434 point[2] = tr.world->lightGridOrigin[2] + bounds[2];
2435 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2436 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2437
2438 point[0] = tr.world->lightGridOrigin[0];
2439 point[1] = tr.world->lightGridOrigin[1] + bounds[1];
2440 point[2] = tr.world->lightGridOrigin[2] + bounds[2];
2441 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2442 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2443
2444 point[0] = tr.world->lightGridOrigin[0] + bounds[0];
2445 point[1] = tr.world->lightGridOrigin[1] + bounds[1];
2446 point[2] = tr.world->lightGridOrigin[2] + bounds[2];
2447 Mat4Transform(lightViewMatrix, point, lightViewPoint);
2448 AddPointToBounds(lightViewPoint, lightviewBounds[0], lightviewBounds[1]);
2449 }
2450
2451 if (!glRefConfig.depthClamp)
2452 lightviewBounds[0][0] = lightviewBounds[1][0] - 8192;
2453
2454 // Moving the Light in Texel-Sized Increments
2455 // from http://msdn.microsoft.com/en-us/library/windows/desktop/ee416324%28v=vs.85%29.aspx
2456 //
2457 if (lightViewIndependentOfCameraView)
2458 {
2459 float cascadeBound, worldUnitsPerTexel, invWorldUnitsPerTexel;
2460
2461 cascadeBound = MAX(lightviewBounds[1][0] - lightviewBounds[0][0], lightviewBounds[1][1] - lightviewBounds[0][1]);
2462 cascadeBound = MAX(cascadeBound, lightviewBounds[1][2] - lightviewBounds[0][2]);
2463 worldUnitsPerTexel = cascadeBound / tr.sunShadowFbo[level]->width;
2464 invWorldUnitsPerTexel = 1.0f / worldUnitsPerTexel;
2465
2466 VectorScale(lightviewBounds[0], invWorldUnitsPerTexel, lightviewBounds[0]);
2467 lightviewBounds[0][0] = floor(lightviewBounds[0][0]);
2468 lightviewBounds[0][1] = floor(lightviewBounds[0][1]);
2469 lightviewBounds[0][2] = floor(lightviewBounds[0][2]);
2470 VectorScale(lightviewBounds[0], worldUnitsPerTexel, lightviewBounds[0]);
2471
2472 VectorScale(lightviewBounds[1], invWorldUnitsPerTexel, lightviewBounds[1]);
2473 lightviewBounds[1][0] = floor(lightviewBounds[1][0]);
2474 lightviewBounds[1][1] = floor(lightviewBounds[1][1]);
2475 lightviewBounds[1][2] = floor(lightviewBounds[1][2]);
2476 VectorScale(lightviewBounds[1], worldUnitsPerTexel, lightviewBounds[1]);
2477 }
2478
2479 //ri.Printf(PRINT_ALL, "level %d znear %f zfar %f\n", level, lightviewBounds[0][0], lightviewBounds[1][0]);
2480 //ri.Printf(PRINT_ALL, "xmin %f xmax %f ymin %f ymax %f\n", lightviewBounds[0][1], lightviewBounds[1][1], -lightviewBounds[1][2], -lightviewBounds[0][2]);
2481 }
2482
2483 {
2484 int firstDrawSurf;
2485
2486 Com_Memset( &shadowParms, 0, sizeof( shadowParms ) );
2487
2488 if (glRefConfig.framebufferObject)
2489 {
2490 shadowParms.viewportX = 0;
2491 shadowParms.viewportY = 0;
2492 }
2493 else
2494 {
2495 shadowParms.viewportX = tr.refdef.x;
2496 shadowParms.viewportY = glConfig.vidHeight - ( tr.refdef.y + tr.sunShadowFbo[level]->height );
2497 }
2498 shadowParms.viewportWidth = tr.sunShadowFbo[level]->width;
2499 shadowParms.viewportHeight = tr.sunShadowFbo[level]->height;
2500 shadowParms.isPortal = qfalse;
2501 shadowParms.isMirror = qfalse;
2502
2503 shadowParms.fovX = 90;
2504 shadowParms.fovY = 90;
2505
2506 if (glRefConfig.framebufferObject)
2507 shadowParms.targetFbo = tr.sunShadowFbo[level];
2508
2509 shadowParms.flags = VPF_DEPTHSHADOW | VPF_DEPTHCLAMP | VPF_ORTHOGRAPHIC | VPF_NOVIEWMODEL;
2510 shadowParms.zFar = lightviewBounds[1][0];
2511
2512 VectorCopy(lightOrigin, shadowParms.or.origin);
2513
2514 VectorCopy(lightViewAxis[0], shadowParms.or.axis[0]);
2515 VectorCopy(lightViewAxis[1], shadowParms.or.axis[1]);
2516 VectorCopy(lightViewAxis[2], shadowParms.or.axis[2]);
2517
2518 VectorCopy(lightOrigin, shadowParms.pvsOrigin );
2519
2520 {
2521 tr.viewCount++;
2522
2523 tr.viewParms = shadowParms;
2524 tr.viewParms.frameSceneNum = tr.frameSceneNum;
2525 tr.viewParms.frameCount = tr.frameCount;
2526
2527 firstDrawSurf = tr.refdef.numDrawSurfs;
2528
2529 tr.viewCount++;
2530
2531 // set viewParms.world
2532 R_RotateForViewer ();
2533
2534 R_SetupProjectionOrtho(&tr.viewParms, lightviewBounds);
2535
2536 R_AddWorldSurfaces ();
2537
2538 R_AddPolygonSurfaces();
2539
2540 R_AddEntitySurfaces ();
2541
2542 R_SortDrawSurfs( tr.refdef.drawSurfs + firstDrawSurf, tr.refdef.numDrawSurfs - firstDrawSurf );
2543 }
2544
2545 Mat4Multiply(tr.viewParms.projectionMatrix, tr.viewParms.world.modelMatrix, tr.refdef.sunShadowMvp[level]);
2546 }
2547 }
2548
2549 void R_RenderCubemapSide( int cubemapIndex, int cubemapSide, qboolean subscene )
2550 {
2551 refdef_t refdef;
2552 viewParms_t parms;
2553
2554 memset( &refdef, 0, sizeof( refdef ) );
2555 refdef.rdflags = 0;
2556 VectorCopy(tr.cubemaps[cubemapIndex].origin, refdef.vieworg);
2557
2558 switch(cubemapSide)
2559 {
2560 case 0:
2561 // -X
2562 VectorSet( refdef.viewaxis[0], -1, 0, 0);
2563 VectorSet( refdef.viewaxis[1], 0, 0, -1);
2564 VectorSet( refdef.viewaxis[2], 0, 1, 0);
2565 break;
2566 case 1:
2567 // +X
2568 VectorSet( refdef.viewaxis[0], 1, 0, 0);
2569 VectorSet( refdef.viewaxis[1], 0, 0, 1);
2570 VectorSet( refdef.viewaxis[2], 0, 1, 0);
2571 break;
2572 case 2:
2573 // -Y
2574 VectorSet( refdef.viewaxis[0], 0, -1, 0);
2575 VectorSet( refdef.viewaxis[1], 1, 0, 0);
2576 VectorSet( refdef.viewaxis[2], 0, 0, -1);
2577 break;
2578 case 3:
2579 // +Y
2580 VectorSet( refdef.viewaxis[0], 0, 1, 0);
2581 VectorSet( refdef.viewaxis[1], 1, 0, 0);
2582 VectorSet( refdef.viewaxis[2], 0, 0, 1);
2583 break;
2584 case 4:
2585 // -Z
2586 VectorSet( refdef.viewaxis[0], 0, 0, -1);
2587 VectorSet( refdef.viewaxis[1], 1, 0, 0);
2588 VectorSet( refdef.viewaxis[2], 0, 1, 0);
2589 break;
2590 case 5:
2591 // +Z
2592 VectorSet( refdef.viewaxis[0], 0, 0, 1);
2593 VectorSet( refdef.viewaxis[1], -1, 0, 0);
2594 VectorSet( refdef.viewaxis[2], 0, 1, 0);
2595 break;
2596 }
2597
2598 refdef.fov_x = 90;
2599 refdef.fov_y = 90;
2600
2601 refdef.x = 0;
2602 refdef.y = 0;
2603 refdef.width = tr.renderCubeFbo->width;
2604 refdef.height = tr.renderCubeFbo->height;
2605
2606 refdef.time = 0;
2607
2608 if (!subscene)
2609 {
2610 RE_BeginScene(&refdef);
2611
2612 // FIXME: sun shadows aren't rendered correctly in cubemaps
2613 // fix involves changing r_FBufScale to fit smaller cubemap image size, or rendering cubemap to framebuffer first
2614 if(0) //(glRefConfig.framebufferObject && r_sunlightMode->integer && (r_forceSun->integer || tr.sunShadows))
2615 {
2616 R_RenderSunShadowMaps(&refdef, 0);
2617 R_RenderSunShadowMaps(&refdef, 1);
2618 R_RenderSunShadowMaps(&refdef, 2);
2619 R_RenderSunShadowMaps(&refdef, 3);
2620 }
2621 }
2622
2623 {
2624 vec3_t ambient, directed, lightDir;
2625 float scale;
2626
2627 R_LightForPoint(tr.refdef.vieworg, ambient, directed, lightDir);
2628 scale = directed[0] + directed[1] + directed[2] + ambient[0] + ambient[1] + ambient[2] + 1.0f;
2629
2630 // only print message for first side
2631 if (scale < 1.0001f && cubemapSide == 0)
2632 {
2633 ri.Printf(PRINT_ALL, "cubemap %d %s (%f, %f, %f) is outside the lightgrid or inside a wall!\n", cubemapIndex, tr.cubemaps[cubemapIndex].name, tr.refdef.vieworg[0], tr.refdef.vieworg[1], tr.refdef.vieworg[2]);
2634 }
2635 }
2636
2637 Com_Memset( &parms, 0, sizeof( parms ) );
2638
2639 parms.viewportX = 0;
2640 parms.viewportY = 0;
2641 parms.viewportWidth = tr.renderCubeFbo->width;
2642 parms.viewportHeight = tr.renderCubeFbo->height;
2643 parms.isPortal = qfalse;
2644 parms.isMirror = qtrue;
2645 parms.flags = VPF_NOVIEWMODEL | VPF_NOCUBEMAPS;
2646
2647 parms.fovX = 90;
2648 parms.fovY = 90;
2649
2650 VectorCopy( refdef.vieworg, parms.or.origin );
2651 VectorCopy( refdef.viewaxis[0], parms.or.axis[0] );
2652 VectorCopy( refdef.viewaxis[1], parms.or.axis[1] );
2653 VectorCopy( refdef.viewaxis[2], parms.or.axis[2] );
2654
2655 VectorCopy( refdef.vieworg, parms.pvsOrigin );
2656
2657 // FIXME: sun shadows aren't rendered correctly in cubemaps
2658 // fix involves changing r_FBufScale to fit smaller cubemap image size, or rendering cubemap to framebuffer first
2659 if (0) //(r_depthPrepass->value && ((r_forceSun->integer) || tr.sunShadows))
2660 {
2661 parms.flags = VPF_USESUNLIGHT;
2662 }
2663
2664 parms.targetFbo = tr.renderCubeFbo;
2665 parms.targetFboLayer = cubemapSide;
2666 parms.targetFboCubemapIndex = cubemapIndex;
2667
2668 R_RenderView(&parms);
2669
2670 if (!subscene)
2671 RE_EndScene();
2672 }