Skip to content

Commit

Permalink
renderer: port from corec branch
Browse files Browse the repository at this point in the history
  • Loading branch information
rafal1137 committed May 17, 2024
1 parent ac85aaf commit 3208004
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 76 deletions.
68 changes: 37 additions & 31 deletions src/renderer2/glsl/liquid_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

uniform bool SHOW_LIGHTMAP;
//uniform bool SHOW_DELUXEMAP;
uniform bool UNDERWATER;

uniform vec4 u_Color;
uniform sampler2D u_DepthMap;
Expand All @@ -24,6 +25,8 @@ uniform sampler2D u_CurrentMap;
#endif // USE_DIFFUSE
#if defined(USE_NORMAL_MAPPING)
uniform sampler2D u_NormalMap;
// sunlight
//uniform vec3 u_LightDir;
uniform vec3 u_LightColor;
// fresnel
uniform float u_FresnelBias;
Expand Down Expand Up @@ -109,7 +112,12 @@ void main()
#endif


#if defined(USE_NORMAL_MAPPING)
#if !defined(USE_NORMAL_MAPPING)
// normal mapping is disabled.
// calculate the screen texcoord in the 0.0 to 1.0 range
vec2 texScreen = gl_FragCoord.st * r_FBufScale * r_NPOTScale;
color.rgb = texture2D(u_CurrentMap, texScreen).rgb;
#else // USE_NORMAL_MAPPING
// the view direction in tangentspace
vec3 V = normalize(var_ViewDirT);

Expand All @@ -129,7 +137,7 @@ void main()
#endif //USE_PARALLAX_MAPPING


// normal
// pixel normal
vec3 Ntex = texture2D(u_NormalMap, texNormal).xyz * 2.0 - 1.0; // static bumpmap
#if defined(USE_WATER)
vec3 Ntex2 = texture2D(u_NormalMap, texDiffuse).xyz * 2.0 - 1.0; // tcMod moving bumpmap
Expand All @@ -144,32 +152,38 @@ void main()
vec3 refractColor = texture2D(u_CurrentMap, texScreen).rgb;


// set the initial color to the refracted underwater scene
// set the initial color to the refracted scene
color.rgb = refractColor;



