Experimentally allow external texture loading. Also prevent loading RGB textures as RGBA, which wastes a lot of texture memory, and looks ugly on 16bpp boards.

This commit is contained in:
Ragnvald Maartmann-Moe IV 2002-09-06 21:37:22 +00:00
parent 2ba3585d06
commit c4609f5195
5 changed files with 62 additions and 76 deletions

View file

@ -46,6 +46,7 @@ static const char rcsid[] =
#include "QF/sys.h" #include "QF/sys.h"
#include "QF/texture.h" #include "QF/texture.h"
#include "QF/tga.h" #include "QF/tga.h"
#include "QF/va.h"
#include "QF/GL/qf_textures.h" #include "QF/GL/qf_textures.h"
#include "compat.h" #include "compat.h"
@ -69,42 +70,35 @@ Mod_ProcessTexture (miptex_t *mt, texture_t *tx)
void void
Mod_LoadExternalTextures (model_t *mod) Mod_LoadExternalTextures (model_t *mod)
{ {
char filename[MAX_QPATH + 8]; char *filename;
int length, i; int i;
tex_t *targa; tex_t *targa;
texture_t *tx; texture_t *tx;
QFile *f; QFile *f;
for (i = 0; i < mod->numtextures; i++) for (i = 0; i < mod->numtextures; i++) {
{
tx = mod->textures[i]; tx = mod->textures[i];
if (!tx) if (!tx)
continue; continue;
length = strlen (tx->name) - 1;
// backslash at the end of texture name indicates external texture // backslash at the end of texture name indicates external texture
if (tx->name[length] != '\\') // if (tx->name[length] != '\\')
continue; // continue;
// replace special flag characters with underscores // replace special flag characters with underscores
if (tx->name[0] == '+' || tx->name[0] == '*') if (tx->name[0] == '+' || tx->name[0] == '*') {
snprintf (filename, sizeof (filename), "maps/_%s", tx->name + 1); filename = va ("maps/_%s.tga", tx->name + 1);
else } else {
snprintf (filename, sizeof (filename), "maps/%s", tx->name); filename = va ("maps/%s.tga", tx->name);
}
length += 5; // add "maps/" to the string length calculation
snprintf (filename + length, sizeof (filename) - length, ".tga");
COM_FOpenFile (filename, &f); COM_FOpenFile (filename, &f);
if (f) { if (f) {
targa = LoadTGA (f); targa = LoadTGA (f);
Qclose (f); Qclose (f);
if (targa->format < 4) tx->gl_texturenum =
tx->gl_texturenum = GL_LoadTexture (tx->name, targa->width, GL_LoadTexture (tx->name, targa->width, targa->height,
targa->height, targa->data, true, false, 3); targa->data, true, false, 3);
else
tx->gl_texturenum = GL_LoadTexture (tx->name, targa->width,
targa->height, targa->data, true, true, 4);
} }
} }
} }

View file

