Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some cubemap reflections issues #1311

Merged
merged 15 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/engine/renderer/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,9 @@ void UpdateSurfaceDataReflection( uint32_t* materials, Material& material, drawS

// bind u_ColorMap
if ( backEnd.currentEntity && ( backEnd.currentEntity != &tr.worldEntity ) ) {
GL_BindNearestCubeMap( gl_reflectionShaderMaterial->GetUniformLocation_ColorMap(), backEnd.currentEntity->e.origin );
GL_BindNearestCubeMap( gl_reflectionShaderMaterial->GetUniformLocation_ColorMapCube(), backEnd.currentEntity->e.origin );
} else {
GL_BindNearestCubeMap( gl_reflectionShaderMaterial->GetUniformLocation_ColorMap(), backEnd.viewParms.orientation.origin );
GL_BindNearestCubeMap( gl_reflectionShaderMaterial->GetUniformLocation_ColorMapCube(), backEnd.viewParms.orientation.origin );
}

if ( pStage->enableNormalMapping ) {
Expand Down
17 changes: 15 additions & 2 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,11 +707,20 @@ static std::string GenEngineConstants() {
{
AddDefine( str, "r_showDeluxeMaps", 1 );
}
else if ( r_showReflectionMaps.Get() )
{
AddDefine( str, "r_showReflectionMaps", 1 );
}
else if ( r_showVertexColors.Get() )
{
AddDefine( str, "r_showVertexColors", 1 );
}

if( r_showCubeProbes.Get() )
{
AddDefine( str, "r_showCubeProbes", 1 );
}

if ( glConfig2.vboVertexSkinningAvailable )
{
AddDefine( str, "r_vertexSkinning", 1 );
Expand Down Expand Up @@ -2423,7 +2432,7 @@ void GLShader_shadowFill::SetShaderProgramUniforms( shaderProgram_t *shaderProgr

GLShader_reflection::GLShader_reflection( GLShaderManager *manager ):
GLShader("reflection", "reflection_CB", ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
u_ColorMap( this ),
u_ColorMapCube( this ),
u_NormalMap( this ),
u_HeightMap( this ),
u_TextureMatrix( this ),
Expand All @@ -2435,6 +2444,8 @@ GLShader_reflection::GLShader_reflection( GLShaderManager *manager ):
u_ReliefOffsetBias( this ),
u_NormalScale( this ),
u_VertexInterpolation( this ),
u_CameraPosition( this ),
u_InverseLightFactor( this ),
GLDeformStage( this ),
GLCompileMacro_USE_VERTEX_SKINNING( this ),
GLCompileMacro_USE_VERTEX_ANIMATION( this ),
Expand All @@ -2452,7 +2463,7 @@ void GLShader_reflection::SetShaderProgramUniforms( shaderProgram_t *shaderProgr

GLShader_reflectionMaterial::GLShader_reflectionMaterial( GLShaderManager* manager ) :
GLShader( "reflectionMaterial", "reflection_CB", true, ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT, manager ),
u_ColorMap( this ),
u_ColorMapCube( this ),
u_NormalMap( this ),
u_HeightMap( this ),
u_TextureMatrix( this ),
Expand All @@ -2464,6 +2475,8 @@ GLShader_reflectionMaterial::GLShader_reflectionMaterial( GLShaderManager* manag
u_ReliefOffsetBias( this ),
u_NormalScale( this ),
u_VertexInterpolation( this ),
u_CameraPosition( this ),
u_InverseLightFactor( this ),
GLDeformStage( this ),
// GLCompileMacro_USE_VERTEX_SKINNING( this ),
GLCompileMacro_USE_VERTEX_ANIMATION( this ),
Expand Down
8 changes: 6 additions & 2 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4253,7 +4253,7 @@ class GLShader_shadowFill :

class GLShader_reflection :
public GLShader,
public u_ColorMap,
public u_ColorMapCube,
public u_NormalMap,
public u_HeightMap,
public u_TextureMatrix,
Expand All @@ -4265,6 +4265,8 @@ class GLShader_reflection :
public u_ReliefOffsetBias,
public u_NormalScale,
public u_VertexInterpolation,
public u_CameraPosition,
public u_InverseLightFactor,
public GLDeformStage,
public GLCompileMacro_USE_VERTEX_SKINNING,
public GLCompileMacro_USE_VERTEX_ANIMATION,
Expand All @@ -4278,7 +4280,7 @@ class GLShader_reflection :

class GLShader_reflectionMaterial :
public GLShader,
public u_ColorMap,
public u_ColorMapCube,
public u_NormalMap,
public u_HeightMap,
public u_TextureMatrix,
Expand All @@ -4290,6 +4292,8 @@ class GLShader_reflectionMaterial :
public u_ReliefOffsetBias,
public u_NormalScale,
public u_VertexInterpolation,
public u_CameraPosition,
public u_InverseLightFactor,
public GLDeformStage,
// public GLCompileMacro_USE_VERTEX_SKINNING,
public GLCompileMacro_USE_VERTEX_ANIMATION,
Expand Down
12 changes: 12 additions & 0 deletions src/engine/renderer/glsl_source/lightMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ void main()
outputColor = texture2D(u_LightMap, var_TexLight);
#elif defined(r_showDeluxeMaps) && defined(USE_DELUXE_MAPPING)
outputColor = texture2D(u_DeluxeMap, var_TexLight);
#elif defined(USE_REFLECTIVE_SPECULAR) && defined(r_showReflectionMaps)
vec4 envColor0 = textureCube(u_EnvironmentMap0, reflect(-viewDir, normal));
vec4 envColor1 = textureCube(u_EnvironmentMap1, reflect(-viewDir, normal));

outputColor = vec4( mix(envColor0, envColor1, u_EnvironmentInterpolation).rgb, 1.0 );

/* HACK: use sign to know if there is a light or not, and
then if it will receive overbright multiplication or not. */
if ( u_InverseLightFactor < 0 )
{
outputColor *= - u_InverseLightFactor;
}
#elif defined(r_showVertexColors)
/* We need to keep the texture alpha channel so impact
marks like creep don't fully overwrite the world texture. */
Expand Down
2 changes: 1 addition & 1 deletion src/engine/renderer/glsl_source/material_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ sampler3D u_LightGrid2 = sampler3D( u_LightGrid2_initial );
#endif // !LIQUID_GLSL

#if defined(REFLECTION_CB_GLSL)
samplerCube u_ColorMap = samplerCube( u_ColorMap_initial );
samplerCube u_ColorMapCube = samplerCube( u_ColorMapCube_initial );
#endif // !REFLECTION_CB_GLSL

#if defined(RELIEFMAPPING_GLSL)
Expand Down
13 changes: 11 additions & 2 deletions src/engine/renderer/glsl_source/reflection_CB_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#define REFLECTION_CB_GLSL

uniform samplerCube u_ColorMap;
uniform samplerCube u_ColorMapCube;
uniform vec3 u_ViewOrigin;
uniform mat4 u_ModelMatrix;

uniform float u_InverseLightFactor;

IN(smooth) vec3 var_Position;
IN(smooth) vec2 var_TexCoords;
IN(smooth) vec4 var_Tangent;
Expand Down Expand Up @@ -66,6 +68,13 @@ void main()
// compute reflection ray
vec3 reflectionRay = reflect(viewDir, normal);

outputColor = textureCube(u_ColorMap, reflectionRay).rgba;
outputColor = textureCube(u_ColorMapCube, reflectionRay).rgba;

#if defined(r_showCubeProbes)
viewDir = normalize(var_Position);
outputColor = textureCube(u_ColorMapCube, viewDir);

outputColor.rgb *= u_InverseLightFactor;
#endif
// outputColor = vec4(1.0, 0.0, 0.0, 1.0);
}
12 changes: 11 additions & 1 deletion src/engine/renderer/glsl_source/reflection_CB_vp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ uniform mat4 u_ModelViewProjectionMatrix;

uniform float u_Time;

#if defined(r_showCubeProbes)
uniform vec3 u_CameraPosition;
#endif

OUT(smooth) vec3 var_Position;
OUT(smooth) vec2 var_TexCoords;
OUT(smooth) vec4 var_Tangent;
Expand Down Expand Up @@ -65,7 +69,13 @@ void main()
gl_Position = u_ModelViewProjectionMatrix * position;

// transform position into world space
var_Position = (u_ModelMatrix * position).xyz;
#if defined(r_showCubeProbes)
/* Hack: This is used for debug purposes only,
but it will break ST_REFLECTIONMAP and ST_COLLAPSE_REFLECTIONMAP stages */
var_Position = (u_ModelMatrix * ( position - vec4( u_CameraPosition, 0.0 ) )).xyz;
#else
var_Position = (u_ModelMatrix * position).xyz;
#endif

var_Tangent.xyz = (u_ModelMatrix * vec4(LB.tangent, 0.0)).xyz;
var_Binormal.xyz = (u_ModelMatrix * vec4(LB.binormal, 0.0)).xyz;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/renderer/glsl_source/refraction_C_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

/* refraction_C_fp.glsl */

uniform samplerCube u_ColorMap;
uniform samplerCube u_ColorMapCube;
uniform vec3 u_ViewOrigin;
uniform float u_RefractionIndex;
uniform float u_FresnelPower;
Expand Down Expand Up @@ -51,8 +51,8 @@ void main()
// compute fresnel term
float fresnel = u_FresnelBias + pow(1.0 - dot(incidentRay, normal), u_FresnelPower) * u_FresnelScale;

vec3 reflectColor = textureCube(u_ColorMap, reflectionRay).rgb;
vec3 refractColor = textureCube(u_ColorMap, T).rgb;
vec3 reflectColor = textureCube(u_ColorMapCube, reflectionRay).rgb;
vec3 refractColor = textureCube(u_ColorMapCube, T).rgb;

// compute final color
vec4 color;
Expand Down
45 changes: 24 additions & 21 deletions src/engine/renderer/tr_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4040,12 +4040,15 @@ static void RB_RenderDebugUtils()
}

// GLSL shader isn't built when reflection mapping is disabled.
if ( r_showCubeProbes->integer && tr.cubeHashTable && r_reflectionMapping->integer &&
if ( r_showCubeProbes.Get() && tr.cubeHashTable && r_reflectionMapping->integer &&
!( backEnd.refdef.rdflags & ( RDF_NOWORLDMODEL | RDF_NOCUBEMAP ) ) )
{
static const vec3_t mins = { -8, -8, -8 };
static const vec3_t maxs = { 8, 8, 8 };

static const vec3_t outlineMins = { -9, -9, -9 };
static const vec3_t outlineMaxs = { 9, 9, 9 };

// choose right shader program ----------------------------------
gl_reflectionShader->SetVertexSkinning( false );
gl_reflectionShader->SetVertexAnimation( false );
Expand All @@ -4066,7 +4069,7 @@ static void RB_RenderDebugUtils()
gl_reflectionShader->SetUniform_ModelMatrix( backEnd.orientation.transformMatrix );
gl_reflectionShader->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[ glState.stackIndex ] );

Tess_Begin( Tess_StageIteratorDebug, nullptr, nullptr, true, -1, 0 );
gl_reflectionShader->SetUniform_InverseLightFactor( tr.mapInverseLightFactor );

for ( cubemapProbe_t *cubeProbe : tr.cubeProbes )
{
Expand All @@ -4077,15 +4080,20 @@ static void RB_RenderDebugUtils()
continue;
}

Tess_Begin( Tess_StageIteratorDebug, nullptr, nullptr, true, -1, 0 );

gl_reflectionShader->SetUniform_CameraPosition( cubeProbe->origin );

// bind u_ColorMap
gl_reflectionShader->SetUniform_ColorMapBindless(
GL_BindToTMU( 0, cubeProbe->cubemap )
gl_reflectionShader->SetUniform_ColorMapCubeBindless(
GL_BindToTMU( 0, cubeProbe->cubemap )
);

Tess_AddCubeWithNormals( cubeProbe->origin, mins, maxs, Color::White );

Tess_End();
}

Tess_End();

{
cubemapProbe_t *cubeProbeNearest;
Expand Down Expand Up @@ -4121,6 +4129,8 @@ static void RB_RenderDebugUtils()
);
gl_genericShader->SetUniform_TextureMatrix( matrixIdentity );

GL_State( GLS_POLYMODE_LINE | GLS_DEPTHFUNC_ALWAYS );

GL_CheckErrors();

R_FindTwoNearestCubeMaps( backEnd.viewParms.orientation.origin, &cubeProbeNearest, &cubeProbeSecondNearest );
Expand All @@ -4133,12 +4143,12 @@ static void RB_RenderDebugUtils()
}
else if ( cubeProbeSecondNearest == nullptr )
{
Tess_AddCubeWithNormals( cubeProbeNearest->origin, mins, maxs, Color::Yellow );
Tess_AddCubeWithNormals( cubeProbeNearest->origin, outlineMins, outlineMaxs, Color::Yellow );
}
else
{
Tess_AddCubeWithNormals( cubeProbeNearest->origin, mins, maxs, Color::Green );
Tess_AddCubeWithNormals( cubeProbeSecondNearest->origin, mins, maxs, Color::Red );
Tess_AddCubeWithNormals( cubeProbeNearest->origin, outlineMins, outlineMaxs, Color::Green );
Tess_AddCubeWithNormals( cubeProbeSecondNearest->origin, outlineMins, outlineMaxs, Color::Red );
}

Tess_End();
Expand Down Expand Up @@ -4814,23 +4824,16 @@ static void RB_RenderPostProcess()

RB_FXAA();

// render chromatric aberration
// render chromatic aberration
RB_CameraPostFX();

// copy to given byte buffer that is NOT a FBO
if (tr.refdef.pixelTarget != nullptr)
{
int i;

// need to convert Y axis
// Bugfix: drivers absolutely hate running in high res and using glReadPixels near the top or bottom edge.
// Sooo... let's do it in the middle.
glReadPixels(glConfig.vidWidth / 2, glConfig.vidHeight / 2, tr.refdef.pixelTargetWidth, tr.refdef.pixelTargetHeight, GL_RGBA,
GL_UNSIGNED_BYTE, tr.refdef.pixelTarget);
if ( tr.refdef.pixelTarget != nullptr ) {
glReadPixels( 0, 0, tr.refdef.pixelTargetWidth, tr.refdef.pixelTargetHeight, GL_RGBA,
GL_UNSIGNED_BYTE, tr.refdef.pixelTarget );

for (i = 0; i < tr.refdef.pixelTargetWidth * tr.refdef.pixelTargetHeight; i++)
{
tr.refdef.pixelTarget[(i * 4) + 3] = 255; //set the alpha pure white
for ( int i = 0; i < tr.refdef.pixelTargetWidth * tr.refdef.pixelTargetHeight; i++ ) {
tr.refdef.pixelTarget[( i * 4 ) + 3] = 255; // Set the alpha to 1.0
}
}

Expand Down
Loading