1 /*
2 ===========================================================================
3 
4 Return to Castle Wolfenstein multiplayer GPL Source Code
5 Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Return to Castle Wolfenstein multiplayer GPL Source Code (“RTCW MP Source Code”).
8 
9 RTCW MP Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 RTCW MP Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with RTCW MP Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the RTCW MP Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the RTCW MP Source Code.  If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 // tr_shade.c
29 
30 #include "tr_local.h"
31 
32 /*
33 
34   THIS ENTIRE FILE IS BACK END
35 
36   This file deals with applying shaders to surface data in the tess struct.
37 */
38 
39 
40 /*
41 ==================
42 R_DrawElements
43 
44 ==================
45 */
46 
R_DrawElements(int numIndexes,int firstIndex)47 void R_DrawElements( int numIndexes, int firstIndex )
48 {
49 	qglDrawElements(GL_TRIANGLES, numIndexes, GL_INDEX_TYPE, BUFFER_OFFSET(firstIndex * sizeof(glIndex_t)));
50 }
51 
52 
53 /*
54 =============================================================
55 
56 SURFACE SHADERS
57 
58 =============================================================
59 */
60 
61 shaderCommands_t	tess;
62 
63 
64 /*
65 =================
66 R_BindAnimatedImageToTMU
67 
68 =================
69 */
R_BindAnimatedImageToTMU(textureBundle_t * bundle,int tmu)70 static void R_BindAnimatedImageToTMU( textureBundle_t *bundle, int tmu ) {
71 	int64_t index;
72 
73 	if ( bundle->isVideoMap ) {
74 		ri.CIN_RunCinematic(bundle->videoMapHandle);
75 		ri.CIN_UploadCinematic(bundle->videoMapHandle);
76 		GL_BindToTMU(tr.scratchImage[bundle->videoMapHandle], tmu);
77 		return;
78 	}
79 
80 	if ( bundle->numImageAnimations <= 1 ) {
81 		if ( bundle->isLightmap && ( backEnd.refdef.rdflags & RDF_SNOOPERVIEW ) ) {
82 			GL_BindToTMU( tr.whiteImage, tmu );
83 		} else {
84 			GL_BindToTMU( bundle->image[0], tmu);
85 		}
86 		return;
87 	}
88 
89 	// it is necessary to do this messy calc to make sure animations line up
90 	// exactly with waveforms of the same frequency
91 	index = tess.shaderTime * bundle->imageAnimationSpeed * FUNCTABLE_SIZE;
92 	index >>= FUNCTABLE_SIZE2;
93 
94 	if ( index < 0 ) {
95 		index = 0;	// may happen with shader time offsets
96 	}
97 
98 	// Windows x86 doesn't load renderer DLL with 64 bit modulus
99 	//index %= bundle->numImageAnimations;
100 	while ( index >= bundle->numImageAnimations ) {
101 		index -= bundle->numImageAnimations;
102 	}
103 
104 	if ( bundle->isLightmap && ( backEnd.refdef.rdflags & RDF_SNOOPERVIEW ) ) {
105 		GL_BindToTMU( tr.whiteImage, tmu );
106 	} else {
107 		GL_BindToTMU( bundle->image[ index ], tmu );
108 	}
109 }
110 
111 
112 /*
113 ================
114 DrawTris
115 
116 Draws triangle outlines for debugging
117 ================
118 */
DrawTris(shaderCommands_t * input)119 static void DrawTris (shaderCommands_t *input) {
120 	GL_BindToTMU( tr.whiteImage, TB_COLORMAP );
121 
122 	GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE );
123 	qglDepthRange( 0, 0 );
124 
125 	{
126 		shaderProgram_t *sp = &tr.textureColorShader;
127 		vec4_t color;
128 
129 		GLSL_BindProgram(sp);
130 
131 		GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
132 		VectorSet4(color, 1, 1, 1, 1);
133 		GLSL_SetUniformVec4(sp, UNIFORM_COLOR, color);
134 		GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
135 
136 		R_DrawElements(input->numIndexes, input->firstIndex);
137 	}
138 
139 	qglDepthRange( 0, 1 );
140 }
141 
142 
143 /*
144 ================
145 DrawNormals
146 
147 Draws vertex normals for debugging
148 ================
149 */
DrawNormals(shaderCommands_t * input)150 static void DrawNormals (shaderCommands_t *input) {
151 	//FIXME: implement this
152 }
153 
154 
155 /*
156 ==============
157 RB_BeginSurface
158 
159 We must set some things up before beginning any tesselation,
160 because a surface may be forced to perform a RB_End due
161 to overflow.
162 ==============
163 */
RB_BeginSurface(shader_t * shader,int fogNum,int cubemapIndex)164 void RB_BeginSurface( shader_t *shader, int fogNum, int cubemapIndex ) {
165 
166 	shader_t *state = (shader->remappedShader) ? shader->remappedShader : shader;
167 
168 	tess.numIndexes = 0;
169 	tess.firstIndex = 0;
170 	tess.numVertexes = 0;
171 	tess.shader = state;
172 	tess.fogNum = fogNum;
173 	tess.cubemapIndex = cubemapIndex;
174 	tess.dlightBits = 0;		// will be OR'd in by surface functions
175 	tess.pshadowBits = 0;       // will be OR'd in by surface functions
176 	tess.xstages = state->stages;
177 	tess.numPasses = state->numUnfoggedPasses;
178 	tess.currentStageIteratorFunc = state->optimalStageIteratorFunc;
179 	tess.useInternalVao = qtrue;
180 	tess.useCacheVao = qfalse;
181 
182 	tess.shaderTime = backEnd.refdef.floatTime - tess.shader->timeOffset;
183 	if (tess.shader->clampTime && tess.shaderTime >= tess.shader->clampTime) {
184 		tess.shaderTime = tess.shader->clampTime;
185 	}
186 
187 	if (backEnd.viewParms.flags & VPF_SHADOWMAP)
188 	{
189 		tess.currentStageIteratorFunc = RB_StageIteratorGeneric;
190 	}
191 }
192 
193 
194 extern float EvalWaveForm( const waveForm_t *wf );
195 extern float EvalWaveFormClamped( const waveForm_t *wf );
196 
197 
ComputeTexMods(shaderStage_t * pStage,int bundleNum,float * outMatrix,float * outOffTurb)198 static void ComputeTexMods( shaderStage_t *pStage, int bundleNum, float *outMatrix, float *outOffTurb)
199 {
200 	int tm;
201 	float matrix[6], currentmatrix[6];
202 	textureBundle_t *bundle = &pStage->bundle[bundleNum];
203 
204 	matrix[0] = 1.0f; matrix[2] = 0.0f; matrix[4] = 0.0f;
205 	matrix[1] = 0.0f; matrix[3] = 1.0f; matrix[5] = 0.0f;
206 
207 	currentmatrix[0] = 1.0f; currentmatrix[2] = 0.0f; currentmatrix[4] = 0.0f;
208 	currentmatrix[1] = 0.0f; currentmatrix[3] = 1.0f; currentmatrix[5] = 0.0f;
209 
210 	outMatrix[0] = 1.0f; outMatrix[2] = 0.0f;
211 	outMatrix[1] = 0.0f; outMatrix[3] = 1.0f;
212 
213 	outOffTurb[0] = 0.0f; outOffTurb[1] = 0.0f; outOffTurb[2] = 0.0f; outOffTurb[3] = 0.0f;
214 
215 	for ( tm = 0; tm < bundle->numTexMods ; tm++ ) {
216 		switch ( bundle->texMods[tm].type )
217 		{
218 
219 		case TMOD_NONE:
220 			tm = TR_MAX_TEXMODS;		// break out of for loop
221 			break;
222 
223 		case TMOD_TURBULENT:
224 			RB_CalcTurbulentFactors(&bundle->texMods[tm].wave, &outOffTurb[2], &outOffTurb[3]);
225 			break;
226 
227 		case TMOD_ENTITY_TRANSLATE:
228 			RB_CalcScrollTexMatrix( backEnd.currentEntity->e.shaderTexCoord, matrix );
229 			break;
230 
231 		case TMOD_SCROLL:
232 			RB_CalcScrollTexMatrix( bundle->texMods[tm].scroll,
233 									 matrix );
234 			break;
235 
236 		case TMOD_SCALE:
237 			RB_CalcScaleTexMatrix( bundle->texMods[tm].scale,
238 								  matrix );
239 			break;
240 
241 		case TMOD_STRETCH:
242 			RB_CalcStretchTexMatrix( &bundle->texMods[tm].wave,
243 								   matrix );
244 			break;
245 
246 		case TMOD_TRANSFORM:
247 			RB_CalcTransformTexMatrix( &bundle->texMods[tm],
248 									 matrix );
249 			break;
250 
251 		case TMOD_ROTATE:
252 			RB_CalcRotateTexMatrix( bundle->texMods[tm].rotateSpeed,
253 									matrix );
254 			break;
255 
256 		default:
257 			ri.Error( ERR_DROP, "ERROR: unknown texmod '%d' in shader '%s'", bundle->texMods[tm].type, tess.shader->name );
258 			break;
259 		}
260 
261 		switch ( bundle->texMods[tm].type )
262 		{
263 		case TMOD_NONE:
264 		case TMOD_TURBULENT:
265 		default:
266 			break;
267 
268 		case TMOD_ENTITY_TRANSLATE:
269 		case TMOD_SCROLL:
270 		case TMOD_SCALE:
271 		case TMOD_STRETCH:
272 		case TMOD_TRANSFORM:
273 		case TMOD_ROTATE:
274 			outMatrix[0] = matrix[0] * currentmatrix[0] + matrix[2] * currentmatrix[1];
275 			outMatrix[1] = matrix[1] * currentmatrix[0] + matrix[3] * currentmatrix[1];
276 
277 			outMatrix[2] = matrix[0] * currentmatrix[2] + matrix[2] * currentmatrix[3];
278 			outMatrix[3] = matrix[1] * currentmatrix[2] + matrix[3] * currentmatrix[3];
279 
280 			outOffTurb[0] = matrix[0] * currentmatrix[4] + matrix[2] * currentmatrix[5] + matrix[4];
281 			outOffTurb[1] = matrix[1] * currentmatrix[4] + matrix[3] * currentmatrix[5] + matrix[5];
282 
283 			currentmatrix[0] = outMatrix[0];
284 			currentmatrix[1] = outMatrix[1];
285 			currentmatrix[2] = outMatrix[2];
286 			currentmatrix[3] = outMatrix[3];
287 			currentmatrix[4] = outOffTurb[0];
288 			currentmatrix[5] = outOffTurb[1];
289 			break;
290 		}
291 	}
292 }
293 
294 
ComputeDeformValues(int * deformGen,vec5_t deformParams)295 static void ComputeDeformValues(int *deformGen, vec5_t deformParams)
296 {
297 	// u_DeformGen
298 	*deformGen = DGEN_NONE;
299 	if(!ShaderRequiresCPUDeforms(tess.shader))
300 	{
301 		deformStage_t  *ds;
302 
303 		// only support the first one
304 		ds = &tess.shader->deforms[0];
305 
306 		switch (ds->deformation)
307 		{
308 			case DEFORM_WAVE:
309 				*deformGen = ds->deformationWave.func;
310 
311 				deformParams[0] = ds->deformationWave.base;
312 				deformParams[1] = ds->deformationWave.amplitude;
313 				deformParams[2] = ds->deformationWave.phase;
314 				deformParams[3] = ds->deformationWave.frequency;
315 				deformParams[4] = ds->deformationSpread;
316 				break;
317 
318 			case DEFORM_BULGE:
319 				*deformGen = DGEN_BULGE;
320 
321 				deformParams[0] = 0;
322 				deformParams[1] = ds->bulgeHeight; // amplitude
323 				deformParams[2] = ds->bulgeWidth;  // phase
324 				deformParams[3] = ds->bulgeSpeed;  // frequency
325 				deformParams[4] = 0;
326 				break;
327 
328 			default:
329 				break;
330 		}
331 	}
332 }
333 
334 
ProjectDlightTexture(void)335 static void ProjectDlightTexture( void ) {
336 	int		l;
337 	vec3_t	origin;
338 	float	scale;
339 	float	radius;
340 	int deformGen;
341 	vec5_t deformParams;
342 
343 	if ( !backEnd.refdef.num_dlights ) {
344 		return;
345 	}
346 
347 	if ( backEnd.refdef.rdflags & RDF_SNOOPERVIEW ) {  // no dlights for snooper
348 		return;
349 	}
350 
351 	ComputeDeformValues(&deformGen, deformParams);
352 
353 	for ( l = 0 ; l < backEnd.refdef.num_dlights ; l++ ) {
354 		dlight_t	*dl;
355 		shaderProgram_t *sp;
356 		vec4_t vector;
357 
358 		if ( !( tess.dlightBits & ( 1 << l ) ) ) {
359 			continue;	// this surface definately doesn't have any of this light
360 		}
361 
362 		dl = &backEnd.refdef.dlights[l];
363 		VectorCopy( dl->transformed, origin );
364 		radius = dl->radius;
365 		scale = 1.0f / radius;
366 
367 		sp = &tr.dlightShader[deformGen == DGEN_NONE ? 0 : 1];
368 
369 		backEnd.pc.c_dlightDraws++;
370 
371 		GLSL_BindProgram(sp);
372 
373 		GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
374 
375 		GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
376 
377 		GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
378 		if (deformGen != DGEN_NONE)
379 		{
380 			GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
381 			GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
382 
383 			if (tess.shader->deforms[0].deformationWave.frequency < 0)
384 			{
385 				vec3_t worldUp;
386 
387 				if ( VectorCompare( backEnd.currentEntity->e.fireRiseDir, vec3_origin ) ) {
388 					VectorSet( backEnd.currentEntity->e.fireRiseDir, 0, 0, 1 );
389 				}
390 
391 				if ( backEnd.currentEntity->e.hModel ) {    // world surfaces dont have an axis
392 					VectorRotate( backEnd.currentEntity->e.fireRiseDir, backEnd.currentEntity->e.axis, worldUp );
393 				} else {
394 					VectorCopy( backEnd.currentEntity->e.fireRiseDir, worldUp );
395 				}
396 
397 				GLSL_SetUniformVec3(sp, UNIFORM_FIRERISEDIR, worldUp);
398 			}
399 		}
400 
401 		vector[0] = dl->color[0];
402 		vector[1] = dl->color[1];
403 		vector[2] = dl->color[2];
404 		vector[3] = 1.0f;
405 		GLSL_SetUniformVec4(sp, UNIFORM_COLOR, vector);
406 
407 		vector[0] = origin[0];
408 		vector[1] = origin[1];
409 		vector[2] = origin[2];
410 		vector[3] = scale;
411 		GLSL_SetUniformVec4(sp, UNIFORM_DLIGHTINFO, vector);
412 
413 		GL_BindToTMU( tr.dlightImage, TB_COLORMAP );
414 
415 		// include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
416 		// where they aren't rendered
417 		if ( dl->additive ) {
418 			GL_State( GLS_ATEST_GT_0 | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
419 		}
420 		else {
421 			GL_State( GLS_ATEST_GT_0 | GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
422 		}
423 
424 		GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 1);
425 
426 		R_DrawElements(tess.numIndexes, tess.firstIndex);
427 
428 		backEnd.pc.c_totalIndexes += tess.numIndexes;
429 		backEnd.pc.c_dlightIndexes += tess.numIndexes;
430 		backEnd.pc.c_dlightVertexes += tess.numVertexes;
431 	}
432 }
433 
434 
ComputeShaderColors(shaderStage_t * pStage,vec4_t baseColor,vec4_t vertColor,int blend)435 static void ComputeShaderColors( shaderStage_t *pStage, vec4_t baseColor, vec4_t vertColor, int blend )
436 {
437 	qboolean isBlend = ((blend & GLS_SRCBLEND_BITS) == GLS_SRCBLEND_DST_COLOR)
438 		|| ((blend & GLS_SRCBLEND_BITS) == GLS_SRCBLEND_ONE_MINUS_DST_COLOR)
439 		|| ((blend & GLS_DSTBLEND_BITS) == GLS_DSTBLEND_SRC_COLOR)
440 		|| ((blend & GLS_DSTBLEND_BITS) == GLS_DSTBLEND_ONE_MINUS_SRC_COLOR);
441 
442 	qboolean is2DDraw = backEnd.currentEntity == &backEnd.entity2D;
443 
444 	float overbright = (isBlend || is2DDraw) ? 1.0f : (float)(1 << tr.overbrightBits);
445 
446 	fog_t *fog;
447 
448 	baseColor[0] =
449 	baseColor[1] =
450 	baseColor[2] =
451 	baseColor[3] = 1.0f;
452 
453 	vertColor[0] =
454 	vertColor[1] =
455 	vertColor[2] =
456 	vertColor[3] = 0.0f;
457 
458 	//
459 	// rgbGen
460 	//
461 	switch ( pStage->rgbGen )
462 	{
463 		case CGEN_EXACT_VERTEX:
464 		case CGEN_EXACT_VERTEX_LIT:
465 			baseColor[0] =
466 			baseColor[1] =
467 			baseColor[2] =
468 			baseColor[3] = 0.0f;
469 
470 			vertColor[0] =
471 			vertColor[1] =
472 			vertColor[2] = overbright;
473 			vertColor[3] = 1.0f;
474 			break;
475 		case CGEN_CONST:
476 			baseColor[0] = pStage->constantColor[0] / 255.0f;
477 			baseColor[1] = pStage->constantColor[1] / 255.0f;
478 			baseColor[2] = pStage->constantColor[2] / 255.0f;
479 			baseColor[3] = pStage->constantColor[3] / 255.0f;
480 			break;
481 		case CGEN_VERTEX:
482 		case CGEN_VERTEX_LIT:
483 			baseColor[0] =
484 			baseColor[1] =
485 			baseColor[2] =
486 			baseColor[3] = 0.0f;
487 
488 			vertColor[0] =
489 			vertColor[1] =
490 			vertColor[2] =
491 			vertColor[3] = 1.0f;
492 			break;
493 		case CGEN_ONE_MINUS_VERTEX:
494 			baseColor[0] =
495 			baseColor[1] =
496 			baseColor[2] = 1.0f;
497 
498 			vertColor[0] =
499 			vertColor[1] =
500 			vertColor[2] = -1.0f;
501 			break;
502 		case CGEN_FOG:
503 			fog = tr.world->fogs + tess.fogNum;
504 
505 			baseColor[0] = ((unsigned char *)(&fog->colorInt))[0] / 255.0f;
506 			baseColor[1] = ((unsigned char *)(&fog->colorInt))[1] / 255.0f;
507 			baseColor[2] = ((unsigned char *)(&fog->colorInt))[2] / 255.0f;
508 			baseColor[3] = ((unsigned char *)(&fog->colorInt))[3] / 255.0f;
509 			break;
510 		case CGEN_WAVEFORM:
511 			baseColor[0] =
512 			baseColor[1] =
513 			baseColor[2] = RB_CalcWaveColorSingle( &pStage->rgbWave );
514 			break;
515 		case CGEN_ENTITY:
516 			if (backEnd.currentEntity)
517 			{
518 				baseColor[0] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[0] / 255.0f;
519 				baseColor[1] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[1] / 255.0f;
520 				baseColor[2] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[2] / 255.0f;
521 				baseColor[3] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f;
522 			}
523 			break;
524 		case CGEN_ONE_MINUS_ENTITY:
525 			if (backEnd.currentEntity)
526 			{
527 				baseColor[0] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[0] / 255.0f;
528 				baseColor[1] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[1] / 255.0f;
529 				baseColor[2] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[2] / 255.0f;
530 				baseColor[3] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f;
531 			}
532 			break;
533 		case CGEN_IDENTITY:
534 		case CGEN_LIGHTING_DIFFUSE:
535 			baseColor[0] =
536 			baseColor[1] =
537 			baseColor[2] = overbright;
538 			break;
539 		case CGEN_IDENTITY_LIGHTING:
540 		case CGEN_BAD:
541 			break;
542 	}
543 
544 	//
545 	// alphaGen
546 	//
547 	switch ( pStage->alphaGen )
548 	{
549 		case AGEN_SKIP:
550 			break;
551 		case AGEN_CONST:
552 			baseColor[3] = pStage->constantColor[3] / 255.0f;
553 			vertColor[3] = 0.0f;
554 			break;
555 		case AGEN_WAVEFORM:
556 			baseColor[3] = RB_CalcWaveAlphaSingle( &pStage->alphaWave );
557 			vertColor[3] = 0.0f;
558 			break;
559 		case AGEN_ENTITY:
560 			if (backEnd.currentEntity)
561 			{
562 				baseColor[3] = ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f;
563 			}
564 			vertColor[3] = 0.0f;
565 			break;
566 		case AGEN_ONE_MINUS_ENTITY:
567 			if (backEnd.currentEntity)
568 			{
569 				baseColor[3] = 1.0f - ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f;
570 			}
571 			vertColor[3] = 0.0f;
572 			break;
573 		case AGEN_NORMALZFADE:
574 			baseColor[3] = pStage->constantColor[3] / 255.0f;
575 			if (backEnd.currentEntity && backEnd.currentEntity->e.hModel)
576 			{
577 				baseColor[3] *= ((unsigned char *)backEnd.currentEntity->e.shaderRGBA)[3] / 255.0f;
578 			}
579 			vertColor[3] = 0.0f;
580 			break;
581 		case AGEN_VERTEX:
582 			baseColor[3] = 0.0f;
583 			vertColor[3] = 1.0f;
584 			break;
585 		case AGEN_ONE_MINUS_VERTEX:
586 			baseColor[3] = 1.0f;
587 			vertColor[3] = -1.0f;
588 			break;
589 		case AGEN_IDENTITY:
590 		case AGEN_LIGHTING_SPECULAR:
591 		case AGEN_PORTAL:
592 			// Done entirely in vertex program
593 			baseColor[3] = 1.0f;
594 			vertColor[3] = 0.0f;
595 			break;
596 	}
597 
598 	// FIXME: find some way to implement this.
599 #if 0
600 	// if in greyscale rendering mode turn all color values into greyscale.
601 	if(r_greyscale->integer)
602 	{
603 		int scale;
604 
605 		for(i = 0; i < tess.numVertexes; i++)
606 		{
607 			scale = (tess.svars.colors[i][0] + tess.svars.colors[i][1] + tess.svars.colors[i][2]) / 3;
608 			tess.svars.colors[i][0] = tess.svars.colors[i][1] = tess.svars.colors[i][2] = scale;
609 		}
610 	}
611 #endif
612 }
613 
614 
ComputeFogValues(vec4_t fogDistanceVector,vec4_t fogDepthVector,float * eyeT,glfog_t * glFog)615 static void ComputeFogValues(vec4_t fogDistanceVector, vec4_t fogDepthVector, float *eyeT, glfog_t *glFog)
616 {
617 	// from RB_CalcFogTexCoords()
618 	fog_t  *fog;
619 	vec3_t  local;
620 
621 	if (glFog)
622 	{
623 		VectorSubtract( backEnd.or.origin, backEnd.viewParms.or.origin, local );
624 		fogDistanceVector[0] = -backEnd.or.modelMatrix[2];
625 		fogDistanceVector[1] = -backEnd.or.modelMatrix[6];
626 		fogDistanceVector[2] = -backEnd.or.modelMatrix[10];
627 		fogDistanceVector[3] = DotProduct( local, backEnd.viewParms.or.axis[0] );
628 
629 		fogDepthVector[0] = glFog->start;
630 		fogDepthVector[1] = glFog->end;
631 		fogDepthVector[2] = glFog->density;
632 		fogDepthVector[3] = 1.0;
633 
634 		return;
635 	}
636 
637 	if (!tess.fogNum)
638 		return;
639 
640 	fog = tr.world->fogs + tess.fogNum;
641 
642 	VectorSubtract( backEnd.or.origin, backEnd.viewParms.or.origin, local );
643 	fogDistanceVector[0] = -backEnd.or.modelMatrix[2];
644 	fogDistanceVector[1] = -backEnd.or.modelMatrix[6];
645 	fogDistanceVector[2] = -backEnd.or.modelMatrix[10];
646 	fogDistanceVector[3] = DotProduct( local, backEnd.viewParms.or.axis[0] );
647 
648 	// scale the fog vectors based on the fog's thickness
649 	VectorScale4(fogDistanceVector, fog->tcScale, fogDistanceVector);
650 
651 	// rotate the gradient vector for this orientation
652 	if ( fog->hasSurface ) {
653 		fogDepthVector[0] = fog->surface[0] * backEnd.or.axis[0][0] +
654 			fog->surface[1] * backEnd.or.axis[0][1] + fog->surface[2] * backEnd.or.axis[0][2];
655 		fogDepthVector[1] = fog->surface[0] * backEnd.or.axis[1][0] +
656 			fog->surface[1] * backEnd.or.axis[1][1] + fog->surface[2] * backEnd.or.axis[1][2];
657 		fogDepthVector[2] = fog->surface[0] * backEnd.or.axis[2][0] +
658 			fog->surface[1] * backEnd.or.axis[2][1] + fog->surface[2] * backEnd.or.axis[2][2];
659 		fogDepthVector[3] = -fog->surface[3] + DotProduct( backEnd.or.origin, fog->surface );
660 
661 		*eyeT = DotProduct( backEnd.or.viewOrigin, fogDepthVector ) + fogDepthVector[3];
662 	} else {
663 		*eyeT = 1;	// non-surface fog always has eye inside
664 	}
665 }
666 
667 
ComputeFogColorMask(shaderStage_t * pStage,vec4_t fogColorMask)668 static void ComputeFogColorMask( shaderStage_t *pStage, vec4_t fogColorMask )
669 {
670 	switch(pStage->adjustColorsForFog)
671 	{
672 		case ACFF_MODULATE_RGB:
673 			fogColorMask[0] =
674 			fogColorMask[1] =
675 			fogColorMask[2] = 1.0f;
676 			fogColorMask[3] = 0.0f;
677 			break;
678 		case ACFF_MODULATE_ALPHA:
679 			fogColorMask[0] =
680 			fogColorMask[1] =
681 			fogColorMask[2] = 0.0f;
682 			fogColorMask[3] = 1.0f;
683 			break;
684 		case ACFF_MODULATE_RGBA:
685 			fogColorMask[0] =
686 			fogColorMask[1] =
687 			fogColorMask[2] =
688 			fogColorMask[3] = 1.0f;
689 			break;
690 		default:
691 			fogColorMask[0] =
692 			fogColorMask[1] =
693 			fogColorMask[2] =
694 			fogColorMask[3] = 0.0f;
695 			break;
696 	}
697 }
698 
699 
ForwardDlight(void)700 static void ForwardDlight( void ) {
701 	int		l;
702 	//vec3_t	origin;
703 	//float	scale;
704 	float	radius;
705 
706 	int deformGen;
707 	vec5_t deformParams;
708 
709 	vec4_t fogDistanceVector, fogDepthVector = {0, 0, 0, 0};
710 	float eyeT = 0;
711 
712 	shaderCommands_t *input = &tess;
713 	shaderStage_t *pStage = tess.xstages[0];
714 
715 	if ( !backEnd.refdef.num_dlights ) {
716 		return;
717 	}
718 
719 	ComputeDeformValues(&deformGen, deformParams);
720 
721 	ComputeFogValues(fogDistanceVector, fogDepthVector, &eyeT, NULL);
722 
723 	for ( l = 0 ; l < backEnd.refdef.num_dlights ; l++ ) {
724 		dlight_t	*dl;
725 		shaderProgram_t *sp;
726 		vec4_t vector;
727 		vec4_t texMatrix;
728 		vec4_t texOffTurb;
729 
730 		if ( !( tess.dlightBits & ( 1 << l ) ) ) {
731 			continue;	// this surface definately doesn't have any of this light
732 		}
733 
734 		dl = &backEnd.refdef.dlights[l];
735 		//VectorCopy( dl->transformed, origin );
736 		radius = dl->radius;
737 		//scale = 1.0f / radius;
738 
739 		//if (pStage->glslShaderGroup == tr.lightallShader)
740 		{
741 			int index = pStage->glslShaderIndex;
742 
743 			index &= ~LIGHTDEF_LIGHTTYPE_MASK;
744 			index |= LIGHTDEF_USE_LIGHT_VECTOR;
745 
746 			sp = &tr.lightallShader[index];
747 		}
748 
749 		backEnd.pc.c_lightallDraws++;
750 
751 		GLSL_BindProgram(sp);
752 
753 		GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
754 		GLSL_SetUniformVec3(sp, UNIFORM_VIEWORIGIN, backEnd.viewParms.or.origin);
755 		GLSL_SetUniformVec3(sp, UNIFORM_LOCALVIEWORIGIN, backEnd.or.viewOrigin);
756 
757 		GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
758 
759 		if ((deformGen != DGEN_NONE && tess.shader->deforms[0].deformationWave.frequency < 0 )
760 			|| pStage->alphaGen == AGEN_NORMALZFADE)
761 		{
762 			vec3_t worldUp;
763 
764 			if ( VectorCompare( backEnd.currentEntity->e.fireRiseDir, vec3_origin ) ) {
765 				VectorSet( backEnd.currentEntity->e.fireRiseDir, 0, 0, 1 );
766 			}
767 
768 			if ( backEnd.currentEntity->e.hModel ) {    // world surfaces dont have an axis
769 				VectorRotate( backEnd.currentEntity->e.fireRiseDir, backEnd.currentEntity->e.axis, worldUp );
770 			} else {
771 				VectorCopy( backEnd.currentEntity->e.fireRiseDir, worldUp );
772 			}
773 
774 			GLSL_SetUniformVec3(sp, UNIFORM_FIRERISEDIR, worldUp);
775 		}
776 
777 		GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
778 		if (deformGen != DGEN_NONE)
779 		{
780 			GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
781 			GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
782 		}
783 
784 		if ( input->fogNum ) {
785 			vec4_t fogColorMask;
786 
787 			GLSL_SetUniformVec4(sp, UNIFORM_FOGDISTANCE, fogDistanceVector);
788 			GLSL_SetUniformVec4(sp, UNIFORM_FOGDEPTH, fogDepthVector);
789 			GLSL_SetUniformFloat(sp, UNIFORM_FOGEYET, eyeT);
790 
791 			ComputeFogColorMask(pStage, fogColorMask);
792 
793 			GLSL_SetUniformVec4(sp, UNIFORM_FOGCOLORMASK, fogColorMask);
794 		}
795 
796 		{
797 			vec4_t baseColor;
798 			vec4_t vertColor;
799 
800 			ComputeShaderColors(pStage, baseColor, vertColor, GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE);
801 
802 			GLSL_SetUniformVec4(sp, UNIFORM_BASECOLOR, baseColor);
803 			GLSL_SetUniformVec4(sp, UNIFORM_VERTCOLOR, vertColor);
804 		}
805 
806 		if (pStage->alphaGen == AGEN_PORTAL)
807 		{
808 			GLSL_SetUniformFloat(sp, UNIFORM_PORTALRANGE, tess.shader->portalRange);
809 		}
810 		else if (pStage->alphaGen == AGEN_NORMALZFADE)
811 		{
812 			float lowest, highest;
813 			//qboolean zombieEffect = qfalse;
814 
815 			lowest = pStage->zFadeBounds[0];
816 			if ( lowest == -1000 ) {    // use entity alpha
817 				lowest = backEnd.currentEntity->e.shaderTime;
818 				//zombieEffect = qtrue;
819 			}
820 			highest = pStage->zFadeBounds[1];
821 			if ( highest == -1000 ) {   // use entity alpha
822 				highest = backEnd.currentEntity->e.shaderTime;
823 				//zombieEffect = qtrue;
824 			}
825 
826 			// TODO: Handle normalzfade zombie effect
827 
828 			GLSL_SetUniformFloat(sp, UNIFORM_ZFADELOWEST, lowest);
829 			GLSL_SetUniformFloat(sp, UNIFORM_ZFADEHIGHEST, highest);
830 		}
831 
832 		GLSL_SetUniformInt(sp, UNIFORM_COLORGEN, pStage->rgbGen);
833 		GLSL_SetUniformInt(sp, UNIFORM_ALPHAGEN, pStage->alphaGen);
834 
835 		GLSL_SetUniformVec3(sp, UNIFORM_DIRECTEDLIGHT, dl->color);
836 
837 		VectorSet(vector, 0, 0, 0);
838 		GLSL_SetUniformVec3(sp, UNIFORM_AMBIENTLIGHT, vector);
839 
840 		VectorCopy(dl->origin, vector);
841 		vector[3] = 1.0f;
842 		GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, vector);
843 
844 		GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, radius);
845 
846 		GLSL_SetUniformVec4(sp, UNIFORM_NORMALSCALE, pStage->normalScale);
847 		GLSL_SetUniformVec4(sp, UNIFORM_SPECULARSCALE, pStage->specularScale);
848 
849 		// include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
850 		// where they aren't rendered
851 		GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHFUNC_EQUAL );
852 		GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
853 
854 		GLSL_SetUniformMat4(sp, UNIFORM_MODELMATRIX, backEnd.or.transformMatrix);
855 
856 		if (pStage->bundle[TB_DIFFUSEMAP].image[0])
857 			R_BindAnimatedImageToTMU( &pStage->bundle[TB_DIFFUSEMAP], TB_DIFFUSEMAP);
858 
859 		// bind textures that are sampled and used in the glsl shader, and
860 		// bind whiteImage to textures that are sampled but zeroed in the glsl shader
861 		//
862 		// alternatives:
863 		//  - use the last bound texture
864 		//     -> costs more to sample a higher res texture then throw out the result
865 		//  - disable texture sampling in glsl shader with #ifdefs, as before
866 		//     -> increases the number of shaders that must be compiled
867 		//
868 
869 		if (pStage->bundle[TB_NORMALMAP].image[0])
870 		{
871 			R_BindAnimatedImageToTMU( &pStage->bundle[TB_NORMALMAP], TB_NORMALMAP);
872 		}
873 		else if (r_normalMapping->integer)
874 			GL_BindToTMU( tr.whiteImage, TB_NORMALMAP );
875 
876 		if (pStage->bundle[TB_SPECULARMAP].image[0])
877 		{
878 			R_BindAnimatedImageToTMU( &pStage->bundle[TB_SPECULARMAP], TB_SPECULARMAP);
879 		}
880 		else if (r_specularMapping->integer)
881 			GL_BindToTMU( tr.whiteImage, TB_SPECULARMAP );
882 
883 		{
884 			vec4_t enableTextures;
885 
886 			VectorSet4(enableTextures, 0.0f, 0.0f, 0.0f, 0.0f);
887 			GLSL_SetUniformVec4(sp, UNIFORM_ENABLETEXTURES, enableTextures);
888 		}
889 
890 		if (r_dlightMode->integer >= 2)
891 			GL_BindToTMU(tr.shadowCubemaps[l], TB_SHADOWMAP);
892 
893 		ComputeTexMods( pStage, TB_DIFFUSEMAP, texMatrix, texOffTurb );
894 		GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX, texMatrix);
895 		GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXOFFTURB, texOffTurb);
896 
897 		GLSL_SetUniformInt(sp, UNIFORM_TCGEN0, pStage->bundle[0].tcGen);
898 
899 		//
900 		// draw
901 		//
902 
903 		R_DrawElements(input->numIndexes, input->firstIndex);
904 
905 		backEnd.pc.c_totalIndexes += tess.numIndexes;
906 		backEnd.pc.c_dlightIndexes += tess.numIndexes;
907 		backEnd.pc.c_dlightVertexes += tess.numVertexes;
908 	}
909 }
910 
911 
ProjectPshadowVBOGLSL(void)912 static void ProjectPshadowVBOGLSL( void ) {
913 	int		l;
914 	vec3_t	origin;
915 	float	radius;
916 
917 	int deformGen;
918 	vec5_t deformParams;
919 
920 	shaderCommands_t *input = &tess;
921 
922 	if ( !backEnd.refdef.num_pshadows ) {
923 		return;
924 	}
925 
926 	ComputeDeformValues(&deformGen, deformParams);
927 
928 	for ( l = 0 ; l < backEnd.refdef.num_pshadows ; l++ ) {
929 		pshadow_t	*ps;
930 		shaderProgram_t *sp;
931 		vec4_t vector;
932 
933 		if ( !( tess.pshadowBits & ( 1 << l ) ) ) {
934 			continue;	// this surface definately doesn't have any of this shadow
935 		}
936 
937 		ps = &backEnd.refdef.pshadows[l];
938 		VectorCopy( ps->lightOrigin, origin );
939 		radius = ps->lightRadius;
940 
941 		sp = &tr.pshadowShader;
942 
943 		GLSL_BindProgram(sp);
944 
945 		GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
946 
947 		VectorCopy(origin, vector);
948 		vector[3] = 1.0f;
949 		GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, vector);
950 
951 		VectorScale(ps->lightViewAxis[0], 1.0f / ps->viewRadius, vector);
952 		GLSL_SetUniformVec3(sp, UNIFORM_LIGHTFORWARD, vector);
953 
954 		VectorScale(ps->lightViewAxis[1], 1.0f / ps->viewRadius, vector);
955 		GLSL_SetUniformVec3(sp, UNIFORM_LIGHTRIGHT, vector);
956 
957 		VectorScale(ps->lightViewAxis[2], 1.0f / ps->viewRadius, vector);
958 		GLSL_SetUniformVec3(sp, UNIFORM_LIGHTUP, vector);
959 
960 		GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, radius);
961 
962 		// include GLS_DEPTHFUNC_EQUAL so alpha tested surfaces don't add light
963 		// where they aren't rendered
964 		GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA | GLS_DEPTHFUNC_EQUAL );
965 		GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
966 
967 		GL_BindToTMU( tr.pshadowMaps[l], TB_DIFFUSEMAP );
968 
969 		//
970 		// draw
971 		//
972 
973 		R_DrawElements(input->numIndexes, input->firstIndex);
974 
975 		backEnd.pc.c_totalIndexes += tess.numIndexes;
976 		//backEnd.pc.c_dlightIndexes += tess.numIndexes;
977 	}
978 }
979 
980 
981 /*
982 ===================
983 RB_FogPass
984 
985 Blends a fog texture on top of everything else
986 ===================
987 */
RB_FogPass(int wolfFog)988 static void RB_FogPass( int wolfFog ) {
989 	fog_t	*fog = NULL;
990 	vec4_t  color;
991 	vec4_t	fogDistanceVector, fogDepthVector = {0, 0, 0, 0};
992 	float	eyeT = 0;
993 	shaderProgram_t *sp;
994 	glfog_t *glFog = NULL;
995 
996 	int deformGen;
997 	vec5_t deformParams;
998 
999 	if ( tr.refdef.rdflags & RDF_SNOOPERVIEW ) { // no fog pass in snooper
1000 		return;
1001 	}
1002 
1003 	if (wolfFog)
1004 	{
1005 		if ( backEnd.projection2D ) {
1006 			return;
1007 		}
1008 
1009 		if ( backEnd.refdef.rdflags & RDF_DRAWINGSKY ) {
1010 			if ( glfogsettings[FOG_SKY].registered ) {
1011 				glFog = &glfogsettings[FOG_SKY];
1012 			}
1013 		}
1014 
1015 		if ( skyboxportal && backEnd.refdef.rdflags & RDF_SKYBOXPORTAL ) {
1016 			if ( glfogsettings[FOG_PORTALVIEW].registered ) {
1017 				glFog = &glfogsettings[FOG_PORTALVIEW];
1018 			}
1019 		} else {
1020 			if ( glfogNum > FOG_NONE ) {
1021 				glFog = &glfogsettings[FOG_CURRENT];
1022 			}
1023 		}
1024 
1025 		if (!glFog)
1026 			return;
1027 	}
1028 
1029 	ComputeDeformValues(&deformGen, deformParams);
1030 
1031 	{
1032 		int index = 0;
1033 
1034 		if (deformGen != DGEN_NONE)
1035 			index |= FOGDEF_USE_DEFORM_VERTEXES;
1036 
1037 		if (glState.vertexAnimation)
1038 			index |= FOGDEF_USE_VERTEX_ANIMATION;
1039 		else if (glState.boneAnimation)
1040 			index |= FOGDEF_USE_BONE_ANIMATION;
1041 
1042 		if (wolfFog)
1043 		{
1044 			if (glFog->mode == GL_LINEAR)
1045 				index |= FOGDEF_USE_WOLF_FOG_LINEAR;
1046 			else // if (glFog->mode == GL_EXP)
1047 				index |= FOGDEF_USE_WOLF_FOG_EXPONENTIAL;
1048 		}
1049 
1050 		sp = &tr.fogShader[index];
1051 	}
1052 
1053 	backEnd.pc.c_fogDraws++;
1054 
1055 	GLSL_BindProgram(sp);
1056 
1057 	if (!wolfFog)
1058 		fog = tr.world->fogs + tess.fogNum;
1059 
1060 	GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
1061 
1062 	GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
1063 
1064 	if (glState.boneAnimation)
1065 	{
1066 		GLSL_SetUniformMat4BoneMatrix(sp, UNIFORM_BONEMATRIX, glState.boneMatrix, glState.boneAnimation);
1067 	}
1068 
1069 	GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
1070 	if (deformGen != DGEN_NONE)
1071 	{
1072 		GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
1073 		GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
1074 
1075 		if (tess.shader->deforms[0].deformationWave.frequency < 0)
1076 		{
1077 			vec3_t worldUp;
1078 
1079 			if ( VectorCompare( backEnd.currentEntity->e.fireRiseDir, vec3_origin ) ) {
1080 				VectorSet( backEnd.currentEntity->e.fireRiseDir, 0, 0, 1 );
1081 			}
1082 
1083 			if ( backEnd.currentEntity->e.hModel ) {    // world surfaces dont have an axis
1084 				VectorRotate( backEnd.currentEntity->e.fireRiseDir, backEnd.currentEntity->e.axis, worldUp );
1085 			} else {
1086 				VectorCopy( backEnd.currentEntity->e.fireRiseDir, worldUp );
1087 			}
1088 
1089 			GLSL_SetUniformVec3(sp, UNIFORM_FIRERISEDIR, worldUp);
1090 		}
1091 	}
1092 
1093 	if (wolfFog)
1094 	{
1095 		color[0] = glFog->color[0];
1096 		color[1] = glFog->color[1];
1097 		color[2] = glFog->color[2];
1098 		color[3] = glFog->color[3];
1099 	}
1100 	else
1101 	{
1102 		color[0] = ((unsigned char *)(&fog->colorInt))[0] / 255.0f;
1103 		color[1] = ((unsigned char *)(&fog->colorInt))[1] / 255.0f;
1104 		color[2] = ((unsigned char *)(&fog->colorInt))[2] / 255.0f;
1105 		color[3] = ((unsigned char *)(&fog->colorInt))[3] / 255.0f;
1106 	}
1107 	GLSL_SetUniformVec4(sp, UNIFORM_COLOR, color);
1108 
1109 	ComputeFogValues(fogDistanceVector, fogDepthVector, &eyeT, glFog);
1110 
1111 	GLSL_SetUniformVec4(sp, UNIFORM_FOGDISTANCE, fogDistanceVector);
1112 	GLSL_SetUniformVec4(sp, UNIFORM_FOGDEPTH, fogDepthVector);
1113 	GLSL_SetUniformFloat(sp, UNIFORM_FOGEYET, eyeT);
1114 
1115 	if ( tess.shader->fogPass == FP_EQUAL ) {
1116 		GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA | GLS_DEPTHFUNC_EQUAL );
1117 	} else {
1118 		GL_State( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA );
1119 	}
1120 	GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
1121 
1122 	R_DrawElements(tess.numIndexes, tess.firstIndex);
1123 }
1124 
1125 
RB_CalcShaderVertexAttribs(shaderCommands_t * input)1126 static unsigned int RB_CalcShaderVertexAttribs( shaderCommands_t *input )
1127 {
1128 	unsigned int vertexAttribs = input->shader->vertexAttribs;
1129 
1130 	if(glState.vertexAnimation)
1131 	{
1132 		vertexAttribs |= ATTR_POSITION2;
1133 		if (vertexAttribs & ATTR_NORMAL)
1134 		{
1135 			vertexAttribs |= ATTR_NORMAL2;
1136 			vertexAttribs |= ATTR_TANGENT2;
1137 		}
1138 	}
1139 
1140 	return vertexAttribs;
1141 }
1142 
1143 
RB_IterateStagesGeneric(shaderCommands_t * input)1144 static void RB_IterateStagesGeneric( shaderCommands_t *input )
1145 {
1146 	int stage;
1147 
1148 	glfog_t *glFog;
1149 	vec4_t fogDistanceVector, fogDepthVector = {0, 0, 0, 0};
1150 	float eyeT = 0;
1151 
1152 	int deformGen;
1153 	vec5_t deformParams;
1154 
1155 	qboolean renderToCubemap = tr.renderCubeFbo && glState.currentFBO == tr.renderCubeFbo;
1156 
1157 	ComputeDeformValues(&deformGen, deformParams);
1158 
1159 	for ( stage = 0; stage < MAX_SHADER_STAGES; stage++ )
1160 	{
1161 		shaderStage_t *pStage = input->xstages[stage];
1162 		shaderProgram_t *sp;
1163 		vec4_t texMatrix;
1164 		vec4_t texOffTurb;
1165 
1166 		if ( !pStage )
1167 		{
1168 			break;
1169 		}
1170 
1171 		glFog = NULL;
1172 
1173 		if (backEnd.depthFill)
1174 		{
1175 			if (pStage->glslShaderGroup == tr.lightallShader)
1176 			{
1177 				int index = 0;
1178 
1179 				if (backEnd.currentEntity && backEnd.currentEntity != &tr.worldEntity)
1180 				{
1181 					if (glState.boneAnimation)
1182 					{
1183 						index |= LIGHTDEF_ENTITY_BONE_ANIMATION;
1184 					}
1185 					else
1186 					{
1187 						index |= LIGHTDEF_ENTITY_VERTEX_ANIMATION;
1188 					}
1189 				}
1190 
1191 				if (pStage->stateBits & GLS_ATEST_BITS)
1192 				{
1193 					index |= LIGHTDEF_USE_TCGEN_AND_TCMOD;
1194 				}
1195 
1196 				sp = &pStage->glslShaderGroup[index];
1197 			}
1198 			else
1199 			{
1200 				int shaderAttribs = 0;
1201 
1202 				if (tess.shader->numDeforms && !ShaderRequiresCPUDeforms(tess.shader))
1203 				{
1204 					shaderAttribs |= GENERICDEF_USE_DEFORM_VERTEXES;
1205 				}
1206 
1207 				if (glState.vertexAnimation)
1208 				{
1209 					shaderAttribs |= GENERICDEF_USE_VERTEX_ANIMATION;
1210 				}
1211 				else if (glState.boneAnimation)
1212 				{
1213 					shaderAttribs |= GENERICDEF_USE_BONE_ANIMATION;
1214 				}
1215 
1216 				if (pStage->stateBits & GLS_ATEST_BITS)
1217 				{
1218 					shaderAttribs |= GENERICDEF_USE_TCGEN_AND_TCMOD;
1219 				}
1220 
1221 				sp = &tr.genericShader[shaderAttribs];
1222 			}
1223 		}
1224 		else if (pStage->glslShaderGroup == tr.lightallShader)
1225 		{
1226 			int index = pStage->glslShaderIndex;
1227 
1228 			if (backEnd.currentEntity && backEnd.currentEntity != &tr.worldEntity)
1229 			{
1230 				if (glState.boneAnimation)
1231 				{
1232 					index |= LIGHTDEF_ENTITY_BONE_ANIMATION;
1233 				}
1234 				else
1235 				{
1236 					index |= LIGHTDEF_ENTITY_VERTEX_ANIMATION;
1237 				}
1238 			}
1239 
1240 			if (r_sunlightMode->integer && (backEnd.viewParms.flags & VPF_USESUNLIGHT) && (index & LIGHTDEF_LIGHTTYPE_MASK))
1241 			{
1242 				index |= LIGHTDEF_USE_SHADOWMAP;
1243 			}
1244 
1245 			if (r_lightmap->integer && ((index & LIGHTDEF_LIGHTTYPE_MASK) == LIGHTDEF_USE_LIGHTMAP))
1246 			{
1247 				index = LIGHTDEF_USE_TCGEN_AND_TCMOD;
1248 			}
1249 
1250 			sp = &pStage->glslShaderGroup[index];
1251 
1252 			backEnd.pc.c_lightallDraws++;
1253 		}
1254 		else
1255 		{
1256 			if ( r_wolffog->integer && pStage->adjustColorsForFog && !backEnd.projection2D )
1257 			{
1258 				if ( !tess.shader->noFog || pStage->isFogged ) {
1259 					if ( backEnd.refdef.rdflags & RDF_DRAWINGSKY ) {
1260 						if ( glfogsettings[FOG_SKY].registered ) {
1261 							glFog = &glfogsettings[FOG_SKY];
1262 						}
1263 					}
1264 
1265 					if ( skyboxportal && backEnd.refdef.rdflags & RDF_SKYBOXPORTAL ) {
1266 						if ( glfogsettings[FOG_PORTALVIEW].registered ) {
1267 							glFog = &glfogsettings[FOG_PORTALVIEW];
1268 						}
1269 					} else {
1270 						if ( glfogNum > FOG_NONE ) {
1271 							glFog = &glfogsettings[FOG_CURRENT];
1272 						}
1273 					}
1274 				}
1275 			}
1276 
1277 			sp = GLSL_GetGenericShaderProgram(stage, glFog);
1278 
1279 			backEnd.pc.c_genericDraws++;
1280 		}
1281 
1282 		GLSL_BindProgram(sp);
1283 
1284 		GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
1285 		GLSL_SetUniformVec3(sp, UNIFORM_VIEWORIGIN, backEnd.viewParms.or.origin);
1286 		GLSL_SetUniformVec3(sp, UNIFORM_LOCALVIEWORIGIN, backEnd.or.viewOrigin);
1287 
1288 		GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
1289 
1290 		if (glState.boneAnimation)
1291 		{
1292 			GLSL_SetUniformMat4BoneMatrix(sp, UNIFORM_BONEMATRIX, glState.boneMatrix, glState.boneAnimation);
1293 		}
1294 
1295 		if ((deformGen != DGEN_NONE && tess.shader->deforms[0].deformationWave.frequency < 0 )
1296 			|| pStage->alphaGen == AGEN_NORMALZFADE)
1297 		{
1298 			vec3_t worldUp;
1299 
1300 			if ( VectorCompare( backEnd.currentEntity->e.fireRiseDir, vec3_origin ) ) {
1301 				VectorSet( backEnd.currentEntity->e.fireRiseDir, 0, 0, 1 );
1302 			}
1303 
1304 			if ( backEnd.currentEntity->e.hModel ) {    // world surfaces dont have an axis
1305 				VectorRotate( backEnd.currentEntity->e.fireRiseDir, backEnd.currentEntity->e.axis, worldUp );
1306 			} else {
1307 				VectorCopy( backEnd.currentEntity->e.fireRiseDir, worldUp );
1308 			}
1309 
1310 			GLSL_SetUniformVec3(sp, UNIFORM_FIRERISEDIR, worldUp);
1311 		}
1312 
1313 		GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
1314 		if (deformGen != DGEN_NONE)
1315 		{
1316 			GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
1317 			GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
1318 		}
1319 
1320 		if ( input->fogNum || glFog ) {
1321 			ComputeFogValues(fogDistanceVector, fogDepthVector, &eyeT, glFog);
1322 
1323 			GLSL_SetUniformVec4(sp, UNIFORM_FOGDISTANCE, fogDistanceVector);
1324 			GLSL_SetUniformVec4(sp, UNIFORM_FOGDEPTH, fogDepthVector);
1325 			GLSL_SetUniformFloat(sp, UNIFORM_FOGEYET, eyeT);
1326 		}
1327 
1328 		if ((pStage->stateBits & GLS_ATEST_BITS) == GLS_ATEST_GT_0)
1329 		{
1330 			GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 1);
1331 		}
1332 		else if ((pStage->stateBits & GLS_ATEST_BITS) == GLS_ATEST_LT_80)
1333 		{
1334 			GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 2);
1335 		}
1336 		else if ((pStage->stateBits & GLS_ATEST_BITS) == GLS_ATEST_GE_80)
1337 		{
1338 			GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 3);
1339 		}
1340 		else
1341 		{
1342 			GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
1343 		}
1344 
1345 
1346 		{
1347 			vec4_t baseColor;
1348 			vec4_t vertColor;
1349 			int fadeStart, fadeEnd;
1350 
1351 			ComputeShaderColors(pStage, baseColor, vertColor, pStage->stateBits);
1352 
1353 			//----(SA)	fading model stuff
1354 			if ( backEnd.currentEntity )
1355 			{
1356 				fadeStart = backEnd.currentEntity->e.fadeStartTime;
1357 			}
1358 			else
1359 			{
1360 				fadeStart = 0;
1361 			}
1362 
1363 			if ( fadeStart )
1364 			{
1365 				fadeEnd = backEnd.currentEntity->e.fadeEndTime;
1366 
1367 				if ( fadeStart > tr.refdef.time )
1368 				{
1369 					// has not started to fade yet
1370 					GL_State( pStage->stateBits );
1371 				}
1372 				else
1373 				{
1374 					unsigned int tempState;
1375 					float alphaval;
1376 
1377 					if ( fadeEnd < tr.refdef.time )
1378 					{
1379 						// entity faded out completely
1380 						continue;
1381 					}
1382 
1383 					alphaval = (float)( fadeEnd - tr.refdef.time ) / (float)( fadeEnd - fadeStart );
1384 
1385 					tempState = pStage->stateBits;
1386 					// remove the current blend, and don't write to Z buffer
1387 					tempState &= ~( GLS_SRCBLEND_BITS | GLS_DSTBLEND_BITS | GLS_DEPTHMASK_TRUE );
1388 					// set the blend to src_alpha, dst_one_minus_src_alpha
1389 					tempState |= ( GLS_SRCBLEND_SRC_ALPHA | GLS_DSTBLEND_ONE_MINUS_SRC_ALPHA );
1390 					GL_State( tempState );
1391 					GL_Cull( CT_FRONT_SIDED );
1392 					// modulate the alpha component of each vertex in the render list
1393 					baseColor[3] *= alphaval;
1394 					vertColor[3] *= alphaval;
1395 				}
1396 			}
1397 			else
1398 			{
1399 				GL_State( pStage->stateBits );
1400 			}
1401 			//----(SA)	end
1402 
1403 			GLSL_SetUniformVec4(sp, UNIFORM_BASECOLOR, baseColor);
1404 			GLSL_SetUniformVec4(sp, UNIFORM_VERTCOLOR, vertColor);
1405 		}
1406 
1407 		if (pStage->rgbGen == CGEN_LIGHTING_DIFFUSE)
1408 		{
1409 			vec4_t vec;
1410 
1411 			VectorScale(backEnd.currentEntity->ambientLight, 1.0f / 255.0f, vec);
1412 			GLSL_SetUniformVec3(sp, UNIFORM_AMBIENTLIGHT, vec);
1413 
1414 			VectorScale(backEnd.currentEntity->directedLight, 1.0f / 255.0f, vec);
1415 			GLSL_SetUniformVec3(sp, UNIFORM_DIRECTEDLIGHT, vec);
1416 
1417 			VectorCopy(backEnd.currentEntity->lightDir, vec);
1418 			vec[3] = 0.0f;
1419 			GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, vec);
1420 			GLSL_SetUniformVec3(sp, UNIFORM_MODELLIGHTDIR, backEnd.currentEntity->modelLightDir);
1421 
1422 			GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, 0.0f);
1423 		}
1424 
1425 		if (pStage->alphaGen == AGEN_PORTAL)
1426 		{
1427 			GLSL_SetUniformFloat(sp, UNIFORM_PORTALRANGE, tess.shader->portalRange);
1428 		}
1429 		else if (pStage->alphaGen == AGEN_NORMALZFADE)
1430 		{
1431 			float lowest, highest;
1432 			//qboolean zombieEffect = qfalse;
1433 
1434 			lowest = pStage->zFadeBounds[0];
1435 			if ( lowest == -1000 ) {    // use entity alpha
1436 				lowest = backEnd.currentEntity->e.shaderTime;
1437 				//zombieEffect = qtrue;
1438 			}
1439 			highest = pStage->zFadeBounds[1];
1440 			if ( highest == -1000 ) {   // use entity alpha
1441 				highest = backEnd.currentEntity->e.shaderTime;
1442 				//zombieEffect = qtrue;
1443 			}
1444 
1445 			// TODO: Handle normalzfade zombie effect
1446 
1447 			GLSL_SetUniformFloat(sp, UNIFORM_ZFADELOWEST, lowest);
1448 			GLSL_SetUniformFloat(sp, UNIFORM_ZFADEHIGHEST, highest);
1449 		}
1450 
1451 		GLSL_SetUniformInt(sp, UNIFORM_COLORGEN, pStage->rgbGen);
1452 		GLSL_SetUniformInt(sp, UNIFORM_ALPHAGEN, pStage->alphaGen);
1453 
1454 		if ( input->fogNum || glFog )
1455 		{
1456 			vec4_t fogColorMask;
1457 
1458 			ComputeFogColorMask(pStage, fogColorMask);
1459 
1460 			GLSL_SetUniformVec4(sp, UNIFORM_FOGCOLORMASK, fogColorMask);
1461 		}
1462 
1463 		if (r_lightmap->integer)
1464 		{
1465 			vec4_t v;
1466 			VectorSet4(v, 1.0f, 0.0f, 0.0f, 1.0f);
1467 			GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX, v);
1468 			VectorSet4(v, 0.0f, 0.0f, 0.0f, 0.0f);
1469 			GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXOFFTURB, v);
1470 
1471 			GLSL_SetUniformInt(sp, UNIFORM_TCGEN0, TCGEN_LIGHTMAP);
1472 		}
1473 		else
1474 		{
1475 			ComputeTexMods(pStage, TB_DIFFUSEMAP, texMatrix, texOffTurb);
1476 			GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXMATRIX, texMatrix);
1477 			GLSL_SetUniformVec4(sp, UNIFORM_DIFFUSETEXOFFTURB, texOffTurb);
1478 
1479 			GLSL_SetUniformInt(sp, UNIFORM_TCGEN0, pStage->bundle[0].tcGen);
1480 			if (pStage->bundle[0].tcGen == TCGEN_VECTOR)
1481 			{
1482 				vec3_t vec;
1483 
1484 				VectorCopy(pStage->bundle[0].tcGenVectors[0], vec);
1485 				GLSL_SetUniformVec3(sp, UNIFORM_TCGEN0VECTOR0, vec);
1486 				VectorCopy(pStage->bundle[0].tcGenVectors[1], vec);
1487 				GLSL_SetUniformVec3(sp, UNIFORM_TCGEN0VECTOR1, vec);
1488 			}
1489 		}
1490 
1491 		GLSL_SetUniformMat4(sp, UNIFORM_MODELMATRIX, backEnd.or.transformMatrix);
1492 
1493 		GLSL_SetUniformVec4(sp, UNIFORM_NORMALSCALE, pStage->normalScale);
1494 
1495 		{
1496 			vec4_t specularScale;
1497 			Vector4Copy(pStage->specularScale, specularScale);
1498 
1499 			if (renderToCubemap)
1500 			{
1501 				// force specular to nonmetal if rendering cubemaps
1502 				if (r_pbr->integer)
1503 					specularScale[1] = 0.0f;
1504 			}
1505 
1506 			GLSL_SetUniformVec4(sp, UNIFORM_SPECULARSCALE, specularScale);
1507 		}
1508 
1509 		//GLSL_SetUniformFloat(sp, UNIFORM_MAPLIGHTSCALE, backEnd.refdef.mapLightScale);
1510 
1511 		//
1512 		// do multitexture
1513 		//
1514 		if ( backEnd.depthFill )
1515 		{
1516 			if (!(pStage->stateBits & GLS_ATEST_BITS))
1517 				GL_BindToTMU( tr.whiteImage, TB_COLORMAP );
1518 			else if ( pStage->bundle[TB_COLORMAP].image[0] != 0 )
1519 				R_BindAnimatedImageToTMU( &pStage->bundle[TB_COLORMAP], TB_COLORMAP );
1520 		}
1521 		else if ( pStage->glslShaderGroup == tr.lightallShader )
1522 		{
1523 			int i;
1524 			vec4_t enableTextures;
1525 
1526 			if (r_sunlightMode->integer && (backEnd.viewParms.flags & VPF_USESUNLIGHT) && (pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK))
1527 			{
1528 				// FIXME: screenShadowImage is NULL if no framebuffers
1529 				if (tr.screenShadowImage)
1530 					GL_BindToTMU(tr.screenShadowImage, TB_SHADOWMAP);
1531 				GLSL_SetUniformVec3(sp, UNIFORM_PRIMARYLIGHTAMBIENT, backEnd.refdef.sunAmbCol);
1532 				if (r_pbr->integer)
1533 				{
1534 					vec3_t color;
1535 
1536 					color[0] = backEnd.refdef.sunCol[0] * backEnd.refdef.sunCol[0];
1537 					color[1] = backEnd.refdef.sunCol[1] * backEnd.refdef.sunCol[1];
1538 					color[2] = backEnd.refdef.sunCol[2] * backEnd.refdef.sunCol[2];
1539 					GLSL_SetUniformVec3(sp, UNIFORM_PRIMARYLIGHTCOLOR, color);
1540 				}
1541 				else
1542 				{
1543 					GLSL_SetUniformVec3(sp, UNIFORM_PRIMARYLIGHTCOLOR, backEnd.refdef.sunCol);
1544 				}
1545 				GLSL_SetUniformVec4(sp, UNIFORM_PRIMARYLIGHTORIGIN,  backEnd.refdef.sunDir);
1546 			}
1547 
1548 			VectorSet4(enableTextures, 0, 0, 0, 0);
1549 			if ((r_lightmap->integer == 1 || r_lightmap->integer == 2) && pStage->bundle[TB_LIGHTMAP].image[0])
1550 			{
1551 				for (i = 0; i < NUM_TEXTURE_BUNDLES; i++)
1552 				{
1553 					if (i == TB_COLORMAP)
1554 						R_BindAnimatedImageToTMU( &pStage->bundle[TB_LIGHTMAP], i);
1555 					else
1556 						GL_BindToTMU( tr.whiteImage, i );
1557 				}
1558 			}
1559 			else if (r_lightmap->integer == 3 && pStage->bundle[TB_DELUXEMAP].image[0])
1560 			{
1561 				for (i = 0; i < NUM_TEXTURE_BUNDLES; i++)
1562 				{
1563 					if (i == TB_COLORMAP)
1564 						R_BindAnimatedImageToTMU( &pStage->bundle[TB_DELUXEMAP], i);
1565 					else
1566 						GL_BindToTMU( tr.whiteImage, i );
1567 				}
1568 			}
1569 			else
1570 			{
1571 				qboolean light = (pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK) != 0;
1572 				qboolean fastLight = !(r_normalMapping->integer || r_specularMapping->integer);
1573 
1574 				if (pStage->bundle[TB_DIFFUSEMAP].image[0])
1575 					R_BindAnimatedImageToTMU( &pStage->bundle[TB_DIFFUSEMAP], TB_DIFFUSEMAP);
1576 
1577 				if (pStage->bundle[TB_LIGHTMAP].image[0])
1578 					R_BindAnimatedImageToTMU( &pStage->bundle[TB_LIGHTMAP], TB_LIGHTMAP);
1579 
1580 				// bind textures that are sampled and used in the glsl shader, and
1581 				// bind whiteImage to textures that are sampled but zeroed in the glsl shader
1582 				//
1583 				// alternatives:
1584 				//  - use the last bound texture
1585 				//     -> costs more to sample a higher res texture then throw out the result
1586 				//  - disable texture sampling in glsl shader with #ifdefs, as before
1587 				//     -> increases the number of shaders that must be compiled
1588 				//
1589 				if (light && !fastLight)
1590 				{
1591 					if (pStage->bundle[TB_NORMALMAP].image[0])
1592 					{
1593 						R_BindAnimatedImageToTMU( &pStage->bundle[TB_NORMALMAP], TB_NORMALMAP);
1594 						enableTextures[0] = 1.0f;
1595 					}
1596 					else if (r_normalMapping->integer)
1597 						GL_BindToTMU( tr.whiteImage, TB_NORMALMAP );
1598 
1599 					if (pStage->bundle[TB_DELUXEMAP].image[0])
1600 					{
1601 						R_BindAnimatedImageToTMU( &pStage->bundle[TB_DELUXEMAP], TB_DELUXEMAP);
1602 						enableTextures[1] = 1.0f;
1603 					}
1604 					else if (r_deluxeMapping->integer)
1605 						GL_BindToTMU( tr.whiteImage, TB_DELUXEMAP );
1606 
1607 					if (pStage->bundle[TB_SPECULARMAP].image[0])
1608 					{
1609 						R_BindAnimatedImageToTMU( &pStage->bundle[TB_SPECULARMAP], TB_SPECULARMAP);
1610 						enableTextures[2] = 1.0f;
1611 					}
1612 					else if (r_specularMapping->integer)
1613 						GL_BindToTMU( tr.whiteImage, TB_SPECULARMAP );
1614 				}
1615 
1616 				enableTextures[3] = (r_cubeMapping->integer && !(tr.viewParms.flags & VPF_NOCUBEMAPS) && input->cubemapIndex) ? 1.0f : 0.0f;
1617 			}
1618 
1619 			GLSL_SetUniformVec4(sp, UNIFORM_ENABLETEXTURES, enableTextures);
1620 		}
1621 		else if ( pStage->bundle[1].image[0] != 0 )
1622 		{
1623 			R_BindAnimatedImageToTMU( &pStage->bundle[0], 0 );
1624 
1625 			R_BindAnimatedImageToTMU( &pStage->bundle[1], 1 );
1626 		}
1627 		else
1628 		{
1629 			//
1630 			// set state
1631 			//
1632 			R_BindAnimatedImageToTMU( &pStage->bundle[0], 0 );
1633 		}
1634 
1635 		//
1636 		// testing cube map
1637 		//
1638 		if (!(tr.viewParms.flags & VPF_NOCUBEMAPS) && input->cubemapIndex && r_cubeMapping->integer)
1639 		{
1640 			vec4_t vec;
1641 			cubemap_t *cubemap = &tr.cubemaps[input->cubemapIndex - 1];
1642 
1643 			// FIXME: cubemap image could be NULL if cubemap isn't renderer or loaded
1644 			if (cubemap->image)
1645 				GL_BindToTMU( cubemap->image, TB_CUBEMAP);
1646 
1647 			VectorSubtract(cubemap->origin, backEnd.viewParms.or.origin, vec);
1648 			vec[3] = 1.0f;
1649 
1650 			VectorScale4(vec, 1.0f / cubemap->parallaxRadius, vec);
1651 
1652 			GLSL_SetUniformVec4(sp, UNIFORM_CUBEMAPINFO, vec);
1653 		}
1654 
1655 		//
1656 		// draw
1657 		//
1658 		R_DrawElements(input->numIndexes, input->firstIndex);
1659 
1660 		// allow skipping out to show just lightmaps during development
1661 		if ( r_lightmap->integer && ( pStage->bundle[0].isLightmap || pStage->bundle[1].isLightmap ) )
1662 		{
1663 			break;
1664 		}
1665 
1666 		if (backEnd.depthFill)
1667 			break;
1668 	}
1669 }
1670 
1671 
RB_RenderShadowmap(shaderCommands_t * input)1672 static void RB_RenderShadowmap( shaderCommands_t *input )
1673 {
1674 	int deformGen;
1675 	vec5_t deformParams;
1676 
1677 	ComputeDeformValues(&deformGen, deformParams);
1678 
1679 	{
1680 		shaderProgram_t *sp = &tr.shadowmapShader[0];
1681 		if (glState.vertexAnimation)
1682 		{
1683 			sp = &tr.shadowmapShader[SHADOWMAPDEF_USE_VERTEX_ANIMATION];
1684 		}
1685 		else if (glState.boneAnimation)
1686 		{
1687 			sp = &tr.shadowmapShader[SHADOWMAPDEF_USE_BONE_ANIMATION];
1688 		}
1689 
1690 		vec4_t vector;
1691 
1692 		GLSL_BindProgram(sp);
1693 
1694 		GLSL_SetUniformMat4(sp, UNIFORM_MODELVIEWPROJECTIONMATRIX, glState.modelviewProjection);
1695 
1696 		GLSL_SetUniformMat4(sp, UNIFORM_MODELMATRIX, backEnd.or.transformMatrix);
1697 
1698 		GLSL_SetUniformFloat(sp, UNIFORM_VERTEXLERP, glState.vertexAttribsInterpolation);
1699 
1700 		if (glState.boneAnimation)
1701 		{
1702 			GLSL_SetUniformMat4BoneMatrix(sp, UNIFORM_BONEMATRIX, glState.boneMatrix, glState.boneAnimation);
1703 		}
1704 
1705 		GLSL_SetUniformInt(sp, UNIFORM_DEFORMGEN, deformGen);
1706 		if (deformGen != DGEN_NONE)
1707 		{
1708 			GLSL_SetUniformFloat5(sp, UNIFORM_DEFORMPARAMS, deformParams);
1709 			GLSL_SetUniformFloat(sp, UNIFORM_TIME, tess.shaderTime);
1710 
1711 			if (tess.shader->deforms[0].deformationWave.frequency < 0)
1712 			{
1713 				vec3_t worldUp;
1714 
1715 				if ( VectorCompare( backEnd.currentEntity->e.fireRiseDir, vec3_origin ) ) {
1716 					VectorSet( backEnd.currentEntity->e.fireRiseDir, 0, 0, 1 );
1717 				}
1718 
1719 				if ( backEnd.currentEntity->e.hModel ) {    // world surfaces dont have an axis
1720 					VectorRotate( backEnd.currentEntity->e.fireRiseDir, backEnd.currentEntity->e.axis, worldUp );
1721 				} else {
1722 					VectorCopy( backEnd.currentEntity->e.fireRiseDir, worldUp );
1723 				}
1724 
1725 				GLSL_SetUniformVec3(sp, UNIFORM_FIRERISEDIR, worldUp);
1726 			}
1727 		}
1728 
1729 		VectorCopy(backEnd.viewParms.or.origin, vector);
1730 		vector[3] = 1.0f;
1731 		GLSL_SetUniformVec4(sp, UNIFORM_LIGHTORIGIN, vector);
1732 		GLSL_SetUniformFloat(sp, UNIFORM_LIGHTRADIUS, backEnd.viewParms.zFar);
1733 
1734 		GL_State( 0 );
1735 		GLSL_SetUniformInt(sp, UNIFORM_ALPHATEST, 0);
1736 
1737 		//
1738 		// do multitexture
1739 		//
1740 		//if ( pStage->glslShaderGroup )
1741 		{
1742 			//
1743 			// draw
1744 			//
1745 
1746 			R_DrawElements(input->numIndexes, input->firstIndex);
1747 		}
1748 	}
1749 }
1750 
1751 
1752 /*
1753 ** RB_StageIteratorGeneric
1754 */
RB_StageIteratorGeneric(void)1755 void RB_StageIteratorGeneric( void )
1756 {
1757 	shaderCommands_t *input;
1758 	unsigned int vertexAttribs = 0;
1759 
1760 	input = &tess;
1761 
1762 	if (!input->numVertexes || !input->numIndexes)
1763 	{
1764 		return;
1765 	}
1766 
1767 	if (tess.useInternalVao)
1768 	{
1769 		RB_DeformTessGeometry();
1770 	}
1771 
1772 	vertexAttribs = RB_CalcShaderVertexAttribs( input );
1773 
1774 	if (tess.useInternalVao)
1775 	{
1776 		RB_UpdateTessVao(vertexAttribs);
1777 	}
1778 	else
1779 	{
1780 		backEnd.pc.c_staticVaoDraws++;
1781 	}
1782 
1783 	//
1784 	// log this call
1785 	//
1786 	if ( r_logFile->integer )
1787 	{
1788 		// don't just call LogComment, or we will get
1789 		// a call to va() every frame!
1790 		GLimp_LogComment( va("--- RB_StageIteratorGeneric( %s ) ---\n", tess.shader->name) );
1791 	}
1792 
1793 	//
1794 	// set face culling appropriately
1795 	//
1796 	if (input->shader->cullType == CT_TWO_SIDED)
1797 	{
1798 		GL_Cull( CT_TWO_SIDED );
1799 	}
1800 	else
1801 	{
1802 		qboolean cullFront = (input->shader->cullType == CT_FRONT_SIDED);
1803 
1804 		if ( backEnd.viewParms.flags & VPF_DEPTHSHADOW )
1805 			cullFront = !cullFront;
1806 
1807 		if ( backEnd.viewParms.isMirror )
1808 			cullFront = !cullFront;
1809 
1810 		if ( backEnd.currentEntity && backEnd.currentEntity->mirrored )
1811 			cullFront = !cullFront;
1812 
1813 		if (cullFront)
1814 			GL_Cull( CT_FRONT_SIDED );
1815 		else
1816 			GL_Cull( CT_BACK_SIDED );
1817 	}
1818 
1819 	// set polygon offset if necessary
1820 	if ( input->shader->polygonOffset )
1821 	{
1822 		qglEnable( GL_POLYGON_OFFSET_FILL );
1823 	}
1824 
1825 	//
1826 	// render depth if in depthfill mode
1827 	//
1828 	if (backEnd.depthFill)
1829 	{
1830 		RB_IterateStagesGeneric( input );
1831 
1832 		//
1833 		// reset polygon offset
1834 		//
1835 		if ( input->shader->polygonOffset )
1836 		{
1837 			qglDisable( GL_POLYGON_OFFSET_FILL );
1838 		}
1839 
1840 		return;
1841 	}
1842 
1843 	//
1844 	// render shadowmap if in shadowmap mode
1845 	//
1846 	if (backEnd.viewParms.flags & VPF_SHADOWMAP)
1847 	{
1848 		if ( input->shader->sort == SS_OPAQUE )
1849 		{
1850 			RB_RenderShadowmap( input );
1851 		}
1852 		//
1853 		// reset polygon offset
1854 		//
1855 		if ( input->shader->polygonOffset )
1856 		{
1857 			qglDisable( GL_POLYGON_OFFSET_FILL );
1858 		}
1859 
1860 		return;
1861 	}
1862 
1863 	//
1864 	//
1865 	// call shader function
1866 	//
1867 	RB_IterateStagesGeneric( input );
1868 
1869 	//
1870 	// pshadows!
1871 	//
1872 	if (glRefConfig.framebufferObject && r_shadows->integer == 4 && tess.pshadowBits
1873 		&& tess.shader->sort <= SS_OPAQUE && !(tess.shader->surfaceFlags & (SURF_NODLIGHT | SURF_SKY) ) ) {
1874 		ProjectPshadowVBOGLSL();
1875 	}
1876 
1877 
1878 	//
1879 	// now do any dynamic lighting needed
1880 	//
1881 	if ( tess.dlightBits && tess.shader->sort <= SS_OPAQUE && r_lightmap->integer == 0
1882 		&& !(tess.shader->surfaceFlags & (SURF_NODLIGHT | SURF_SKY) ) ) {
1883 		if (tess.shader->numUnfoggedPasses == 1 && tess.xstages[0]->glslShaderGroup == tr.lightallShader
1884 			&& (tess.xstages[0]->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK) && r_dlightMode->integer)
1885 		{
1886 			ForwardDlight();
1887 		}
1888 		else
1889 		{
1890 			ProjectDlightTexture();
1891 		}
1892 	}
1893 
1894 	//
1895 	// now do fog
1896 	//
1897 	if ( tess.fogNum && tess.shader->fogPass ) {
1898 		RB_FogPass(0);
1899 	}
1900 
1901 	//
1902 	// RTCW fog
1903 	// may not match original RTCW fog, since that's done per stage
1904 	//
1905 	if ( r_wolffog->integer && tess.shader->fogPass && tess.shader->sort <= SS_OPAQUE )
1906 	{
1907 		int stage, stageFog = 0;
1908 
1909 		if ( tess.shader->noFog ) {
1910 			// make sure at least one stage has fog
1911 			for ( stage = 0; stage < MAX_SHADER_STAGES; stage++ )
1912 			{
1913 				shaderStage_t *pStage = tess.xstages[stage];
1914 
1915 				if ( !pStage )
1916 				{
1917 					break;
1918 				}
1919 
1920 				if (pStage->isFogged)
1921 				{
1922 					stageFog = 1;
1923 					break;
1924 				}
1925 			}
1926 		}
1927 
1928 		if ( !tess.shader->noFog || stageFog )
1929 		{
1930 			RB_FogPass(1);
1931 		}
1932 	}
1933 
1934 	//
1935 	// reset polygon offset
1936 	//
1937 	if ( input->shader->polygonOffset )
1938 	{
1939 		qglDisable( GL_POLYGON_OFFSET_FILL );
1940 	}
1941 }
1942 
1943 
1944 /*
1945 ** RB_EndSurface
1946 */
RB_EndSurface(void)1947 void RB_EndSurface( void ) {
1948 	shaderCommands_t *input;
1949 
1950 	input = &tess;
1951 
1952 	if (input->numIndexes == 0 || input->numVertexes == 0) {
1953 		return;
1954 	}
1955 
1956 	if (input->indexes[SHADER_MAX_INDEXES-1] != 0) {
1957 		ri.Error (ERR_DROP, "RB_EndSurface() - SHADER_MAX_INDEXES hit");
1958 	}
1959 	if (input->xyz[SHADER_MAX_VERTEXES-1][0] != 0) {
1960 		ri.Error (ERR_DROP, "RB_EndSurface() - SHADER_MAX_VERTEXES hit");
1961 	}
1962 
1963 	if ( tess.shader == tr.shadowShader ) {
1964 		RB_ShadowTessEnd();
1965 		return;
1966 	}
1967 
1968 	// for debugging of sort order issues, stop rendering after a given sort value
1969 	if ( r_debugSort->integer && r_debugSort->integer < tess.shader->sort ) {
1970 		return;
1971 	}
1972 
1973 	if (tess.useCacheVao)
1974 	{
1975 		// upload indexes now
1976 		VaoCache_Commit();
1977 	}
1978 
1979 	//
1980 	// update performance counters
1981 	//
1982 	backEnd.pc.c_shaders++;
1983 	backEnd.pc.c_vertexes += tess.numVertexes;
1984 	backEnd.pc.c_indexes += tess.numIndexes;
1985 	backEnd.pc.c_totalIndexes += tess.numIndexes * tess.numPasses;
1986 
1987 	//
1988 	// call off to shader specific tess end function
1989 	//
1990 	tess.currentStageIteratorFunc();
1991 
1992 	//
1993 	// draw debugging stuff
1994 	//
1995 	if ( r_showtris->integer ) {
1996 		DrawTris (input);
1997 	}
1998 	if ( r_shownormals->integer ) {
1999 		DrawNormals (input);
2000 	}
2001 	// clear shader so we can tell we don't have any unclosed surfaces
2002 	tess.numIndexes = 0;
2003 	tess.numVertexes = 0;
2004 	tess.firstIndex = 0;
2005 
2006 	GLimp_LogComment( "----------\n" );
2007 }
2008