- got rid of a lot of stuff that's no longer required with GLEW.

- replaced GLUs texture scaling with our own function. This is only used to scale down textures larger than what the hardware can handle so we do not need a dependency to an essentially deprecated library for it.
This commit is contained in:
Christoph Oelckers 2014-04-15 11:59:41 +02:00
commit 94b06900cb
9 changed files with 101 additions and 13770 deletions

View file

@ -75,6 +75,105 @@ int FHardwareTexture::GetTexDimension(int value)
return value;
}
//===========================================================================
//
// Quick'n dirty image rescaling.
//
// This will only be used when the source texture is larger than
// what the hardware can manage (extremely rare in Doom)
//
// Code taken from wxWidgets
//
//===========================================================================
struct BoxPrecalc
{
int boxStart;
int boxEnd;
};
static void ResampleBoxPrecalc(TArray<BoxPrecalc>& boxes, int oldDim)
{
int newDim = boxes.Size();
const double scale_factor_1 = double(oldDim) / newDim;
const int scale_factor_2 = (int)(scale_factor_1 / 2);
for (int dst = 0; dst < newDim; ++dst)
{
// Source pixel in the Y direction
const int src_p = int(dst * scale_factor_1);
BoxPrecalc& precalc = boxes[dst];
precalc.boxStart = clamp<int>(int(src_p - scale_factor_1 / 2.0 + 1), 0, oldDim - 1);
precalc.boxEnd = clamp<int>(MAX<int>(precalc.boxStart + 1, int(src_p + scale_factor_2)), 0, oldDim - 1);
}
}
void FHardwareTexture::Resize(int width, int height, unsigned char *src_data, unsigned char *dst_data)
{
// This function implements a simple pre-blur/box averaging method for
// downsampling that gives reasonably smooth results To scale the image
// down we will need to gather a grid of pixels of the size of the scale
// factor in each direction and then do an averaging of the pixels.
TArray<BoxPrecalc> vPrecalcs(height);
TArray<BoxPrecalc> hPrecalcs(width);
ResampleBoxPrecalc(vPrecalcs, texheight);
ResampleBoxPrecalc(hPrecalcs, texwidth);
int averaged_pixels, averaged_alpha, src_pixel_index;
double sum_r, sum_g, sum_b, sum_a;
for (int y = 0; y < height; y++) // Destination image - Y direction
{
// Source pixel in the Y direction
const BoxPrecalc& vPrecalc = vPrecalcs[y];
for (int x = 0; x < width; x++) // Destination image - X direction
{
// Source pixel in the X direction
const BoxPrecalc& hPrecalc = hPrecalcs[x];
// Box of pixels to average
averaged_pixels = 0;
averaged_alpha = 0;
sum_r = sum_g = sum_b = sum_a = 0.0;
for (int j = vPrecalc.boxStart; j <= vPrecalc.boxEnd; ++j)
{
for (int i = hPrecalc.boxStart; i <= hPrecalc.boxEnd; ++i)
{
// Calculate the actual index in our source pixels
src_pixel_index = j * texwidth + i;
int a = src_data[src_pixel_index * 4 + 3];
if (a > 0) // do not use color from fully transparent pixels
{
sum_r += src_data[src_pixel_index * 4 + 0];
sum_g += src_data[src_pixel_index * 4 + 1];
sum_b += src_data[src_pixel_index * 4 + 2];
sum_a += a;
averaged_pixels++;
}
averaged_alpha++;
}
}
// Calculate the average from the sum and number of averaged pixels
dst_data[0] = (unsigned char)xs_CRoundToInt(sum_r / averaged_pixels);
dst_data[1] = (unsigned char)xs_CRoundToInt(sum_g / averaged_pixels);
dst_data[2] = (unsigned char)xs_CRoundToInt(sum_b / averaged_pixels);
dst_data[3] = (unsigned char)xs_CRoundToInt(sum_a / averaged_alpha);
dst_data += 4;
}
}
}
//===========================================================================
//
// Loads the texture image into the hardware
@ -124,7 +223,7 @@ void FHardwareTexture::LoadImage(unsigned char * buffer,int w, int h, unsigned i
unsigned char * scaledbuffer=(unsigned char *)calloc(4,rw * (rh+1));
if (scaledbuffer)
{
gluScaleImage(GL_RGBA,w, h,GL_UNSIGNED_BYTE,buffer, rw, rh, GL_UNSIGNED_BYTE,scaledbuffer);
Resize(rw, rh, buffer, scaledbuffer);
deletebuffer=true;
buffer=scaledbuffer;
}