Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/hzqst/MetaHookSv
Browse files Browse the repository at this point in the history
  • Loading branch information
hzqst committed Dec 23, 2023
2 parents 0e28d79 + 50e4f95 commit 01020da
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 89 deletions.
14 changes: 7 additions & 7 deletions Plugins/Renderer/gl_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion Plugins/Renderer/gl_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 5 additions & 8 deletions Plugins/Renderer/gl_rmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 2 additions & 3 deletions Plugins/Renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
123 changes: 54 additions & 69 deletions Plugins/Renderer/gl_studio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<double>(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<byte>(newRed / colorScale + 0.5); // Adding 0.5 for rounding
color[1] = static_cast<byte>(newGreen / colorScale + 0.5);
color[2] = static_cast<byte>(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)
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Plugins/Renderer/gl_water.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 01020da

Please sign in to comment.