Merge branch 'model-texture-fix' into 'master'

Model texture fix

Closes #97 and #100

See merge request KartKrew/Kart!325
This commit is contained in:
Sal 2020-10-28 16:31:19 -04:00
commit 1aef75bff3
2 changed files with 66 additions and 50 deletions

View file

@ -419,7 +419,6 @@ static void md2_loadTexture(md2_t *model)
}
HWD.pfnSetTexture(grpatch->mipmap);
HWR_UnlockCachedPatch(grpatch);
}
// -----------------+
@ -470,7 +469,6 @@ static void md2_loadBlendTexture(md2_t *model)
}
HWD.pfnSetTexture(grpatch->mipmap); // We do need to do this so that it can be cleared and knows to recreate it when necessary
HWR_UnlockCachedPatch(grpatch);
Z_Free(filename);
}
@ -609,9 +607,6 @@ void HWR_AddPlayerModel(int skin) // For skins that were added after startup
}
}
// Check for any MD2s that match the names of sprite names!
while (fscanf(f, "%19s %31s %f %f", name, filename, &scale, &offset) == 4)
// length of the player model prefix
prefixlen = strlen(PLAYERMODELPREFIX);
@ -703,10 +698,12 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
UINT16 w = gpatch->width, h = gpatch->height;
UINT32 size = w*h;
RGBA_t *image, *blendimage, *cur, blendcolor;
UINT16 translation[16]; // First the color index
UINT8 translation[16]; // First the color index
UINT8 cutoff[16]; // Brightness cutoff before using the next color
UINT8 translen = 0;
UINT8 i;
UINT8 colorbrightnesses[16];
UINT8 color_match_lookup[256]; // optimization attempt
blendcolor = V_GetColor(0); // initialize
memset(translation, 0, sizeof(translation));
@ -742,6 +739,7 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
if (color != SKINCOLOR_NONE && color < numskincolors)
{
UINT8 numdupes = 1;
UINT8 prevdupes = numdupes;
translation[translen] = skincolors[color].ramp[0];
cutoff[translen] = 255;
@ -756,18 +754,56 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
if (translen > 0)
{
cutoff[translen] = cutoff[translen-1] - (256 / (16 / numdupes));
INT16 newcutoff = cutoff[translen-1] - (255 / (16 / prevdupes));
if (newcutoff < 0)
newcutoff = 0;
cutoff[translen] = (UINT8)newcutoff;
}
prevdupes = numdupes;
numdupes = 1;
translen++;
translation[translen] = (UINT16)skincolors[color].ramp[i];
translation[translen] = (UINT8)skincolors[color].ramp[i];
}
translen++;
}
if (skinnum == TC_RAINBOW && translen > 0)
{
UINT16 b;
INT32 compare;
for (i = 0; i < translen; i++) // moved from inside the loop to here
{
RGBA_t tempc = V_GetColor(translation[i]);
colorbrightnesses[i] = K_ColorRelativeLuminance(tempc.s.red, tempc.s.green, tempc.s.blue); // store brightnesses for comparison
}
// generate lookup table for color brightness matching
for (b = 0; b < 256; b++)
{
UINT16 brightdif = 256;
color_match_lookup[i] = 0;
for (i = 0; i < translen; i++)
{
if (b > colorbrightnesses[i]) // don't allow greater matches (because calculating a makeshift gradient for this is already a huge mess as is)
continue;
compare = abs((INT16)(colorbrightnesses[i]) - (INT16)(b));
if (compare < brightdif)
{
brightdif = (UINT16)compare;
color_match_lookup[b] = i; // best matching color that's equal brightness or darker
}
}
}
}
while (size--)
{
if (skinnum == TC_BOSS)
@ -839,7 +875,7 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
else
{
// All settings that use skincolors!
UINT8 brightness;
UINT16 brightness;
if (translen <= 0)
{
@ -857,8 +893,11 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
}
else
{
UINT8 imagebright = K_ColorRelativeLuminance(image->s.red, image->s.green, image->s.blue);
UINT8 blendbright = K_ColorRelativeLuminance(blendimage->s.red, blendimage->s.green, blendimage->s.blue);
UINT16 imagebright, blendbright;
imagebright = K_ColorRelativeLuminance(image->s.red, image->s.green, image->s.blue);
blendbright = K_ColorRelativeLuminance(blendimage->s.red, blendimage->s.green, blendimage->s.blue);
// slightly dumb average between the blend image color and base image colour, usually one or the other will be fully opaque anyway
brightness = (imagebright*(255-blendimage->s.alpha))/255 + (blendbright*blendimage->s.alpha)/255;
}
@ -887,9 +926,8 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
// Ensue horrible mess.
if (skinnum == TC_RAINBOW)
{
UINT16 brightdif = 256;
UINT8 colorbrightnesses[16];
INT32 compare, m, d;
//UINT16 brightdif = 256;
INT32 /*compare,*/ m, d;
// Ignore pure white & pitch black
if (brightness > 253 || brightness < 2)
@ -903,13 +941,7 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
mul = 0;
mulmax = 1;
for (i = 0; i < translen; i++)
{
RGBA_t tempc = V_GetColor(translation[i]);
colorbrightnesses[i] = K_ColorRelativeLuminance(tempc.s.red, tempc.s.green, tempc.s.blue); // store brightnesses for comparison
}
for (i = 0; i < translen; i++)
/*for (i = 0; i < translen; i++)
{
if (brightness > colorbrightnesses[i]) // don't allow greater matches (because calculating a makeshift gradient for this is already a huge mess as is)
continue;
@ -921,19 +953,13 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
brightdif = (UINT16)compare;
firsti = i; // best matching color that's equal brightness or darker
}
}
}*/
firsti = color_match_lookup[brightness];
secondi = firsti+1; // next color in line
if (secondi >= translen)
{
m = (INT16)brightness; // - 0;
d = (INT16)colorbrightnesses[firsti]; // - 0;
}
else
{
m = (INT16)brightness - (INT16)colorbrightnesses[secondi];
d = (INT16)colorbrightnesses[firsti] - (INT16)colorbrightnesses[secondi];
}
m = (INT16)brightness - (INT16)colorbrightnesses[secondi];
d = (INT16)colorbrightnesses[firsti] - (INT16)colorbrightnesses[secondi];
if (m >= d)
m = d-1;
@ -960,29 +986,15 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
secondi = firsti+1;
mulmax = cutoff[firsti];
if (secondi < translen)
mulmax -= cutoff[secondi];
mulmax = cutoff[firsti] - cutoff[secondi];
mul = cutoff[firsti] - brightness;
}
blendcolor = V_GetColor(translation[firsti]);
if (secondi >= translen)
mul = 0;
if (mul > 0) // If it's 0, then we only need the first color.
{
#if 0
if (secondi >= translen)
{
// blend to black
nextcolor = V_GetColor(31);
}
else
#endif
nextcolor = V_GetColor(translation[secondi]);
nextcolor = V_GetColor(translation[secondi]);
// Find difference between points
r = (INT32)(nextcolor.s.red - blendcolor.s.red);
@ -1004,10 +1016,14 @@ static void HWR_CreateBlendedTexture(GLPatch_t *gpatch, GLPatch_t *blendgpatch,
if (skinnum == TC_RAINBOW)
{
UINT32 tempcolor;
UINT8 colorbright = K_ColorRelativeLuminance(blendcolor.s.red, blendcolor.s.green, blendcolor.s.blue);
UINT16 colorbright;
colorbright = K_ColorRelativeLuminance(blendcolor.s.red, blendcolor.s.green, blendcolor.s.blue);
if (colorbright == 0)
{
colorbright = 1; // no dividing by 0 please
}
tempcolor = (brightness * blendcolor.s.red) / colorbright;
tempcolor = min(255, tempcolor);

View file

@ -62,7 +62,7 @@ enum
PU_CACHE_UNLOCKED = 101, // Note: unused
PU_HWRCACHE_UNLOCKED = 102, // 'unlocked' PU_HWRCACHE memory:
// 'second-level' cache for graphics
// stored in hardware format and downloaded as needed
// stored in hardware format and downloaded as needed
PU_HWRPATCHINFO_UNLOCKED = 103, // 'unlocked' PU_HWRPATCHINFO memory
PU_HWRMODELTEXTURE_UNLOCKED = 104, // 'unlocked' PU_HWRMODELTEXTURE memory
};