// reflection
#if defined(USE_REFLECTIONS)
vec3 reflectColor;
// compute fresnel term
// ratio reflection/refraction. Value 1.0 = only refraction, no reflection. 0.0 = only reflection, no refraction.
float dotNV = dot(N, V);
float dotAbsNV = abs(dotNV);
#if 1
float fresnel = 1.0 - clamp(u_FresnelBias + pow(dotAbsNV, u_FresnelPower) * u_FresnelScale, 0.0, 1.0);
// use the cubeProbes
#if 0
// Always use the cubeProbes, if above or below the watersurface.
// compute fresnel term. This is the ratio reflection/refraction.
// Value 1.0 = only refraction, no reflection. 0.0 = only reflection, no refraction.
float fresnel = 1.0 - clamp(u_FresnelBias + pow(abs(dotNV), u_FresnelPower) * u_FresnelScale, 0.0, 1.0);
reflectColor = computeReflectionsW(V, N, var_worldMatrix, u_EnvironmentMap0, u_EnvironmentMap1, u_EnvironmentInterpolation, u_ReflectionScale);
#else
// surface reflections above/under water are different
float fresnel = clamp(u_FresnelBias + pow(1.0 + dotNV, u_FresnelPower) * u_FresnelScale, 0.0, 1.0);
// test surface reflections above/under water are different
if (dotNV >= 0) {
if (!UNDERWATER) {
// Above surface: use the cubeProbes
reflectColor = computeReflectionsW(V, N, var_worldMatrix, u_EnvironmentMap0, u_EnvironmentMap1, u_EnvironmentInterpolation, u_ReflectionScale);
} else {
#if 1
// Below surface: use the currentmap
vec3 R = reflect(V, N); // the reflection vector
reflectColor = texture2D(u_CurrentMap, R.st).rgb; // it a test..
const vec3 surfaceNormalT = vec3(0.0, 0.0, 1.0); // the tangentspace surface normal is just a constant
vec3 R = reflect(V, -surfaceNormalT); // the reflection vector
vec2 texScreen2 = texScreen + ((N.xy - R.xy) * u_NormalScale);
reflectColor = texture2D(u_CurrentMap, texScreen2.st).rgb;
#else
// Below surface: no reflections
reflectColor = refractColor;
#endif
}
#endif
#endif // USE_REFLECTIONS
Expand All @@ -179,19 +193,12 @@ void main()
// color.rgb *= computeDiffuseLighting(N, L, 0.2);


// compute the specular term
// compute the specular term.
// Liquids need no specularmap. Liquids have the specular term calculated from any provided normalmap.
// We don't use u_SpecularExponent here, but instead a constant value.
vec3 specular = computeSpecular(V, N, L, u_LightColor, 64.0, u_SpecularScale); // u_SpecularExponent


#else // USE_NORMAL_MAPPING


// calculate the screen texcoord in the 0.0 to 1.0 range
vec2 texScreen = gl_FragCoord.st * r_FBufScale * r_NPOTScale;

color.rgb = texture2D(u_CurrentMap, texScreen).rgb;

#endif // USE_NORMAL_MAPPING


Expand All @@ -206,16 +213,16 @@ void main()
#endif // USE_DIFFUSE


// the water-surface fog
if (u_FogDensity > 0.0) {
// the water-surface fog.
if (!UNDERWATER && (u_FogDensity > 0.0)) {
// reconstruct vertex position in world space
float depth = texture2D(u_DepthMap, texScreen).r;
//? // scale to Normalized Device Coordinates
//? vec4 P = vec4(gl_FragCoord.xy, depth, 1.0) * 2.0 - 1.0;
vec4 P = vec4(gl_FragCoord.xy, depth, 1.0);
// unproject to get into viewspace
P = u_UnprojectMatrix * P;
// normalize to homogeneous coordinates (where w is always 1)
// normalize to homogeneous coordinates
P.xyz /= P.w;
// calculate fog distance
float fogDistance = distance(P.xyz, var_Position);
Expand All @@ -233,16 +240,15 @@ void main()
color.rgb = mix(color.rgb, reflectColor, fresnel);
#endif

#if defined(USE_NORMAL_MAPPING)
color.rgb += specular;
#endif // USE_NORMAL_MAPPING

// compute the light term
// color.rgb *= var_LightColor.rgb;

#if defined(USE_LIGHT_MAPPING)
// lightmap
color.rgb *= lightmapColor.rgb;
#endif

#if defined(USE_NORMAL_MAPPING)
color.rgb += specular; // liquids need no specularmap. Liquids have the specular term calculated from any provided normalmap
#endif // USE_NORMAL_MAPPING
#if defined(USE_PARALLAX_MAPPING)
color.rgb *= parallaxShadow;
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/renderer2/tr_bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4673,8 +4673,8 @@ static void R_LoadFogs(lump_t *l, lump_t *brushesLump, lump_t *sidesLump)
if (out->originalBrushNumber == -1)
{
s_worldData.globalFog = i + 1;
VectorCopy(shader->fogParms.color, s_worldData.globalOriginalFog);
s_worldData.globalOriginalFog[3] = d; // shader->fogParms.depthForOpaque;
VectorCopy(shader->fogParms.color, s_worldData.globalFog_Original);
s_worldData.globalFog_Original[3] = d; // shader->fogParms.depthForOpaque;
}

// set the gradient vector
Expand Down
54 changes: 33 additions & 21 deletions src/renderer2/tr_fog.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,13 @@ void RE_SetGlobalFog(qboolean restore, int duration, float r, float g, float b,

}
else {
tr.world->fogs[tr.world->globalFog].color[0] = tr.world->globalOriginalFog[0];
tr.world->fogs[tr.world->globalFog].color[1] = tr.world->globalOriginalFog[1];
tr.world->fogs[tr.world->globalFog].color[2] = tr.world->globalOriginalFog[2];
tr.world->fogs[tr.world->globalFog].color[0] = tr.world->globalFog_Original[0];
tr.world->fogs[tr.world->globalFog].color[1] = tr.world->globalFog_Original[1];
tr.world->fogs[tr.world->globalFog].color[2] = tr.world->globalFog_Original[2];
tr.world->fogs[tr.world->globalFog].color[3] = 1.0;
tr.world->fogs[tr.world->globalFog].depthForOpaque = tr.world->globalOriginalFog[3];
tr.world->fogs[tr.world->globalFog].tcScale = rcp(tr.world->globalOriginalFog[3]);
//tr.world->fogs[tr.world->globalFog].depthForOpaque = tr.world->globalFog_Original[3];
tr.world->fogs[tr.world->globalFog].tcScale = rcp(tr.world->globalFog_Original[3]);
}
//tr.glfogsettings[FOG_CURRENT].drawsky = qtrue; // tr_sky needs this..
}
else
{
Expand All @@ -192,17 +191,30 @@ void RE_SetGlobalFog(qboolean restore, int duration, float r, float g, float b,
tr.world->fogs[tr.world->globalFog].color[1] = g;
tr.world->fogs[tr.world->globalFog].color[2] = b;
tr.world->fogs[tr.world->globalFog].color[3] = 1.0;
tr.world->fogs[tr.world->globalFog].depthForOpaque = depthForOpaque < 1.0f ? 1.0f : depthForOpaque;
tr.world->fogs[tr.world->globalFog].tcScale = rcp(tr.world->fogs[tr.world->globalFog].depthForOpaque);

//tr.world->fogs[tr.world->globalFog].depthForOpaque = depthForOpaque < 1.0f ? 1.0f : depthForOpaque;
//tr.world->fogs[tr.world->globalFog].tcScale = rcp(tr.world->fogs[tr.world->globalFog].depthForOpaque);
tr.world->fogs[tr.world->globalFog].tcScale = rcp(depthForOpaque < 1.0f ? 1.0f : depthForOpaque);

/*
Vector4Set(tr.glfogsettings[FOG_TARGET].color, r, g, b, 1.0);
tr.glfogsettings[FOG_TARGET].end = tr.world->fogs[tr.world->globalFog].depthForOpaque;
tr.glfogsettings[FOG_TARGET].density = tr.world->fogs[tr.world->globalFog].density;
tr.glfogsettings[FOG_TARGET].finishTime = tr.refdef.time;
tr.glfogsettings[FOG_TARGET].mode = GL_LINEAR;
tr.glfogsettings[FOG_TARGET].registered = qtrue;
*/

/*
tr.glfogsettings[FOG_CURRENT].drawsky = qtrue; // tr_sky needs this..
tr.glfogsettings[FOG_CURRENT].clearscreen = qfalse;
tr.glfogsettings[FOG_CURRENT].useEndForClip = qtrue;
*/
}
}
/*
tr.glfogNum = tr.world->globalFog;
tr.glfogsettings[FOG_CURRENT].drawsky = qtrue; // tr_sky needs this..
*/
}

/**
Expand All @@ -215,39 +227,39 @@ void R_SetFrameFog(void)
{
// new style global fog transitions

if (tr.world->globalFogTransEndTime)
if (tr.world->globalFog_TransitionEndTime)
{
if (tr.world->globalFogTransEndTime >= tr.refdef.time)
if (tr.world->globalFog_TransitionEndTime >= tr.refdef.time)
{
int fadeTime = tr.world->globalFogTransEndTime - tr.world->globalFogTransStartTime;
int fadeTime = tr.world->globalFog_TransitionEndTime - tr.world->globalFog_TransitionEndTime;
float lerpPos;
if (fadeTime == 0)
{
lerpPos = 1.f;
}
else
{
lerpPos = (float)(tr.refdef.time - tr.world->globalFogTransStartTime) / (float)fadeTime;
lerpPos = (float)(tr.refdef.time - tr.world->globalFog_TransitionEndTime) / (float)fadeTime;

if (lerpPos > 1)
{
lerpPos = 1;
}
}
vec3_t vec;
VectorSubtract(tr.world->globalTransEndFog, tr.world->globalTransStartFog, vec);
VectorMA(vec, lerpPos, tr.world->globalTransStartFog, tr.world->fogs[tr.world->globalFog].color);

tr.world->fogs[tr.world->globalFog].depthForOpaque = (tr.world->globalTransEndFog[3] - tr.world->globalTransStartFog[3]) * lerpPos + tr.world->globalTransStartFog[3];
VectorSubtract(tr.world->globalFog_TransitionEndTime, tr.world->globalTransStartFog, vec);
VectorSubtract(tr.world->globalFog_TransitionEndFog, tr.world->globalFog_TransitionStartFog, vec);
VectorMA(vec, lerpPos, tr.world->globalFog_TransitionStartFog, tr.world->fogs[tr.world->globalFog].color);
tr.world->fogs[tr.world->globalFog].depthForOpaque = (tr.world->globalFog_TransitionEndFog[3] - tr.world->globalFog_TransitionStartFog[3]) * lerpPos + tr.world->globalFog_TransitionStartFog[3];
tr.world->fogs[tr.world->globalFog].tcScale = rcp(tr.world->fogs[tr.world->globalFog].depthForOpaque);
}
else
{
// transition complete
VectorCopy(tr.world->globalTransEndFog, tr.world->fogs[tr.world->globalFog].color);
tr.world->fogs[tr.world->globalFog].depthForOpaque = tr.world->globalTransEndFog[3];
tr.world->fogs[tr.world->globalFog].tcScale = rcp(tr.world->globalTransEndFog[3]);
tr.world->globalFogTransEndTime = 0; // stop any transition
VectorCopy(tr.world->globalFog_TransitionEndFog, tr.world->fogs[tr.world->globalFog].color);
tr.world->fogs[tr.world->globalFog].depthForOpaque = tr.world->globalFog_TransitionEndFog[3];
tr.world->fogs[tr.world->globalFog].tcScale = rcp(tr.world->globalFog_TransitionEndFog[3]);
tr.world->globalFog_TransitionEndTime = 0; // stop any transition
}

}
Expand Down
1 change: 1 addition & 0 deletions src/renderer2/tr_glsldef.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ UNIFORM_DEF(UNIFORM_B_SHOW_LIGHTMAP, SHOW_LIGHTMAP, GLSL_BOOL)
UNIFORM_DEF(UNIFORM_B_SHOW_DELUXEMAP, SHOW_DELUXEMAP, GLSL_BOOL)
UNIFORM_DEF(UNIFORM_B_NORMALMAP, NORMALMAP, GLSL_BOOL)
UNIFORM_DEF(UNIFORM_B_PARALLAXMAP, PARALLAXMAP, GLSL_BOOL)
UNIFORM_DEF(UNIFORM_B_UNDERWATER, UNDERWATER, GLSL_BOOL)

UNIFORM_DEF(UNIFORM_GAMMA, u_gamma, GLSL_FLOAT)

Expand Down
10 changes: 5 additions & 5 deletions src/renderer2/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -2883,11 +2883,11 @@ typedef struct
fog_t *fogs;

int globalFog; ///< index of global fog
vec4_t globalOriginalFog; ///< to be able to restore original global fog
vec4_t globalTransStartFog; ///< start fog for switch fog transition
vec4_t globalTransEndFog; ///< end fog for switch fog transition
int globalFogTransStartTime;
int globalFogTransEndTime;
vec4_t globalFog_Original; ///< to be able to restore original global fog
vec4_t globalFog_TransitionStartFog; ///< start fog when transitioning to another fog
vec4_t globalFog_TransitionEndFog; ///< end fog when transitioning from another fog
int globalFog_TransitionStartTime; ///< fog transition start time
int globalFog_TransitionEndTime; ///< fog transition end time

vec3_t lightGridOrigin;
vec3_t lightGridSize;
Expand Down
2 changes: 2 additions & 0 deletions src/renderer2/tr_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ void RE_RenderScene(const refdef_t *fd)
// entering the water?
if (!(tr.refdef.rdflags & RDF_UNDERWATER) && (fd->rdflags & RDF_UNDERWATER))
{
// if the water has a volumetric fog assigned (like in Battery),
// then not change the global fog. => do not add waterfogvars
RE_SetGlobalFog(qfalse, 0, tr.glfogsettings[FOG_WATER].color[0], tr.glfogsettings[FOG_WATER].color[1], tr.glfogsettings[FOG_WATER].color[2], tr.glfogsettings[FOG_WATER].end);
}
// exiting the water?
Expand Down

0 comments on commit 3208004

Please sign in to comment.