From 50e4f954c55210e1334c45d84df9d6f5a263f1bc Mon Sep 17 00:00:00 2001 From: hzqst <113660872@qq.com> Date: Sat, 23 Dec 2023 00:43:10 +0800 Subject: [PATCH] Fix a bug that remap skin not working. --- Plugins/Renderer/gl_draw.cpp | 14 ++-- Plugins/Renderer/gl_model.cpp | 2 +- Plugins/Renderer/gl_rmain.cpp | 13 ++-- Plugins/Renderer/gl_shader.cpp | 5 +- Plugins/Renderer/gl_studio.cpp | 123 +++++++++++++++------------------ Plugins/Renderer/gl_water.cpp | 2 +- 6 files changed, 70 insertions(+), 89 deletions(-) diff --git a/Plugins/Renderer/gl_draw.cpp b/Plugins/Renderer/gl_draw.cpp index f8f66dcd..856c5e39 100644 --- a/Plugins/Renderer/gl_draw.cpp +++ b/Plugins/Renderer/gl_draw.cpp @@ -394,17 +394,17 @@ void GL_UnloadTextures(void) void GL_UnloadTextureWithType(const char* identifier, GL_TEXTURETYPE textureType, bool notify_callback) { char hashedIdentifier[64] = { 0 }; - GL_GenerateHashedTextureIndentifier(identifier, textureType, hashedIdentifier, sizeof(hashedIdentifier) - 1); + GL_GenerateHashedTextureIndentifier(identifier, textureType, hashedIdentifier, sizeof(hashedIdentifier)); - GL_UnloadTextureByIdentifier(identifier, notify_callback); + GL_UnloadTextureByIdentifier(hashedIdentifier, notify_callback); } void GL_UnloadTextureWithType(const char* identifier, GL_TEXTURETYPE textureType, int width, int height, bool notify_callback) { char hashedIdentifier[64] = { 0 }; - GL_GenerateHashedTextureIndentifier2(identifier, textureType, width, height, hashedIdentifier, sizeof(hashedIdentifier) - 1); + GL_GenerateHashedTextureIndentifier2(identifier, textureType, width, height, hashedIdentifier, sizeof(hashedIdentifier)); - GL_UnloadTextureByIdentifier(identifier, notify_callback); + GL_UnloadTextureByIdentifier(hashedIdentifier, notify_callback); } void GL_UnloadTextureByTextureId(int gltexturenum, bool notify_callback) @@ -575,7 +575,7 @@ int GL_FindTextureByHashedIdentifier(const char *hashedIdentifier, GL_TEXTURETYP int GL_FindTexture(const char* identifier, GL_TEXTURETYPE textureType, int* width, int* height) { char hashedIdentifier[64] = { 0 }; - GL_GenerateHashedTextureIndentifier(identifier, textureType, hashedIdentifier, sizeof(hashedIdentifier) - 1); + GL_GenerateHashedTextureIndentifier(identifier, textureType, hashedIdentifier, sizeof(hashedIdentifier)); return GL_FindTextureByHashedIdentifier(hashedIdentifier, textureType, width, height); @@ -686,7 +686,7 @@ int GL_LoadTexture(const char* identifier, GL_TEXTURETYPE textureType, int width int GL_LoadTexture2(const char* identifier, GL_TEXTURETYPE textureType, int width, int height, byte* data, qboolean mipmap, int iType, byte* pPal, int filter) { char hashedIdentifier[64] = { 0 }; - GL_GenerateHashedTextureIndentifier2(identifier, textureType, width, height, hashedIdentifier, sizeof(hashedIdentifier) - 1); + GL_GenerateHashedTextureIndentifier2(identifier, textureType, width, height, hashedIdentifier, sizeof(hashedIdentifier)); int gltexturenum = gPrivateFuncs.GL_LoadTexture2(hashedIdentifier, textureType, width, height, data, mipmap, iType, pPal, filter); @@ -704,7 +704,7 @@ int GL_LoadTextureInternal(const char *identifier, GL_TEXTURETYPE textureType, i } char hashedIdentifier[64] = { 0 }; - GL_GenerateHashedTextureIndentifier2(identifier, textureType, width, height, hashedIdentifier, sizeof(hashedIdentifier) - 1); + GL_GenerateHashedTextureIndentifier2(identifier, textureType, width, height, hashedIdentifier, sizeof(hashedIdentifier)); int gltexturenum = GL_AllocTexture(hashedIdentifier, textureType, width, height, mipmap); diff --git a/Plugins/Renderer/gl_model.cpp b/Plugins/Renderer/gl_model.cpp index fd4db978..2a363be4 100644 --- a/Plugins/Renderer/gl_model.cpp +++ b/Plugins/Renderer/gl_model.cpp @@ -81,7 +81,7 @@ void Mod_UnloadSpriteTextures(model_t* mod) for (int i = 0; i < pSprite->numframes; i++) { char name[260] = {0}; - snprintf(name, sizeof(name) - 1, "%s_%i", mod->name, i); + snprintf(name, sizeof(name), "%s_%i", mod->name, i); GL_UnloadTextureWithType(name, GLT_SPRITE, true); GL_UnloadTextureWithType(name, GLT_HUDSPRITE, true); diff --git a/Plugins/Renderer/gl_rmain.cpp b/Plugins/Renderer/gl_rmain.cpp index 4dd75900..2eb3fe75 100644 --- a/Plugins/Renderer/gl_rmain.cpp +++ b/Plugins/Renderer/gl_rmain.cpp @@ -3427,7 +3427,7 @@ void R_LoadDetailSkyTextures(const char* name) for (int i = 0; i < 6; i++) { - char fullpath[256]; + char fullpath[260] = {0}; snprintf(fullpath, sizeof(fullpath), "gfx/env/%s%s.dds", name, suf[i]); int width, height; @@ -3487,14 +3487,14 @@ void R_BuildCubemap_Snapshot(cubemap_t *cubemap, int index) g_pFileSystem->CreateDirHierarchy("gfx/cubemap"); char path[64]; - snprintf(path, sizeof(path) - 1, "gfx/cubemap/%s", name); + snprintf(path, sizeof(path), "gfx/cubemap/%s", name); path[sizeof(path) - 1] = 0; if (!g_pFileSystem->IsDirectory(path)) g_pFileSystem->CreateDirHierarchy(path); char filepath[1024]; - snprintf(filepath, sizeof(filepath) - 1, "gfx/cubemap/%s/%s_%d.%s", name, cubemap->name.c_str(), index, cubemap->extension.c_str()); + snprintf(filepath, sizeof(filepath), "gfx/cubemap/%s/%s_%d.%s", name, cubemap->name.c_str(), index, cubemap->extension.c_str()); filepath[sizeof(filepath) - 1] = 0; byte *pBuf = (byte *)malloc(cubemap->size * cubemap->size * 3); @@ -3617,11 +3617,8 @@ void R_LoadCubemap(cubemap_t *cubemap) for (int i = 0; i < 6; ++i) { - snprintf(filepath, sizeof(filepath) - 1, "gfx/cubemap/%s/%s_%d.%s", name, cubemap->name.c_str(), i, cubemap->extension.c_str()); - filepath[sizeof(filepath) - 1] = 0; - - snprintf(identifier, sizeof(identifier) - 1, "cubemap_%s", cubemap->name.c_str()); - identifier[sizeof(identifier) - 1] = 0; + snprintf(filepath, sizeof(filepath), "gfx/cubemap/%s/%s_%d.%s", name, cubemap->name.c_str(), i, cubemap->extension.c_str()); + snprintf(identifier, sizeof(identifier), "cubemap_%s", cubemap->name.c_str()); gl_loadtexture_cubemap = i + 1; diff --git a/Plugins/Renderer/gl_shader.cpp b/Plugins/Renderer/gl_shader.cpp index ed35ec86..747fc653 100644 --- a/Plugins/Renderer/gl_shader.cpp +++ b/Plugins/Renderer/gl_shader.cpp @@ -57,9 +57,8 @@ GLuint R_CompileShaderObject(int type, const char *code, const char *filename) FILESYSTEM_ANY_CREATEDIR("logs/renderer"); FILESYSTEM_ANY_CREATEDIR("logs/renderer/shader"); - char filepath[256]; - snprintf(filepath, 255, "logs\\%s", filename); - filepath[255] = 0; + char filepath[256] = {0}; + snprintf(filepath, sizeof(filepath), "logs\\%s", filename); //gEngfuncs.Con_DPrintf("writing %s...", filepath); diff --git a/Plugins/Renderer/gl_studio.cpp b/Plugins/Renderer/gl_studio.cpp index 016dd34c..87e2fca4 100644 --- a/Plugins/Renderer/gl_studio.cpp +++ b/Plugins/Renderer/gl_studio.cpp @@ -1117,8 +1117,6 @@ void R_StudioSetupVBOMaterial(const studio_vbo_t* VBOData, const studio_vbo_mate } } -#if 1 - size_t safe_strlen(const char* str, size_t maxChars) { size_t count; @@ -1253,17 +1251,17 @@ byte* R_StudioReloadSkin(model_t* pModel, int index, skin_t* pskin) size = ptexture->height * ptexture->width; - if ((byte*)pbase + ptexture->index < (byte*)studiohdr) { + if (pbase + ptexture->index < pbase) { gEngfuncs.COM_FreeFile(pbase); return NULL; } - if ((byte*)pbase + ptexture->index + size > (byte*)studiohdr + fileLength) { + if (pbase + ptexture->index + size > pbase + fileLength) { gEngfuncs.COM_FreeFile(pbase); return NULL; } - Cache_Alloc(pCache, size + 768 + 8, pskin->name); + Cache_Alloc(pCache, size + 768 + offsetof(model_texture_cache_t, data), pskin->name); pData = (model_texture_cache_t*)pCache->data; pData->width = ptexture->width; @@ -1288,81 +1286,69 @@ void R_StudioFlushSkins(int keynum) } } -void PaletteHueReplace(byte* palette, int newHue, int Start, int end) -{ - int i; - float r, b, g; - float maxcol, mincol; - float hue, val, sat; +void PaletteHueReplace(byte* palette, int newHue, int start, int end) { + // Convert the new hue value to a range used in the algorithm + double targetHue = static_cast(newHue) * 1.411764705882353; + double colorScale = 0.0039215689; // This is 1/255 to scale color values - hue = (float)(newHue * (360.0 / 255)); + if (start > end) { + return; // No range to process + } - for (i = Start; i <= end; i++) - { - b = palette[i + 0]; - g = palette[i + 1]; - r = palette[i + 2]; + // Iterate over the palette entries from start to end + for (int i = start; i <= end; ++i) { + byte* color = &palette[i * 3]; // Pointer to the current color (RGB) - maxcol = max(max(r, g), b) / 255.0f; - mincol = min(min(r, g), b) / 255.0f; + // Extract the RGB components and convert to the range [0, 1] + double red = color[0] * colorScale; + double green = color[1] * colorScale; + double blue = color[2] * colorScale; - val = maxcol; - sat = (maxcol - mincol) / maxcol; + // Find the maximum and minimum RGB components manually + double maxColor = red; + if (green > maxColor) maxColor = green; + if (blue > maxColor) maxColor = blue; - mincol = val * (1.0f - sat); + double minColor = red; + if (green < minColor) minColor = green; + if (blue < minColor) minColor = blue; - if (hue <= 120) - { - b = mincol; - if (hue < 60) - { - r = val; - g = mincol + hue * (val - mincol) / (120 - hue); - } - else - { - g = val; - r = mincol + (120 - hue) * (val - mincol) / hue; - } + // Calculate the chroma (difference between max and min color components) + double chroma = maxColor - minColor; + + // If chroma is 0, the color is grayscale, and hue replacement is not needed + if (chroma == 0) { + continue; } - else if (hue <= 240) - { - r = mincol; - if (hue < 180) - { - g = val; - b = mincol + (hue - 120) * (val - mincol) / (240 - hue); - } - else - { - b = val; - g = mincol + (240 - hue) * (val - mincol) / (hue - 120); - } + + // Calculate the intermediate value used for creating the new color + double intermediateValue = (1.0 - chroma / maxColor) * maxColor; + + // Calculate the new RGB components based on the target hue + double newRed, newGreen, newBlue; + if (targetHue < 120.0) { + newRed = intermediateValue + chroma * (120.0 - targetHue) / 120.0; + newGreen = intermediateValue + chroma * targetHue / 120.0; + newBlue = intermediateValue; } - else - { - g = mincol; - if (hue < 300) - { - b = val; - r = mincol + (hue - 240) * (val - mincol) / (360 - hue); - } - else - { - r = val; - b = mincol + (360 - hue) * (val - mincol) / (hue - 240); - } + else if (targetHue < 240.0) { + newRed = intermediateValue; + newGreen = intermediateValue + chroma * (240.0 - targetHue) / 120.0; + newBlue = intermediateValue + chroma * (targetHue - 120.0) / 120.0; + } + else { + newRed = intermediateValue + chroma * (targetHue - 240.0) / 120.0; + newGreen = intermediateValue; + newBlue = intermediateValue + chroma * (360.0 - targetHue) / 120.0; } - palette[i + 0] = b * 255; - palette[i + 1] = g * 255; - palette[i + 2] = r * 255; - palette += 3; + // Assign the new RGB components to the palette, converting back to [0, 255] range + color[0] = static_cast(newRed / colorScale + 0.5); // Adding 0.5 for rounding + color[1] = static_cast(newGreen / colorScale + 0.5); + color[2] = static_cast(newBlue / colorScale + 0.5); } } -#endif - void R_StudioSetupSkinEx(const studio_vbo_t* VBOData, studiohdr_t* ptexturehdr, int index, float* width, float* height, program_state_t *StudioProgramState) { if ((*g_ForcedFaceFlags) & STUDIO_NF_CHROME) @@ -1395,7 +1381,6 @@ void R_StudioSetupSkinEx(const studio_vbo_t* VBOData, studiohdr_t* ptexturehdr, { char fullname[1024] = {0}; snprintf(fullname, sizeof(fullname), "%s_%s_%d", ptexturehdr->name, ptexture[index].name, (*currententity)->index); - fullname[MAX_PATH - 1] = 0; byte* orig_palette = pData + (ptexture[index].height * ptexture[index].width); @@ -1413,7 +1398,7 @@ void R_StudioSetupSkinEx(const studio_vbo_t* VBOData, studiohdr_t* ptexturehdr, GL_UnloadTextureWithType(fullname, GLT_STUDIO, true); - pskin->gl_index = GL_LoadTexture(fullname, GLT_STUDIO, ptexture[index].width, ptexture[index].height, pData, false, (ptexture[index].flags & STUDIO_NF_MASKED) ? TEX_TYPE_ALPHA : TEX_TYPE_NONE, tmp_palette); + pskin->gl_index = GL_LoadTexture(fullname, GLT_STUDIO, pskin->width, pskin->height, pData, false, (ptexture[index].flags & STUDIO_NF_MASKED) ? TEX_TYPE_ALPHA : TEX_TYPE_NONE, tmp_palette); } } diff --git a/Plugins/Renderer/gl_water.cpp b/Plugins/Renderer/gl_water.cpp index 3ab33117..a9295495 100644 --- a/Plugins/Renderer/gl_water.cpp +++ b/Plugins/Renderer/gl_water.cpp @@ -630,7 +630,7 @@ water_vbo_t *R_CreateWaterVBO(msurface_t *surf, int direction, wsurf_vbo_leaf_t glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, WaterVBO->ripple_image); char identifier[64] = { 0 }; - snprintf(identifier, sizeof(identifier) - 1, "%s_ripple", surf->texinfo->texture->name); + snprintf(identifier, sizeof(identifier), "%s_ripple", surf->texinfo->texture->name); WaterVBO->ripplemap = R_LoadRGBATextureFromMemory(identifier, WaterVBO->ripple_width, WaterVBO->ripple_height, WaterVBO->ripple_image, GLT_WORLD, true, true);