Debian Codegraph @

code/renderergl1/tr_shade.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_shade.c
23
24 #include "tr_local.h"
25
26 /*
27
28 THIS ENTIRE FILE IS BACK END
29
30 This file deals with applying shaders to surface data in the tess struct.
31 */
32
33 /*
34 ================
35 R_ArrayElementDiscrete
36
37 This is just for OpenGL conformance testing, it should never be the fastest
38 ================
39 */
40 static void APIENTRY R_ArrayElementDiscrete( GLint index ) {
41 qglColor4ubv( tess.svars.colors[ index ] );
42 if ( glState.currenttmu ) {
43 qglMultiTexCoord2fARB( 0, tess.svars.texcoords[ 0 ][ index ][0], tess.svars.texcoords[ 0 ][ index ][1] );
44 qglMultiTexCoord2fARB( 1, tess.svars.texcoords[ 1 ][ index ][0], tess.svars.texcoords[ 1 ][ index ][1] );
45 } else {
46 qglTexCoord2fv( tess.svars.texcoords[ 0 ][ index ] );
47 }
48 qglVertex3fv( tess.xyz[ index ] );
49 }
50
51 /*
52 ===================
53 R_DrawStripElements
54
55 ===================
56 */
57 static int c_vertexes; // for seeing how long our average strips are
58 static int c_begins;
59 static void R_DrawStripElements( int numIndexes, const glIndex_t *indexes, void ( APIENTRY *element )(GLint) ) {
60 int i;
61 glIndex_t last[3];
62 qboolean even;
63
64 c_begins++;
65
66 if ( numIndexes <= 0 ) {
67 return;
68 }
69
70 qglBegin( GL_TRIANGLE_STRIP );
71
72 // prime the strip
73 element( indexes[0] );
74 element( indexes[1] );
75 element( indexes[2] );
76 c_vertexes += 3;
77
78 last[0] = indexes[0];
79 last[1] = indexes[1];
80 last[2] = indexes[2];
81
82 even = qfalse;
83
84 for ( i = 3; i < numIndexes; i += 3 )
85 {
86 // odd numbered triangle in potential strip
87 if ( !even )
88 {
89 // check previous triangle to see if we're continuing a strip
90 if ( ( indexes[i+0] == last[2] ) && ( indexes[i+1] == last[1] ) )
91 {
92 element( indexes[i+2] );
93 c_vertexes++;
94 assert( indexes[i+2] < tess.numVertexes );
95 even = qtrue;
96 }
97 // otherwise we're done with this strip so finish it and start
98 // a new one
99 else
100 {
101 qglEnd();
102
103 qglBegin( GL_TRIANGLE_STRIP );
104 c_begins++;
105
106 element( indexes[i+0] );
107 element( indexes[i+1] );
108 element( indexes[i+2] );
109
110 c_vertexes += 3;
111
112 even = qfalse;
113 }
114 }
115 else
116 {
117 // check previous triangle to see if we're continuing a strip
118 if ( ( last[2] == indexes[i+1] ) && ( last[0] == indexes[i+0] ) )
119 {
120 element( indexes[i+2] );
121 c_vertexes++;
122
123 even = qfalse;
124 }
125 // otherwise we're done with this strip so finish it and start
126 // a new one
127 else
128 {
129 qglEnd();
130
131 qglBegin( GL_TRIANGLE_STRIP );
132 c_begins++;
133
134 element( indexes[i+0] );
135 element( indexes[i+1] );
136 element( indexes[i+2] );
137 c_vertexes += 3;
138
139 even = qfalse;
140 }
141 }
142
143 // cache the last three vertices
144 last[0] = indexes[i+0];
145 last[1] = indexes[i+1];
146 last[2] = indexes[i+2];
147 }
148
149 qglEnd();
150 }
151
152
153
154 /*
155 ==================
156 R_DrawElements
157
158 Optionally performs our own glDrawElements that looks for strip conditions
159 instead of using the single glDrawElements call that may be inefficient
160 without compiled vertex arrays.
161 ==================
162 */
163 void R_DrawElements( int numIndexes, const glIndex_t *indexes ) {
164 int primitives;
165
166 primitives = r_primitives->integer;
167
168 // default is to use triangles if compiled vertex arrays are present
169 if ( primitives == 0 ) {
170 if ( qglLockArraysEXT ) {
171 primitives = 2;
172 } else {
173 primitives = 1;
174 }
175 }
176
177
178 if ( primitives == 2 ) {
179 qglDrawElements( GL_TRIANGLES,
180 numIndexes,
181 GL_INDEX_TYPE,
182 indexes );
183 return;
184 }
185
186 if ( primitives == 1 ) {
187 R_DrawStripElements( numIndexes, indexes, qglArrayElement );
188 return;
189 }
190
191 if ( primitives == 3 ) {
192 R_DrawStripElements( numIndexes, indexes, R_ArrayElementDiscrete );
193 return;
194 }
195
196 // anything else will cause no drawing
197 }
198
199
200 /*
201 =============================================================
202
203 SURFACE SHADERS
204
205 =============================================================
206 */
207
208 shaderCommands_t tess;
209 static qboolean setArraysOnce;
210
211 /*
212 =================
213 R_BindAnimatedImage
214
215 =================
216 */
217 static void R_BindAnimatedImage( textureBundle_t *bundle ) {
218 int64_t index;
219
220 if ( bundle->isVideoMap ) {
221 ri.CIN_RunCinematic(bundle->videoMapHandle);
222 ri.CIN_UploadCinematic(bundle->videoMapHandle);
223 return;
224 }
225
226 if ( bundle->numImageAnimations <= 1 ) {
227 GL_Bind( bundle->image[0] );
228 return;
229 }
230
231 // it is necessary to do this messy calc to make sure animations line up
232 // exactly with waveforms of the same frequency
233 index = tess.shaderTime * bundle->imageAnimationSpeed * FUNCTABLE_SIZE;
234 index >>= FUNCTABLE_SIZE2;
235
236 if ( index < 0 ) {
237 index = 0; // may happen with shader time offsets
238 }
239
240 // Windows x86 doesn't load renderer DLL with 64 bit modulus
241 //index %= bundle->numImageAnimations;
242 while ( index >= bundle->numImageAnimations ) {
243 index -= bundle->numImageAnimations;
244 }
245
246 GL_Bind( bundle->image[ index ] );
247 }
248
249 /*
250 ================
251 DrawTris
252
253 Draws triangle outlines for debugging
254 ================
255 */
256 static void DrawTris (shaderCommands_t *input) {
257 GL_Bind( tr.whiteImage );
258 qglColor3f (1,1,1);
259
260 GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE );
261 qglDepthRange( 0, 0 );
262
263 qglDisableClientState (GL_COLOR_ARRAY);
264 qglDisableClientState (GL_TEXTURE_COORD_ARRAY);
265
266 qglVertexPointer (3, GL_FLOAT, 16, input->xyz); // padded for SIMD
267
268 if (qglLockArraysEXT) {
269 qglLockArraysEXT(0, input->numVertexes);
270 GLimp_LogComment( "glLockArraysEXT\n" );
271 }
272
273 R_DrawElements( input->numIndexes, input->indexes );
274
275 if (qglUnlockArraysEXT) {
276 qglUnlockArraysEXT();
277 GLimp_LogComment( "glUnlockArraysEXT\n" );
278 }
279 qglDepthRange( 0, 1 );
280 }
281
282
283 /*
284 ================
285 DrawNormals
286
287 Draws vertex normals for debugging
288 ================
289 */
290 static void DrawNormals (shaderCommands_t *input) {
291 int i;
292 vec3_t temp;
293
294 GL_Bind( tr.whiteImage );
295 qglColor3f (1,1,1);
296 qglDepthRange( 0, 0 ); // never occluded
297 GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE );
298
299 qglBegin (GL_LINES);
300 for (i = 0 ; i < input->numVertexes ; i++) {
301 qglVertex3fv (input->xyz[i]);
302 VectorMA (input->xyz[i], 2, input->normal[i], temp);
303 qglVertex3fv (temp);
304 }
305 qglEnd ();
306
307 qglDepthRange( 0, 1 );
308 }
309
310 /*
311 ==============
312 RB_BeginSurface
313
314 We must set some things up before beginning any tesselation,
315 because a surface may be forced to perform a RB_End due
316 to overflow.
317 ==============
318 */
319 void RB_BeginSurface( shader_t *shader, int fogNum ) {
320
321 shader_t *state = (shader->remappedShader) ? shader->remappedShader : shader;
322
323 tess.numIndexes = 0;
324 tess.numVertexes = 0;
325 tess.shader = state;
326 tess.fogNum = fogNum;
327 tess.dlightBits = 0; // will be OR'd in by surface functions
328 tess.xstages = state->stages;
329 tess.numPasses = state->numUnfoggedPasses;
330 tess.currentStageIteratorFunc = state->optimalStageIteratorFunc;
331
332 tess.shaderTime = backEnd.refdef.floatTime - tess.shader->timeOffset;
333 if (tess.shader->clampTime && tess.shaderTime >= tess.shader->clampTime) {
334 tess.shaderTime = tess.shader->clampTime;
335 }
336
337
338 }
339
340 /*
341 ===================
342 DrawMultitextured
343
344 output = t0 * t1 or t0 + t1
345
346 t0 = most upstream according to spec
347 t1 = most downstream according to spec
348 ===================
349 */
350 static void DrawMultitextured( shaderCommands_t *input, int stage ) {
351 shaderStage_t *pStage;
352
353 pStage = tess.xstages[stage];
354
355 GL_State( pStage->stateBits );
356
357 // this is an ugly hack to work around a GeForce driver
358 // bug with multitexture and clip planes
359 if ( backEnd.viewParms.isPortal ) {
360 qglPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
361 }
362
363 //
364 // base
365 //
366 GL_SelectTexture( 0 );
367 qglTexCoordPointer( 2, GL_FLOAT, 0, input->svars.texcoords[0] );
368 R_BindAnimatedImage( &pStage->bundle[0] );
369
370 //
371 // lightmap/secondary pass
372 //
373 GL_SelectTexture( 1 );
374 qglEnable( GL_TEXTURE_2D );
375 qglEnableClientState( GL_TEXTURE_COORD_ARRAY );
376
377 if ( r_lightmap->integer ) {
378 GL_TexEnv( GL_REPLACE );
379 } else {
380 GL_TexEnv( tess.shader->multitextureEnv );
381 }
382
383 qglTexCoordPointer( 2, GL_FLOAT, 0, input->svars.texcoords[1] );
384
385 R_BindAnimatedImage( &pStage->bundle[1] );
386
387 R_DrawElements( input->numIndexes, input->indexes );
388
389 //
390 // disable texturing on TEXTURE1, then select TEXTURE0
391 //
392 //qglDisableClientState( GL_TEXTURE_COORD_ARRAY );
393 qglDisable( GL_TEXTURE_2D );
394
395 GL_SelectTexture( 0 );
396 }
397
398
399
400 /*
401 ===================
402 ProjectDlightTexture
403
404 Perform dynamic lighting with another rendering pass
405 ===================
406 */
407 static void ProjectDlightTexture_scalar( void ) {
408 int i, l;
409 vec3_t origin;
410 float *texCoords;
411 byte *colors;
412 byte clipBits[SHADER_MAX_VERTEXES];
413 float texCoordsArray[SHADER_MAX_VERTEXES][2];
414 byte colorArray[SHADER_MAX_VERTEXES][4];
415 glIndex_t hitIndexes[SHADER_MAX_INDEXES];
416 int numIndexes;
417 float scale;
418 float radius;
419 vec3_t floatColor;
420 float modulate = 0.0f;
421
422 if ( !backEnd.refdef.num_dlights ) {
423 return;
424 }
425
426 for ( l = 0 ; l < backEnd.refdef.num_dlights ; l++ ) {
427 dlight_t *dl;
428
429 if ( !( tess.dlightBits & ( 1 << l ) ) ) {
430 continue; // this surface definitely doesn't have any of this light
431 }
432 texCoords = texCoordsArray[0];
433 colors = colorArray[0];
434
435 dl = &backEnd.refdef.dlights[l];
436 VectorCopy( dl->transformed, origin );
437 radius = dl->radius;
438 scale = 1.0f / radius;
439
440 if(r_greyscale->integer)
441 {
442 float luminance;
443
444 luminance = LUMA(dl->color[0], dl->color[1], dl->color[2]) * 255.0f;
445 floatColor[0] = floatColor[1] = floatColor[2] = luminance;
446 }
447 else if(r_greyscale->value)
448 {
449 float luminance;
450
451 luminance = LUMA(dl->color[0], dl->color[1], dl->color[2]) * 255.0f;
452 floatColor[0] = LERP(dl->color[0] * 255.0f, luminance, r_greyscale->value);
453 floatColor[1] = LERP(dl->color[1] * 255.0f, luminance, r_greyscale->value);
454 floatColor[2] = LERP(dl->color[2] * 255.0f, luminance, r_greyscale->value);
455 }
456 else
457 {
458 floatColor[0] = dl->color[0] * 255.0f;
459 floatColor[1] = dl->color[1] * 255.0f;
460 floatColor[2] = dl->color[2] * 255.0f;
461 }
462
463 for ( i = 0 ; i < tess.numVertexes ; i++, texCoords += 2, colors += 4 ) {
464 int clip = 0;
465 vec3_t dist;
466
467 VectorSubtract( origin, tess.xyz[i], dist );
468
469 backEnd.pc.c_dlightVertexes++;
470
471 texCoords[0] = 0.5f + dist[0] * scale;
472 texCoords[1] = 0.5f + dist[1] * scale;
473
474 if( !r_dlightBacks->integer &&
475 // dist . tess.normal[i]
476 ( dist[0] * tess.normal[i][0] +
477 dist[1] * tess.normal[i][1] +
478 dist[2] * tess.normal[i][2] ) < 0.0f ) {
479 clip = 63;
480 } else {
481 if ( texCoords[0] < 0.0f ) {
482 clip |= 1;
483 } else if ( texCoords[0] > 1.0f ) {
484 clip |= 2;
485 }
486 if ( texCoords[1] < 0.0f ) {
487 clip |= 4;
488 } else if ( texCoords[1] > 1.0f ) {
489 clip |= 8;
490 }
491 texCoords[0] = texCoords[0];
492 texCoords[1] = texCoords[1];
493
494 // modulate the strength based on the height and color
495 if ( dist[2] > radius ) {
496 clip |= 16;
497 modulate = 0.0f;
498 } else if ( dist[2] < -radius ) {
499 clip |= 32;
500 modulate = 0.0f;
501 } else {
502 dist[2] = Q_fabs(dist[2]);
503 if ( dist[2] < radius * 0.5f ) {
504 modulate = 1.0f;
505 } else {
506 modulate = 2.0f * (radius - dist[2]) * scale;
507 }
508 }
509 }
510 clipBits[i] = clip;
511 colors[0] = ri.ftol(floatColor[0] * modulate);
512 colors[1] = ri.ftol(floatColor[1] * modulate);
513 colors[2] = ri.ftol(floatColor[2] * modulate);
514 colors[3] = 255;
515 }
516
517 // build a list of triangles that need light
518 numIndexes = 0;
519 for ( i = 0 ; i < tess.numIndexes ; i += 3 ) {
520 int a, b, c;
521
522 a = tess.indexes[i];
523 b = tess.indexes[i+1];
524 c = tess.indexes[i+2];
525 if ( clipBits[a] & clipBits[b] & clipBits[c] ) {
526 continue; // not lighted
527 }
528 hitIndexes[numIndexes] = a;
529 hitIndexes[numIndexes+1] = b;
530 hitIndexes[numIndexes+2] = c;
531 numIndexes += 3;
532 }
533
534 if ( !numIndexes ) {
535 continue;
536 }
537
538 qglEnableClientState( GL_TEXTURE_COORD_ARRAY );
539 qglTexCoordPointer( 2, GL_FLOAT, 0, texCoordsArray[0] );
540
541 qglEnableClientState( GL_COLOR_ARRAY );
542 qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorArray );
543
544 GL_Bind( tr.dlightImage );
545 // include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
546 // where they aren't rendered
547 if ( dl->additive ) {
548 GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
549 }
550 else {
551 GL_State( GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
552 }
553 R_DrawElements( numIndexes, hitIndexes );
554 backEnd.pc.c_totalIndexes += numIndexes;
555 backEnd.pc.c_dlightIndexes += numIndexes;
556 }
557 }
558
559 static void ProjectDlightTexture( void ) {
560 #if idppc_altivec
561 if (com_altivec->integer) {
562 // must be in a separate translation unit or G3 systems will crash.
563 ProjectDlightTexture_altivec();
564 return;
565 }
566 #endif
567 ProjectDlightTexture_scalar();
568 }
569
570
571 /*
572 ===================
573 RB_FogPass
574
575 Blends a fog texture on top of everything else
576 ===================
577 */
578 static void RB_FogPass( void ) {
579 fog_t *fog;
580 int i;
581
582 qglEnableClientState( GL_COLOR_ARRAY );
583 qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, tess.svars.colors );
584
585 qglEnableClientState( GL_TEXTURE_COORD_ARRAY);
586 qglTexCoordPointer( 2, GL_FLOAT, 0, tess.svars.texcoords[0] );
587
588 fog = tr.world->fogs + tess.fogNum;
589
590 for ( i = 0; i < tess.numVertexes; i++ ) {
591 * ( int * )&tess.svars.colors[i] = fog->colorInt;
592 }
593
594 RB_CalcFogTexCoords( ( float * ) tess.svars.texcoords[0] );
595
596 GL_Bind( tr.fogImage );
597
598 if ( tess.shader->fogPass == FP_EQUAL ) {
599 GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA | GLS_DEPTHFUNC_EQUAL );
600 } else {
601 GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA );
602 }
603
604 R_DrawElements( tess.numIndexes, tess.indexes );
605 }
606
607 /*
608 ===============
609 ComputeColors
610 ===============
611 */
612 static void ComputeColors( shaderStage_t *pStage )
613 {
614 int i;
615
616 //
617 // rgbGen
618 //
619 switch ( pStage->rgbGen )
620 {
621 case CGEN_IDENTITY:
622 Com_Memset( tess.svars.colors, 0xff, tess.numVertexes * 4 );
623 break;
624 default:
625 case CGEN_IDENTITY_LIGHTING:
626 Com_Memset( tess.svars.colors, tr.identityLightByte, tess.numVertexes * 4 );
627 break;
628 case CGEN_LIGHTING_DIFFUSE:
629 RB_CalcDiffuseColor( ( unsigned char * ) tess.svars.colors );
630 break;
631 case CGEN_EXACT_VERTEX:
632 Com_Memcpy( tess.svars.colors, tess.vertexColors, tess.numVertexes * sizeof( tess.vertexColors[0] ) );
633 break;
634 case CGEN_CONST:
635 for ( i = 0; i < tess.numVertexes; i++ ) {
636 *(int *)tess.svars.colors[i] = *(int *)pStage->constantColor;
637 }
638 break;
639 case CGEN_VERTEX:
640 if ( tr.identityLight == 1 )
641 {
642 Com_Memcpy( tess.svars.colors, tess.vertexColors, tess.numVertexes * sizeof( tess.vertexColors[0] ) );
643 }
644 else
645 {
646 for ( i = 0; i < tess.numVertexes; i++ )
647 {
648 tess.svars.colors[i][0] = tess.vertexColors[i][0] * tr.identityLight;
649 tess.svars.colors[i][1] = tess.vertexColors[i][1] * tr.identityLight;
650 tess.svars.colors[i][2] = tess.vertexColors[i][2] * tr.identityLight;
651 tess.svars.colors[i][3] = tess.vertexColors[i][3];
652 }
653 }
654 break;
655 case CGEN_ONE_MINUS_VERTEX:
656 if ( tr.identityLight == 1 )
657 {
658 for ( i = 0; i < tess.numVertexes; i++ )
659 {
660 tess.svars.colors[i][0] = 255 - tess.vertexColors[i][0];
661 tess.svars.colors[i][1] = 255 - tess.vertexColors[i][1];
662 tess.svars.colors[i][2] = 255 - tess.vertexColors[i][2];
663 }
664 }
665 else
666 {
667 for ( i = 0; i < tess.numVertexes; i++ )
668 {
669 tess.svars.colors[i][0] = ( 255 - tess.vertexColors[i][0] ) * tr.identityLight;
670 tess.svars.colors[i][1] = ( 255 - tess.vertexColors[i][1] ) * tr.identityLight;
671 tess.svars.colors[i][2] = ( 255 - tess.vertexColors[i][2] ) * tr.identityLight;
672 }
673 }
674 break;
675 case CGEN_FOG:
676 {
677 fog_t *fog;
678
679 fog = tr.world->fogs + tess.fogNum;
680
681 for ( i = 0; i < tess.numVertexes; i++ ) {
682 * ( int * )&tess.svars.colors[i] = fog->colorInt;
683 }
684 }
685 break;
686 case CGEN_WAVEFORM:
687 RB_CalcWaveColor( &pStage->rgbWave, ( unsigned char * ) tess.svars.colors );
688 break;
689 case CGEN_ENTITY:
690 RB_CalcColorFromEntity( ( unsigned char * ) tess.svars.colors );
691 break;
692 case CGEN_ONE_MINUS_ENTITY:
693 RB_CalcColorFromOneMinusEntity( ( unsigned char * ) tess.svars.colors );
694 break;
695 }
696
697 //
698 // alphaGen
699 //
700 switch ( pStage->alphaGen )
701 {
702 case AGEN_SKIP:
703 break;
704 case AGEN_IDENTITY:
705 if ( pStage->rgbGen != CGEN_IDENTITY ) {
706 if ( ( pStage->rgbGen == CGEN_VERTEX && tr.identityLight != 1 ) ||
707 pStage->rgbGen != CGEN_VERTEX ) {
708 for ( i = 0; i < tess.numVertexes; i++ ) {
709 tess.svars.colors[i][3] = 0xff;
710 }
711 }
712 }
713 break;
714 case AGEN_CONST:
715 if ( pStage->rgbGen != CGEN_CONST ) {
716 for ( i = 0; i < tess.numVertexes; i++ ) {
717 tess.svars.colors[i][3] = pStage->constantColor[3];
718 }
719 }
720 break;
721 case AGEN_WAVEFORM:
722 RB_CalcWaveAlpha( &pStage->alphaWave, ( unsigned char * ) tess.svars.colors );
723 break;
724 case AGEN_LIGHTING_SPECULAR:
725 RB_CalcSpecularAlpha( ( unsigned char * ) tess.svars.colors );
726 break;
727 case AGEN_ENTITY:
728 RB_CalcAlphaFromEntity( ( unsigned char * ) tess.svars.colors );
729 break;
730 case AGEN_ONE_MINUS_ENTITY:
731 RB_CalcAlphaFromOneMinusEntity( ( unsigned char * ) tess.svars.colors );
732 break;
733 case AGEN_VERTEX:
734 if ( pStage->rgbGen != CGEN_VERTEX ) {
735 for ( i = 0; i < tess.numVertexes; i++ ) {
736 tess.svars.colors[i][3] = tess.vertexColors[i][3];
737 }
738 }
739 break;
740 case AGEN_ONE_MINUS_VERTEX:
741 for ( i = 0; i < tess.numVertexes; i++ )
742 {
743 tess.svars.colors[i][3] = 255 - tess.vertexColors[i][3];
744 }
745 break;
746 case AGEN_PORTAL:
747 {
748 unsigned char alpha;
749
750 for ( i = 0; i < tess.numVertexes; i++ )
751 {
752 float len;
753 vec3_t v;
754
755 VectorSubtract( tess.xyz[i], backEnd.viewParms.or.origin, v );
756 len = VectorLength( v );
757
758 len /= tess.shader->portalRange;
759
760 if ( len < 0 )
761 {
762 alpha = 0;
763 }
764 else if ( len > 1 )
765 {
766 alpha = 0xff;
767 }
768 else
769 {
770 alpha = len * 0xff;
771 }
772
773 tess.svars.colors[i][3] = alpha;
774 }
775 }
776 break;
777 }
778
779 //
780 // fog adjustment for colors to fade out as fog increases
781 //
782 if ( tess.fogNum )
783 {
784 switch ( pStage->adjustColorsForFog )
785 {
786 case ACFF_MODULATE_RGB:
787 RB_CalcModulateColorsByFog( ( unsigned char * ) tess.svars.colors );
788 break;
789 case ACFF_MODULATE_ALPHA:
790 RB_CalcModulateAlphasByFog( ( unsigned char * ) tess.svars.colors );
791 break;
792 case ACFF_MODULATE_RGBA:
793 RB_CalcModulateRGBAsByFog( ( unsigned char * ) tess.svars.colors );
794 break;
795 case ACFF_NONE:
796 break;
797 }
798 }
799
800 // if in greyscale rendering mode turn all color values into greyscale.
801 if(r_greyscale->integer)
802 {
803 int scale;
804 for(i = 0; i < tess.numVertexes; i++)
805 {
806 scale = LUMA(tess.svars.colors[i][0], tess.svars.colors[i][1], tess.svars.colors[i][2]);
807 tess.svars.colors[i][0] = tess.svars.colors[i][1] = tess.svars.colors[i][2] = scale;
808 }
809 }
810 else if(r_greyscale->value)
811 {
812 float scale;
813
814 for(i = 0; i < tess.numVertexes; i++)
815 {
816 scale = LUMA(tess.svars.colors[i][0], tess.svars.colors[i][1], tess.svars.colors[i][2]);
817 tess.svars.colors[i][0] = LERP(tess.svars.colors[i][0], scale, r_greyscale->value);
818 tess.svars.colors[i][1] = LERP(tess.svars.colors[i][1], scale, r_greyscale->value);
819 tess.svars.colors[i][2] = LERP(tess.svars.colors[i][2], scale, r_greyscale->value);
820 }
821 }
822 }
823
824 /*
825 ===============
826 ComputeTexCoords
827 ===============
828 */
829 static void ComputeTexCoords( shaderStage_t *pStage ) {
830 int i;
831 int b;
832
833 for ( b = 0; b < NUM_TEXTURE_BUNDLES; b++ ) {
834 int tm;
835
836 //
837 // generate the texture coordinates
838 //
839 switch ( pStage->bundle[b].tcGen )
840 {
841 case TCGEN_IDENTITY:
842 Com_Memset( tess.svars.texcoords[b], 0, sizeof( float ) * 2 * tess.numVertexes );
843 break;
844 case TCGEN_TEXTURE:
845 for ( i = 0 ; i < tess.numVertexes ; i++ ) {
846 tess.svars.texcoords[b][i][0] = tess.texCoords[i][0][0];
847 tess.svars.texcoords[b][i][1] = tess.texCoords[i][0][1];
848 }
849 break;
850 case TCGEN_LIGHTMAP:
851 for ( i = 0 ; i < tess.numVertexes ; i++ ) {
852 tess.svars.texcoords[b][i][0] = tess.texCoords[i][1][0];
853 tess.svars.texcoords[b][i][1] = tess.texCoords[i][1][1];
854 }
855 break;
856 case TCGEN_VECTOR:
857 for ( i = 0 ; i < tess.numVertexes ; i++ ) {
858 tess.svars.texcoords[b][i][0] = DotProduct( tess.xyz[i], pStage->bundle[b].tcGenVectors[0] );
859 tess.svars.texcoords[b][i][1] = DotProduct( tess.xyz[i], pStage->bundle[b].tcGenVectors[1] );
860 }
861 break;
862 case TCGEN_FOG:
863 RB_CalcFogTexCoords( ( float * ) tess.svars.texcoords[b] );
864 break;
865 case TCGEN_ENVIRONMENT_MAPPED:
866 RB_CalcEnvironmentTexCoords( ( float * ) tess.svars.texcoords[b] );
867 break;
868 case TCGEN_BAD:
869 return;
870 }
871
872 //
873 // alter texture coordinates
874 //
875 for ( tm = 0; tm < pStage->bundle[b].numTexMods ; tm++ ) {
876 switch ( pStage->bundle[b].texMods[tm].type )
877 {
878 case TMOD_NONE:
879 tm = TR_MAX_TEXMODS; // break out of for loop
880 break;
881
882 case TMOD_TURBULENT:
883 RB_CalcTurbulentTexCoords( &pStage->bundle[b].texMods[tm].wave,
884 ( float * ) tess.svars.texcoords[b] );
885 break;
886
887 case TMOD_ENTITY_TRANSLATE:
888 RB_CalcScrollTexCoords( backEnd.currentEntity->e.shaderTexCoord,
889 ( float * ) tess.svars.texcoords[b] );
890 break;
891
892 case TMOD_SCROLL:
893 RB_CalcScrollTexCoords( pStage->bundle[b].texMods[tm].scroll,
894 ( float * ) tess.svars.texcoords[b] );
895 break;
896
897 case TMOD_SCALE:
898 RB_CalcScaleTexCoords( pStage->bundle[b].texMods[tm].scale,
899 ( float * ) tess.svars.texcoords[b] );
900 break;
901
902 case TMOD_STRETCH:
903 RB_CalcStretchTexCoords( &pStage->bundle[b].texMods[tm].wave,
904 ( float * ) tess.svars.texcoords[b] );
905 break;
906
907 case TMOD_TRANSFORM:
908 RB_CalcTransformTexCoords( &pStage->bundle[b].texMods[tm],
909 ( float * ) tess.svars.texcoords[b] );
910 break;
911
912 case TMOD_ROTATE:
913 RB_CalcRotateTexCoords( pStage->bundle[b].texMods[tm].rotateSpeed,
914 ( float * ) tess.svars.texcoords[b] );
915 break;
916
917 default:
918 ri.Error( ERR_DROP, "ERROR: unknown texmod '%d' in shader '%s'", pStage->bundle[b].texMods[tm].type, tess.shader->name );
919 break;
920 }
921 }
922 }
923 }
924
925 /*
926 ** RB_IterateStagesGeneric
927 */
928 static void RB_IterateStagesGeneric( shaderCommands_t *input )
929 {
930 int stage;
931
932 for ( stage = 0; stage < MAX_SHADER_STAGES; stage++ )
933 {
934 shaderStage_t *pStage = tess.xstages[stage];
935
936 if ( !pStage )
937 {
938 break;
939 }
940
941 ComputeColors( pStage );
942 ComputeTexCoords( pStage );
943
944 if ( !setArraysOnce )
945 {
946 qglEnableClientState( GL_COLOR_ARRAY );
947 qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, input->svars.colors );
948 }
949
950 //
951 // do multitexture
952 //
953 if ( pStage->bundle[1].image[0] != 0 )
954 {
955 DrawMultitextured( input, stage );
956 }
957 else
958 {
959 if ( !setArraysOnce )
960 {
961 qglTexCoordPointer( 2, GL_FLOAT, 0, input->svars.texcoords[0] );
962 }
963
964 //
965 // set state
966 //
967 R_BindAnimatedImage( &pStage->bundle[0] );
968
969 GL_State( pStage->stateBits );
970
971 //
972 // draw
973 //
974 R_DrawElements( input->numIndexes, input->indexes );
975 }
976 // allow skipping out to show just lightmaps during development
977 if ( r_lightmap->integer && ( pStage->bundle[0].isLightmap || pStage->bundle[1].isLightmap ) )
978 {
979 break;
980 }
981 }
982 }
983
984
985 /*
986 ** RB_StageIteratorGeneric
987 */
988 void RB_StageIteratorGeneric( void )
989 {
990 shaderCommands_t *input;
991 shader_t *shader;
992
993 input = &tess;
994 shader = input->shader;
995
996 RB_DeformTessGeometry();
997
998 //
999 // log this call
1000 //
1001 if ( r_logFile->integer )
1002 {
1003 // don't just call LogComment, or we will get
1004 // a call to va() every frame!
1005 GLimp_LogComment( va("--- RB_StageIteratorGeneric( %s ) ---\n", tess.shader->name) );
1006 }
1007
1008 //
1009 // set face culling appropriately
1010 //
1011 GL_Cull( shader->cullType );
1012
1013 // set polygon offset if necessary
1014 if ( shader->polygonOffset )
1015 {
1016 qglEnable( GL_POLYGON_OFFSET_FILL );
1017 qglPolygonOffset( r_offsetFactor->value, r_offsetUnits->value );
1018 }
1019
1020 //
1021 // if there is only a single pass then we can enable color
1022 // and texture arrays before we compile, otherwise we need
1023 // to avoid compiling those arrays since they will change
1024 // during multipass rendering
1025 //
1026 if ( tess.numPasses > 1 || shader->multitextureEnv )
1027 {
1028 setArraysOnce = qfalse;
1029 qglDisableClientState (GL_COLOR_ARRAY);
1030 qglDisableClientState (GL_TEXTURE_COORD_ARRAY);
1031 }
1032 else
1033 {
1034 setArraysOnce = qtrue;
1035
1036 qglEnableClientState( GL_COLOR_ARRAY);
1037 qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, tess.svars.colors );
1038
1039 qglEnableClientState( GL_TEXTURE_COORD_ARRAY);
1040 qglTexCoordPointer( 2, GL_FLOAT, 0, tess.svars.texcoords[0] );
1041 }
1042
1043 //
1044 // lock XYZ
1045 //
1046 qglVertexPointer (3, GL_FLOAT, 16, input->xyz); // padded for SIMD
1047 if (qglLockArraysEXT)
1048 {
1049 qglLockArraysEXT(0, input->numVertexes);
1050 GLimp_LogComment( "glLockArraysEXT\n" );
1051 }
1052
1053 //
1054 // enable color and texcoord arrays after the lock if necessary
1055 //
1056 if ( !setArraysOnce )
1057 {
1058 qglEnableClientState( GL_TEXTURE_COORD_ARRAY );
1059 qglEnableClientState( GL_COLOR_ARRAY );
1060 }
1061
1062 //
1063 // call shader function
1064 //
1065 RB_IterateStagesGeneric( input );
1066
1067 //
1068 // now do any dynamic lighting needed
1069 //
1070 if ( tess.dlightBits && tess.shader->sort <= SS_OPAQUE
1071 && !(tess.shader->surfaceFlags & (SURF_NODLIGHT | SURF_SKY) ) ) {
1072 ProjectDlightTexture();
1073 }
1074
1075 //
1076 // now do fog
1077 //
1078 if ( tess.fogNum && tess.shader->fogPass ) {
1079 RB_FogPass();
1080 }
1081
1082 //
1083 // unlock arrays
1084 //
1085 if (qglUnlockArraysEXT)
1086 {
1087 qglUnlockArraysEXT();
1088 GLimp_LogComment( "glUnlockArraysEXT\n" );
1089 }
1090
1091 //
1092 // reset polygon offset
1093 //
1094 if ( shader->polygonOffset )
1095 {
1096 qglDisable( GL_POLYGON_OFFSET_FILL );
1097 }
1098 }
1099
1100
1101 /*
1102 ** RB_StageIteratorVertexLitTexture
1103 */
1104 void RB_StageIteratorVertexLitTexture( void )
1105 {
1106 shaderCommands_t *input;
1107 shader_t *shader;
1108
1109 input = &tess;
1110 shader = input->shader;
1111
1112 //
1113 // compute colors
1114 //
1115 RB_CalcDiffuseColor( ( unsigned char * ) tess.svars.colors );
1116
1117 //
1118 // log this call
1119 //
1120 if ( r_logFile->integer )
1121 {
1122 // don't just call LogComment, or we will get
1123 // a call to va() every frame!
1124 GLimp_LogComment( va("--- RB_StageIteratorVertexLitTexturedUnfogged( %s ) ---\n", tess.shader->name) );
1125 }
1126
1127 //
1128 // set face culling appropriately
1129 //
1130 GL_Cull( shader->cullType );
1131
1132 //
1133 // set arrays and lock
1134 //
1135 qglEnableClientState( GL_COLOR_ARRAY);
1136 qglEnableClientState( GL_TEXTURE_COORD_ARRAY);
1137
1138 qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, tess.svars.colors );
1139 qglTexCoordPointer( 2, GL_FLOAT, 16, tess.texCoords[0][0] );
1140 qglVertexPointer (3, GL_FLOAT, 16, input->xyz);
1141
1142 if ( qglLockArraysEXT )
1143 {
1144 qglLockArraysEXT(0, input->numVertexes);
1145 GLimp_LogComment( "glLockArraysEXT\n" );
1146 }
1147
1148 //
1149 // call special shade routine
1150 //
1151 R_BindAnimatedImage( &tess.xstages[0]->bundle[0] );
1152 GL_State( tess.xstages[0]->stateBits );
1153 R_DrawElements( input->numIndexes, input->indexes );
1154
1155 //
1156 // now do any dynamic lighting needed
1157 //
1158 if ( tess.dlightBits && tess.shader->sort <= SS_OPAQUE ) {
1159 ProjectDlightTexture();
1160 }
1161
1162 //
1163 // now do fog
1164 //
1165 if ( tess.fogNum && tess.shader->fogPass ) {
1166 RB_FogPass();
1167 }
1168
1169 //
1170 // unlock arrays
1171 //
1172 if (qglUnlockArraysEXT)
1173 {
1174 qglUnlockArraysEXT();
1175 GLimp_LogComment( "glUnlockArraysEXT\n" );
1176 }
1177 }
1178
1179 //define REPLACE_MODE
1180
1181 void RB_StageIteratorLightmappedMultitexture( void ) {
1182 shaderCommands_t *input;
1183 shader_t *shader;
1184
1185 input = &tess;
1186 shader = input->shader;
1187
1188 //
1189 // log this call
1190 //
1191 if ( r_logFile->integer ) {
1192 // don't just call LogComment, or we will get
1193 // a call to va() every frame!
1194 GLimp_LogComment( va("--- RB_StageIteratorLightmappedMultitexture( %s ) ---\n", tess.shader->name) );
1195 }
1196
1197 //
1198 // set face culling appropriately
1199 //
1200 GL_Cull( shader->cullType );
1201
1202 //
1203 // set color, pointers, and lock
1204 //
1205 GL_State( GLS_DEFAULT );
1206 qglVertexPointer( 3, GL_FLOAT, 16, input->xyz );
1207
1208 #ifdef REPLACE_MODE
1209 qglDisableClientState( GL_COLOR_ARRAY );
1210 qglColor3f( 1, 1, 1 );
1211 qglShadeModel( GL_FLAT );
1212 #else
1213 qglEnableClientState( GL_COLOR_ARRAY );
1214 qglColorPointer( 4, GL_UNSIGNED_BYTE, 0, tess.constantColor255 );
1215 #endif
1216
1217 //
1218 // select base stage
1219 //
1220 GL_SelectTexture( 0 );
1221
1222 qglEnableClientState( GL_TEXTURE_COORD_ARRAY );
1223 R_BindAnimatedImage( &tess.xstages[0]->bundle[0] );
1224 qglTexCoordPointer( 2, GL_FLOAT, 16, tess.texCoords[0][0] );
1225
1226 //
1227 // configure second stage
1228 //
1229 GL_SelectTexture( 1 );
1230 qglEnable( GL_TEXTURE_2D );
1231 if ( r_lightmap->integer ) {
1232 GL_TexEnv( GL_REPLACE );
1233 } else {
1234 GL_TexEnv( GL_MODULATE );
1235 }
1236 R_BindAnimatedImage( &tess.xstages[0]->bundle[1] );
1237 qglEnableClientState( GL_TEXTURE_COORD_ARRAY );
1238 qglTexCoordPointer( 2, GL_FLOAT, 16, tess.texCoords[0][1] );
1239
1240 //
1241 // lock arrays
1242 //
1243 if ( qglLockArraysEXT ) {
1244 qglLockArraysEXT(0, input->numVertexes);
1245 GLimp_LogComment( "glLockArraysEXT\n" );
1246 }
1247
1248 R_DrawElements( input->numIndexes, input->indexes );
1249
1250 //
1251 // disable texturing on TEXTURE1, then select TEXTURE0
1252 //
1253 qglDisable( GL_TEXTURE_2D );
1254 qglDisableClientState( GL_TEXTURE_COORD_ARRAY );
1255
1256 GL_SelectTexture( 0 );
1257 #ifdef REPLACE_MODE
1258 GL_TexEnv( GL_MODULATE );
1259 qglShadeModel( GL_SMOOTH );
1260 #endif
1261
1262 //
1263 // now do any dynamic lighting needed
1264 //
1265 if ( tess.dlightBits && tess.shader->sort <= SS_OPAQUE ) {
1266 ProjectDlightTexture();
1267 }
1268
1269 //
1270 // now do fog
1271 //
1272 if ( tess.fogNum && tess.shader->fogPass ) {
1273 RB_FogPass();
1274 }
1275
1276 //
1277 // unlock arrays
1278 //
1279 if ( qglUnlockArraysEXT ) {
1280 qglUnlockArraysEXT();
1281 GLimp_LogComment( "glUnlockArraysEXT\n" );
1282 }
1283 }
1284
1285 /*
1286 ** RB_EndSurface
1287 */
1288 void RB_EndSurface( void ) {
1289 shaderCommands_t *input;
1290
1291 input = &tess;
1292
1293 if (input->numIndexes == 0) {
1294 return;
1295 }
1296
1297 if (input->indexes[SHADER_MAX_INDEXES-1] != 0) {
1298 ri.Error (ERR_DROP, "RB_EndSurface() - SHADER_MAX_INDEXES hit");
1299 }
1300 if (input->xyz[SHADER_MAX_VERTEXES-1][0] != 0) {
1301 ri.Error (ERR_DROP, "RB_EndSurface() - SHADER_MAX_VERTEXES hit");
1302 }
1303
1304 if ( tess.shader == tr.shadowShader ) {
1305 RB_ShadowTessEnd();
1306 return;
1307 }
1308
1309 // for debugging of sort order issues, stop rendering after a given sort value
1310 if ( r_debugSort->integer && r_debugSort->integer < tess.shader->sort ) {
1311 return;
1312 }
1313
1314 //
1315 // update performance counters
1316 //
1317 backEnd.pc.c_shaders++;
1318 backEnd.pc.c_vertexes += tess.numVertexes;
1319 backEnd.pc.c_indexes += tess.numIndexes;
1320 backEnd.pc.c_totalIndexes += tess.numIndexes * tess.numPasses;
1321
1322 //
1323 // call off to shader specific tess end function
1324 //
1325 tess.currentStageIteratorFunc();
1326
1327 //
1328 // draw debugging stuff
1329 //
1330 if ( r_showtris->integer ) {
1331 DrawTris (input);
1332 }
1333 if ( r_shownormals->integer ) {
1334 DrawNormals (input);
1335 }
1336 // clear shader so we can tell we don't have any unclosed surfaces
1337 tess.numIndexes = 0;
1338
1339 GLimp_LogComment( "----------\n" );
1340 }
1341