Debian Codegraph @

code/renderergl1/tr_backend.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 #include "tr_local.h"
23
24 backEndData_t *backEndData;
25 backEndState_t backEnd;
26
27
28 static float s_flipMatrix[16] = {
29 // convert from our coordinate system (looking down X)
30 // to OpenGL's coordinate system (looking down -Z)
31 0, 0, -1, 0,
32 -1, 0, 0, 0,
33 0, 1, 0, 0,
34 0, 0, 0, 1
35 };
36
37
38 /*
39 ** GL_Bind
40 */
41 void GL_Bind( image_t *image ) {
42 int texnum;
43
44 if ( !image ) {
45 ri.Printf( PRINT_WARNING, "GL_Bind: NULL image\n" );
46 texnum = tr.defaultImage->texnum;
47 } else {
48 texnum = image->texnum;
49 }
50
51 if ( r_nobind->integer && tr.dlightImage ) { // performance evaluation option
52 texnum = tr.dlightImage->texnum;
53 }
54
55 if ( glState.currenttextures[glState.currenttmu] != texnum ) {
56 if ( image ) {
57 image->frameUsed = tr.frameCount;
58 }
59 glState.currenttextures[glState.currenttmu] = texnum;
60 qglBindTexture (GL_TEXTURE_2D, texnum);
61 }
62 }
63
64 /*
65 ** GL_SelectTexture
66 */
67 void GL_SelectTexture( int unit )
68 {
69 if ( glState.currenttmu == unit )
70 {
71 return;
72 }
73
74 if ( unit == 0 )
75 {
76 qglActiveTextureARB( GL_TEXTURE0_ARB );
77 GLimp_LogComment( "glActiveTextureARB( GL_TEXTURE0_ARB )\n" );
78 qglClientActiveTextureARB( GL_TEXTURE0_ARB );
79 GLimp_LogComment( "glClientActiveTextureARB( GL_TEXTURE0_ARB )\n" );
80 }
81 else if ( unit == 1 )
82 {
83 qglActiveTextureARB( GL_TEXTURE1_ARB );
84 GLimp_LogComment( "glActiveTextureARB( GL_TEXTURE1_ARB )\n" );
85 qglClientActiveTextureARB( GL_TEXTURE1_ARB );
86 GLimp_LogComment( "glClientActiveTextureARB( GL_TEXTURE1_ARB )\n" );
87 } else {
88 ri.Error( ERR_DROP, "GL_SelectTexture: unit = %i", unit );
89 }
90
91 glState.currenttmu = unit;
92 }
93
94
95 /*
96 ** GL_BindMultitexture
97 */
98 void GL_BindMultitexture( image_t *image0, GLuint env0, image_t *image1, GLuint env1 ) {
99 int texnum0, texnum1;
100
101 texnum0 = image0->texnum;
102 texnum1 = image1->texnum;
103
104 if ( r_nobind->integer && tr.dlightImage ) { // performance evaluation option
105 texnum0 = texnum1 = tr.dlightImage->texnum;
106 }
107
108 if ( glState.currenttextures[1] != texnum1 ) {
109 GL_SelectTexture( 1 );
110 image1->frameUsed = tr.frameCount;
111 glState.currenttextures[1] = texnum1;
112 qglBindTexture( GL_TEXTURE_2D, texnum1 );
113 }
114 if ( glState.currenttextures[0] != texnum0 ) {
115 GL_SelectTexture( 0 );
116 image0->frameUsed = tr.frameCount;
117 glState.currenttextures[0] = texnum0;
118 qglBindTexture( GL_TEXTURE_2D, texnum0 );
119 }
120 }
121
122
123 /*
124 ** GL_Cull
125 */
126 void GL_Cull( int cullType ) {
127 if ( glState.faceCulling == cullType ) {
128 return;
129 }
130
131 glState.faceCulling = cullType;
132
133 if ( cullType == CT_TWO_SIDED )
134 {
135 qglDisable( GL_CULL_FACE );
136 }
137 else
138 {
139 qboolean cullFront;
140 qglEnable( GL_CULL_FACE );
141
142 cullFront = (cullType == CT_FRONT_SIDED);
143 if ( backEnd.viewParms.isMirror )
144 {
145 cullFront = !cullFront;
146 }
147
148 qglCullFace( cullFront ? GL_FRONT : GL_BACK );
149 }
150 }
151
152 /*
153 ** GL_TexEnv
154 */
155 void GL_TexEnv( int env )
156 {
157 if ( env == glState.texEnv[glState.currenttmu] )
158 {
159 return;
160 }
161
162 glState.texEnv[glState.currenttmu] = env;
163
164
165 switch ( env )
166 {
167 case GL_MODULATE:
168 qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
169 break;
170 case GL_REPLACE:
171 qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
172 break;
173 case GL_DECAL:
174 qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
175 break;
176 case GL_ADD:
177 qglTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
178 break;
179 default:
180 ri.Error( ERR_DROP, "GL_TexEnv: invalid env '%d' passed", env );
181 break;
182 }
183 }
184
185 /*
186 ** GL_State
187 **
188 ** This routine is responsible for setting the most commonly changed state
189 ** in Q3.
190 */
191 void GL_State( unsigned long stateBits )
192 {
193 unsigned long diff = stateBits ^ glState.glStateBits;
194
195 if ( !diff )
196 {
197 return;
198 }
199
200 //
201 // check depthFunc bits
202 //
203 if ( diff & GLS_DEPTHFUNC_EQUAL )
204 {
205 if ( stateBits & GLS_DEPTHFUNC_EQUAL )
206 {
207 qglDepthFunc( GL_EQUAL );
208 }
209 else
210 {
211 qglDepthFunc( GL_LEQUAL );
212 }
213 }
214
215 //
216 // check blend bits
217 //
218 if ( diff & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS ) )
219 {
220 GLenum srcFactor = GL_ONE, dstFactor = GL_ONE;
221
222 if ( stateBits & ( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS ) )
223 {
224 switch ( stateBits & GLS_SRCBLEND_BITS )
225 {
226 case GLS_SRCBLEND_ZERO:
227 srcFactor = GL_ZERO;
228 break;
229 case GLS_SRCBLEND_ONE:
230 srcFactor = GL_ONE;
231 break;
232 case GLS_SRCBLEND_DST_COLOR:
233 srcFactor = GL_DST_COLOR;
234 break;
235 case GLS_SRCBLEND_ONE_MINUS_DST_COLOR:
236 srcFactor = GL_ONE_MINUS_DST_COLOR;
237 break;
238 case GLS_SRCBLEND_SRC_ALPHA:
239 srcFactor = GL_SRC_ALPHA;
240 break;
241 case GLS_SRCBLEND_ONE_MINUS_SRC_ALPHA:
242 srcFactor = GL_ONE_MINUS_SRC_ALPHA;
243 break;
244 case GLS_SRCBLEND_DST_ALPHA:
245 srcFactor = GL_DST_ALPHA;
246 break;
247 case GLS_SRCBLEND_ONE_MINUS_DST_ALPHA:
248 srcFactor = GL_ONE_MINUS_DST_ALPHA;
249 break;
250 case GLS_SRCBLEND_ALPHA_SATURATE:
251 srcFactor = GL_SRC_ALPHA_SATURATE;
252 break;
253 default:
254 ri.Error( ERR_DROP, "GL_State: invalid src blend state bits" );
255 break;
256 }
257
258 switch ( stateBits & GLS_DSTBLEND_BITS )
259 {
260 case GLS_DSTBLEND_ZERO:
261 dstFactor = GL_ZERO;
262 break;
263 case GLS_DSTBLEND_ONE:
264 dstFactor = GL_ONE;
265 break;
266 case GLS_DSTBLEND_SRC_COLOR:
267 dstFactor = GL_SRC_COLOR;
268 break;
269 case GLS_DSTBLEND_ONE_MINUS_SRC_COLOR:
270 dstFactor = GL_ONE_MINUS_SRC_COLOR;
271 break;
272 case GLS_DSTBLEND_SRC_ALPHA:
273 dstFactor = GL_SRC_ALPHA;
274 break;
275 case GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA:
276 dstFactor = GL_ONE_MINUS_SRC_ALPHA;
277 break;
278 case GLS_DSTBLEND_DST_ALPHA:
279 dstFactor = GL_DST_ALPHA;
280 break;
281 case GLS_DSTBLEND_ONE_MINUS_DST_ALPHA:
282 dstFactor = GL_ONE_MINUS_DST_ALPHA;
283 break;
284 default:
285 ri.Error( ERR_DROP, "GL_State: invalid dst blend state bits" );
286 break;
287 }
288
289 qglEnable( GL_BLEND );
290 qglBlendFunc( srcFactor, dstFactor );
291 }
292 else
293 {
294 qglDisable( GL_BLEND );
295 }
296 }
297
298 //
299 // check depthmask
300 //
301 if ( diff & GLS_DEPTHMASK_TRUE )
302 {
303 if ( stateBits & GLS_DEPTHMASK_TRUE )
304 {
305 qglDepthMask( GL_TRUE );
306 }
307 else
308 {
309 qglDepthMask( GL_FALSE );
310 }
311 }
312
313 //
314 // fill/line mode
315 //
316 if ( diff & GLS_POLYMODE_LINE )
317 {
318 if ( stateBits & GLS_POLYMODE_LINE )
319 {
320 qglPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
321 }
322 else
323 {
324 qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
325 }
326 }
327
328 //
329 // depthtest
330 //
331 if ( diff & GLS_DEPTHTEST_DISABLE )
332 {
333 if ( stateBits & GLS_DEPTHTEST_DISABLE )
334 {
335 qglDisable( GL_DEPTH_TEST );
336 }
337 else
338 {
339 qglEnable( GL_DEPTH_TEST );
340 }
341 }
342
343 //
344 // alpha test
345 //
346 if ( diff & GLS_ATEST_BITS )
347 {
348 switch ( stateBits & GLS_ATEST_BITS )
349 {
350 case 0:
351 qglDisable( GL_ALPHA_TEST );
352 break;
353 case GLS_ATEST_GT_0:
354 qglEnable( GL_ALPHA_TEST );
355 qglAlphaFunc( GL_GREATER, 0.0f );
356 break;
357 case GLS_ATEST_LT_80:
358 qglEnable( GL_ALPHA_TEST );
359 qglAlphaFunc( GL_LESS, 0.5f );
360 break;
361 case GLS_ATEST_GE_80:
362 qglEnable( GL_ALPHA_TEST );
363 qglAlphaFunc( GL_GEQUAL, 0.5f );
364 break;
365 default:
366 assert( 0 );
367 break;
368 }
369 }
370
371 glState.glStateBits = stateBits;
372 }
373
374
375
376 /*
377 ================
378 RB_Hyperspace
379
380 A player has predicted a teleport, but hasn't arrived yet
381 ================
382 */
383 static void RB_Hyperspace( void ) {
384 float c;
385
386 if ( !backEnd.isHyperspace ) {
387 // do initialization shit
388 }
389
390 c = ( backEnd.refdef.time & 255 ) / 255.0f;
391 qglClearColor( c, c, c, 1 );
392 qglClear( GL_COLOR_BUFFER_BIT );
393
394 backEnd.isHyperspace = qtrue;
395 }
396
397
398 static void SetViewportAndScissor( void ) {
399 qglMatrixMode(GL_PROJECTION);
400 qglLoadMatrixf( backEnd.viewParms.projectionMatrix );
401 qglMatrixMode(GL_MODELVIEW);
402
403 // set the window clipping
404 qglViewport( backEnd.viewParms.viewportX, backEnd.viewParms.viewportY,
405 backEnd.viewParms.viewportWidth, backEnd.viewParms.viewportHeight );
406 qglScissor( backEnd.viewParms.viewportX, backEnd.viewParms.viewportY,
407 backEnd.viewParms.viewportWidth, backEnd.viewParms.viewportHeight );
408 }
409
410 /*
411 =================
412 RB_BeginDrawingView
413
414 Any mirrored or portaled views have already been drawn, so prepare
415 to actually render the visible surfaces for this view
416 =================
417 */
418 void RB_BeginDrawingView (void) {
419 int clearBits = 0;
420
421 // sync with gl if needed
422 if ( r_finish->integer == 1 && !glState.finishCalled ) {
423 qglFinish ();
424 glState.finishCalled = qtrue;
425 }
426 if ( r_finish->integer == 0 ) {
427 glState.finishCalled = qtrue;
428 }
429
430 // we will need to change the projection matrix before drawing
431 // 2D images again
432 backEnd.projection2D = qfalse;
433
434 //
435 // set the modelview matrix for the viewer
436 //
437 SetViewportAndScissor();
438
439 // ensures that depth writes are enabled for the depth clear
440 GL_State( GLS_DEFAULT );
441 // clear relevant buffers
442 clearBits = GL_DEPTH_BUFFER_BIT;
443
444 if ( r_measureOverdraw->integer || r_shadows->integer == 2 )
445 {
446 clearBits |= GL_STENCIL_BUFFER_BIT;
447 }
448 if ( r_fastsky->integer && !( backEnd.refdef.rdflags & RDF_NOWORLDMODEL ) )
449 {
450 clearBits |= GL_COLOR_BUFFER_BIT; // FIXME: only if sky shaders have been used
451 #ifdef _DEBUG
452 qglClearColor( 0.8f, 0.7f, 0.4f, 1.0f ); // FIXME: get color of sky
453 #else
454 qglClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); // FIXME: get color of sky
455 #endif
456 }
457 qglClear( clearBits );
458
459 if ( ( backEnd.refdef.rdflags & RDF_HYPERSPACE ) )
460 {
461 RB_Hyperspace();
462 return;
463 }
464 else
465 {
466 backEnd.isHyperspace = qfalse;
467 }
468
469 glState.faceCulling = -1; // force face culling to set next time
470
471 // we will only draw a sun if there was sky rendered in this view
472 backEnd.skyRenderedThisView = qfalse;
473
474 // clip to the plane of the portal
475 if ( backEnd.viewParms.isPortal ) {
476 float plane[4];
477 GLdouble plane2[4];
478
479 plane[0] = backEnd.viewParms.portalPlane.normal[0];
480 plane[1] = backEnd.viewParms.portalPlane.normal[1];
481 plane[2] = backEnd.viewParms.portalPlane.normal[2];
482 plane[3] = backEnd.viewParms.portalPlane.dist;
483
484 plane2[0] = DotProduct (backEnd.viewParms.or.axis[0], plane);
485 plane2[1] = DotProduct (backEnd.viewParms.or.axis[1], plane);
486 plane2[2] = DotProduct (backEnd.viewParms.or.axis[2], plane);
487 plane2[3] = DotProduct (plane, backEnd.viewParms.or.origin) - plane[3];
488
489 qglLoadMatrixf( s_flipMatrix );
490 qglClipPlane (GL_CLIP_PLANE0, plane2);
491 qglEnable (GL_CLIP_PLANE0);
492 } else {
493 qglDisable (GL_CLIP_PLANE0);
494 }
495 }
496
497
498 /*
499 ==================
500 RB_RenderDrawSurfList
501 ==================
502 */
503 void RB_RenderDrawSurfList( drawSurf_t *drawSurfs, int numDrawSurfs ) {
504 shader_t *shader, *oldShader;
505 int fogNum, oldFogNum;
506 int entityNum, oldEntityNum;
507 int dlighted, oldDlighted;
508 qboolean depthRange, oldDepthRange, isCrosshair, wasCrosshair;
509 int i;
510 drawSurf_t *drawSurf;
511 int oldSort;
512 double originalTime;
513
514 // save original time for entity shader offsets
515 originalTime = backEnd.refdef.floatTime;
516
517 // clear the z buffer, set the modelview, etc
518 RB_BeginDrawingView ();
519
520 // draw everything
521 oldEntityNum = -1;
522 backEnd.currentEntity = &tr.worldEntity;
523 oldShader = NULL;
524 oldFogNum = -1;
525 oldDepthRange = qfalse;
526 wasCrosshair = qfalse;
527 oldDlighted = qfalse;
528 oldSort = -1;
529 depthRange = qfalse;
530
531 backEnd.pc.c_surfaces += numDrawSurfs;
532
533 for (i = 0, drawSurf = drawSurfs ; i < numDrawSurfs ; i++, drawSurf++) {
534 if ( drawSurf->sort == (unsigned)oldSort ) {
535 // fast path, same as previous sort
536 rb_surfaceTable[ *drawSurf->surface ]( drawSurf->surface );
537 continue;
538 }
539 oldSort = (int)drawSurf->sort;
540 R_DecomposeSort( drawSurf->sort, &entityNum, &shader, &fogNum, &dlighted );
541
542 //
543 // change the tess parameters if needed
544 // a "entityMergable" shader is a shader that can have surfaces from separate
545 // entities merged into a single batch, like smoke and blood puff sprites
546 if ( shader != NULL && ( shader != oldShader || fogNum != oldFogNum || dlighted != oldDlighted
547 || ( entityNum != oldEntityNum && !shader->entityMergable ) ) ) {
548 if (oldShader != NULL) {
549 RB_EndSurface();
550 }
551 RB_BeginSurface( shader, fogNum );
552 oldShader = shader;
553 oldFogNum = fogNum;
554 oldDlighted = dlighted;
555 }
556
557 //
558 // change the modelview matrix if needed
559 //
560 if ( entityNum != oldEntityNum ) {
561 depthRange = isCrosshair = qfalse;
562
563 if ( entityNum != REFENTITYNUM_WORLD ) {
564 backEnd.currentEntity = &backEnd.refdef.entities[entityNum];
565
566 // FIXME: e.shaderTime must be passed as int to avoid fp-precision loss issues
567 backEnd.refdef.floatTime = originalTime - (double)backEnd.currentEntity->e.shaderTime;
568
569 // we have to reset the shaderTime as well otherwise image animations start
570 // from the wrong frame
571 tess.shaderTime = backEnd.refdef.floatTime - tess.shader->timeOffset;
572
573 // set up the transformation matrix
574 R_RotateForEntity( backEnd.currentEntity, &backEnd.viewParms, &backEnd.or );
575
576 // set up the dynamic lighting if needed
577 if ( backEnd.currentEntity->needDlights ) {
578 R_TransformDlights( backEnd.refdef.num_dlights, backEnd.refdef.dlights, &backEnd.or );
579 }
580
581 if(backEnd.currentEntity->e.renderfx & RF_DEPTHHACK)
582 {
583 // hack the depth range to prevent view model from poking into walls
584 depthRange = qtrue;
585
586 if(backEnd.currentEntity->e.renderfx & RF_CROSSHAIR)
587 isCrosshair = qtrue;
588 }
589 } else {
590 backEnd.currentEntity = &tr.worldEntity;
591 backEnd.refdef.floatTime = originalTime;
592 backEnd.or = backEnd.viewParms.world;
593 // we have to reset the shaderTime as well otherwise image animations on
594 // the world (like water) continue with the wrong frame
595 tess.shaderTime = backEnd.refdef.floatTime - tess.shader->timeOffset;
596 R_TransformDlights( backEnd.refdef.num_dlights, backEnd.refdef.dlights, &backEnd.or );
597 }
598
599 qglLoadMatrixf( backEnd.or.modelMatrix );
600
601 //
602 // change depthrange. Also change projection matrix so first person weapon does not look like coming
603 // out of the screen.
604 //
605 if (oldDepthRange != depthRange || wasCrosshair != isCrosshair)
606 {
607 if (depthRange)
608 {
609 if(backEnd.viewParms.stereoFrame != STEREO_CENTER)
610 {
611 if(isCrosshair)
612 {
613 if(oldDepthRange)
614 {
615 // was not a crosshair but now is, change back proj matrix
616 qglMatrixMode(GL_PROJECTION);
617 qglLoadMatrixf(backEnd.viewParms.projectionMatrix);
618 qglMatrixMode(GL_MODELVIEW);
619 }
620 }
621 else
622 {
623 viewParms_t temp = backEnd.viewParms;
624
625 R_SetupProjection(&temp, r_znear->value, qfalse);
626
627 qglMatrixMode(GL_PROJECTION);
628 qglLoadMatrixf(temp.projectionMatrix);
629 qglMatrixMode(GL_MODELVIEW);
630 }
631 }
632
633 if(!oldDepthRange)
634 qglDepthRange (0, 0.3);
635 }
636 else
637 {
638 if(!wasCrosshair && backEnd.viewParms.stereoFrame != STEREO_CENTER)
639 {
640 qglMatrixMode(GL_PROJECTION);
641 qglLoadMatrixf(backEnd.viewParms.projectionMatrix);
642 qglMatrixMode(GL_MODELVIEW);
643 }
644
645 qglDepthRange (0, 1);
646 }
647
648 oldDepthRange = depthRange;
649 wasCrosshair = isCrosshair;
650 }
651
652 oldEntityNum = entityNum;
653 }
654
655 // add the triangles for this surface
656 rb_surfaceTable[ *drawSurf->surface ]( drawSurf->surface );
657 }
658
659 backEnd.refdef.floatTime = originalTime;
660
661 // draw the contents of the last shader batch
662 if (oldShader != NULL) {
663 RB_EndSurface();
664 }
665
666 // go back to the world modelview matrix
667 qglLoadMatrixf( backEnd.viewParms.world.modelMatrix );
668 if ( depthRange ) {
669 qglDepthRange (0, 1);
670 }
671
672 if (r_drawSun->integer) {
673 RB_DrawSun(0.1f, tr.sunShader);
674 }
675
676 // darken down any stencil shadows
677 RB_ShadowFinish();
678
679 // add light flares on lights that aren't obscured
680 RB_RenderFlares();
681 }
682
683
684 /*
685 ============================================================================
686
687 RENDER BACK END FUNCTIONS
688
689 ============================================================================
690 */
691
692 /*
693 ================
694 RB_SetGL2D
695
696 ================
697 */
698 void RB_SetGL2D (void) {
699 backEnd.projection2D = qtrue;
700
701 // set 2D virtual screen size
702 qglViewport( 0, 0, glConfig.vidWidth, glConfig.vidHeight );
703 qglScissor( 0, 0, glConfig.vidWidth, glConfig.vidHeight );
704 qglMatrixMode(GL_PROJECTION);
705 qglLoadIdentity ();
706 qglOrtho (0, glConfig.vidWidth, glConfig.vidHeight, 0, 0, 1);
707 qglMatrixMode(GL_MODELVIEW);
708 qglLoadIdentity ();
709
710 GL_State( GLS_DEPTHTEST_DISABLE |
711 GLS_SRCBLEND_SRC_ALPHA |
712 GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA );
713
714 GL_Cull( CT_TWO_SIDED );
715 qglDisable( GL_CLIP_PLANE0 );
716
717 // set time for 2D shaders
718 backEnd.refdef.time = ri.Milliseconds();
719 backEnd.refdef.floatTime = backEnd.refdef.time * 0.001;
720 }
721
722
723 /*
724 =============
725 RE_StretchRaw
726
727 FIXME: not exactly backend
728 Stretches a raw 32 bit power of 2 bitmap image over the given screen rectangle.
729 Used for cinematics.
730 =============
731 */
732 void RE_StretchRaw (int x, int y, int w, int h, int cols, int rows, const byte *data, int client, qboolean dirty) {
733 int i, j;
734 int start, end;
735
736 if ( !tr.registered ) {
737 return;
738 }
739 R_IssuePendingRenderCommands();
740
741 if ( tess.numIndexes ) {
742 RB_EndSurface();
743 }
744
745 // we definitely want to sync every frame for the cinematics
746 qglFinish();
747
748 start = 0;
749 if ( r_speeds->integer ) {
750 start = ri.Milliseconds();
751 }
752
753 // make sure rows and cols are powers of 2
754 for ( i = 0 ; ( 1 << i ) < cols ; i++ ) {
755 }
756 for ( j = 0 ; ( 1 << j ) < rows ; j++ ) {
757 }
758 if ( ( 1 << i ) != cols || ( 1 << j ) != rows) {
759 ri.Error (ERR_DROP, "Draw_StretchRaw: size not a power of 2: %i by %i", cols, rows);
760 }
761
762 RE_UploadCinematic (w, h, cols, rows, data, client, dirty);
763 GL_Bind( tr.scratchImage[client] );
764
765 if ( r_speeds->integer ) {
766 end = ri.Milliseconds();
767 ri.Printf( PRINT_ALL, "qglTexSubImage2D %i, %i: %i msec\n", cols, rows, end - start );
768 }
769
770 RB_SetGL2D();
771
772 qglColor3f( tr.identityLight, tr.identityLight, tr.identityLight );
773
774 qglBegin (GL_QUADS);
775 qglTexCoord2f ( 0.5f / cols, 0.5f / rows );
776 qglVertex2f (x, y);
777 qglTexCoord2f ( ( cols - 0.5f ) / cols , 0.5f / rows );
778 qglVertex2f (x+w, y);
779 qglTexCoord2f ( ( cols - 0.5f ) / cols, ( rows - 0.5f ) / rows );
780 qglVertex2f (x+w, y+h);
781 qglTexCoord2f ( 0.5f / cols, ( rows - 0.5f ) / rows );
782 qglVertex2f (x, y+h);
783 qglEnd ();
784 }
785
786 void RE_UploadCinematic (int w, int h, int cols, int rows, const byte *data, int client, qboolean dirty) {
787
788 GL_Bind( tr.scratchImage[client] );
789
790 // if the scratchImage isn't in the format we want, specify it as a new texture
791 if ( cols != tr.scratchImage[client]->width || rows != tr.scratchImage[client]->height ) {
792 tr.scratchImage[client]->width = tr.scratchImage[client]->uploadWidth = cols;
793 tr.scratchImage[client]->height = tr.scratchImage[client]->uploadHeight = rows;
794 qglTexImage2D( GL_TEXTURE_2D, 0, GL_RGB8, cols, rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
795 qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
796 qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
797 qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, haveClampToEdge ? GL_CLAMP_TO_EDGE : GL_CLAMP );
798 qglTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, haveClampToEdge ? GL_CLAMP_TO_EDGE : GL_CLAMP );
799 } else {
800 if (dirty) {
801 // otherwise, just subimage upload it so that drivers can tell we are going to be changing
802 // it and don't try and do a texture compression
803 qglTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, cols, rows, GL_RGBA, GL_UNSIGNED_BYTE, data );
804 }
805 }
806 }
807
808
809 /*
810 =============
811 RB_SetColor
812
813 =============
814 */
815 const void *RB_SetColor( const void *data ) {
816 const setColorCommand_t *cmd;
817
818 cmd = (const setColorCommand_t *)data;
819
820 backEnd.color2D[0] = cmd->color[0] * 255;
821 backEnd.color2D[1] = cmd->color[1] * 255;
822 backEnd.color2D[2] = cmd->color[2] * 255;
823 backEnd.color2D[3] = cmd->color[3] * 255;
824
825 return (const void *)(cmd + 1);
826 }
827
828 /*
829 =============
830 RB_StretchPic
831 =============
832 */
833 const void *RB_StretchPic ( const void *data ) {
834 const stretchPicCommand_t *cmd;
835 shader_t *shader;
836 int numVerts, numIndexes;
837
838 cmd = (const stretchPicCommand_t *)data;
839
840 if ( !backEnd.projection2D ) {
841 RB_SetGL2D();
842 }
843
844 shader = cmd->shader;
845 if ( shader != tess.shader ) {
846 if ( tess.numIndexes ) {
847 RB_EndSurface();
848 }
849 backEnd.currentEntity = &backEnd.entity2D;
850 RB_BeginSurface( shader, 0 );
851 }
852
853 RB_CHECKOVERFLOW( 4, 6 );
854 numVerts = tess.numVertexes;
855 numIndexes = tess.numIndexes;
856
857 tess.numVertexes += 4;
858 tess.numIndexes += 6;
859
860 tess.indexes[ numIndexes ] = numVerts + 3;
861 tess.indexes[ numIndexes + 1 ] = numVerts + 0;
862 tess.indexes[ numIndexes + 2 ] = numVerts + 2;
863 tess.indexes[ numIndexes + 3 ] = numVerts + 2;
864 tess.indexes[ numIndexes + 4 ] = numVerts + 0;
865 tess.indexes[ numIndexes + 5 ] = numVerts + 1;
866
867 *(int *)tess.vertexColors[ numVerts ] =
868 *(int *)tess.vertexColors[ numVerts + 1 ] =
869 *(int *)tess.vertexColors[ numVerts + 2 ] =
870 *(int *)tess.vertexColors[ numVerts + 3 ] = *(int *)backEnd.color2D;
871
872 tess.xyz[ numVerts ][0] = cmd->x;
873 tess.xyz[ numVerts ][1] = cmd->y;
874 tess.xyz[ numVerts ][2] = 0;
875
876 tess.texCoords[ numVerts ][0][0] = cmd->s1;
877 tess.texCoords[ numVerts ][0][1] = cmd->t1;
878
879 tess.xyz[ numVerts + 1 ][0] = cmd->x + cmd->w;
880 tess.xyz[ numVerts + 1 ][1] = cmd->y;
881 tess.xyz[ numVerts + 1 ][2] = 0;
882
883 tess.texCoords[ numVerts + 1 ][0][0] = cmd->s2;
884 tess.texCoords[ numVerts + 1 ][0][1] = cmd->t1;
885
886 tess.xyz[ numVerts + 2 ][0] = cmd->x + cmd->w;
887 tess.xyz[ numVerts + 2 ][1] = cmd->y + cmd->h;
888 tess.xyz[ numVerts + 2 ][2] = 0;
889
890 tess.texCoords[ numVerts + 2 ][0][0] = cmd->s2;
891 tess.texCoords[ numVerts + 2 ][0][1] = cmd->t2;
892
893 tess.xyz[ numVerts + 3 ][0] = cmd->x;
894 tess.xyz[ numVerts + 3 ][1] = cmd->y + cmd->h;
895 tess.xyz[ numVerts + 3 ][2] = 0;
896
897 tess.texCoords[ numVerts + 3 ][0][0] = cmd->s1;
898 tess.texCoords[ numVerts + 3 ][0][1] = cmd->t2;
899
900 return (const void *)(cmd + 1);
901 }
902
903
904 /*
905 =============
906 RB_DrawSurfs
907
908 =============
909 */
910 const void *RB_DrawSurfs( const void *data ) {
911 const drawSurfsCommand_t *cmd;
912
913 // finish any 2D drawing if needed
914 if ( tess.numIndexes ) {
915 RB_EndSurface();
916 }
917
918 cmd = (const drawSurfsCommand_t *)data;
919
920 backEnd.refdef = cmd->refdef;
921 backEnd.viewParms = cmd->viewParms;
922
923 RB_RenderDrawSurfList( cmd->drawSurfs, cmd->numDrawSurfs );
924
925 return (const void *)(cmd + 1);
926 }
927
928
929 /*
930 =============
931 RB_DrawBuffer
932
933 =============
934 */
935 const void *RB_DrawBuffer( const void *data ) {
936 const drawBufferCommand_t *cmd;
937
938 cmd = (const drawBufferCommand_t *)data;
939
940 qglDrawBuffer( cmd->buffer );
941
942 // clear screen for debugging
943 if ( r_clear->integer ) {
944 qglClearColor( 1, 0, 0.5, 1 );
945 qglClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
946 }
947
948 return (const void *)(cmd + 1);
949 }
950
951 /*
952 ===============
953 RB_ShowImages
954
955 Draw all the images to the screen, on top of whatever
956 was there. This is used to test for texture thrashing.
957
958 Also called by RE_EndRegistration
959 ===============
960 */
961 void RB_ShowImages( void ) {
962 int i;
963 image_t *image;
964 float x, y, w, h;
965 int start, end;
966
967 if ( !backEnd.projection2D ) {
968 RB_SetGL2D();
969 }
970
971 qglClear( GL_COLOR_BUFFER_BIT );
972
973 qglFinish();
974
975 start = ri.Milliseconds();
976
977 for ( i=0 ; i<tr.numImages ; i++ ) {
978 image = tr.images[i];
979
980 w = glConfig.vidWidth / 20;
981 h = glConfig.vidHeight / 15;
982 x = i % 20 * w;
983 y = i / 20 * h;
984
985 // show in proportional size in mode 2
986 if ( r_showImages->integer == 2 ) {
987 w *= image->uploadWidth / 512.0f;
988 h *= image->uploadHeight / 512.0f;
989 }
990
991 GL_Bind( image );
992 qglBegin (GL_QUADS);
993 qglTexCoord2f( 0, 0 );
994 qglVertex2f( x, y );
995 qglTexCoord2f( 1, 0 );
996 qglVertex2f( x + w, y );
997 qglTexCoord2f( 1, 1 );
998 qglVertex2f( x + w, y + h );
999 qglTexCoord2f( 0, 1 );
1000 qglVertex2f( x, y + h );
1001 qglEnd();
1002 }
1003
1004 qglFinish();
1005
1006 end = ri.Milliseconds();
1007 ri.Printf( PRINT_ALL, "%i msec to draw all images\n", end - start );
1008
1009 }
1010
1011 /*
1012 =============
1013 RB_ColorMask
1014
1015 =============
1016 */
1017 const void *RB_ColorMask(const void *data)
1018 {
1019 const colorMaskCommand_t *cmd = data;
1020
1021 qglColorMask(cmd->rgba[0], cmd->rgba[1], cmd->rgba[2], cmd->rgba[3]);
1022
1023 return (const void *)(cmd + 1);
1024 }
1025
1026 /*
1027 =============
1028 RB_ClearDepth
1029
1030 =============
1031 */
1032 const void *RB_ClearDepth(const void *data)
1033 {
1034 const clearDepthCommand_t *cmd = data;
1035
1036 if(tess.numIndexes)
1037 RB_EndSurface();
1038
1039 // texture swapping test
1040 if (r_showImages->integer)
1041 RB_ShowImages();
1042
1043 qglClear(GL_DEPTH_BUFFER_BIT);
1044
1045 return (const void *)(cmd + 1);
1046 }
1047
1048 /*
1049 =============
1050 RB_SwapBuffers
1051
1052 =============
1053 */
1054 const void *RB_SwapBuffers( const void *data ) {
1055 const swapBuffersCommand_t *cmd;
1056
1057 // finish any 2D drawing if needed
1058 if ( tess.numIndexes ) {
1059 RB_EndSurface();
1060 }
1061
1062 // texture swapping test
1063 if ( r_showImages->integer ) {
1064 RB_ShowImages();
1065 }
1066
1067 cmd = (const swapBuffersCommand_t *)data;
1068
1069 // we measure overdraw by reading back the stencil buffer and
1070 // counting up the number of increments that have happened
1071 if ( r_measureOverdraw->integer ) {
1072 int i;
1073 long sum = 0;
1074 unsigned char *stencilReadback;
1075
1076 stencilReadback = ri.Hunk_AllocateTempMemory( glConfig.vidWidth * glConfig.vidHeight );
1077 qglReadPixels( 0, 0, glConfig.vidWidth, glConfig.vidHeight, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, stencilReadback );
1078
1079 for ( i = 0; i < glConfig.vidWidth * glConfig.vidHeight; i++ ) {
1080 sum += stencilReadback[i];
1081 }
1082
1083 backEnd.pc.c_overDraw += sum;
1084 ri.Hunk_FreeTempMemory( stencilReadback );
1085 }
1086
1087
1088 if ( !glState.finishCalled ) {
1089 qglFinish();
1090 }
1091
1092 GLimp_LogComment( "***************** RB_SwapBuffers *****************\n\n\n" );
1093
1094 GLimp_EndFrame();
1095
1096 backEnd.projection2D = qfalse;
1097
1098 return (const void *)(cmd + 1);
1099 }
1100
1101 /*
1102 ====================
1103 RB_ExecuteRenderCommands
1104 ====================
1105 */
1106 void RB_ExecuteRenderCommands( const void *data ) {
1107 int t1, t2;
1108
1109 t1 = ri.Milliseconds ();
1110
1111 while ( 1 ) {
1112 data = PADP(data, sizeof(void *));
1113
1114 switch ( *(const int *)data ) {
1115 case RC_SET_COLOR:
1116 data = RB_SetColor( data );
1117 break;
1118 case RC_STRETCH_PIC:
1119 data = RB_StretchPic( data );
1120 break;
1121 case RC_DRAW_SURFS:
1122 data = RB_DrawSurfs( data );
1123 break;
1124 case RC_DRAW_BUFFER:
1125 data = RB_DrawBuffer( data );
1126 break;
1127 case RC_SWAP_BUFFERS:
1128 data = RB_SwapBuffers( data );
1129 break;
1130 case RC_SCREENSHOT:
1131 data = RB_TakeScreenshotCmd( data );
1132 break;
1133 case RC_VIDEOFRAME:
1134 data = RB_TakeVideoFrameCmd( data );
1135 break;
1136 case RC_COLORMASK:
1137 data = RB_ColorMask(data);
1138 break;
1139 case RC_CLEARDEPTH:
1140 data = RB_ClearDepth(data);
1141 break;
1142 case RC_END_OF_LIST:
1143 default:
1144 // stop rendering
1145 t2 = ri.Milliseconds ();
1146 backEnd.pc.msec = t2 - t1;
1147 return;
1148 }
1149 }
1150
1151 }