1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
|
/*
===========================================================================
Return to Castle Wolfenstein multiplayer GPL Source Code
Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
This file is part of the Return to Castle Wolfenstein multiplayer GPL Source Code (RTCW MP Source Code).
RTCW MP Source Code is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RTCW MP Source Code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTCW MP Source Code. If not, see <http://www.gnu.org/licenses/>.
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.
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.
===========================================================================
*/
// tr_shade_calc.c
#include "tr_local.h"
#define WAVEVALUE( table, base, amplitude, phase, freq ) ( ( base ) + table[ ( ( int64_t ) ( ( ( phase ) + tess.shaderTime * ( freq ) ) * FUNCTABLE_SIZE ) ) & FUNCTABLE_MASK ] * ( amplitude ) )
static float *TableForFunc( genFunc_t func ) {
switch ( func )
{
case GF_SIN:
return tr.sinTable;
case GF_TRIANGLE:
return tr.triangleTable;
case GF_SQUARE:
return tr.squareTable;
case GF_SAWTOOTH:
return tr.sawToothTable;
case GF_INVERSE_SAWTOOTH:
return tr.inverseSawToothTable;
case GF_NONE:
default:
break;
}
ri.Error( ERR_DROP, "TableForFunc called with invalid function '%d' in shader '%s'", func, tess.shader->name );
return NULL;
}
/*
** EvalWaveForm
**
** Evaluates a given waveForm_t, referencing backEnd.refdef.time directly
*/
static float EvalWaveForm( const waveForm_t *wf )
{
float *table;
table = TableForFunc( wf->func );
return WAVEVALUE( table, wf->base, wf->amplitude, wf->phase, wf->frequency );
}
static float EvalWaveFormClamped( const waveForm_t *wf )
{
float glow = EvalWaveForm( wf );
if ( glow < 0 )
{
return 0;
}
if ( glow > 1 )
{
return 1;
}
return glow;
}
/*
** RB_CalcStretchTexMatrix
*/
void RB_CalcStretchTexMatrix( const waveForm_t *wf, float *matrix )
{
float p;
p = 1.0f / EvalWaveForm( wf );
matrix[0] = p; matrix[2] = 0; matrix[4] = 0.5f - 0.5f * p;
matrix[1] = 0; matrix[3] = p; matrix[5] = 0.5f - 0.5f * p;
}
/*
====================================================================
DEFORMATIONS
====================================================================
*/
/*
========================
RB_CalcDeformVertexes
========================
*/
void RB_CalcDeformVertexes( deformStage_t *ds ) {
int i;
vec3_t offset;
float scale;
float *xyz = ( float * ) tess.xyz;
int16_t *normal = tess.normal[0];
float *table;
// Ridah
if ( ds->deformationWave.frequency < 0 ) {
qboolean inverse = qfalse;
vec3_t worldUp;
//static vec3_t up = {0,0,1};
if ( VectorCompare( backEnd.currentEntity->e.fireRiseDir, vec3_origin ) ) {
VectorSet( backEnd.currentEntity->e.fireRiseDir, 0, 0, 1 );
}
// get the world up vector in local coordinates
if ( backEnd.currentEntity->e.hModel ) { // world surfaces dont have an axis
VectorRotate( backEnd.currentEntity->e.fireRiseDir, backEnd.currentEntity->e.axis, worldUp );
} else {
VectorCopy( backEnd.currentEntity->e.fireRiseDir, worldUp );
}
// don't go so far if sideways, since they must be moving
VectorScale( worldUp, 0.4 + 0.6 * fabs( backEnd.currentEntity->e.fireRiseDir[2] ), worldUp );
ds->deformationWave.frequency *= -1;
if ( ds->deformationWave.frequency > 999 ) { // hack for negative Z deformation (ack)
inverse = qtrue;
ds->deformationWave.frequency -= 999;
}
table = TableForFunc( ds->deformationWave.func );
for ( i = 0; i < tess.numVertexes; i++, xyz += 4, normal += 4 )
{
float off = ( xyz[0] + xyz[1] + xyz[2] ) * ds->deformationSpread;
float dot;
vec3_t fNormal;
R_VaoUnpackNormal(fNormal, normal);
scale = WAVEVALUE( table, ds->deformationWave.base,
ds->deformationWave.amplitude,
ds->deformationWave.phase + off,
ds->deformationWave.frequency );
dot = DotProduct( worldUp, fNormal );
if ( dot * scale > 0 ) {
if ( inverse ) {
scale *= -1;
}
VectorMA( xyz, dot * scale, worldUp, xyz );
}
}
if ( inverse ) {
ds->deformationWave.frequency += 999;
}
ds->deformationWave.frequency *= -1;
}
// done.
else if ( ds->deformationWave.frequency == 0 ) {
scale = EvalWaveForm( &ds->deformationWave );
for ( i = 0; i < tess.numVertexes; i++, xyz += 4, normal += 4 )
{
R_VaoUnpackNormal(offset, normal);
xyz[0] += offset[0] * scale;
xyz[1] += offset[1] * scale;
xyz[2] += offset[2] * scale;
}
}
else {
table = TableForFunc( ds->deformationWave.func );
for ( i = 0; i < tess.numVertexes; i++, xyz += 4, normal += 4 )
{
float off = ( xyz[0] + xyz[1] + xyz[2] ) * ds->deformationSpread;
scale = WAVEVALUE( table, ds->deformationWave.base,
ds->deformationWave.amplitude,
ds->deformationWave.phase + off,
ds->deformationWave.frequency );
R_VaoUnpackNormal(offset, normal);
xyz[0] += offset[0] * scale;
xyz[1] += offset[1] * scale;
xyz[2] += offset[2] * scale;
}
}
}
/*
=========================
RB_CalcDeformNormals
Wiggle the normals for wavy environment mapping
=========================
*/
void RB_CalcDeformNormals( deformStage_t *ds ) {
int i;
float scale;
float *xyz = ( float * ) tess.xyz;
int16_t *normal = tess.normal[0];
for ( i = 0; i < tess.numVertexes; i++, xyz += 4, normal += 4 ) {
vec3_t fNormal;
R_VaoUnpackNormal(fNormal, normal);
scale = 0.98f;
scale = R_NoiseGet4f( xyz[0] * scale, xyz[1] * scale, xyz[2] * scale,
tess.shaderTime * ds->deformationWave.frequency );
fNormal[ 0 ] += ds->deformationWave.amplitude * scale;
scale = 0.98f;
scale = R_NoiseGet4f( 100 + xyz[0] * scale, xyz[1] * scale, xyz[2] * scale,
tess.shaderTime * ds->deformationWave.frequency );
fNormal[ 1 ] += ds->deformationWave.amplitude * scale;
scale = 0.98f;
scale = R_NoiseGet4f( 200 + xyz[0] * scale, xyz[1] * scale, xyz[2] * scale,
tess.shaderTime * ds->deformationWave.frequency );
fNormal[ 2 ] += ds->deformationWave.amplitude * scale;
VectorNormalizeFast( fNormal );
R_VaoPackNormal(normal, fNormal);
}
}
/*
========================
RB_CalcBulgeVertexes
========================
*/
void RB_CalcBulgeVertexes( deformStage_t *ds ) {
int i;
const float *st = ( const float * ) tess.texCoords[0];
float *xyz = ( float * ) tess.xyz;
int16_t *normal = tess.normal[0];
double now;
now = backEnd.refdef.time * 0.001 * ds->bulgeSpeed;
for ( i = 0; i < tess.numVertexes; i++, xyz += 4, st += 2, normal += 4 ) {
int64_t off;
float scale;
vec3_t fNormal;
R_VaoUnpackNormal(fNormal, normal);
off = (float)( FUNCTABLE_SIZE / ( M_PI * 2 ) ) * ( st[0] * ds->bulgeWidth + now );
scale = tr.sinTable[ off & FUNCTABLE_MASK ] * ds->bulgeHeight;
xyz[0] += fNormal[0] * scale;
xyz[1] += fNormal[1] * scale;
xyz[2] += fNormal[2] * scale;
}
}
/*
======================
RB_CalcMoveVertexes
A deformation that can move an entire surface along a wave path
======================
*/
void RB_CalcMoveVertexes( deformStage_t *ds ) {
int i;
float *xyz;
float *table;
float scale;
vec3_t offset;
table = TableForFunc( ds->deformationWave.func );
scale = WAVEVALUE( table, ds->deformationWave.base,
ds->deformationWave.amplitude,
ds->deformationWave.phase,
ds->deformationWave.frequency );
VectorScale( ds->moveVector, scale, offset );
xyz = ( float * ) tess.xyz;
for ( i = 0; i < tess.numVertexes; i++, xyz += 4 ) {
VectorAdd( xyz, offset, xyz );
}
}
/*
=============
DeformText
Change a polygon into a bunch of text polygons
=============
*/
void DeformText( const char *text ) {
int i;
vec3_t origin, width, height;
int len;
int ch;
float color[4];
float bottom, top;
vec3_t mid;
vec3_t fNormal;
height[0] = 0;
height[1] = 0;
height[2] = -1;
R_VaoUnpackNormal(fNormal, tess.normal[0]);
CrossProduct( fNormal, height, width );
// find the midpoint of the box
VectorClear( mid );
bottom = 999999;
top = -999999;
for ( i = 0 ; i < 4 ; i++ ) {
VectorAdd( tess.xyz[i], mid, mid );
if ( tess.xyz[i][2] < bottom ) {
bottom = tess.xyz[i][2];
}
if ( tess.xyz[i][2] > top ) {
top = tess.xyz[i][2];
}
}
VectorScale( mid, 0.25f, origin );
// determine the individual character size
height[0] = 0;
height[1] = 0;
height[2] = ( top - bottom ) * 0.5f;
VectorScale( width, height[2] * -0.75f, width );
// determine the starting position
len = strlen( text );
VectorMA( origin, ( len - 1 ), width, origin );
// clear the shader indexes
tess.numIndexes = 0;
tess.numVertexes = 0;
tess.firstIndex = 0;
color[0] = color[1] = color[2] = color[3] = 1.0f;
// draw each character
for ( i = 0 ; i < len ; i++ ) {
ch = text[i];
ch &= 255;
if ( ch != ' ' ) {
int row, col;
float frow, fcol, size;
row = ch >> 4;
col = ch & 15;
frow = row * 0.0625f;
fcol = col * 0.0625f;
size = 0.0625f;
RB_AddQuadStampExt( origin, width, height, color, fcol, frow, fcol + size, frow + size );
}
VectorMA( origin, -2, width, origin );
}
}
/*
==================
GlobalVectorToLocal
==================
*/
void GlobalVectorToLocal( const vec3_t in, vec3_t out ) {
out[0] = DotProduct( in, backEnd.or.axis[0] );
out[1] = DotProduct( in, backEnd.or.axis[1] );
out[2] = DotProduct( in, backEnd.or.axis[2] );
}
/*
=====================
AutospriteDeform
Assuming all the triangles for this shader are independant
quads, rebuild them as forward facing sprites
=====================
*/
static void AutospriteDeform( void ) {
int i;
int oldVerts;
float *xyz;
vec3_t mid, delta;
float radius;
vec3_t left, up;
vec3_t leftDir, upDir;
if ( tess.numVertexes & 3 ) {
ri.Printf( PRINT_WARNING, "Autosprite shader %s had odd vertex count\n", tess.shader->name );
}
if ( tess.numIndexes != ( tess.numVertexes >> 2 ) * 6 ) {
ri.Printf( PRINT_WARNING, "Autosprite shader %s had odd index count\n", tess.shader->name );
}
oldVerts = tess.numVertexes;
tess.numVertexes = 0;
tess.numIndexes = 0;
tess.firstIndex = 0;
if ( backEnd.currentEntity != &tr.worldEntity ) {
GlobalVectorToLocal( backEnd.viewParms.or.axis[1], leftDir );
GlobalVectorToLocal( backEnd.viewParms.or.axis[2], upDir );
} else {
VectorCopy( backEnd.viewParms.or.axis[1], leftDir );
VectorCopy( backEnd.viewParms.or.axis[2], upDir );
}
for ( i = 0 ; i < oldVerts ; i += 4 ) {
vec4_t color;
// find the midpoint
xyz = tess.xyz[i];
mid[0] = 0.25f * ( xyz[0] + xyz[4] + xyz[8] + xyz[12] );
mid[1] = 0.25f * ( xyz[1] + xyz[5] + xyz[9] + xyz[13] );
mid[2] = 0.25f * ( xyz[2] + xyz[6] + xyz[10] + xyz[14] );
VectorSubtract( xyz, mid, delta );
radius = VectorLength( delta ) * 0.707f; // / sqrt(2)
VectorScale( leftDir, radius, left );
VectorScale( upDir, radius, up );
if ( backEnd.viewParms.isMirror ) {
VectorSubtract( vec3_origin, left, left );
}
// compensate for scale in the axes if necessary
if ( backEnd.currentEntity->e.nonNormalizedAxes ) {
float axisLength;
axisLength = VectorLength( backEnd.currentEntity->e.axis[0] );
if ( !axisLength ) {
axisLength = 0;
} else {
axisLength = 1.0f / axisLength;
}
VectorScale( left, axisLength, left );
VectorScale( up, axisLength, up );
}
VectorScale4(tess.color[i], 1.0f / 65535.0f, color);
RB_AddQuadStamp( mid, left, up, color );
}
}
/*
=====================
Autosprite2Deform
Autosprite2 will pivot a rectangular quad along the center of its long axis
=====================
*/
int edgeVerts[6][2] = {
{ 0, 1 },
{ 0, 2 },
{ 0, 3 },
{ 1, 2 },
{ 1, 3 },
{ 2, 3 }
};
static void Autosprite2Deform( void ) {
int i, j, k;
int indexes;
float *xyz;
vec3_t forward;
if ( tess.numVertexes & 3 ) {
ri.Printf( PRINT_WARNING, "Autosprite2 shader %s had odd vertex count\n", tess.shader->name );
}
if ( tess.numIndexes != ( tess.numVertexes >> 2 ) * 6 ) {
ri.Printf( PRINT_WARNING, "Autosprite2 shader %s had odd index count\n", tess.shader->name );
}
if ( backEnd.currentEntity != &tr.worldEntity ) {
GlobalVectorToLocal( backEnd.viewParms.or.axis[0], forward );
} else {
VectorCopy( backEnd.viewParms.or.axis[0], forward );
}
// this is a lot of work for two triangles...
// we could precalculate a lot of it is an issue, but it would mess up
// the shader abstraction
for ( i = 0, indexes = 0 ; i < tess.numVertexes ; i += 4, indexes += 6 ) {
float lengths[2];
int nums[2];
vec3_t mid[2];
vec3_t major, minor;
float *v1, *v2;
// find the midpoint
xyz = tess.xyz[i];
// identify the two shortest edges
nums[0] = nums[1] = 0;
lengths[0] = lengths[1] = 999999;
for ( j = 0 ; j < 6 ; j++ ) {
float l;
vec3_t temp;
v1 = xyz + 4 * edgeVerts[j][0];
v2 = xyz + 4 * edgeVerts[j][1];
VectorSubtract( v1, v2, temp );
l = DotProduct( temp, temp );
if ( l < lengths[0] ) {
nums[1] = nums[0];
lengths[1] = lengths[0];
nums[0] = j;
lengths[0] = l;
} else if ( l < lengths[1] ) {
nums[1] = j;
lengths[1] = l;
}
}
for ( j = 0 ; j < 2 ; j++ ) {
v1 = xyz + 4 * edgeVerts[nums[j]][0];
v2 = xyz + 4 * edgeVerts[nums[j]][1];
mid[j][0] = 0.5f * ( v1[0] + v2[0] );
mid[j][1] = 0.5f * ( v1[1] + v2[1] );
mid[j][2] = 0.5f * ( v1[2] + v2[2] );
}
// find the vector of the major axis
VectorSubtract( mid[1], mid[0], major );
// cross this with the view direction to get minor axis
CrossProduct( major, forward, minor );
VectorNormalize( minor );
// re-project the points
for ( j = 0 ; j < 2 ; j++ ) {
float l;
v1 = xyz + 4 * edgeVerts[nums[j]][0];
v2 = xyz + 4 * edgeVerts[nums[j]][1];
l = 0.5f * sqrt( lengths[j] );
// we need to see which direction this edge
// is used to determine direction of projection
for ( k = 0 ; k < 5 ; k++ ) {
if ( tess.indexes[ indexes + k ] == i + edgeVerts[nums[j]][0]
&& tess.indexes[ indexes + k + 1 ] == i + edgeVerts[nums[j]][1] ) {
break;
}
}
if ( k == 5 ) {
VectorMA( mid[j], l, minor, v1 );
VectorMA( mid[j], -l, minor, v2 );
} else {
VectorMA( mid[j], -l, minor, v1 );
VectorMA( mid[j], l, minor, v2 );
}
}
}
}
/*
=====================
RB_DeformTessGeometry
=====================
*/
void RB_DeformTessGeometry( void ) {
int i;
deformStage_t *ds;
if(!ShaderRequiresCPUDeforms(tess.shader))
{
// we don't need the following CPU deforms
return;
}
for ( i = 0 ; i < tess.shader->numDeforms ; i++ ) {
ds = &tess.shader->deforms[ i ];
switch ( ds->deformation ) {
case DEFORM_NONE:
break;
case DEFORM_NORMALS:
RB_CalcDeformNormals( ds );
break;
case DEFORM_WAVE:
RB_CalcDeformVertexes( ds );
break;
case DEFORM_BULGE:
RB_CalcBulgeVertexes( ds );
break;
case DEFORM_MOVE:
RB_CalcMoveVertexes( ds );
break;
case DEFORM_PROJECTION_SHADOW:
RB_ProjectionShadowDeform();
break;
case DEFORM_AUTOSPRITE:
AutospriteDeform();
break;
case DEFORM_AUTOSPRITE2:
Autosprite2Deform();
break;
case DEFORM_TEXT0:
case DEFORM_TEXT1:
case DEFORM_TEXT2:
case DEFORM_TEXT3:
case DEFORM_TEXT4:
case DEFORM_TEXT5:
case DEFORM_TEXT6:
case DEFORM_TEXT7:
DeformText( backEnd.refdef.text[ds->deformation - DEFORM_TEXT0] );
break;
}
}
}
/*
====================================================================
COLORS
====================================================================
*/
/*
** RB_CalcWaveColorSingle
*/
float RB_CalcWaveColorSingle( const waveForm_t *wf )
{
float glow;
if ( wf->func == GF_NOISE ) {
glow = wf->base + R_NoiseGet4f( 0, 0, 0, ( tess.shaderTime + wf->phase ) * wf->frequency ) * wf->amplitude;
} else {
glow = EvalWaveForm( wf ) * tr.identityLight;
}
if ( glow < 0 ) {
glow = 0;
}
else if ( glow > 1 ) {
glow = 1;
}
return glow;
}
/*
** RB_CalcWaveAlphaSingle
*/
float RB_CalcWaveAlphaSingle( const waveForm_t *wf )
{
return EvalWaveFormClamped( wf );
}
/*
** RB_CalcModulateColorsByFog
*/
void RB_CalcModulateColorsByFog( unsigned char *colors ) {
int i;
float texCoords[SHADER_MAX_VERTEXES][2] = {{0.0f}};
// calculate texcoords so we can derive density
// this is not wasted, because it would only have
// been previously called if the surface was opaque
RB_CalcFogTexCoords( texCoords[0] );
for ( i = 0; i < tess.numVertexes; i++, colors += 4 ) {
float f = 1.0 - R_FogFactor( texCoords[i][0], texCoords[i][1] );
colors[0] *= f;
colors[1] *= f;
colors[2] *= f;
}
}
/*
====================================================================
TEX COORDS
====================================================================
*/
/*
========================
RB_CalcFogTexCoords
To do the clipped fog plane really correctly, we should use
projected textures, but I don't trust the drivers and it
doesn't fit our shader data.
========================
*/
void RB_CalcFogTexCoords( float *st ) {
int i;
float *v;
float s, t;
float eyeT;
qboolean eyeOutside;
fog_t *fog;
vec3_t local;
vec4_t fogDistanceVector, fogDepthVector = {0, 0, 0, 0};
fog = tr.world->fogs + tess.fogNum;
// all fogging distance is based on world Z units
VectorSubtract( backEnd.or.origin, backEnd.viewParms.or.origin, local );
fogDistanceVector[0] = -backEnd.or.modelMatrix[2];
fogDistanceVector[1] = -backEnd.or.modelMatrix[6];
fogDistanceVector[2] = -backEnd.or.modelMatrix[10];
fogDistanceVector[3] = DotProduct( local, backEnd.viewParms.or.axis[0] );
// scale the fog vectors based on the fog's thickness
fogDistanceVector[0] *= fog->tcScale;
fogDistanceVector[1] *= fog->tcScale;
fogDistanceVector[2] *= fog->tcScale;
fogDistanceVector[3] *= fog->tcScale;
// rotate the gradient vector for this orientation
if ( fog->hasSurface ) {
fogDepthVector[0] = fog->surface[0] * backEnd.or.axis[0][0] +
fog->surface[1] * backEnd.or.axis[0][1] + fog->surface[2] * backEnd.or.axis[0][2];
fogDepthVector[1] = fog->surface[0] * backEnd.or.axis[1][0] +
fog->surface[1] * backEnd.or.axis[1][1] + fog->surface[2] * backEnd.or.axis[1][2];
fogDepthVector[2] = fog->surface[0] * backEnd.or.axis[2][0] +
fog->surface[1] * backEnd.or.axis[2][1] + fog->surface[2] * backEnd.or.axis[2][2];
fogDepthVector[3] = -fog->surface[3] + DotProduct( backEnd.or.origin, fog->surface );
eyeT = DotProduct( backEnd.or.viewOrigin, fogDepthVector ) + fogDepthVector[3];
} else {
eyeT = 1; // non-surface fog always has eye inside
}
// see if the viewpoint is outside
// this is needed for clipping distance even for constant fog
if ( eyeT < 0 ) {
eyeOutside = qtrue;
} else {
eyeOutside = qfalse;
}
fogDistanceVector[3] += 1.0 / 512;
// calculate density for each point
for ( i = 0, v = tess.xyz[0] ; i < tess.numVertexes ; i++, v += 4 ) {
// calculate the length in fog
s = DotProduct( v, fogDistanceVector ) + fogDistanceVector[3];
t = DotProduct( v, fogDepthVector ) + fogDepthVector[3];
// partially clipped fogs use the T axis
if ( eyeOutside ) {
if ( t < 1.0 ) {
t = 1.0 / 32; // point is outside, so no fogging
} else {
t = 1.0 / 32 + 30.0 / 32 * t / ( t - eyeT ); // cut the distance at the fog plane
}
} else {
if ( t < 0 ) {
t = 1.0 / 32; // point is outside, so no fogging
} else {
t = 31.0 / 32;
}
}
st[0] = s;
st[1] = t;
st += 2;
}
}
/*
** RB_CalcFireRiseEnvTexCoords
*/
void RB_CalcFireRiseEnvTexCoords( float *st ) {
int i;
float *v;
int16_t *normal = tess.normal[0];
vec3_t fNormal, viewer, reflected;
float d;
v = tess.xyz[0];
VectorNegate( backEnd.currentEntity->e.fireRiseDir, viewer );
for ( i = 0 ; i < tess.numVertexes ; i++, v += 4, normal += 4, st += 2 )
{
VectorNormalizeFast( viewer );
R_VaoUnpackNormal(fNormal, normal);
d = DotProduct( fNormal, viewer );
reflected[0] = fNormal[0] * 2 * d - viewer[0];
reflected[1] = fNormal[1] * 2 * d - viewer[1];
reflected[2] = fNormal[2] * 2 * d - viewer[2];
st[0] = 0.5 + reflected[1] * 0.5;
st[1] = 0.5 - reflected[2] * 0.5;
}
}
/*
** RB_CalcSwapTexCoords
*/
void RB_CalcSwapTexCoords( float *st ) {
int i;
for ( i = 0; i < tess.numVertexes; i++, st += 2 )
{
float s = st[0];
float t = st[1];
st[0] = t;
st[1] = 1.0 - s; // err, flaming effect needs this
}
}
/*
** RB_CalcTurbulentFactors
*/
void RB_CalcTurbulentFactors( const waveForm_t *wf, float *amplitude, float *now )
{
*now = wf->phase + tess.shaderTime * wf->frequency;
*amplitude = wf->amplitude;
}
/*
** RB_CalcScaleTexMatrix
*/
void RB_CalcScaleTexMatrix( const float scale[2], float *matrix )
{
matrix[0] = scale[0]; matrix[2] = 0.0f; matrix[4] = 0.0f;
matrix[1] = 0.0f; matrix[3] = scale[1]; matrix[5] = 0.0f;
}
/*
** RB_CalcScrollTexMatrix
*/
void RB_CalcScrollTexMatrix( const float scrollSpeed[2], float *matrix )
{
double timeScale = tess.shaderTime;
double adjustedScrollS, adjustedScrollT;
adjustedScrollS = scrollSpeed[0] * timeScale;
adjustedScrollT = scrollSpeed[1] * timeScale;
// clamp so coordinates don't continuously get larger, causing problems
// with hardware limits
adjustedScrollS = adjustedScrollS - floor( adjustedScrollS );
adjustedScrollT = adjustedScrollT - floor( adjustedScrollT );
matrix[0] = 1.0f; matrix[2] = 0.0f; matrix[4] = adjustedScrollS;
matrix[1] = 0.0f; matrix[3] = 1.0f; matrix[5] = adjustedScrollT;
}
/*
** RB_CalcTransformTexMatrix
*/
void RB_CalcTransformTexMatrix( const texModInfo_t *tmi, float *matrix )
{
matrix[0] = tmi->matrix[0][0]; matrix[2] = tmi->matrix[1][0]; matrix[4] = tmi->translate[0];
matrix[1] = tmi->matrix[0][1]; matrix[3] = tmi->matrix[1][1]; matrix[5] = tmi->translate[1];
}
/*
** RB_CalcRotateTexMatrix
*/
void RB_CalcRotateTexMatrix( float degsPerSecond, float *matrix )
{
double timeScale = tess.shaderTime;
double degs;
int64_t index;
float sinValue, cosValue;
degs = -degsPerSecond * timeScale;
index = degs * ( FUNCTABLE_SIZE / 360.0f );
sinValue = tr.sinTable[ index & FUNCTABLE_MASK ];
cosValue = tr.sinTable[ ( index + FUNCTABLE_SIZE / 4 ) & FUNCTABLE_MASK ];
matrix[0] = cosValue; matrix[2] = -sinValue; matrix[4] = 0.5 - 0.5 * cosValue + 0.5 * sinValue;
matrix[1] = sinValue; matrix[3] = cosValue; matrix[5] = 0.5 - 0.5 * sinValue - 0.5 * cosValue;
}
|