@ -56,6 +56,7 @@ blit_rgb (byte *buf, int count, byte red, byte green, byte blue)
*buf++ = red; *buf++ = red;
*buf++ = green; *buf++ = green;
*buf++ = blue; *buf++ = blue;
*buf++ = 255;
} }
return buf; return buf;
} }
@ -147,7 +148,7 @@ LoadTGA (QFile *fin)
switch (targa->pixel_size) { switch (targa->pixel_size) {
case 24: case 24:
tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 3])); tex = Hunk_TempAlloc (field_offset (tex_t, data[numPixels * 4]));
tex->format = tex_rgb; tex->format = tex_rgb;
break; break;
default: default:
@ -165,7 +166,7 @@ LoadTGA (QFile *fin)
dataByte = (byte *) (targa + 1); dataByte = (byte *) (targa + 1);
dataByte += targa->id_length; dataByte += targa->id_length;
span = columns * tex->format; span = columns * 4; // tex->format
pixrow = tex->data + (rows - 1) * span; pixrow = tex->data + (rows - 1) * span;
if (targa->image_type == 2) { // Uncompressed image if (targa->image_type == 2) { // Uncompressed image

View file

@ -96,10 +96,7 @@ build_skin_8 (byte * original, int tinwidth, int tinheight,
unsigned int scaled_width, unsigned int scaled_height, unsigned int scaled_width, unsigned int scaled_height,
int inwidth, qboolean alpha) int inwidth, qboolean alpha)
{ {
/* // Improvements should be mirrored in GL_ResampleTexture in gl_textures.c
any improvements in here should be mirrored in
GL_Resample8BitTexture in gl_textures.c
*/
byte *inrow; byte *inrow;
byte pixels[512 * 256], *out; byte pixels[512 * 256], *out;
int i, j; int i, j;
@ -126,10 +123,7 @@ build_skin_32 (byte * original, int tinwidth, int tinheight,
unsigned int scaled_width, unsigned int scaled_height, unsigned int scaled_width, unsigned int scaled_height,
int inwidth, qboolean alpha) int inwidth, qboolean alpha)
{ {
/* // Improvements should be mirrored in GL_ResampleTexture in gl_textures.c
any improvements in here should be mirrored in
GL_ResampleTexture in gl_textures.c
*/
byte *inrow; byte *inrow;
int i, j; int i, j;
int samples = alpha ? gl_alpha_format : gl_solid_format; int samples = alpha ? gl_alpha_format : gl_solid_format;
@ -148,9 +142,8 @@ build_skin_32 (byte * original, int tinwidth, int tinheight,
} }
} }
qfglTexImage2D (GL_TEXTURE_2D, 0, samples, qfglTexImage2D (GL_TEXTURE_2D, 0, samples, scaled_width, scaled_height, 0,
scaled_width, scaled_height, 0, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
GL_UNSIGNED_BYTE, pixels);
qfglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); qfglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
qfglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); qfglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
@ -175,11 +168,11 @@ build_skin (int texnum, byte *ptexels, int width, int height,
scaled_height >>= gl_playermip->int_val; scaled_height >>= gl_playermip->int_val;
if (VID_Is8bit ()) { // 8bit texture upload if (VID_Is8bit ()) { // 8bit texture upload
build_skin_8 (ptexels, owidth, oheight, scaled_width, build_skin_8 (ptexels, owidth, oheight, scaled_width, scaled_height,
scaled_height, width, alpha); width, alpha);
} else { } else {
build_skin_32 (ptexels, owidth, oheight, scaled_width, build_skin_32 (ptexels, owidth, oheight, scaled_width, scaled_height,
scaled_height, width, alpha); width, alpha);
} }
} }
@ -191,7 +184,7 @@ Skin_Do_Translation (skin_t *player_skin, int slot, skin_t *skin)
int texnum = skin->texture; int texnum = skin->texture;
tex_t *skin_texels; tex_t *skin_texels;
if ((skin_texels = (tex_t*)Skin_Cache (player_skin)) != NULL) { if ((skin_texels = (tex_t *) Skin_Cache (player_skin)) != NULL) {
// skin data width // skin data width
inwidth = 320; inwidth = 320;
inheight = 200; inheight = 200;

View file

@ -86,7 +86,8 @@ R_LoadSkys (const char *skyname)
if (!f) { if (!f) {
Con_DPrintf ("Couldn't load %s\n", name); Con_DPrintf ("Couldn't load %s\n", name);
// also look in gfx/env, where Darkplaces looks for skies // also look in gfx/env, where Darkplaces looks for skies
snprintf (name, sizeof (name), "gfx/env/%s%s.tga", skyname, suf[i]); snprintf (name, sizeof (name), "gfx/env/%s%s.tga", skyname,
suf[i]);
COM_FOpenFile (name, &f); COM_FOpenFile (name, &f);
if (!f) { if (!f) {
Con_DPrintf ("Couldn't load %s\n", name); Con_DPrintf ("Couldn't load %s\n", name);
@ -97,14 +98,9 @@ R_LoadSkys (const char *skyname)
targa = LoadTGA (f); targa = LoadTGA (f);
Qclose (f); Qclose (f);
if (targa->format < 4) qfglTexImage2D (GL_TEXTURE_2D, 0, gl_solid_format, targa->width,
qfglTexImage2D (GL_TEXTURE_2D, 0, gl_solid_format, targa->width, targa->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
targa->height, 0, GL_RGB, GL_UNSIGNED_BYTE, &targa->data);
&targa->data);
else
qfglTexImage2D (GL_TEXTURE_2D, 0, gl_solid_format, targa->width,
targa->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
&targa->data);
qfglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); qfglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
qfglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); qfglTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

View file

@ -178,7 +178,6 @@ static glformat_t formats[] = {
int gl_alpha_format = 4, gl_lightmap_format = 4, gl_solid_format = 3; int gl_alpha_format = 4, gl_lightmap_format = 4, gl_solid_format = 3;
void void
GL_TextureMode_f (void) GL_TextureMode_f (void)
{ {
@ -249,12 +248,9 @@ GL_TextureDepth_f (int format)
static void static void
GL_ResampleTexture (unsigned int *in, int inwidth, int inheight, GL_ResampleTexture (unsigned int *in, int inwidth, int inheight,
unsigned int *out, int outwidth, int outheight) unsigned int *out, int outwidth, int outheight)
{ {
/* // Improvements here should be mirrored in build_skin_32 in gl_skin.c
any improvements in here should be mirrored in build_skin_32 in
gl_skin.c
*/
int i, j; int i, j;
unsigned int frac, fracstep; unsigned int frac, fracstep;
unsigned int *inrow; unsigned int *inrow;
@ -274,12 +270,9 @@ GL_ResampleTexture (unsigned int *in, int inwidth, int inheight,
static void static void
GL_Resample8BitTexture (unsigned char *in, int inwidth, int inheight, GL_Resample8BitTexture (unsigned char *in, int inwidth, int inheight,
unsigned char *out, int outwidth, int outheight) unsigned char *out, int outwidth, int outheight)
{ {
/* // Improvements here should be mirrored in build_skin_8 in gl_skin.c
any improvements in here should be mirrored in build_skin_8 in
gl_skin.c
*/
unsigned char *inrow; unsigned char *inrow;
int i, j; int i, j;
unsigned int frac, fracstep; unsigned int frac, fracstep;
@ -303,7 +296,7 @@ GL_Resample8BitTexture (unsigned char *in, int inwidth, int inheight,
Operates in place, quartering the size of the texture. Operates in place, quartering the size of the texture.
*/ */
static void static void
GL_MipMap (byte * in, int width, int height) GL_MipMap (byte *in, int width, int height)
{ {
byte *out; byte *out;
int i, j; int i, j;
@ -328,7 +321,7 @@ GL_MipMap (byte * in, int width, int height)
Mipping for 8 bit textures Mipping for 8 bit textures
*/ */
static void static void
GL_MipMap8Bit (byte * in, int width, int height) GL_MipMap8Bit (byte *in, int width, int height)
{ {
byte *at1, *at2, *at3, *at4, *out; byte *at1, *at2, *at3, *at4, *out;
int i, j; int i, j;
@ -357,7 +350,7 @@ GL_MipMap8Bit (byte * in, int width, int height)
static void static void
GL_Upload32 (unsigned int *data, int width, int height, qboolean mipmap, GL_Upload32 (unsigned int *data, int width, int height, qboolean mipmap,
qboolean alpha) qboolean alpha)
{ {
int scaled_width, scaled_height, intformat; int scaled_width, scaled_height, intformat;
unsigned int *scaled; unsigned int *scaled;
@ -386,7 +379,7 @@ GL_Upload32 (unsigned int *data, int width, int height, qboolean mipmap,
memcpy (scaled, data, width * height * sizeof (GLuint)); memcpy (scaled, data, width * height * sizeof (GLuint));
} else { } else {
GL_ResampleTexture (data, width, height, scaled, scaled_width, GL_ResampleTexture (data, width, height, scaled, scaled_width,
scaled_height); scaled_height);
} }
qfglTexImage2D (GL_TEXTURE_2D, 0, intformat, scaled_width, scaled_height, qfglTexImage2D (GL_TEXTURE_2D, 0, intformat, scaled_width, scaled_height,
@ -403,7 +396,8 @@ GL_Upload32 (unsigned int *data, int width, int height, qboolean mipmap,
scaled_height = max (scaled_height, 1); scaled_height = max (scaled_height, 1);
miplevel++; miplevel++;
qfglTexImage2D (GL_TEXTURE_2D, miplevel, intformat, scaled_width, qfglTexImage2D (GL_TEXTURE_2D, miplevel, intformat, scaled_width,
scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaled); scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
scaled);
} }
} }
@ -433,8 +427,8 @@ GL_Upload32 (unsigned int *data, int width, int height, qboolean mipmap,
If we don't, this function does nothing. If we don't, this function does nothing.
*/ */
void void
GL_Upload8_EXT (byte * data, int width, int height, qboolean mipmap, GL_Upload8_EXT (byte *data, int width, int height, qboolean mipmap,
qboolean alpha) qboolean alpha)
{ {
byte *scaled; byte *scaled;
int scaled_width, scaled_height; int scaled_width, scaled_height;
@ -458,10 +452,11 @@ GL_Upload8_EXT (byte * data, int width, int height, qboolean mipmap,
memcpy (scaled, data, width * height); memcpy (scaled, data, width * height);
} else { } else {
GL_Resample8BitTexture (data, width, height, scaled, scaled_width, GL_Resample8BitTexture (data, width, height, scaled, scaled_width,
scaled_height); scaled_height);
} }
qfglTexImage2D (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, scaled_width, qfglTexImage2D (GL_TEXTURE_2D, 0, GL_COLOR_INDEX8_EXT, scaled_width,
scaled_height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, scaled); scaled_height, 0, GL_COLOR_INDEX, GL_UNSIGNED_BYTE,
scaled);
if (mipmap) { if (mipmap) {
int miplevel; int miplevel;
@ -475,8 +470,8 @@ GL_Upload8_EXT (byte * data, int width, int height, qboolean mipmap,
scaled_height = max (scaled_height, 1); scaled_height = max (scaled_height, 1);
miplevel++; miplevel++;
qfglTexImage2D (GL_TEXTURE_2D, miplevel, GL_COLOR_INDEX8_EXT, qfglTexImage2D (GL_TEXTURE_2D, miplevel, GL_COLOR_INDEX8_EXT,
scaled_width, scaled_height, 0, GL_COLOR_INDEX, scaled_width, scaled_height, 0, GL_COLOR_INDEX,
GL_UNSIGNED_BYTE, scaled); GL_UNSIGNED_BYTE, scaled);
} }
} }
@ -501,7 +496,7 @@ GL_Upload8_EXT (byte * data, int width, int height, qboolean mipmap,
void void
GL_Upload8 (byte * data, int width, int height, qboolean mipmap, qboolean alpha) GL_Upload8 (byte *data, int width, int height, qboolean mipmap, qboolean alpha)
{ {
int i, s, p; int i, s, p;
unsigned int *trans; unsigned int *trans;
@ -538,23 +533,27 @@ GL_Upload8 (byte * data, int width, int height, qboolean mipmap, qboolean alpha)
} }
int int
GL_LoadTexture (const char *identifier, int width, int height, byte * data, GL_LoadTexture (const char *identifier, int width, int height, byte *data,
qboolean mipmap, qboolean alpha, int bytesperpixel) qboolean mipmap, qboolean alpha, int bytesperpixel)
{ {
int crc, i; int crc, i;
gltexture_t *glt; gltexture_t *glt;
// LordHavoc: now just using a standard CRC for texture validation // LordHavoc: now just using a standard CRC for texture validation
crc = CRC_Block (data, width * height * bytesperpixel); if (bytesperpixel == 1)
crc = CRC_Block (data, width * height * bytesperpixel);
else
crc = CRC_Block (data, width * height * 4);
// see if the texture is already present // see if the texture is already present
if (identifier[0]) { if (identifier[0]) {
for (i = 0, glt = gltextures; i < numgltextures; i++, glt++) { for (i = 0, glt = gltextures; i < numgltextures; i++, glt++) {
if (strequal (identifier, glt->identifier)) { if (strequal (identifier, glt->identifier)) {
if (crc != glt->crc if (crc != glt->crc
|| width != glt->width || width != glt->width
|| height != glt->height || height != glt->height
|| bytesperpixel != glt->bytesperpixel) goto SetupTexture; || bytesperpixel != glt->bytesperpixel)
goto SetupTexture;
else else
return gltextures[i].texnum; return gltextures[i].texnum;
} }
@ -586,12 +585,15 @@ SetupTexture:
case 1: case 1:
GL_Upload8 (data, width, height, mipmap, alpha); GL_Upload8 (data, width, height, mipmap, alpha);
break; break;
case 3:
GL_Upload32 ((GLuint *) data, width, height, mipmap, 0);
break;
case 4: case 4:
GL_Upload32 ((GLuint *) data, width, height, mipmap, alpha); GL_Upload32 ((GLuint *) data, width, height, mipmap, alpha);
break; break;
default: default:
Sys_Error ("SetupTexture: unknown bytesperpixel %i", Sys_Error ("SetupTexture: unknown bytesperpixel %i",
glt->bytesperpixel); glt->bytesperpixel);
} }
return glt->texnum; return glt->texnum;