Add code for retore mips

This commit is contained in:
Denis Pauk 2019-01-30 23:03:38 +02:00
parent 203bc4b202
commit 1dfb54bf38
2 changed files with 89 additions and 67 deletions

View file

@ -124,6 +124,44 @@ R_ImageShrink(const unsigned char* src, unsigned char *dst, int width, int realw
}
}
static void
R_RestoreMips(image_t *image, int min_mips)
{
int i;
for (i=min_mips+1; i<NUM_MIPS; i++)
{
R_ImageShrink(image->pixels[i-1], image->pixels[i],
image->height / (1 << (i - 1)), image->height / (1 << i),
image->width / (1 << (i - 1)), image->width / (1 << i));
}
}
static size_t
R_GetImageMipsSize(size_t mip1_size)
{
size_t full_size = 0;
int i;
for (i=0; i<NUM_MIPS; i++)
{
full_size += mip1_size >> (i * 2);
}
return full_size;
}
static void
R_RestoreImagePointers(image_t *image, int min_mips)
{
int i;
for (i=min_mips+1; i<NUM_MIPS; i++)
{
image->pixels[i] = image->pixels[i - 1] + (
image->width * image->height / (1 << ((i - 1) * 2)));
}
}
/*
================
R_LoadPic
@ -147,7 +185,7 @@ R_LoadPic (char *name, byte *pic, int width, int height, imagetype_t type)
image->type = type;
size = width * height;
full_size = size * (256+64+16+4)/256;
full_size = R_GetImageMipsSize(size);
image->pixels[0] = malloc(full_size);
image->transparent = false;
for (i=0 ; i<size ; i++)
@ -161,19 +199,9 @@ R_LoadPic (char *name, byte *pic, int width, int height, imagetype_t type)
memcpy(image->pixels[0], pic, size);
// restore mips
image->pixels[1] = image->pixels[0] + image->width*image->height;
image->pixels[2] = image->pixels[1] + image->width*image->height/4;
image->pixels[3] = image->pixels[2] + image->width*image->height/16;
R_RestoreImagePointers(image, 0);
// restore everything from first image
R_ImageShrink(image->pixels[0], image->pixels[1],
image->height, image->height/2,
image->width, image->width/2);
R_ImageShrink(image->pixels[1], image->pixels[2],
image->height/2, image->height/4,
image->width/2, image->width/4);
R_ImageShrink(image->pixels[2], image->pixels[3],
image->height/4, image->height/8,
image->width/4, image->width/8);
R_RestoreMips(image, 0);
return image;
}
@ -188,7 +216,7 @@ R_LoadWal (char *name, imagetype_t type)
miptex_t *mt;
int ofs;
image_t *image;
int size, file_size;
size_t size, file_size;
file_size = ri.FS_LoadFile (name, (void **)&mt);
if (!mt)
@ -211,7 +239,7 @@ R_LoadWal (char *name, imagetype_t type)
image->type = type;
image->registration_sequence = registration_sequence;
ofs = LittleLong(mt->offsets[0]);
size = image->width * image->height * (256+64+16+4)/256;
size = R_GetImageMipsSize(image->width * image->height);
if ((ofs <= 0) || (image->width <= 0) || (image->height <= 0) ||
((file_size - ofs) / image->width < image->height))
@ -222,23 +250,13 @@ R_LoadWal (char *name, imagetype_t type)
}
image->pixels[0] = malloc (size);
image->pixels[1] = image->pixels[0] + image->width*image->height;
image->pixels[2] = image->pixels[1] + image->width*image->height/4;
image->pixels[3] = image->pixels[2] + image->width*image->height/16;
R_RestoreImagePointers(image, 0);
if (size > (file_size - ofs))
{
memcpy(image->pixels[0], (byte *)mt + ofs, file_size - ofs);
// looks short, restore everything from first image
R_ImageShrink(image->pixels[0], image->pixels[1],
image->height, image->height/2,
image->width, image->width/2);
R_ImageShrink(image->pixels[1], image->pixels[2],
image->height/2, image->height/4,
image->width/2, image->width/4);
R_ImageShrink(image->pixels[2], image->pixels[3],
image->height/4, image->height/8,
image->width/4, image->width/8);
R_RestoreMips(image, 0);
}
else
{
@ -301,7 +319,7 @@ R_LoadHiColorImage(char *name, const char* namewe, const char *ext, imagetype_t
if (width >= realwidth && height >= realheight)
{
byte* pic8 = NULL;
int size;
size_t size;
if (realheight == 0 || realwidth == 0)
{
@ -309,7 +327,7 @@ R_LoadHiColorImage(char *name, const char* namewe, const char *ext, imagetype_t
realwidth = width;
}
size = width * height * (256+64+16+4)/256;
size = R_GetImageMipsSize(width * height);
pic8 = malloc(size);
R_Convert32To8bit(pic, pic8, width * height);
if (width != realwidth || height != realheight)
@ -461,6 +479,46 @@ R_FreeUnusedImages (void)
}
}
static struct texture_buffer {
image_t image;
byte buffer[4096];
} r_notexture_buffer;
/*
==================
R_InitTextures
==================
*/
static void
R_InitTextures (void)
{
int x,y, m;
// create a simple checkerboard texture for the default
r_notexture_mip = &r_notexture_buffer.image;
r_notexture_mip->width = r_notexture_mip->height = 16;
r_notexture_mip->pixels[0] = r_notexture_buffer.buffer;
R_RestoreImagePointers(r_notexture_mip, 0);
for (m=0 ; m<NUM_MIPS ; m++)
{
byte *dest;
dest = r_notexture_mip->pixels[m];
for (y=0 ; y< (16>>m) ; y++)
for (x=0 ; x< (16>>m) ; x++)
{
if ( (y< (8>>m) ) ^ (x< (8>>m) ) )
*dest++ = 0;
else
*dest++ = 0xff;
}
}
}
/*
===============
R_InitImages
@ -482,6 +540,8 @@ R_InitImages (void)
d_16to8table = malloc(0x10000);
memcpy(d_16to8table, table16to8, 0x10000);
ri.FS_FreeFile((void *)table16to8);
R_InitTextures ();
}
/*

View file

@ -201,43 +201,6 @@ static void Draw_BuildGammaTable(void);
static void RE_EndFrame(void);
static void R_DrawBeam(const entity_t *e);
/*
==================
R_InitTextures
==================
*/
static void
R_InitTextures (void)
{
int x,y, m;
// create a simple checkerboard texture for the default
r_notexture_mip = &r_notexture_buffer.image;
r_notexture_mip->width = r_notexture_mip->height = 16;
r_notexture_mip->pixels[0] = r_notexture_buffer.buffer;
r_notexture_mip->pixels[1] = r_notexture_mip->pixels[0] + 16*16;
r_notexture_mip->pixels[2] = r_notexture_mip->pixels[1] + 8*8;
r_notexture_mip->pixels[3] = r_notexture_mip->pixels[2] + 4*4;
for (m=0 ; m<4 ; m++)
{
byte *dest;
dest = r_notexture_mip->pixels[m];
for (y=0 ; y< (16>>m) ; y++)
for (x=0 ; x< (16>>m) ; x++)
{
if ( (y< (8>>m) ) ^ (x< (8>>m) ) )
*dest++ = 0;
else
*dest++ = 0xff;
}
}
}
/*
================
R_InitTurb
@ -332,7 +295,6 @@ RE_Init(void)
R_InitImages ();
Mod_Init ();
Draw_InitLocal ();
R_InitTextures ();
view_clipplanes[0].leftedge = true;
view_clipplanes[1].rightedge = true;