Debian Codegraph @

code/renderergl1/tr_surface.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_surf.c
23 #include "tr_local.h"
24
25 /*
26
27 THIS ENTIRE FILE IS BACK END
28
29 backEnd.currentEntity will be valid.
30
31 Tess_Begin has already been called for the surface's shader.
32
33 The modelview matrix will be set.
34
35 It is safe to actually issue drawing commands here if you don't want to
36 use the shader system.
37 */
38
39
40 //============================================================================
41
42
43 /*
44 ==============
45 RB_CheckOverflow
46 ==============
47 */
48 void RB_CheckOverflow( int verts, int indexes ) {
49 if (tess.numVertexes + verts < SHADER_MAX_VERTEXES
50 && tess.numIndexes + indexes < SHADER_MAX_INDEXES) {
51 return;
52 }
53
54 RB_EndSurface();
55
56 if ( verts >= SHADER_MAX_VERTEXES ) {
57 ri.Error(ERR_DROP, "RB_CheckOverflow: verts > MAX (%d > %d)", verts, SHADER_MAX_VERTEXES );
58 }
59 if ( indexes >= SHADER_MAX_INDEXES ) {
60 ri.Error(ERR_DROP, "RB_CheckOverflow: indices > MAX (%d > %d)", indexes, SHADER_MAX_INDEXES );
61 }
62
63 RB_BeginSurface(tess.shader, tess.fogNum );
64 }
65
66
67 /*
68 ==============
69 RB_AddQuadStampExt
70 ==============
71 */
72 void RB_AddQuadStampExt( vec3_t origin, vec3_t left, vec3_t up, byte *color, float s1, float t1, float s2, float t2 ) {
73 vec3_t normal;
74 int ndx;
75
76 RB_CHECKOVERFLOW( 4, 6 );
77
78 ndx = tess.numVertexes;
79
80 // triangle indexes for a simple quad
81 tess.indexes[ tess.numIndexes ] = ndx;
82 tess.indexes[ tess.numIndexes + 1 ] = ndx + 1;
83 tess.indexes[ tess.numIndexes + 2 ] = ndx + 3;
84
85 tess.indexes[ tess.numIndexes + 3 ] = ndx + 3;
86 tess.indexes[ tess.numIndexes + 4 ] = ndx + 1;
87 tess.indexes[ tess.numIndexes + 5 ] = ndx + 2;
88
89 tess.xyz[ndx][0] = origin[0] + left[0] + up[0];
90 tess.xyz[ndx][1] = origin[1] + left[1] + up[1];
91 tess.xyz[ndx][2] = origin[2] + left[2] + up[2];
92
93 tess.xyz[ndx+1][0] = origin[0] - left[0] + up[0];
94 tess.xyz[ndx+1][1] = origin[1] - left[1] + up[1];
95 tess.xyz[ndx+1][2] = origin[2] - left[2] + up[2];
96
97 tess.xyz[ndx+2][0] = origin[0] - left[0] - up[0];
98 tess.xyz[ndx+2][1] = origin[1] - left[1] - up[1];
99 tess.xyz[ndx+2][2] = origin[2] - left[2] - up[2];
100
101 tess.xyz[ndx+3][0] = origin[0] + left[0] - up[0];
102 tess.xyz[ndx+3][1] = origin[1] + left[1] - up[1];
103 tess.xyz[ndx+3][2] = origin[2] + left[2] - up[2];
104
105
106 // constant normal all the way around
107 VectorSubtract( vec3_origin, backEnd.viewParms.or.axis[0], normal );
108
109 tess.normal[ndx][0] = tess.normal[ndx+1][0] = tess.normal[ndx+2][0] = tess.normal[ndx+3][0] = normal[0];
110 tess.normal[ndx][1] = tess.normal[ndx+1][1] = tess.normal[ndx+2][1] = tess.normal[ndx+3][1] = normal[1];
111 tess.normal[ndx][2] = tess.normal[ndx+1][2] = tess.normal[ndx+2][2] = tess.normal[ndx+3][2] = normal[2];
112
113 // standard square texture coordinates
114 tess.texCoords[ndx][0][0] = tess.texCoords[ndx][1][0] = s1;
115 tess.texCoords[ndx][0][1] = tess.texCoords[ndx][1][1] = t1;
116
117 tess.texCoords[ndx+1][0][0] = tess.texCoords[ndx+1][1][0] = s2;
118 tess.texCoords[ndx+1][0][1] = tess.texCoords[ndx+1][1][1] = t1;
119
120 tess.texCoords[ndx+2][0][0] = tess.texCoords[ndx+2][1][0] = s2;
121 tess.texCoords[ndx+2][0][1] = tess.texCoords[ndx+2][1][1] = t2;
122
123 tess.texCoords[ndx+3][0][0] = tess.texCoords[ndx+3][1][0] = s1;
124 tess.texCoords[ndx+3][0][1] = tess.texCoords[ndx+3][1][1] = t2;
125
126 // constant color all the way around
127 // should this be identity and let the shader specify from entity?
128 * ( unsigned int * ) &tess.vertexColors[ndx] =
129 * ( unsigned int * ) &tess.vertexColors[ndx+1] =
130 * ( unsigned int * ) &tess.vertexColors[ndx+2] =
131 * ( unsigned int * ) &tess.vertexColors[ndx+3] =
132 * ( unsigned int * )color;
133
134
135 tess.numVertexes += 4;
136 tess.numIndexes += 6;
137 }
138
139 /*
140 ==============
141 RB_AddQuadStamp
142 ==============
143 */
144 void RB_AddQuadStamp( vec3_t origin, vec3_t left, vec3_t up, byte *color ) {
145 RB_AddQuadStampExt( origin, left, up, color, 0, 0, 1, 1 );
146 }
147
148 /*
149 ==============
150 RB_SurfaceSprite
151 ==============
152 */
153 static void RB_SurfaceSprite( void ) {
154 vec3_t left, up;
155 float radius;
156
157 // calculate the xyz locations for the four corners
158 radius = backEnd.currentEntity->e.radius;
159 if ( backEnd.currentEntity->e.rotation == 0 ) {
160 VectorScale( backEnd.viewParms.or.axis[1], radius, left );
161 VectorScale( backEnd.viewParms.or.axis[2], radius, up );
162 } else {
163 float s, c;
164 float ang;
165
166 ang = M_PI * backEnd.currentEntity->e.rotation / 180;
167 s = sin( ang );
168 c = cos( ang );
169
170 VectorScale( backEnd.viewParms.or.axis[1], c * radius, left );
171 VectorMA( left, -s * radius, backEnd.viewParms.or.axis[2], left );
172
173 VectorScale( backEnd.viewParms.or.axis[2], c * radius, up );
174 VectorMA( up, s * radius, backEnd.viewParms.or.axis[1], up );
175 }
176 if ( backEnd.viewParms.isMirror ) {
177 VectorSubtract( vec3_origin, left, left );
178 }
179
180 RB_AddQuadStamp( backEnd.currentEntity->e.origin, left, up, backEnd.currentEntity->e.shaderRGBA );
181 }
182
183
184 /*
185 =============
186 RB_SurfacePolychain
187 =============
188 */
189 static void RB_SurfacePolychain( srfPoly_t *p ) {
190 int i;
191 int numv;
192
193 RB_CHECKOVERFLOW( p->numVerts, 3*(p->numVerts - 2) );
194
195 // fan triangles into the tess array
196 numv = tess.numVertexes;
197 for ( i = 0; i < p->numVerts; i++ ) {
198 VectorCopy( p->verts[i].xyz, tess.xyz[numv] );
199 tess.texCoords[numv][0][0] = p->verts[i].st[0];
200 tess.texCoords[numv][0][1] = p->verts[i].st[1];
201 *(int *)&tess.vertexColors[numv] = *(int *)p->verts[ i ].modulate;
202
203 numv++;
204 }
205
206 // generate fan indexes into the tess array
207 for ( i = 0; i < p->numVerts-2; i++ ) {
208 tess.indexes[tess.numIndexes + 0] = tess.numVertexes;
209 tess.indexes[tess.numIndexes + 1] = tess.numVertexes + i + 1;
210 tess.indexes[tess.numIndexes + 2] = tess.numVertexes + i + 2;
211 tess.numIndexes += 3;
212 }
213
214 tess.numVertexes = numv;
215 }
216
217
218 /*
219 =============
220 RB_SurfaceTriangles
221 =============
222 */
223 static void RB_SurfaceTriangles( srfTriangles_t *srf ) {
224 int i;
225 drawVert_t *dv;
226 float *xyz, *normal, *texCoords;
227 byte *color;
228 int dlightBits;
229 qboolean needsNormal;
230
231 dlightBits = srf->dlightBits;
232 tess.dlightBits |= dlightBits;
233
234 RB_CHECKOVERFLOW( srf->numVerts, srf->numIndexes );
235
236 for ( i = 0 ; i < srf->numIndexes ; i += 3 ) {
237 tess.indexes[ tess.numIndexes + i + 0 ] = tess.numVertexes + srf->indexes[ i + 0 ];
238 tess.indexes[ tess.numIndexes + i + 1 ] = tess.numVertexes + srf->indexes[ i + 1 ];
239 tess.indexes[ tess.numIndexes + i + 2 ] = tess.numVertexes + srf->indexes[ i + 2 ];
240 }
241 tess.numIndexes += srf->numIndexes;
242
243 dv = srf->verts;
244 xyz = tess.xyz[ tess.numVertexes ];
245 normal = tess.normal[ tess.numVertexes ];
246 texCoords = tess.texCoords[ tess.numVertexes ][0];
247 color = tess.vertexColors[ tess.numVertexes ];
248 needsNormal = tess.shader->needsNormal;
249
250 for ( i = 0 ; i < srf->numVerts ; i++, dv++, xyz += 4, normal += 4, texCoords += 4, color += 4 ) {
251 xyz[0] = dv->xyz[0];
252 xyz[1] = dv->xyz[1];
253 xyz[2] = dv->xyz[2];
254
255 if ( needsNormal ) {
256 normal[0] = dv->normal[0];
257 normal[1] = dv->normal[1];
258 normal[2] = dv->normal[2];
259 }
260
261 texCoords[0] = dv->st[0];
262 texCoords[1] = dv->st[1];
263
264 texCoords[2] = dv->lightmap[0];
265 texCoords[3] = dv->lightmap[1];
266
267 *(int *)color = *(int *)dv->color;
268 }
269
270 for ( i = 0 ; i < srf->numVerts ; i++ ) {
271 tess.vertexDlightBits[ tess.numVertexes + i] = dlightBits;
272 }
273
274 tess.numVertexes += srf->numVerts;
275 }
276
277
278
279 /*
280 ==============
281 RB_SurfaceBeam
282 ==============
283 */
284 static void RB_SurfaceBeam( void )
285 {
286 #define NUM_BEAM_SEGS 6
287 refEntity_t *e;
288 int i;
289 vec3_t perpvec;
290 vec3_t direction, normalized_direction;
291 vec3_t start_points[NUM_BEAM_SEGS], end_points[NUM_BEAM_SEGS];
292 vec3_t oldorigin, origin;
293
294 e = &backEnd.currentEntity->e;
295
296 oldorigin[0] = e->oldorigin[0];
297 oldorigin[1] = e->oldorigin[1];
298 oldorigin[2] = e->oldorigin[2];
299
300 origin[0] = e->origin[0];
301 origin[1] = e->origin[1];
302 origin[2] = e->origin[2];
303
304 normalized_direction[0] = direction[0] = oldorigin[0] - origin[0];
305 normalized_direction[1] = direction[1] = oldorigin[1] - origin[1];
306 normalized_direction[2] = direction[2] = oldorigin[2] - origin[2];
307
308 if ( VectorNormalize( normalized_direction ) == 0 )
309 return;
310
311 PerpendicularVector( perpvec, normalized_direction );
312
313 VectorScale( perpvec, 4, perpvec );
314
315 for ( i = 0; i < NUM_BEAM_SEGS ; i++ )
316 {
317 RotatePointAroundVector( start_points[i], normalized_direction, perpvec, (360.0/NUM_BEAM_SEGS)*i );
318 // VectorAdd( start_points[i], origin, start_points[i] );
319 VectorAdd( start_points[i], direction, end_points[i] );
320 }
321
322 GL_Bind( tr.whiteImage );
323
324 GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
325
326 qglColor3f( 1, 0, 0 );
327
328 qglBegin( GL_TRIANGLE_STRIP );
329 for ( i = 0; i <= NUM_BEAM_SEGS; i++ ) {
330 qglVertex3fv( start_points[ i % NUM_BEAM_SEGS] );
331 qglVertex3fv( end_points[ i % NUM_BEAM_SEGS] );
332 }
333 qglEnd();
334 }
335
336 //================================================================================
337
338 static void DoRailCore( const vec3_t start, const vec3_t end, const vec3_t up, float len, float spanWidth )
339 {
340 float spanWidth2;
341 int vbase;
342 float t = len / 256.0f;
343
344 RB_CHECKOVERFLOW( 4, 6 );
345
346 vbase = tess.numVertexes;
347
348 spanWidth2 = -spanWidth;
349
350 // FIXME: use quad stamp?
351 VectorMA( start, spanWidth, up, tess.xyz[tess.numVertexes] );
352 tess.texCoords[tess.numVertexes][0][0] = 0;
353 tess.texCoords[tess.numVertexes][0][1] = 0;
354 tess.vertexColors[tess.numVertexes][0] = backEnd.currentEntity->e.shaderRGBA[0] * 0.25;
355 tess.vertexColors[tess.numVertexes][1] = backEnd.currentEntity->e.shaderRGBA[1] * 0.25;
356 tess.vertexColors[tess.numVertexes][2] = backEnd.currentEntity->e.shaderRGBA[2] * 0.25;
357 tess.numVertexes++;
358
359 VectorMA( start, spanWidth2, up, tess.xyz[tess.numVertexes] );
360 tess.texCoords[tess.numVertexes][0][0] = 0;
361 tess.texCoords[tess.numVertexes][0][1] = 1;
362 tess.vertexColors[tess.numVertexes][0] = backEnd.currentEntity->e.shaderRGBA[0];
363 tess.vertexColors[tess.numVertexes][1] = backEnd.currentEntity->e.shaderRGBA[1];
364 tess.vertexColors[tess.numVertexes][2] = backEnd.currentEntity->e.shaderRGBA[2];
365 tess.numVertexes++;
366
367 VectorMA( end, spanWidth, up, tess.xyz[tess.numVertexes] );
368
369 tess.texCoords[tess.numVertexes][0][0] = t;
370 tess.texCoords[tess.numVertexes][0][1] = 0;
371 tess.vertexColors[tess.numVertexes][0] = backEnd.currentEntity->e.shaderRGBA[0];
372 tess.vertexColors[tess.numVertexes][1] = backEnd.currentEntity->e.shaderRGBA[1];
373 tess.vertexColors[tess.numVertexes][2] = backEnd.currentEntity->e.shaderRGBA[2];
374 tess.numVertexes++;
375
376 VectorMA( end, spanWidth2, up, tess.xyz[tess.numVertexes] );
377 tess.texCoords[tess.numVertexes][0][0] = t;
378 tess.texCoords[tess.numVertexes][0][1] = 1;
379 tess.vertexColors[tess.numVertexes][0] = backEnd.currentEntity->e.shaderRGBA[0];
380 tess.vertexColors[tess.numVertexes][1] = backEnd.currentEntity->e.shaderRGBA[1];
381 tess.vertexColors[tess.numVertexes][2] = backEnd.currentEntity->e.shaderRGBA[2];
382 tess.numVertexes++;
383
384 tess.indexes[tess.numIndexes++] = vbase;
385 tess.indexes[tess.numIndexes++] = vbase + 1;
386 tess.indexes[tess.numIndexes++] = vbase + 2;
387
388 tess.indexes[tess.numIndexes++] = vbase + 2;
389 tess.indexes[tess.numIndexes++] = vbase + 1;
390 tess.indexes[tess.numIndexes++] = vbase + 3;
391 }
392
393 static void DoRailDiscs( int numSegs, const vec3_t start, const vec3_t dir, const vec3_t right, const vec3_t up )
394 {
395 int i;
396 vec3_t pos[4];
397 vec3_t v;
398 int spanWidth = r_railWidth->integer;
399 float c, s;
400 float scale;
401
402 if ( numSegs > 1 )
403 numSegs--;
404 if ( !numSegs )
405 return;
406
407 scale = 0.25;
408
409 for ( i = 0; i < 4; i++ )
410 {
411 c = cos( DEG2RAD( 45 + i * 90 ) );
412 s = sin( DEG2RAD( 45 + i * 90 ) );
413 v[0] = ( right[0] * c + up[0] * s ) * scale * spanWidth;
414 v[1] = ( right[1] * c + up[1] * s ) * scale * spanWidth;
415 v[2] = ( right[2] * c + up[2] * s ) * scale * spanWidth;
416 VectorAdd( start, v, pos[i] );
417
418 if ( numSegs > 1 )
419 {
420 // offset by 1 segment if we're doing a long distance shot
421 VectorAdd( pos[i], dir, pos[i] );
422 }
423 }
424
425 for ( i = 0; i < numSegs; i++ )
426 {
427 int j;
428
429 RB_CHECKOVERFLOW( 4, 6 );
430
431 for ( j = 0; j < 4; j++ )
432 {
433 VectorCopy( pos[j], tess.xyz[tess.numVertexes] );
434 tess.texCoords[tess.numVertexes][0][0] = ( j < 2 );
435 tess.texCoords[tess.numVertexes][0][1] = ( j && j != 3 );
436 tess.vertexColors[tess.numVertexes][0] = backEnd.currentEntity->e.shaderRGBA[0];
437 tess.vertexColors[tess.numVertexes][1] = backEnd.currentEntity->e.shaderRGBA[1];
438 tess.vertexColors[tess.numVertexes][2] = backEnd.currentEntity->e.shaderRGBA[2];
439 tess.numVertexes++;
440
441 VectorAdd( pos[j], dir, pos[j] );
442 }
443
444 tess.indexes[tess.numIndexes++] = tess.numVertexes - 4 + 0;
445 tess.indexes[tess.numIndexes++] = tess.numVertexes - 4 + 1;
446 tess.indexes[tess.numIndexes++] = tess.numVertexes - 4 + 3;
447 tess.indexes[tess.numIndexes++] = tess.numVertexes - 4 + 3;
448 tess.indexes[tess.numIndexes++] = tess.numVertexes - 4 + 1;
449 tess.indexes[tess.numIndexes++] = tess.numVertexes - 4 + 2;
450 }
451 }
452
453 /*
454 ** RB_SurfaceRailRinges
455 */
456 static void RB_SurfaceRailRings( void ) {
457 refEntity_t *e;
458 int numSegs;
459 int len;
460 vec3_t vec;
461 vec3_t right, up;
462 vec3_t start, end;
463
464 e = &backEnd.currentEntity->e;
465
466 VectorCopy( e->oldorigin, start );
467 VectorCopy( e->origin, end );
468
469 // compute variables
470 VectorSubtract( end, start, vec );
471 len = VectorNormalize( vec );
472 MakeNormalVectors( vec, right, up );
473 numSegs = ( len ) / r_railSegmentLength->value;
474 if ( numSegs <= 0 ) {
475 numSegs = 1;
476 }
477
478 VectorScale( vec, r_railSegmentLength->value, vec );
479
480 DoRailDiscs( numSegs, start, vec, right, up );
481 }
482
483 /*
484 ** RB_SurfaceRailCore
485 */
486 static void RB_SurfaceRailCore( void ) {
487 refEntity_t *e;
488 int len;
489 vec3_t right;
490 vec3_t vec;
491 vec3_t start, end;
492 vec3_t v1, v2;
493
494 e = &backEnd.currentEntity->e;
495
496 VectorCopy( e->oldorigin, start );
497 VectorCopy( e->origin, end );
498
499 VectorSubtract( end, start, vec );
500 len = VectorNormalize( vec );
501
502 // compute side vector
503 VectorSubtract( start, backEnd.viewParms.or.origin, v1 );
504 VectorNormalize( v1 );
505 VectorSubtract( end, backEnd.viewParms.or.origin, v2 );
506 VectorNormalize( v2 );
507 CrossProduct( v1, v2, right );
508 VectorNormalize( right );
509
510 DoRailCore( start, end, right, len, r_railCoreWidth->integer );
511 }
512
513 /*
514 ** RB_SurfaceLightningBolt
515 */
516 static void RB_SurfaceLightningBolt( void ) {
517 refEntity_t *e;
518 int len;
519 vec3_t right;
520 vec3_t vec;
521 vec3_t start, end;
522 vec3_t v1, v2;
523 int i;
524
525 e = &backEnd.currentEntity->e;
526
527 VectorCopy( e->oldorigin, end );
528 VectorCopy( e->origin, start );
529
530 // compute variables
531 VectorSubtract( end, start, vec );
532 len = VectorNormalize( vec );
533
534 // compute side vector
535 VectorSubtract( start, backEnd.viewParms.or.origin, v1 );
536 VectorNormalize( v1 );
537 VectorSubtract( end, backEnd.viewParms.or.origin, v2 );
538 VectorNormalize( v2 );
539 CrossProduct( v1, v2, right );
540 VectorNormalize( right );
541
542 for ( i = 0 ; i < 4 ; i++ ) {
543 vec3_t temp;
544
545 DoRailCore( start, end, right, len, 8 );
546 RotatePointAroundVector( temp, vec, right, 45 );
547 VectorCopy( temp, right );
548 }
549 }
550
551 /*
552 ** VectorArrayNormalize
553 *
554 * The inputs to this routing seem to always be close to length = 1.0 (about 0.6 to 2.0)
555 * This means that we don't have to worry about zero length or enormously long vectors.
556 */
557 void VectorArrayNormalize(vec4_t *normals, unsigned int count)
558 {
559 // assert(count);
560
561 #if idppc
562 {
563 float half = 0.5;
564 float one = 1.0;
565 float *components = (float *)normals;
566
567 // Vanilla PPC code, but since PPC has a reciprocal square root estimate instruction,
568 // runs *much* faster than calling sqrt(). We'll use a single Newton-Raphson
569 // refinement step to get a little more precision. This seems to yield results
570 // that are correct to 3 decimal places and usually correct to at least 4 (sometimes 5).
571 // (That is, for the given input range of about 0.6 to 2.0).
572 do {
573 float x, y, z;
574 float B, y0, y1;
575
576 x = components[0];
577 y = components[1];
578 z = components[2];
579 components += 4;
580 B = x*x + y*y + z*z;
581
582 #ifdef __GNUC__
583 asm("frsqrte %0,%1" : "=f" (y0) : "f" (B));
584 #else
585 y0 = __frsqrte(B);
586 #endif
587 y1 = y0 + half*y0*(one - B*y0*y0);
588
589 x = x * y1;
590 y = y * y1;
591 components[-4] = x;
592 z = z * y1;
593 components[-3] = y;
594 components[-2] = z;
595 } while(count--);
596 }
597 #else // No assembly version for this architecture, or C_ONLY defined
598 // given the input, it's safe to call VectorNormalizeFast
599 while (count--) {
600 VectorNormalizeFast(normals[0]);
601 normals++;
602 }
603 #endif
604
605 }
606
607
608
609 /*
610 ** LerpMeshVertexes
611 */
612 static void LerpMeshVertexes_scalar(md3Surface_t *surf, float backlerp)
613 {
614 short *oldXyz, *newXyz, *oldNormals, *newNormals;
615 float *outXyz, *outNormal;
616 float oldXyzScale, newXyzScale;
617 float oldNormalScale, newNormalScale;
618 int vertNum;
619 unsigned lat, lng;
620 int numVerts;
621
622 outXyz = tess.xyz[tess.numVertexes];
623 outNormal = tess.normal[tess.numVertexes];
624
625 newXyz = (short *)((byte *)surf + surf->ofsXyzNormals)
626 + (backEnd.currentEntity->e.frame * surf->numVerts * 4);
627 newNormals = newXyz + 3;
628
629 newXyzScale = MD3_XYZ_SCALE * (1.0 - backlerp);
630 newNormalScale = 1.0 - backlerp;
631
632 numVerts = surf->numVerts;
633
634 if ( backlerp == 0 ) {
635 //
636 // just copy the vertexes
637 //
638 for (vertNum=0 ; vertNum < numVerts ; vertNum++,
639 newXyz += 4, newNormals += 4,
640 outXyz += 4, outNormal += 4)
641 {
642
643 outXyz[0] = newXyz[0] * newXyzScale;
644 outXyz[1] = newXyz[1] * newXyzScale;
645 outXyz[2] = newXyz[2] * newXyzScale;
646
647 lat = ( newNormals[0] >> 8 ) & 0xff;
648 lng = ( newNormals[0] & 0xff );
649 lat *= (FUNCTABLE_SIZE/256);
650 lng *= (FUNCTABLE_SIZE/256);
651
652 // decode X as cos( lat ) * sin( long )
653 // decode Y as sin( lat ) * sin( long )
654 // decode Z as cos( long )
655
656 outNormal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng];
657 outNormal[1] = tr.sinTable[lat] * tr.sinTable[lng];
658 outNormal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK];
659 }
660 } else {
661 //
662 // interpolate and copy the vertex and normal
663 //
664 oldXyz = (short *)((byte *)surf + surf->ofsXyzNormals)
665 + (backEnd.currentEntity->e.oldframe * surf->numVerts * 4);
666 oldNormals = oldXyz + 3;
667
668 oldXyzScale = MD3_XYZ_SCALE * backlerp;
669 oldNormalScale = backlerp;
670
671 for (vertNum=0 ; vertNum < numVerts ; vertNum++,
672 oldXyz += 4, newXyz += 4, oldNormals += 4, newNormals += 4,
673 outXyz += 4, outNormal += 4)
674 {
675 vec3_t uncompressedOldNormal, uncompressedNewNormal;
676
677 // interpolate the xyz
678 outXyz[0] = oldXyz[0] * oldXyzScale + newXyz[0] * newXyzScale;
679 outXyz[1] = oldXyz[1] * oldXyzScale + newXyz[1] * newXyzScale;
680 outXyz[2] = oldXyz[2] * oldXyzScale + newXyz[2] * newXyzScale;
681
682 // FIXME: interpolate lat/long instead?
683 lat = ( newNormals[0] >> 8 ) & 0xff;
684 lng = ( newNormals[0] & 0xff );
685 lat *= 4;
686 lng *= 4;
687 uncompressedNewNormal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng];
688 uncompressedNewNormal[1] = tr.sinTable[lat] * tr.sinTable[lng];
689 uncompressedNewNormal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK];
690
691 lat = ( oldNormals[0] >> 8 ) & 0xff;
692 lng = ( oldNormals[0] & 0xff );
693 lat *= 4;
694 lng *= 4;
695
696 uncompressedOldNormal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng];
697 uncompressedOldNormal[1] = tr.sinTable[lat] * tr.sinTable[lng];
698 uncompressedOldNormal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK];
699
700 outNormal[0] = uncompressedOldNormal[0] * oldNormalScale + uncompressedNewNormal[0] * newNormalScale;
701 outNormal[1] = uncompressedOldNormal[1] * oldNormalScale + uncompressedNewNormal[1] * newNormalScale;
702 outNormal[2] = uncompressedOldNormal[2] * oldNormalScale + uncompressedNewNormal[2] * newNormalScale;
703
704 // VectorNormalize (outNormal);
705 }
706 VectorArrayNormalize((vec4_t *)tess.normal[tess.numVertexes], numVerts);
707 }
708 }
709
710 static void LerpMeshVertexes(md3Surface_t *surf, float backlerp)
711 {
712 #if idppc_altivec
713 if (com_altivec->integer) {
714 // must be in a separate translation unit or G3 systems will crash.
715 LerpMeshVertexes_altivec( surf, backlerp );
716 return;
717 }
718 #endif // idppc_altivec
719 LerpMeshVertexes_scalar( surf, backlerp );
720 }
721
722
723 /*
724 =============
725 RB_SurfaceMesh
726 =============
727 */
728 static void RB_SurfaceMesh(md3Surface_t *surface) {
729 int j;
730 float backlerp;
731 int *triangles;
732 float *texCoords;
733 int indexes;
734 int Bob, Doug;
735 int numVerts;
736
737 if ( backEnd.currentEntity->e.oldframe == backEnd.currentEntity->e.frame ) {
738 backlerp = 0;
739 } else {
740 backlerp = backEnd.currentEntity->e.backlerp;
741 }
742
743 RB_CHECKOVERFLOW( surface->numVerts, surface->numTriangles*3 );
744
745 LerpMeshVertexes (surface, backlerp);
746
747 triangles = (int *) ((byte *)surface + surface->ofsTriangles);
748 indexes = surface->numTriangles * 3;
749 Bob = tess.numIndexes;
750 Doug = tess.numVertexes;
751 for (j = 0 ; j < indexes ; j++) {
752 tess.indexes[Bob + j] = Doug + triangles[j];
753 }
754 tess.numIndexes += indexes;
755
756 texCoords = (float *) ((byte *)surface + surface->ofsSt);
757
758 numVerts = surface->numVerts;
759 for ( j = 0; j < numVerts; j++ ) {
760 tess.texCoords[Doug + j][0][0] = texCoords[j*2+0];
761 tess.texCoords[Doug + j][0][1] = texCoords[j*2+1];
762 // FIXME: fill in lightmapST for completeness?
763 }
764
765 tess.numVertexes += surface->numVerts;
766
767 }
768
769
770 /*
771 ==============
772 RB_SurfaceFace
773 ==============
774 */
775 static void RB_SurfaceFace( srfSurfaceFace_t *surf ) {
776 int i;
777 unsigned *indices;
778 glIndex_t *tessIndexes;
779 float *v;
780 float *normal;
781 int ndx;
782 int Bob;
783 int numPoints;
784 int dlightBits;
785
786 RB_CHECKOVERFLOW( surf->numPoints, surf->numIndices );
787
788 dlightBits = surf->dlightBits;
789 tess.dlightBits |= dlightBits;
790
791 indices = ( unsigned * ) ( ( ( char * ) surf ) + surf->ofsIndices );
792
793 Bob = tess.numVertexes;
794 tessIndexes = tess.indexes + tess.numIndexes;
795 for ( i = surf->numIndices-1 ; i >= 0 ; i-- ) {
796 tessIndexes[i] = indices[i] + Bob;
797 }
798
799 tess.numIndexes += surf->numIndices;
800
801 numPoints = surf->numPoints;
802
803 if ( tess.shader->needsNormal ) {
804 normal = surf->plane.normal;
805 for ( i = 0, ndx = tess.numVertexes; i < numPoints; i++, ndx++ ) {
806 VectorCopy( normal, tess.normal[ndx] );
807 }
808 }
809
810 for ( i = 0, v = surf->points[0], ndx = tess.numVertexes; i < numPoints; i++, v += VERTEXSIZE, ndx++ ) {
811 VectorCopy( v, tess.xyz[ndx]);
812 tess.texCoords[ndx][0][0] = v[3];
813 tess.texCoords[ndx][0][1] = v[4];
814 tess.texCoords[ndx][1][0] = v[5];
815 tess.texCoords[ndx][1][1] = v[6];
816 * ( unsigned int * ) &tess.vertexColors[ndx] = * ( unsigned int * ) &v[7];
817 tess.vertexDlightBits[ndx] = dlightBits;
818 }
819
820
821 tess.numVertexes += surf->numPoints;
822 }
823
824
825 static float LodErrorForVolume( vec3_t local, float radius ) {
826 vec3_t world;
827 float d;
828
829 // never let it go negative
830 if ( r_lodCurveError->value < 0 ) {
831 return 0;
832 }
833
834 world[0] = local[0] * backEnd.or.axis[0][0] + local[1] * backEnd.or.axis[1][0] +
835 local[2] * backEnd.or.axis[2][0] + backEnd.or.origin[0];
836 world[1] = local[0] * backEnd.or.axis[0][1] + local[1] * backEnd.or.axis[1][1] +
837 local[2] * backEnd.or.axis[2][1] + backEnd.or.origin[1];
838 world[2] = local[0] * backEnd.or.axis[0][2] + local[1] * backEnd.or.axis[1][2] +
839 local[2] * backEnd.or.axis[2][2] + backEnd.or.origin[2];
840
841 VectorSubtract( world, backEnd.viewParms.or.origin, world );
842 d = DotProduct( world, backEnd.viewParms.or.axis[0] );
843
844 if ( d < 0 ) {
845 d = -d;
846 }
847 d -= radius;
848 if ( d < 1 ) {
849 d = 1;
850 }
851
852 return r_lodCurveError->value / d;
853 }
854
855 /*
856 =============
857 RB_SurfaceGrid
858
859 Just copy the grid of points and triangulate
860 =============
861 */
862 static void RB_SurfaceGrid( srfGridMesh_t *cv ) {
863 int i, j;
864 float *xyz;
865 float *texCoords;
866 float *normal;
867 unsigned char *color;
868 drawVert_t *dv;
869 int rows, irows, vrows;
870 int used;
871 int widthTable[MAX_GRID_SIZE];
872 int heightTable[MAX_GRID_SIZE];
873 float lodError;
874 int lodWidth, lodHeight;
875 int numVertexes;
876 int dlightBits;
877 int *vDlightBits;
878 qboolean needsNormal;
879
880 dlightBits = cv->dlightBits;
881 tess.dlightBits |= dlightBits;
882
883 // determine the allowable discrepance
884 lodError = LodErrorForVolume( cv->lodOrigin, cv->lodRadius );
885
886 // determine which rows and columns of the subdivision
887 // we are actually going to use
888 widthTable[0] = 0;
889 lodWidth = 1;
890 for ( i = 1 ; i < cv->width-1 ; i++ ) {
891 if ( cv->widthLodError[i] <= lodError ) {
892 widthTable[lodWidth] = i;
893 lodWidth++;
894 }
895 }
896 widthTable[lodWidth] = cv->width-1;
897 lodWidth++;
898
899 heightTable[0] = 0;
900 lodHeight = 1;
901 for ( i = 1 ; i < cv->height-1 ; i++ ) {
902 if ( cv->heightLodError[i] <= lodError ) {
903 heightTable[lodHeight] = i;
904 lodHeight++;
905 }
906 }
907 heightTable[lodHeight] = cv->height-1;
908 lodHeight++;
909
910
911 // very large grids may have more points or indexes than can be fit
912 // in the tess structure, so we may have to issue it in multiple passes
913
914 used = 0;
915 while ( used < lodHeight - 1 ) {
916 // see how many rows of both verts and indexes we can add without overflowing
917 do {
918 vrows = ( SHADER_MAX_VERTEXES - tess.numVertexes ) / lodWidth;
919 irows = ( SHADER_MAX_INDEXES - tess.numIndexes ) / ( lodWidth * 6 );
920
921 // if we don't have enough space for at least one strip, flush the buffer
922 if ( vrows < 2 || irows < 1 ) {
923 RB_EndSurface();
924 RB_BeginSurface(tess.shader, tess.fogNum );
925 } else {
926 break;
927 }
928 } while ( 1 );
929
930 rows = irows;
931 if ( vrows < irows + 1 ) {
932 rows = vrows - 1;
933 }
934 if ( used + rows > lodHeight ) {
935 rows = lodHeight - used;
936 }
937
938 numVertexes = tess.numVertexes;
939
940 xyz = tess.xyz[numVertexes];
941 normal = tess.normal[numVertexes];
942 texCoords = tess.texCoords[numVertexes][0];
943 color = ( unsigned char * ) &tess.vertexColors[numVertexes];
944 vDlightBits = &tess.vertexDlightBits[numVertexes];
945 needsNormal = tess.shader->needsNormal;
946
947 for ( i = 0 ; i < rows ; i++ ) {
948 for ( j = 0 ; j < lodWidth ; j++ ) {
949 dv = cv->verts + heightTable[ used + i ] * cv->width
950 + widthTable[ j ];
951
952 xyz[0] = dv->xyz[0];
953 xyz[1] = dv->xyz[1];
954 xyz[2] = dv->xyz[2];
955 texCoords[0] = dv->st[0];
956 texCoords[1] = dv->st[1];
957 texCoords[2] = dv->lightmap[0];
958 texCoords[3] = dv->lightmap[1];
959 if ( needsNormal ) {
960 normal[0] = dv->normal[0];
961 normal[1] = dv->normal[1];
962 normal[2] = dv->normal[2];
963 }
964 * ( unsigned int * ) color = * ( unsigned int * ) dv->color;
965 *vDlightBits++ = dlightBits;
966 xyz += 4;
967 normal += 4;
968 texCoords += 4;
969 color += 4;
970 }
971 }
972
973
974 // add the indexes
975 {
976 int numIndexes;
977 int w, h;
978
979 h = rows - 1;
980 w = lodWidth - 1;
981 numIndexes = tess.numIndexes;
982 for (i = 0 ; i < h ; i++) {
983 for (j = 0 ; j < w ; j++) {
984 int v1, v2, v3, v4;
985
986 // vertex order to be reckognized as tristrips
987 v1 = numVertexes + i*lodWidth + j + 1;
988 v2 = v1 - 1;
989 v3 = v2 + lodWidth;
990 v4 = v3 + 1;
991
992 tess.indexes[numIndexes] = v2;
993 tess.indexes[numIndexes+1] = v3;
994 tess.indexes[numIndexes+2] = v1;
995
996 tess.indexes[numIndexes+3] = v1;
997 tess.indexes[numIndexes+4] = v3;
998 tess.indexes[numIndexes+5] = v4;
999 numIndexes += 6;
1000 }
1001 }
1002
1003 tess.numIndexes = numIndexes;
1004 }
1005
1006 tess.numVertexes += rows * lodWidth;
1007
1008 used += rows - 1;
1009 }
1010 }
1011
1012
1013 /*
1014 ===========================================================================
1015
1016 NULL MODEL
1017
1018 ===========================================================================
1019 */
1020
1021 /*
1022 ===================
1023 RB_SurfaceAxis
1024
1025 Draws x/y/z lines from the origin for orientation debugging
1026 ===================
1027 */
1028 static void RB_SurfaceAxis( void ) {
1029 GL_Bind( tr.whiteImage );
1030 GL_State( GLS_DEFAULT );
1031 qglLineWidth( 3 );
1032 qglBegin( GL_LINES );
1033 qglColor3f( 1,0,0 );
1034 qglVertex3f( 0,0,0 );
1035 qglVertex3f( 16,0,0 );
1036 qglColor3f( 0,1,0 );
1037 qglVertex3f( 0,0,0 );
1038 qglVertex3f( 0,16,0 );
1039 qglColor3f( 0,0,1 );
1040 qglVertex3f( 0,0,0 );
1041 qglVertex3f( 0,0,16 );
1042 qglEnd();
1043 qglLineWidth( 1 );
1044 }
1045
1046 //===========================================================================
1047
1048 /*
1049 ====================
1050 RB_SurfaceEntity
1051
1052 Entities that have a single procedurally generated surface
1053 ====================
1054 */
1055 static void RB_SurfaceEntity( surfaceType_t *surfType ) {
1056 switch( backEnd.currentEntity->e.reType ) {
1057 case RT_SPRITE:
1058 RB_SurfaceSprite();
1059 break;
1060 case RT_BEAM:
1061 RB_SurfaceBeam();
1062 break;
1063 case RT_RAIL_CORE:
1064 RB_SurfaceRailCore();
1065 break;
1066 case RT_RAIL_RINGS:
1067 RB_SurfaceRailRings();
1068 break;
1069 case RT_LIGHTNING:
1070 RB_SurfaceLightningBolt();
1071 break;
1072 default:
1073 RB_SurfaceAxis();
1074 break;
1075 }
1076 }
1077
1078 static void RB_SurfaceBad( surfaceType_t *surfType ) {
1079 ri.Printf( PRINT_ALL, "Bad surface tesselated.\n" );
1080 }
1081
1082 static void RB_SurfaceFlare(srfFlare_t *surf)
1083 {
1084 if (r_flares->integer)
1085 RB_AddFlare(surf, tess.fogNum, surf->origin, surf->color, surf->normal);
1086 }
1087
1088 static void RB_SurfaceSkip( void *surf ) {
1089 }
1090
1091
1092 void (*rb_surfaceTable[SF_NUM_SURFACE_TYPES])( void *) = {
1093 (void(*)(void*))RB_SurfaceBad, // SF_BAD,
1094 (void(*)(void*))RB_SurfaceSkip, // SF_SKIP,
1095 (void(*)(void*))RB_SurfaceFace, // SF_FACE,
1096 (void(*)(void*))RB_SurfaceGrid, // SF_GRID,
1097 (void(*)(void*))RB_SurfaceTriangles, // SF_TRIANGLES,
1098 (void(*)(void*))RB_SurfacePolychain, // SF_POLY,
1099 (void(*)(void*))RB_SurfaceMesh, // SF_MD3,
1100 (void(*)(void*))RB_MDRSurfaceAnim, // SF_MDR,
1101 (void(*)(void*))RB_IQMSurfaceAnim, // SF_IQM,
1102 (void(*)(void*))RB_SurfaceFlare, // SF_FLARE,
1103 (void(*)(void*))RB_SurfaceEntity // SF_ENTITY
1104 };