📄 tr_surface.c
字号:
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena 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 2 of the License,
or (at your option) any later version.
Quake III Arena 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 Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_surf.c
#include "tr_local.h"
/*
THIS ENTIRE FILE IS BACK END
backEnd.currentEntity will be valid.
Tess_Begin has already been called for the surface's shader.
The modelview matrix will be set.
It is safe to actually issue drawing commands here if you don't want to
use the shader system.
*/
//============================================================================
/*
==============
RB_CheckOverflow
==============
*/
void RB_CheckOverflow( int verts, int indexes ) {
if (tess.numVertexes + verts < SHADER_MAX_VERTEXES
&& tess.numIndexes + indexes < SHADER_MAX_INDEXES) {
return;
}
RB_EndSurface();
if ( verts >= SHADER_MAX_VERTEXES ) {
ri.Error(ERR_DROP, "RB_CheckOverflow: verts > MAX (%d > %d)", verts, SHADER_MAX_VERTEXES );
}
if ( indexes >= SHADER_MAX_INDEXES ) {
ri.Error(ERR_DROP, "RB_CheckOverflow: indices > MAX (%d > %d)", indexes, SHADER_MAX_INDEXES );
}
RB_BeginSurface(tess.shader, tess.fogNum );
}
/*
==============
RB_AddQuadStampExt
==============
*/
void RB_AddQuadStampExt( vec3_t origin, vec3_t left, vec3_t up, byte *color, float s1, float t1, float s2, float t2 ) {
vec3_t normal;
int ndx;
RB_CHECKOVERFLOW( 4, 6 );
ndx = tess.numVertexes;
// triangle indexes for a simple quad
tess.indexes[ tess.numIndexes ] = ndx;
tess.indexes[ tess.numIndexes + 1 ] = ndx + 1;
tess.indexes[ tess.numIndexes + 2 ] = ndx + 3;
tess.indexes[ tess.numIndexes + 3 ] = ndx + 3;
tess.indexes[ tess.numIndexes + 4 ] = ndx + 1;
tess.indexes[ tess.numIndexes + 5 ] = ndx + 2;
tess.xyz[ndx][0] = origin[0] + left[0] + up[0];
tess.xyz[ndx][1] = origin[1] + left[1] + up[1];
tess.xyz[ndx][2] = origin[2] + left[2] + up[2];
tess.xyz[ndx+1][0] = origin[0] - left[0] + up[0];
tess.xyz[ndx+1][1] = origin[1] - left[1] + up[1];
tess.xyz[ndx+1][2] = origin[2] - left[2] + up[2];
tess.xyz[ndx+2][0] = origin[0] - left[0] - up[0];
tess.xyz[ndx+2][1] = origin[1] - left[1] - up[1];
tess.xyz[ndx+2][2] = origin[2] - left[2] - up[2];
tess.xyz[ndx+3][0] = origin[0] + left[0] - up[0];
tess.xyz[ndx+3][1] = origin[1] + left[1] - up[1];
tess.xyz[ndx+3][2] = origin[2] + left[2] - up[2];
// constant normal all the way around
VectorSubtract( vec3_origin, backEnd.viewParms.or.axis[0], normal );
tess.normal[ndx][0] = tess.normal[ndx+1][0] = tess.normal[ndx+2][0] = tess.normal[ndx+3][0] = normal[0];
tess.normal[ndx][1] = tess.normal[ndx+1][1] = tess.normal[ndx+2][1] = tess.normal[ndx+3][1] = normal[1];
tess.normal[ndx][2] = tess.normal[ndx+1][2] = tess.normal[ndx+2][2] = tess.normal[ndx+3][2] = normal[2];
// standard square texture coordinates
tess.texCoords[ndx][0][0] = tess.texCoords[ndx][1][0] = s1;
tess.texCoords[ndx][0][1] = tess.texCoords[ndx][1][1] = t1;
tess.texCoords[ndx+1][0][0] = tess.texCoords[ndx+1][1][0] = s2;
tess.texCoords[ndx+1][0][1] = tess.texCoords[ndx+1][1][1] = t1;
tess.texCoords[ndx+2][0][0] = tess.texCoords[ndx+2][1][0] = s2;
tess.texCoords[ndx+2][0][1] = tess.texCoords[ndx+2][1][1] = t2;
tess.texCoords[ndx+3][0][0] = tess.texCoords[ndx+3][1][0] = s1;
tess.texCoords[ndx+3][0][1] = tess.texCoords[ndx+3][1][1] = t2;
// constant color all the way around
// should this be identity and let the shader specify from entity?
* ( unsigned int * ) &tess.vertexColors[ndx] =
* ( unsigned int * ) &tess.vertexColors[ndx+1] =
* ( unsigned int * ) &tess.vertexColors[ndx+2] =
* ( unsigned int * ) &tess.vertexColors[ndx+3] =
* ( unsigned int * )color;
tess.numVertexes += 4;
tess.numIndexes += 6;
}
/*
==============
RB_AddQuadStamp
==============
*/
void RB_AddQuadStamp( vec3_t origin, vec3_t left, vec3_t up, byte *color ) {
RB_AddQuadStampExt( origin, left, up, color, 0, 0, 1, 1 );
}
/*
==============
RB_SurfaceSprite
==============
*/
static void RB_SurfaceSprite( void ) {
vec3_t left, up;
float radius;
// calculate the xyz locations for the four corners
radius = backEnd.currentEntity->e.radius;
if ( backEnd.currentEntity->e.rotation == 0 ) {
VectorScale( backEnd.viewParms.or.axis[1], radius, left );
VectorScale( backEnd.viewParms.or.axis[2], radius, up );
} else {
float s, c;
float ang;
ang = M_PI * backEnd.currentEntity->e.rotation / 180;
s = sin( ang );
c = cos( ang );
VectorScale( backEnd.viewParms.or.axis[1], c * radius, left );
VectorMA( left, -s * radius, backEnd.viewParms.or.axis[2], left );
VectorScale( backEnd.viewParms.or.axis[2], c * radius, up );
VectorMA( up, s * radius, backEnd.viewParms.or.axis[1], up );
}
if ( backEnd.viewParms.isMirror ) {
VectorSubtract( vec3_origin, left, left );
}
RB_AddQuadStamp( backEnd.currentEntity->e.origin, left, up, backEnd.currentEntity->e.shaderRGBA );
}
/*
=============
RB_SurfacePolychain
=============
*/
void RB_SurfacePolychain( srfPoly_t *p ) {
int i;
int numv;
RB_CHECKOVERFLOW( p->numVerts, 3*(p->numVerts - 2) );
// fan triangles into the tess array
numv = tess.numVertexes;
for ( i = 0; i < p->numVerts; i++ ) {
VectorCopy( p->verts[i].xyz, tess.xyz[numv] );
tess.texCoords[numv][0][0] = p->verts[i].st[0];
tess.texCoords[numv][0][1] = p->verts[i].st[1];
*(int *)&tess.vertexColors[numv] = *(int *)p->verts[ i ].modulate;
numv++;
}
// generate fan indexes into the tess array
for ( i = 0; i < p->numVerts-2; i++ ) {
tess.indexes[tess.numIndexes + 0] = tess.numVertexes;
tess.indexes[tess.numIndexes + 1] = tess.numVertexes + i + 1;
tess.indexes[tess.numIndexes + 2] = tess.numVertexes + i + 2;
tess.numIndexes += 3;
}
tess.numVertexes = numv;
}
/*
=============
RB_SurfaceTriangles
=============
*/
void RB_SurfaceTriangles( srfTriangles_t *srf ) {
int i;
drawVert_t *dv;
float *xyz, *normal, *texCoords;
byte *color;
int dlightBits;
qboolean needsNormal;
dlightBits = srf->dlightBits[backEnd.smpFrame];
tess.dlightBits |= dlightBits;
RB_CHECKOVERFLOW( srf->numVerts, srf->numIndexes );
for ( i = 0 ; i < srf->numIndexes ; i += 3 ) {
tess.indexes[ tess.numIndexes + i + 0 ] = tess.numVertexes + srf->indexes[ i + 0 ];
tess.indexes[ tess.numIndexes + i + 1 ] = tess.numVertexes + srf->indexes[ i + 1 ];
tess.indexes[ tess.numIndexes + i + 2 ] = tess.numVertexes + srf->indexes[ i + 2 ];
}
tess.numIndexes += srf->numIndexes;
dv = srf->verts;
xyz = tess.xyz[ tess.numVertexes ];
normal = tess.normal[ tess.numVertexes ];
texCoords = tess.texCoords[ tess.numVertexes ][0];
color = tess.vertexColors[ tess.numVertexes ];
needsNormal = tess.shader->needsNormal;
for ( i = 0 ; i < srf->numVerts ; i++, dv++, xyz += 4, normal += 4, texCoords += 4, color += 4 ) {
xyz[0] = dv->xyz[0];
xyz[1] = dv->xyz[1];
xyz[2] = dv->xyz[2];
if ( needsNormal ) {
normal[0] = dv->normal[0];
normal[1] = dv->normal[1];
normal[2] = dv->normal[2];
}
texCoords[0] = dv->st[0];
texCoords[1] = dv->st[1];
texCoords[2] = dv->lightmap[0];
texCoords[3] = dv->lightmap[1];
*(int *)color = *(int *)dv->color;
}
for ( i = 0 ; i < srf->numVerts ; i++ ) {
tess.vertexDlightBits[ tess.numVertexes + i] = dlightBits;
}
tess.numVertexes += srf->numVerts;
}
/*
==============
RB_SurfaceBeam
==============
*/
void RB_SurfaceBeam( void )
{
#define NUM_BEAM_SEGS 6
refEntity_t *e;
int i;
vec3_t perpvec;
vec3_t direction, normalized_direction;
vec3_t start_points[NUM_BEAM_SEGS], end_points[NUM_BEAM_SEGS];
vec3_t oldorigin, origin;
e = &backEnd.currentEntity->e;
oldorigin[0] = e->oldorigin[0];
oldorigin[1] = e->oldorigin[1];
oldorigin[2] = e->oldorigin[2];
origin[0] = e->origin[0];
origin[1] = e->origin[1];
origin[2] = e->origin[2];
normalized_direction[0] = direction[0] = oldorigin[0] - origin[0];
normalized_direction[1] = direction[1] = oldorigin[1] - origin[1];
normalized_direction[2] = direction[2] = oldorigin[2] - origin[2];
if ( VectorNormalize( normalized_direction ) == 0 )
return;
PerpendicularVector( perpvec, normalized_direction );
VectorScale( perpvec, 4, perpvec );
for ( i = 0; i < NUM_BEAM_SEGS ; i++ )
{
RotatePointAroundVector( start_points[i], normalized_direction, perpvec, (360.0/NUM_BEAM_SEGS)*i );
// VectorAdd( start_points[i], origin, start_points[i] );
VectorAdd( start_points[i], direction, end_points[i] );
}
GL_Bind( tr.whiteImage );
GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
qglColor3f( 1, 0, 0 );
qglBegin( GL_TRIANGLE_STRIP );
for ( i = 0; i <= NUM_BEAM_SEGS; i++ ) {
qglVertex3fv( start_points[ i % NUM_BEAM_SEGS] );
qglVertex3fv( end_points[ i % NUM_BEAM_SEGS] );
}
qglEnd();
}
//================================================================================
static void DoRailCore( const vec3_t start, const vec3_t end, const vec3_t up, float len, float spanWidth )
{
float spanWidth2;
int vbase;
float t = len / 256.0f;
vbase = tess.numVertexes;
spanWidth2 = -spanWidth;
// FIXME: use quad stamp?
VectorMA( start, spanWidth, up, tess.xyz[tess.numVertexes] );
tess.texCoords[tess.numVertexes][0][0] = 0;
tess.texCoords[tess.numVertexes][0][1] = 0;
tess.vertexColors[tess.numVertexes][0] = backEnd.currentEntity->e.shaderRGBA[0] * 0.25;
tess.vertexColors[tess.numVertexes][1] = backEnd.currentEntity->e.shaderRGBA[1] * 0.25;
tess.vertexColors[tess.numVertexes][2] = backEnd.currentEntity->e.shaderRGBA[2] * 0.25;
tess.numVertexes++;
VectorMA( start, spanWidth2, up, tess.xyz[tess.numVertexes] );
tess.texCoords[tess.numVertexes][0][0] = 0;
tess.texCoords[tess.numVertexes][0][1] = 1;
tess.vertexColors[tess.numVertexes][0] = backEnd.currentEntity->e.shaderRGBA[0];
tess.vertexColors[tess.numVertexes][1] = backEnd.currentEntity->e.shaderRGBA[1];
tess.vertexColors[tess.numVertexes][2] = backEnd.currentEntity->e.shaderRGBA[2];
tess.numVertexes++;
VectorMA( end, spanWidth, up, tess.xyz[tess.numVertexes] );
tess.texCoords[tess.numVertexes][0][0] = t;
tess.texCoords[tess.numVertexes][0][1] = 0;
tess.vertexColors[tess.numVertexes][0] = backEnd.currentEntity->e.shaderRGBA[0];
tess.vertexColors[tess.numVertexes][1] = backEnd.currentEntity->e.shaderRGBA[1];
tess.vertexColors[tess.numVertexes][2] = backEnd.currentEntity->e.shaderRGBA[2];
tess.numVertexes++;
VectorMA( end, spanWidth2, up, tess.xyz[tess.numVertexes] );
tess.texCoords[tess.numVertexes][0][0] = t;
tess.texCoords[tess.numVertexes][0][1] = 1;
tess.vertexColors[tess.numVertexes][0] = backEnd.currentEntity->e.shaderRGBA[0];
tess.vertexColors[tess.numVertexes][1] = backEnd.currentEntity->e.shaderRGBA[1];
tess.vertexColors[tess.numVertexes][2] = backEnd.currentEntity->e.shaderRGBA[2];
tess.numVertexes++;
tess.indexes[tess.numIndexes++] = vbase;
tess.indexes[tess.numIndexes++] = vbase + 1;
tess.indexes[tess.numIndexes++] = vbase + 2;
tess.indexes[tess.numIndexes++] = vbase + 2;
tess.indexes[tess.numIndexes++] = vbase + 1;
tess.indexes[tess.numIndexes++] = vbase + 3;
}
static void DoRailDiscs( int numSegs, const vec3_t start, const vec3_t dir, const vec3_t right, const vec3_t up )
{
int i;
vec3_t pos[4];
vec3_t v;
int spanWidth = r_railWidth->integer;
float c, s;
float scale;
if ( numSegs > 1 )
numSegs--;
if ( !numSegs )
return;
scale = 0.25;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -