Skip to content

Commit

Permalink
Update texture loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
hzqst committed Dec 26, 2023
1 parent db282de commit f7b54b1
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 64 deletions.
49 changes: 28 additions & 21 deletions Build/svencoop/renderer/shader/studio_shader.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -260,38 +260,45 @@ vec3 R_StudioCelShade(vec3 v_color, vec3 normalWS, vec3 lightdirWS, float specul

#if 1

#if defined(STUDIO_NF_CELSHADE_FACE)
L.z *= 0.0001;
#else
L.z *= 0.01;
#endif
#if defined(STUDIO_NF_CELSHADE_FACE)
L.z *= 0.0001;
#else
L.z *= 0.01;
#endif

L = normalize(L);
L = normalize(L);

vec3 vecForward = ExtractForwardVector(v_rotmatrix);
vec3 vecUp = ExtractUpVector(v_rotmatrix);
vec3 vecBack = -vecForward;
#if defined(STUDIO_NF_CELSHADE_FACE)
vec3 vecForward = ExtractForwardVector(v_rotmatrix);
vec3 vecUp = ExtractUpVector(v_rotmatrix);

L = ProjectVectorOntoPlane(L, vecUp);

vec3 L2 = normalize(L);
L = ProjectVectorOntoPlane(L, vecUp);

vec3 L2 = normalize(L);

float flFaceUp = abs(vecForward.z);
flFaceUp = pow(flFaceUp, 10.0);

float flFaceUp = abs(vecForward.z);
flFaceUp = pow(flFaceUp, 10.0);
float flFaceDot = dot(vecForward, lightdirWS);
float flFaceMix = step(0.0, flFaceDot) * 2.0 - 1.0;

L = mix(L2, vecBack, flFaceUp);
vec3 vecFaceLightWS = vecForward * flFaceMix;

L = mix(L2, vecFaceLightWS, flFaceUp);

#endif

#endif

#if 0

#if defined(STUDIO_NF_CELSHADE_FACE)
L.z = 0;
#else
L.z *= 0.01;
#endif
#if defined(STUDIO_NF_CELSHADE_FACE)
L.z = 0;
#else
L.z *= 0.01;
#endif

L = normalize(L);
L = normalize(L);

#endif

Expand Down
141 changes: 99 additions & 42 deletions Plugins/Renderer/gl_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ gltexture_t* GL_FindTextureEntryEx(const char* identifier, GL_TEXTURETYPE textur
}

/*
Search for glt and returns glt->texnum, The identifier, textureType, width and height must be matched
Search for the specified glt entry and returns glt->texnum, The identifier, textureType, width and height must be matched
*/
int GL_FindTextureEx2(const char* identifier, GL_TEXTURETYPE textureType, int width, int height)
{
Expand Down Expand Up @@ -685,7 +685,7 @@ int GL_FindTextureEx2(const char* identifier, GL_TEXTURETYPE textureType, int wi
}

/*
Search for glt and returns glt, The identifier, textureType, width and height must be matched
Search for the specified glt entry and returns glt, The identifier, textureType, width and height must be matched
*/
gltexture_t * GL_FindTextureEntryEx2(const char* identifier, GL_TEXTURETYPE textureType, int width, int height)
{
Expand Down Expand Up @@ -731,6 +731,10 @@ gltexture_t * GL_FindTextureEntryEx2(const char* identifier, GL_TEXTURETYPE text
return 0;
}

/*
Search for the specified glt entry and returns glt->texnum, The raw-identifier and textureType must be matched
*/

int GL_FindTexture(const char* identifier, GL_TEXTURETYPE textureType, int* width, int* height)
{
char hashedIdentifier[64] = { 0 };
Expand All @@ -741,6 +745,10 @@ int GL_FindTexture(const char* identifier, GL_TEXTURETYPE textureType, int* widt
return foundTexture;
}

/*
Search for the specified glt entry and returns glt, The raw-identifier and textureType must be matched
*/

gltexture_t *GL_FindTextureEntry(const char* identifier, GL_TEXTURETYPE textureType)
{
char hashedIdentifier[64] = { 0 };
Expand All @@ -751,6 +759,10 @@ gltexture_t *GL_FindTextureEntry(const char* identifier, GL_TEXTURETYPE textureT
return foundGLT;
}

/*
Search for the specified glt entry and returns glt->texnum, The raw-identifier, textureType, width and height must be matched
*/

int GL_FindTexture2(const char* identifier, GL_TEXTURETYPE textureType, int width, int height)
{
char hashedIdentifier[64] = { 0 };
Expand All @@ -761,6 +773,10 @@ int GL_FindTexture2(const char* identifier, GL_TEXTURETYPE textureType, int widt
return foundTexture;
}

/*
Search for the specified glt entry, The raw-identifier, textureType, width and height must be matched
*/

gltexture_t*GL_FindTextureEntry2(const char* identifier, GL_TEXTURETYPE textureType, int width, int height)
{
char hashedIdentifier[64] = { 0 };
Expand All @@ -771,7 +787,11 @@ gltexture_t*GL_FindTextureEntry2(const char* identifier, GL_TEXTURETYPE textureT
return foundGLT;
}

gltexture_t * GL_AllocTextureEx(const char* identifier, GL_TEXTURETYPE textureType, int width, int height, qboolean mipmap)
/*
Allocate an empty glt entry, or returns an existing one if identifier matched
*/

gltexture_t * GL_AllocTextureEntry(const char* identifier, GL_TEXTURETYPE textureType, int width, int height, qboolean mipmap, bool *foundExisting)
{
int i;
gltexture_t* glt;
Expand All @@ -797,6 +817,9 @@ gltexture_t * GL_AllocTextureEx(const char* identifier, GL_TEXTURETYPE textureTy
if (slot->servercount > 0)
slot->servercount = *gHostSpawnCount;

if (foundExisting)
*foundExisting = true;

return slot;
}
}
Expand Down Expand Up @@ -874,12 +897,15 @@ gltexture_t * GL_AllocTextureEx(const char* identifier, GL_TEXTURETYPE textureTy

glt->paletteIndex = -1;

if (foundExisting)
*foundExisting = false;

return glt;
}

int GL_AllocTexture(const char* identifier, GL_TEXTURETYPE textureType, int width, int height, qboolean mipmap)
int GL_AllocTexture(const char* identifier, GL_TEXTURETYPE textureType, int width, int height, qboolean mipmap, bool* foundExisting)
{
auto glt = GL_AllocTextureEx(identifier, textureType, width, height, mipmap);
auto glt = GL_AllocTextureEntry(identifier, textureType, width, height, mipmap, foundExisting);

if (!glt)
return 0;
Expand Down Expand Up @@ -1139,25 +1165,71 @@ void GL_Upload32ToMipmap(byte* pData, int width, int height, int iPalTextureType
GL_ProcessMipmap32(iPalTextureType, state);
}

int GL_LoadTexture(char* identifier, GL_TEXTURETYPE textureType, int width, int height, byte* data, qboolean mipmap, int iPalTextureType, byte* pPal)
int GL_LoadTexture3(gltexture_t* glt, GL_TEXTURETYPE textureType, gl_loadtexture_state_t* state)
{
return GL_LoadTexture2(identifier, textureType, width, height, data, mipmap, iPalTextureType, pPal, (*gl_filter_max));
int iTextureTarget = GL_TEXTURE_2D;

if (state->cubemap)
iTextureTarget = GL_TEXTURE_CUBE_MAP;

if (!state->wrap)
{
state->wrap = GL_REPEAT;

if (textureType == GLT_HUDSPRITE || textureType == GLT_SPRITE)
state->wrap = GL_CLAMP_TO_EDGE;
}

glBindTexture(iTextureTarget, glt->texnum);
(*currenttexture) = -1;

if (state->compressed)
{
GL_UploadDXT(state);
}
else
{
GL_UploadRGBA8(state);
}

glBindTexture(iTextureTarget, 0);
(*currenttexture) = -1;

return glt->texnum;
}

int GL_LoadTexture2(char* identifier, GL_TEXTURETYPE textureType, int width, int height, byte* data, qboolean mipmap, int iPalTextureType, byte* pPal, int filter)
{
char hashedIdentifier[64] = { 0 };
GL_GenerateHashedTextureIndentifier2(identifier, textureType, width, height, hashedIdentifier, sizeof(hashedIdentifier));

if (bUseLegacyTextureLoader)
{
char hashedIdentifier[64] = { 0 };
GL_GenerateHashedTextureIndentifier2(identifier, textureType, width, height, hashedIdentifier, sizeof(hashedIdentifier));

int gltexturenum = gPrivateFuncs.GL_LoadTexture2(hashedIdentifier, textureType, width, height, data, mipmap, iPalTextureType, pPal, filter);

gEngfuncs.Con_DPrintf("GL_LoadTexture2: [%s] -> [%s] [%d]\n", identifier, hashedIdentifier, gltexturenum);
gEngfuncs.Con_DPrintf("GL_LoadTexture2: Using legacy texture loader [%s] -> [%s] [%d]\n", identifier, hashedIdentifier, gltexturenum);

return gltexturenum;
}

bool foundExisting = false;

auto glt = GL_AllocTextureEntry(hashedIdentifier, textureType, width, height, mipmap, &foundExisting);

if (!glt)
{
gEngfuncs.Con_Printf("GL_LoadTexture2: Failed to allocate texture entry for [%s] -> [%s].\n", identifier, hashedIdentifier);
return 0;
}

if (foundExisting)
{
gEngfuncs.Con_DPrintf("GL_LoadTexture2: Found existing texture entry [%s] -> [%s] [%d]\n", identifier, hashedIdentifier, glt->texnum);
return glt->texnum;
}

gEngfuncs.Con_DPrintf("GL_LoadTexture2: Using new texture loader [%s] -> [%s] [%d]\n", identifier, hashedIdentifier, glt->texnum);

gl_loadtexture_state_t state;

state.format = GL_RGBA8;
Expand Down Expand Up @@ -1190,7 +1262,12 @@ int GL_LoadTexture2(char* identifier, GL_TEXTURETYPE textureType, int width, int
}
}

return GL_LoadTextureEx(identifier, textureType, &state);
return GL_LoadTexture3(glt, textureType, &state);
}

int GL_LoadTexture(char* identifier, GL_TEXTURETYPE textureType, int width, int height, byte* data, qboolean mipmap, int iPalTextureType, byte* pPal)
{
return GL_LoadTexture2(identifier, textureType, width, height, data, mipmap, iPalTextureType, pPal, (*gl_filter_max));
}

int GL_LoadTextureEx(const char *identifier, GL_TEXTURETYPE textureType, gl_loadtexture_state_t *state)
Expand All @@ -1204,45 +1281,25 @@ int GL_LoadTextureEx(const char *identifier, GL_TEXTURETYPE textureType, gl_load
char hashedIdentifier[64] = { 0 };
GL_GenerateHashedTextureIndentifier2(identifier, textureType, state->width, state->height, hashedIdentifier, sizeof(hashedIdentifier));

int gltexturenum = GL_AllocTexture(hashedIdentifier, textureType, state->width, state->height, state->mipmap);
bool foundExisting = false;

if (!gltexturenum)
{
gEngfuncs.Con_Printf("GL_LoadTextureEx: Failed to allocate texture entry for %s.\n", identifier);
return 0;
}
auto glt = GL_AllocTextureEntry(hashedIdentifier, textureType, state->width, state->height, state->mipmap, &foundExisting);

gEngfuncs.Con_DPrintf("GL_LoadTextureEx: [%s] -> [%s] [%d]\n", identifier, hashedIdentifier, gltexturenum);

int iTextureTarget = GL_TEXTURE_2D;

if (state->cubemap)
iTextureTarget = GL_TEXTURE_CUBE_MAP;

if (!state->wrap)
if (!glt)
{
state->wrap = GL_REPEAT;

if (textureType == GLT_HUDSPRITE || textureType == GLT_SPRITE)
state->wrap = GL_CLAMP_TO_EDGE;
gEngfuncs.Con_Printf("GL_LoadTextureEx: Failed to allocate texture entry for [%s] -> [%s].\n", identifier, hashedIdentifier);
return 0;
}

glBindTexture(iTextureTarget, gltexturenum);
(*currenttexture) = -1;

if (state->compressed)
if (foundExisting)
{
GL_UploadDXT(state);
}
else
{
GL_UploadRGBA8(state);
gEngfuncs.Con_DPrintf("GL_LoadTextureEx: Found existing texture entry [%s] -> [%s] [%d]\n", identifier, hashedIdentifier, glt->texnum);
return glt->texnum;
}

glBindTexture(iTextureTarget, 0);
(*currenttexture) = -1;
gEngfuncs.Con_DPrintf("GL_LoadTextureEx: [%s] -> [%s] [%d]\n", identifier, hashedIdentifier, glt->texnum);

return gltexturenum;
return GL_LoadTexture3(glt, textureType, state);
}

texture_t *Draw_DecalTexture(int index)
Expand Down
1 change: 0 additions & 1 deletion Plugins/Renderer/gl_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ typedef struct
DDS_HEADER_DXT10 Header10;
} DDS_FILEHEADER10;

int GL_AllocTexture(const char *identifier, GL_TEXTURETYPE textureType, int width, int height, qboolean mipmap);
int GL_FindTexture(const char *identifier, GL_TEXTURETYPE textureType, int *width, int *height);
void GL_UnloadTextureByTextureId(int gltexturenum, bool notify_callback);
void GL_UnloadTextureWithType(const char* identifier, GL_TEXTURETYPE textureType, bool notify_callback);
Expand Down

0 comments on commit f7b54b1

Please sign in to comment.