📄 tr_image.c
字号:
}
if ( t < 31.0/32 ) {
s *= (t - 1.0f/32.0f) / (30.0f/32.0f);
}
// we need to leave a lot of clamp range
s *= 8;
if ( s > 1.0 ) {
s = 1.0;
}
d = tr.fogTable[ (int)(s * (FOG_TABLE_SIZE-1)) ];
return d;
}
/*
================
R_CreateFogImage
================
*/
#define FOG_S 256
#define FOG_T 32
static void R_CreateFogImage( void ) {
int x,y;
byte *data;
float g;
float d;
float borderColor[4];
data = ri.Hunk_AllocateTempMemory( FOG_S * FOG_T * 4 );
g = 2.0;
// S is distance, T is depth
for (x=0 ; x<FOG_S ; x++) {
for (y=0 ; y<FOG_T ; y++) {
d = R_FogFactor( ( x + 0.5f ) / FOG_S, ( y + 0.5f ) / FOG_T );
data[(y*FOG_S+x)*4+0] =
data[(y*FOG_S+x)*4+1] =
data[(y*FOG_S+x)*4+2] = 255;
data[(y*FOG_S+x)*4+3] = 255*d;
}
}
// standard openGL clamping doesn't really do what we want -- it includes
// the border color at the edges. OpenGL 1.2 has clamp-to-edge, which does
// what we want.
tr.fogImage = R_CreateImage("*fog", (byte *)data, FOG_S, FOG_T, qfalse, qfalse, GL_CLAMP );
ri.Hunk_FreeTempMemory( data );
borderColor[0] = 1.0;
borderColor[1] = 1.0;
borderColor[2] = 1.0;
borderColor[3] = 1;
qglTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor );
}
/*
==================
R_CreateDefaultImage
==================
*/
#define DEFAULT_SIZE 16
static void R_CreateDefaultImage( void ) {
int x;
byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
// the default image will be a box, to allow you to see the mapping coordinates
Com_Memset( data, 32, sizeof( data ) );
for ( x = 0 ; x < DEFAULT_SIZE ; x++ ) {
data[0][x][0] =
data[0][x][1] =
data[0][x][2] =
data[0][x][3] = 255;
data[x][0][0] =
data[x][0][1] =
data[x][0][2] =
data[x][0][3] = 255;
data[DEFAULT_SIZE-1][x][0] =
data[DEFAULT_SIZE-1][x][1] =
data[DEFAULT_SIZE-1][x][2] =
data[DEFAULT_SIZE-1][x][3] = 255;
data[x][DEFAULT_SIZE-1][0] =
data[x][DEFAULT_SIZE-1][1] =
data[x][DEFAULT_SIZE-1][2] =
data[x][DEFAULT_SIZE-1][3] = 255;
}
tr.defaultImage = R_CreateImage("*default", (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE, qtrue, qfalse, GL_REPEAT );
}
/*
==================
R_CreateBuiltinImages
==================
*/
void R_CreateBuiltinImages( void ) {
int x,y;
byte data[DEFAULT_SIZE][DEFAULT_SIZE][4];
R_CreateDefaultImage();
// we use a solid white image instead of disabling texturing
Com_Memset( data, 255, sizeof( data ) );
tr.whiteImage = R_CreateImage("*white", (byte *)data, 8, 8, qfalse, qfalse, GL_REPEAT );
// with overbright bits active, we need an image which is some fraction of full color,
// for default lightmaps, etc
for (x=0 ; x<DEFAULT_SIZE ; x++) {
for (y=0 ; y<DEFAULT_SIZE ; y++) {
data[y][x][0] =
data[y][x][1] =
data[y][x][2] = tr.identityLightByte;
data[y][x][3] = 255;
}
}
tr.identityLightImage = R_CreateImage("*identityLight", (byte *)data, 8, 8, qfalse, qfalse, GL_REPEAT );
for(x=0;x<32;x++) {
// scratchimage is usually used for cinematic drawing
tr.scratchImage[x] = R_CreateImage("*scratch", (byte *)data, DEFAULT_SIZE, DEFAULT_SIZE, qfalse, qtrue, GL_CLAMP );
}
R_CreateDlightImage();
R_CreateFogImage();
}
/*
===============
R_SetColorMappings
===============
*/
void R_SetColorMappings( void ) {
int i, j;
float g;
int inf;
int shift;
// setup the overbright lighting
tr.overbrightBits = r_overBrightBits->integer;
if ( !glConfig.deviceSupportsGamma ) {
tr.overbrightBits = 0; // need hardware gamma for overbright
}
// never overbright in windowed mode
if ( !glConfig.isFullscreen )
{
tr.overbrightBits = 0;
}
// allow 2 overbright bits in 24 bit, but only 1 in 16 bit
if ( glConfig.colorBits > 16 ) {
if ( tr.overbrightBits > 2 ) {
tr.overbrightBits = 2;
}
} else {
if ( tr.overbrightBits > 1 ) {
tr.overbrightBits = 1;
}
}
if ( tr.overbrightBits < 0 ) {
tr.overbrightBits = 0;
}
tr.identityLight = 1.0f / ( 1 << tr.overbrightBits );
tr.identityLightByte = 255 * tr.identityLight;
if ( r_intensity->value <= 1 ) {
ri.Cvar_Set( "r_intensity", "1" );
}
if ( r_gamma->value < 0.5f ) {
ri.Cvar_Set( "r_gamma", "0.5" );
} else if ( r_gamma->value > 3.0f ) {
ri.Cvar_Set( "r_gamma", "3.0" );
}
g = r_gamma->value;
shift = tr.overbrightBits;
for ( i = 0; i < 256; i++ ) {
if ( g == 1 ) {
inf = i;
} else {
inf = 255 * pow ( i/255.0f, 1.0f / g ) + 0.5f;
}
inf <<= shift;
if (inf < 0) {
inf = 0;
}
if (inf > 255) {
inf = 255;
}
s_gammatable[i] = inf;
}
for (i=0 ; i<256 ; i++) {
j = i * r_intensity->value;
if (j > 255) {
j = 255;
}
s_intensitytable[i] = j;
}
if ( glConfig.deviceSupportsGamma )
{
GLimp_SetGamma( s_gammatable, s_gammatable, s_gammatable );
}
}
/*
===============
R_InitImages
===============
*/
void R_InitImages( void ) {
Com_Memset(hashTable, 0, sizeof(hashTable));
// build brightness translation tables
R_SetColorMappings();
// create default texture and white texture
R_CreateBuiltinImages();
}
/*
===============
R_DeleteTextures
===============
*/
void R_DeleteTextures( void ) {
int i;
for ( i=0; i<tr.numImages ; i++ ) {
qglDeleteTextures( 1, &tr.images[i]->texnum );
}
Com_Memset( tr.images, 0, sizeof( tr.images ) );
tr.numImages = 0;
Com_Memset( glState.currenttextures, 0, sizeof( glState.currenttextures ) );
if ( qglBindTexture ) {
if ( qglActiveTextureARB ) {
GL_SelectTexture( 1 );
qglBindTexture( GL_TEXTURE_2D, 0 );
GL_SelectTexture( 0 );
qglBindTexture( GL_TEXTURE_2D, 0 );
} else {
qglBindTexture( GL_TEXTURE_2D, 0 );
}
}
}
/*
============================================================================
SKINS
============================================================================
*/
/*
==================
CommaParse
This is unfortunate, but the skin files aren't
compatable with our normal parsing rules.
==================
*/
static char *CommaParse( char **data_p ) {
int c = 0, len;
char *data;
static char com_token[MAX_TOKEN_CHARS];
data = *data_p;
len = 0;
com_token[0] = 0;
// make sure incoming data is valid
if ( !data ) {
*data_p = NULL;
return com_token;
}
while ( 1 ) {
// skip whitespace
while( (c = *data) <= ' ') {
if( !c ) {
break;
}
data++;
}
c = *data;
// skip double slash comments
if ( c == '/' && data[1] == '/' )
{
while (*data && *data != '\n')
data++;
}
// skip /* */ comments
else if ( c=='/' && data[1] == '*' )
{
while ( *data && ( *data != '*' || data[1] != '/' ) )
{
data++;
}
if ( *data )
{
data += 2;
}
}
else
{
break;
}
}
if ( c == 0 ) {
return "";
}
// handle quoted strings
if (c == '\"')
{
data++;
while (1)
{
c = *data++;
if (c=='\"' || !c)
{
com_token[len] = 0;
*data_p = ( char * ) data;
return com_token;
}
if (len < MAX_TOKEN_CHARS)
{
com_token[len] = c;
len++;
}
}
}
// parse a regular word
do
{
if (len < MAX_TOKEN_CHARS)
{
com_token[len] = c;
len++;
}
data++;
c = *data;
} while (c>32 && c != ',' );
if (len == MAX_TOKEN_CHARS)
{
// Com_Printf ("Token exceeded %i chars, discarded.\n", MAX_TOKEN_CHARS);
len = 0;
}
com_token[len] = 0;
*data_p = ( char * ) data;
return com_token;
}
/*
===============
RE_RegisterSkin
===============
*/
qhandle_t RE_RegisterSkin( const char *name ) {
qhandle_t hSkin;
skin_t *skin;
skinSurface_t *surf;
char *text, *text_p;
char *token;
char surfName[MAX_QPATH];
if ( !name || !name[0] ) {
Com_Printf( "Empty name passed to RE_RegisterSkin\n" );
return 0;
}
if ( strlen( name ) >= MAX_QPATH ) {
Com_Printf( "Skin name exceeds MAX_QPATH\n" );
return 0;
}
// see if the skin is already loaded
for ( hSkin = 1; hSkin < tr.numSkins ; hSkin++ ) {
skin = tr.skins[hSkin];
if ( !Q_stricmp( skin->name, name ) ) {
if( skin->numSurfaces == 0 ) {
return 0; // default skin
}
return hSkin;
}
}
// allocate a new skin
if ( tr.numSkins == MAX_SKINS ) {
ri.Printf( PRINT_WARNING, "WARNING: RE_RegisterSkin( '%s' ) MAX_SKINS hit\n", name );
return 0;
}
tr.numSkins++;
skin = ri.Hunk_Alloc( sizeof( skin_t ), h_low );
tr.skins[hSkin] = skin;
Q_strncpyz( skin->name, name, sizeof( skin->name ) );
skin->numSurfaces = 0;
// make sure the render thread is stopped
R_SyncRenderThread();
// If not a .skin file, load as a single shader
if ( strcmp( name + strlen( name ) - 5, ".skin" ) ) {
skin->numSurfaces = 1;
skin->surfaces[0] = ri.Hunk_Alloc( sizeof(skin->surfaces[0]), h_low );
skin->surfaces[0]->shader = R_FindShader( name, LIGHTMAP_NONE, qtrue );
return hSkin;
}
// load and parse the skin file
ri.FS_ReadFile( name, (void **)&text );
if ( !text ) {
return 0;
}
text_p = text;
while ( text_p && *text_p ) {
// get surface name
token = CommaParse( &text_p );
Q_strncpyz( surfName, token, sizeof( surfName ) );
if ( !token[0] ) {
break;
}
// lowercase the surface name so skin compares are faster
Q_strlwr( surfName );
if ( *text_p == ',' ) {
text_p++;
}
if ( strstr( token, "tag_" ) ) {
continue;
}
// parse the shader name
token = CommaParse( &text_p );
surf = skin->surfaces[ skin->numSurfaces ] = ri.Hunk_Alloc( sizeof( *skin->surfaces[0] ), h_low );
Q_strncpyz( surf->name, surfName, sizeof( surf->name ) );
surf->shader = R_FindShader( token, LIGHTMAP_NONE, qtrue );
skin->numSurfaces++;
}
ri.FS_FreeFile( text );
// never let a skin have 0 shaders
if ( skin->numSurfaces == 0 ) {
return 0; // use default skin
}
return hSkin;
}
/*
===============
R_InitSkins
===============
*/
void R_InitSkins( void ) {
skin_t *skin;
tr.numSkins = 1;
// make the default skin have all default shaders
skin = tr.skins[0] = ri.Hunk_Alloc( sizeof( skin_t ), h_low );
Q_strncpyz( skin->na
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -