📄 gl_draw.c
字号:
{
int i, j;
unsigned *inrow;
unsigned frac, fracstep;
fracstep = inwidth*0x10000/outwidth;
for (i=0 ; i<outheight ; i++, out += outwidth)
{
inrow = in + inwidth*(i*inheight/outheight);
frac = fracstep >> 1;
for (j=0 ; j<outwidth ; j+=4)
{
out[j] = inrow[frac>>16];
frac += fracstep;
out[j+1] = inrow[frac>>16];
frac += fracstep;
out[j+2] = inrow[frac>>16];
frac += fracstep;
out[j+3] = inrow[frac>>16];
frac += fracstep;
}
}
}
/*
================
GL_Resample8BitTexture -- JACK
================
*/
void GL_Resample8BitTexture (unsigned char *in, int inwidth, int inheight, unsigned char *out, int outwidth, int outheight)
{
int i, j;
unsigned char *inrow;
unsigned frac, fracstep;
fracstep = inwidth*0x10000/outwidth;
for (i=0 ; i<outheight ; i++, out += outwidth)
{
inrow = in + inwidth*(i*inheight/outheight);
frac = fracstep >> 1;
for (j=0 ; j<outwidth ; j+=4)
{
out[j] = inrow[frac>>16];
frac += fracstep;
out[j+1] = inrow[frac>>16];
frac += fracstep;
out[j+2] = inrow[frac>>16];
frac += fracstep;
out[j+3] = inrow[frac>>16];
frac += fracstep;
}
}
}
/*
================
GL_MipMap
Operates in place, quartering the size of the texture
================
*/
void GL_MipMap (byte *in, int width, int height)
{
int i, j;
byte *out;
width <<=2;
height >>= 1;
out = in;
for (i=0 ; i<height ; i++, in+=width)
{
for (j=0 ; j<width ; j+=8, out+=4, in+=8)
{
out[0] = (in[0] + in[4] + in[width+0] + in[width+4])>>2;
out[1] = (in[1] + in[5] + in[width+1] + in[width+5])>>2;
out[2] = (in[2] + in[6] + in[width+2] + in[width+6])>>2;
out[3] = (in[3] + in[7] + in[width+3] + in[width+7])>>2;
}
}
}
/*
================
GL_MipMap8Bit
Mipping for 8 bit textures
================
*/
void GL_MipMap8Bit (byte *in, int width, int height)
{
int i, j;
unsigned short r,g,b;
byte *out, *at1, *at2, *at3, *at4;
// width <<=2;
height >>= 1;
out = in;
for (i=0 ; i<height ; i++, in+=width)
{
for (j=0 ; j<width ; j+=2, out+=1, in+=2)
{
at1 = (byte *) (d_8to24table + in[0]);
at2 = (byte *) (d_8to24table + in[1]);
at3 = (byte *) (d_8to24table + in[width+0]);
at4 = (byte *) (d_8to24table + in[width+1]);
r = (at1[0]+at2[0]+at3[0]+at4[0]); r>>=5;
g = (at1[1]+at2[1]+at3[1]+at4[1]); g>>=5;
b = (at1[2]+at2[2]+at3[2]+at4[2]); b>>=5;
out[0] = d_15to8table[(r<<0) + (g<<5) + (b<<10)];
}
}
}
/*
===============
GL_Upload32
===============
*/
void GL_Upload32 (unsigned *data, int width, int height, qboolean mipmap, qboolean alpha)
{
int samples;
static unsigned scaled[1024*512]; // [512*256];
int scaled_width, scaled_height;
for (scaled_width = 1 ; scaled_width < width ; scaled_width<<=1)
;
for (scaled_height = 1 ; scaled_height < height ; scaled_height<<=1)
;
scaled_width >>= (int)gl_picmip->value;
scaled_height >>= (int)gl_picmip->value;
if (scaled_width > gl_max_size->value)
scaled_width = gl_max_size->value;
if (scaled_height > gl_max_size->value)
scaled_height = gl_max_size->value;
if (scaled_width * scaled_height > sizeof(scaled)/4)
Sys_Error ("GL_LoadTexture: too big");
samples = alpha ? gl_alpha_format : gl_solid_format;
#if 0
if (mipmap)
gluBuild2DMipmaps (GL_TEXTURE_2D, samples, width, height, GL_RGBA, GL_UNSIGNED_BYTE, trans);
else if (scaled_width == width && scaled_height == height)
glTexImage2D (GL_TEXTURE_2D, 0, samples, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, trans);
else
{
gluScaleImage (GL_RGBA, width, height, GL_UNSIGNED_BYTE, trans,
scaled_width, scaled_height, GL_UNSIGNED_BYTE, scaled);
glTexImage2D (GL_TEXTURE_2D, 0, samples, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaled);
}
#else
texels += scaled_width * scaled_height;
if (scaled_width == width && scaled_height == height)
{
if (!mipmap)
{
glTexImage2D (GL_TEXTURE_2D, 0, samples, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
goto done;
}
memcpy (scaled, data, width*height*4);
}
else
GL_ResampleTexture (data, width, height, scaled, scaled_width, scaled_height);
glTexImage2D (GL_TEXTURE_2D, 0, samples, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaled);
if (mipmap)
{
int miplevel;
miplevel = 0;
while (scaled_width > 1 || scaled_height > 1)
{
GL_MipMap ((byte *)scaled, scaled_width, scaled_height);
scaled_width >>= 1;
scaled_height >>= 1;
if (scaled_width < 1)
scaled_width = 1;
if (scaled_height < 1)
scaled_height = 1;
miplevel++;
glTexImage2D (GL_TEXTURE_2D, miplevel, samples, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaled);
}
}
done: ;
#endif
if (mipmap)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
}
else
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_max);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
}
}
void GL_Upload8_EXT (byte *data, int width, int height, qboolean mipmap, qboolean alpha)
{
int i, s;
qboolean noalpha;
int p;
// static unsigned j; // 2001-12-10 Reduced compiler warnings by Jeff Ford
int samples;
static unsigned char scaled[1024*512]; // [512*256];
int scaled_width, scaled_height;
s = width*height;
// if there are no transparent pixels, make it a 3 component
// texture even if it was specified as otherwise
if (alpha)
{
noalpha = true;
for (i=0 ; i<s ; i++)
{
if (data[i] == 255)
noalpha = false;
}
if (alpha && noalpha)
alpha = false;
}
for (scaled_width = 1 ; scaled_width < width ; scaled_width<<=1)
;
for (scaled_height = 1 ; scaled_height < height ; scaled_height<<=1)
;
scaled_width >>= (int)gl_picmip->value;
scaled_height >>= (int)gl_picmip->value;
if (scaled_width > gl_max_size->value)
scaled_width = gl_max_size->value;
if (scaled_height > gl_max_size->value)
scaled_height = gl_max_size->value;
if (scaled_width * scaled_height > sizeof(scaled))
Sys_Error ("GL_LoadTexture: too big");
samples = 1; // alpha ? gl_alpha_format : gl_solid_format;
texels += scaled_width * scaled_height;
if (scaled_width == width && scaled_height == height)
{
if (!mipmap)
{
glTexImage2D (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, scaled_width, scaled_height, 0, GL_COLOR_INDEX , GL_UNSIGNED_BYTE, data);
goto done;
}
memcpy (scaled, data, width*height);
}
else
GL_Resample8BitTexture (data, width, height, scaled, scaled_width, scaled_height);
glTexImage2D (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, scaled_width, scaled_height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, scaled);
if (mipmap)
{
int miplevel;
miplevel = 0;
while (scaled_width > 1 || scaled_height > 1)
{
GL_MipMap8Bit ((byte *)scaled, scaled_width, scaled_height);
scaled_width >>= 1;
scaled_height >>= 1;
if (scaled_width < 1)
scaled_width = 1;
if (scaled_height < 1)
scaled_height = 1;
miplevel++;
glTexImage2D (GL_TEXTURE_2D, miplevel, GL_COLOR_INDEX8_EXT, scaled_width, scaled_height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, scaled);
}
}
done: ;
if (mipmap)
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
}
else
{
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_max);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
}
}
/*
===============
GL_Upload8
===============
*/
void GL_Upload8 (byte *data, int width, int height, qboolean mipmap, qboolean alpha)
{
static unsigned trans[640*480]; // FIXME, temporary
int i, s;
qboolean noalpha;
int p;
s = width*height;
// if there are no transparent pixels, make it a 3 component
// texture even if it was specified as otherwise
if (alpha)
{
noalpha = true;
for (i=0 ; i<s ; i++)
{
p = data[i];
if (p == 255)
noalpha = false;
trans[i] = d_8to24table[p];
}
if (alpha && noalpha)
alpha = false;
}
else
{
if (s&3)
Sys_Error ("GL_Upload8: s&3");
for (i=0 ; i<s ; i+=4)
{
trans[i] = d_8to24table[data[i]];
trans[i+1] = d_8to24table[data[i+1]];
trans[i+2] = d_8to24table[data[i+2]];
trans[i+3] = d_8to24table[data[i+3]];
}
}
if (VID_Is8bit() && !alpha && (data!=scrap_texels[0])) {
GL_Upload8_EXT (data, width, height, mipmap, alpha);
return;
}
GL_Upload32 (trans, width, height, mipmap, alpha);
}
/*
================
GL_LoadTexture
================
*/
int lhcsumtable[256]; // 2001-09-09 GL Texture memory leak fix by LordHavoc
int GL_LoadTexture (char *identifier, int width, int height, byte *data, qboolean mipmap, qboolean alpha)
{
qboolean noalpha;
int i, p, s;
gltexture_t *glt;
// 2001-09-09 GL Texture memory leak fix by LordHavoc start
int lhcsum;
// do a checksum to confirm the data really is the same as previous
// occurances. well this isn't exactly a checksum, it's better than
// that but not following any standards.
for (i=0 ; i<256 ; i++)
{
lhcsumtable[i] = i + 1;
}
s = width * height;
lhcsum = 0;
for (i=0; i<s ; i++)
{
lhcsum += (lhcsumtable[data[i] & 255]++);
}
// 2001-09-09 GL Texture memory leak fix by LordHavoc end
// see if the texture is already present
if (identifier[0])
{
for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
{
if (!strcmp (identifier, glt->identifier))
{
// 2001-09-09 GL Texture memory leak fix by LordHavoc start
/*
if (width != glt->width || height != glt->height)
Sys_Error ("GL_LoadTexture: cache mismatch");
return gltextures[i].texnum;
*/
// everyone hates cache mismatchs, so I fixed it
if (lhcsum != glt->lhcsum || width != glt->width || height != glt->height)
{
Con_DPrintf("GL_LoadTexture: cache mismatch, replacing old texture\n");
goto GL_LoadTexture_setup; // drop out with glt pointing to the texture to replace
}
return glt->texnum;
// 2001-09-09 GL Texture memory leak fix by LordHavoc end
}
}
}
// 2001-09-09 GL Texture memory leak fix by LordHavoc/Maddes start
// this was an else condition, causing disasterous results,
// whoever at id or threewave must've been half asleep...
// else {
// additional leak check by Maddes start
if (numgltextures >= MAX_GLTEXTURES)
{
Sys_Error ("GL_LoadTexture: cache full, max is %i textures", MAX_GLTEXTURES);
}
// additional leak check by Maddes end
// 2001-09-09 GL Texture memory leak fix by LordHavoc/Maddes end
glt = &gltextures[numgltextures];
numgltextures++;
// } // 2001-09-09 GL Texture memory leak fix by LordHavoc
strcpy (glt->identifier, identifier);
glt->texnum = texture_extension_number;
// 2001-09-09 GL Texture memory leak fix by LordHavoc start
texture_extension_number++;
// label to drop out of the loop into the setup code
GL_LoadTexture_setup:
glt->lhcsum = lhcsum; // used to verify textures are identical
// 2001-09-09 GL Texture memory leak fix by LordHavoc end
glt->width = width;
glt->height = height;
glt->mipmap = mipmap;
// 2000-07-09 Dedicated server bug in GLQuake fix by Nathan Cline start
if (cls.state != ca_dedicated)
{
// 2000-07-09 Dedicated server bug in GLQuake fix by Nathan Cline end
// 2001-09-09 GL Texture memory leak fix by LordHavoc start
// GL_Bind(texture_extension_number );
GL_Bind(glt->texnum); // use value stored in structure
// 2001-09-09 GL Texture memory leak fix by LordHavoc end
GL_Upload8 (data, width, height, mipmap, alpha);
} // 2000-07-09 Dedicated server bug in GLQuake fix by Nathan Cline
// 2001-09-09 GL Texture memory leak fix by LordHavoc start
/*
texture_extension_number++;
return texture_extension_number-1;
*/
return glt->texnum; // use value stored in structure
// 2001-09-09 GL Texture memory leak fix by LordHavoc end
}
/*
================
GL_LoadPicTexture
================
*/
int GL_LoadPicTexture (qpic_t *pic)
{
return GL_LoadTexture ("", pic->width, pic->height, pic->data, false, true);
}
/****************************************/
static GLenum oldtarget = TEXTURE0_SGIS;
void GL_SelectTexture (GLenum target)
{
if (!gl_mtexable)
return;
qglSelectTextureSGIS(target);
if (target == oldtarget)
return;
cnttextures[oldtarget-TEXTURE0_SGIS] = currenttexture;
currenttexture = cnttextures[target-TEXTURE0_SGIS];
oldtarget = target;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -