Skip to content

Commit

Permalink
glsl/parallax: meaningful symbol names
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Mar 20, 2019
1 parent bedf6a3 commit 1b06c22
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 115 deletions.
32 changes: 16 additions & 16 deletions src/engine/renderer/gl_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1407,8 +1407,8 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
u_ViewOrigin( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_OffsetScale( this ),
u_OffsetBias( this ),
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
u_numLights( this ),
Expand Down Expand Up @@ -1460,8 +1460,8 @@ GLShader_vertexLighting_DBS_entity::GLShader_vertexLighting_DBS_entity( GLShader
u_ModelViewProjectionMatrix( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
u_OffsetScale( this ),
u_OffsetBias( this ),
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
u_EnvironmentInterpolation( this ),
Expand Down Expand Up @@ -1524,8 +1524,8 @@ GLShader_vertexLighting_DBS_world::GLShader_vertexLighting_DBS_world( GLShaderMa
u_ViewOrigin( this ),
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_OffsetScale( this ),
u_OffsetBias( this ),
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
u_LightWrapAround( this ),
Expand Down Expand Up @@ -1588,8 +1588,8 @@ GLShader_forwardLighting_omniXYZ::GLShader_forwardLighting_omniXYZ( GLShaderMana
u_ModelViewProjectionMatrix( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
u_OffsetScale( this ),
u_OffsetBias( this ),
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
GLDeformStage( this ),
Expand Down Expand Up @@ -1649,8 +1649,8 @@ GLShader_forwardLighting_projXYZ::GLShader_forwardLighting_projXYZ( GLShaderMana
u_ModelViewProjectionMatrix( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
u_OffsetScale( this ),
u_OffsetBias( this ),
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
GLDeformStage( this ),
Expand Down Expand Up @@ -1713,8 +1713,8 @@ GLShader_forwardLighting_directionalSun::GLShader_forwardLighting_directionalSun
u_ModelViewProjectionMatrix( this ),
u_Bones( this ),
u_VertexInterpolation( this ),
u_OffsetScale( this ),
u_OffsetBias( this ),
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalFormat( this ),
GLDeformStage( this ),
Expand Down Expand Up @@ -1795,8 +1795,8 @@ GLShader_reflection::GLShader_reflection( GLShaderManager *manager ):
u_ModelMatrix( this ),
u_ModelViewProjectionMatrix( this ),
u_Bones( this ),
u_OffsetScale( this ),
u_OffsetBias( this ),
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalScale( this ),
u_NormalFormat( this ),
Expand Down Expand Up @@ -2061,8 +2061,8 @@ GLShader_liquid::GLShader_liquid( GLShaderManager *manager ) :
u_FresnelPower( this ),
u_FresnelScale( this ),
u_FresnelBias( this ),
u_OffsetScale( this ),
u_OffsetBias( this ),
u_ParallaxDepthScale( this ),
u_ParallaxOffsetBias( this ),
u_HeightMapInNormalMap( this ),
u_NormalScale( this ),
u_NormalFormat( this ),
Expand Down
48 changes: 24 additions & 24 deletions src/engine/renderer/gl_shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -1992,31 +1992,31 @@ class u_DepthScale :
}
};

class u_OffsetScale :
class u_ParallaxDepthScale :
GLUniform1f
{
public:
u_OffsetScale( GLShader *shader ) :
GLUniform1f( shader, "u_OffsetScale" )
u_ParallaxDepthScale( GLShader *shader ) :
GLUniform1f( shader, "u_ParallaxDepthScale" )
{
}

void SetUniform_OffsetScale( float value )
void SetUniform_ParallaxDepthScale( float value )
{
this->SetValue( value );
}
};

class u_OffsetBias :
class u_ParallaxOffsetBias :
GLUniform1f
{
public:
u_OffsetBias( GLShader *shader ) :
GLUniform1f( shader, "u_OffsetBias" )
u_ParallaxOffsetBias( GLShader *shader ) :
GLUniform1f( shader, "u_ParallaxOffsetBias" )
{
}

void SetUniform_OffsetBias( float value )
void SetUniform_ParallaxOffsetBias( float value )
{
this->SetValue( value );
}
Expand Down Expand Up @@ -2403,8 +2403,8 @@ class GLShader_lightMapping :
public u_ViewOrigin,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_OffsetScale,
public u_OffsetBias,
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public u_numLights,
Expand Down Expand Up @@ -2434,8 +2434,8 @@ class GLShader_vertexLighting_DBS_entity :
public u_ModelViewProjectionMatrix,
public u_Bones,
public u_VertexInterpolation,
public u_OffsetScale,
public u_OffsetBias,
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public u_EnvironmentInterpolation,
Expand Down Expand Up @@ -2471,8 +2471,8 @@ class GLShader_vertexLighting_DBS_world :
public u_ViewOrigin,
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_OffsetScale,
public u_OffsetBias,
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public u_LightWrapAround,
Expand Down Expand Up @@ -2514,8 +2514,8 @@ class GLShader_forwardLighting_omniXYZ :
public u_ModelViewProjectionMatrix,
public u_Bones,
public u_VertexInterpolation,
public u_OffsetScale,
public u_OffsetBias,
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public GLDeformStage,
Expand Down Expand Up @@ -2555,8 +2555,8 @@ class GLShader_forwardLighting_projXYZ :
public u_ModelViewProjectionMatrix,
public u_Bones,
public u_VertexInterpolation,
public u_OffsetScale,
public u_OffsetBias,
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public GLDeformStage,
Expand Down Expand Up @@ -2598,8 +2598,8 @@ class GLShader_forwardLighting_directionalSun :
public u_ModelViewProjectionMatrix,
public u_Bones,
public u_VertexInterpolation,
public u_OffsetScale,
public u_OffsetBias,
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalFormat,
public GLDeformStage,
Expand Down Expand Up @@ -2646,8 +2646,8 @@ class GLShader_reflection :
public u_ModelMatrix,
public u_ModelViewProjectionMatrix,
public u_Bones,
public u_OffsetScale,
public u_OffsetBias,
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalScale,
public u_NormalFormat,
Expand Down Expand Up @@ -2853,8 +2853,8 @@ class GLShader_liquid :
public u_FresnelPower,
public u_FresnelScale,
public u_FresnelBias,
public u_OffsetScale,
public u_OffsetBias,
public u_ParallaxDepthScale,
public u_ParallaxOffsetBias,
public u_HeightMapInNormalMap,
public u_NormalScale,
public u_NormalFormat,
Expand Down
6 changes: 3 additions & 3 deletions src/engine/renderer/glsl_source/forwardLighting_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ uniform float u_ShadowBlur;

uniform mat4 u_ViewMatrix;

uniform float u_OffsetScale;
uniform float u_OffsetBias;
uniform float u_ParallaxDepthScale;
uniform float u_ParallaxOffsetBias;

IN(smooth) vec3 var_Position;
IN(smooth) vec4 var_TexDiffuse;
Expand Down Expand Up @@ -959,7 +959,7 @@ void main()

#if defined(USE_PARALLAX_MAPPING)
// compute texcoords offset from heightmap
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_OffsetScale, u_OffsetBias, viewDir, tangentToWorldMatrix);
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_ParallaxDepthScale, u_ParallaxOffsetBias, viewDir, tangentToWorldMatrix);

texDiffuse += texOffset;
texNormal += texOffset;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/renderer/glsl_source/lightMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ uniform sampler2D u_LightMap;
uniform sampler2D u_DeluxeMap;
uniform float u_AlphaThreshold;
uniform vec3 u_ViewOrigin;
uniform float u_OffsetScale;
uniform float u_OffsetBias;
uniform float u_ParallaxDepthScale;
uniform float u_ParallaxOffsetBias;

IN(smooth) vec3 var_Position;
IN(smooth) vec4 var_TexDiffuseGlow;
Expand Down Expand Up @@ -59,7 +59,7 @@ void main()

#if defined(USE_PARALLAX_MAPPING)
// compute texcoords offset from heightmap
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_OffsetScale, u_OffsetBias, viewDir, tangentToWorldMatrix);
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_ParallaxDepthScale, u_ParallaxOffsetBias, viewDir, tangentToWorldMatrix);

texDiffuse += texOffset;
texGlow += texOffset;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/renderer/glsl_source/liquid_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ uniform float u_RefractionIndex;
uniform float u_FresnelPower;
uniform float u_FresnelScale;
uniform float u_FresnelBias;
uniform float u_OffsetScale;
uniform float u_OffsetBias;
uniform float u_ParallaxDepthScale;
uniform float u_ParallaxOffsetBias;
uniform float u_NormalScale;
uniform mat4 u_ModelMatrix;
uniform mat4 u_UnprojectMatrix;
Expand Down Expand Up @@ -99,7 +99,7 @@ void main()
// ray intersect in view direction

// compute texcoords offset from heightmap
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_OffsetScale, u_OffsetBias, viewDir, tangentToWorldMatrix);
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_ParallaxDepthScale, u_ParallaxOffsetBias, viewDir, tangentToWorldMatrix);

texScreen += texOffset;
texNormal += texOffset;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/renderer/glsl_source/reflection_CB_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ uniform samplerCube u_ColorMap;
uniform sampler2D u_NormalMap;
uniform vec3 u_ViewOrigin;
uniform mat4 u_ModelMatrix;
uniform float u_OffsetScale;
uniform float u_OffsetBias;
uniform float u_ParallaxDepthScale;
uniform float u_ParallaxOffsetBias;

IN(smooth) vec3 var_Position;
IN(smooth) vec2 var_TexNormal;
Expand All @@ -48,7 +48,7 @@ void main()

#if defined(USE_PARALLAX_MAPPING)
// compute texcoords offset from heightmap
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_OffsetScale, u_OffsetBias, viewDir, tangentToWorldMatrix);
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_ParallaxDepthScale, u_ParallaxOffsetBias, viewDir, tangentToWorldMatrix);

texNormal += texOffset;
#endif // USE_PARALLAX_MAPPING
Expand Down
37 changes: 20 additions & 17 deletions src/engine/renderer/glsl_source/reliefMapping_fp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -270,60 +270,63 @@ vec3 NormalInWorldSpace(sampler2D normalMap, vec2 texNormal, mat3 tangentToWorld

#if defined(USE_PARALLAX_MAPPING)
// compute texcoords offset from heightmap
// was RayIntersectDisplaceMap
vec2 ParallaxTexOffset(sampler2D normalMap, vec2 texCoords, float offsetScale, float offsetBias, vec3 viewDir, mat3 tangentToWorldMatrix)
// most of the code doing somewhat the same is likely to be named
// RayIntersectDisplaceMap in other id tech3-based engines
// so please keep the comment above to enable cross-tree look-up
vec2 ParallaxTexOffset(sampler2D normalMap, vec2 rayStartTexCoords, float parallaxParallaxDepthScale, float parallaxParallaxOffsetBias, vec3 viewDir, mat3 tangentToWorldMatrix)
{
// compute view direction in tangent space
vec3 tangentViewDir = normalize(viewDir * tangentToWorldMatrix);

vec2 ds = tangentViewDir.xy * -offsetScale / tangentViewDir.z;
vec2 ds = tangentViewDir.xy * -parallaxParallaxDepthScale / tangentViewDir.z;

const int linearSearchSteps = 16;
const int binarySearchSteps = 6;

float depthStep = 1.0 / float(linearSearchSteps);
float topDepth = 1.0 - parallaxParallaxOffsetBias;

// current size of search window
float size = depthStep;
float currentSize = depthStep;

// current depth position
float depth = 0.0;
float currentDepth = 0.0;

// best match found (starts with last position 1.0)
float bestDepth = 1.0;

// search front to back for first point inside object
for(int i = 0; i < linearSearchSteps - 1; ++i)
{
depth += size;
currentDepth += currentSize;

float t = 1.0 - offsetBias - texture2D(normalMap, texCoords + ds * depth).a;
float heightMapDepth = topDepth - texture2D(normalMap, rayStartTexCoords + ds * currentDepth).a;

if(bestDepth > 0.996) // if no depth found yet
if(bestDepth > 0.996) // if no depth found yet
{
if(depth >= t)
if(currentDepth >= heightMapDepth)
{
bestDepth = depth; // store best depth
bestDepth = currentDepth;
}
}
}

depth = bestDepth;
currentDepth = bestDepth;

// recurse around first point (depth) for closest match
for(int i = 0; i < binarySearchSteps; ++i)
{
size *= 0.5;
currentSize *= 0.5;

float t = 1.0 - offsetBias - texture2D(normalMap, texCoords + ds * depth).a;
float heightMapDepth = topDepth - texture2D(normalMap, rayStartTexCoords + ds * currentDepth).a;

if(depth >= t)
if(currentDepth >= heightMapDepth)
{
bestDepth = depth;
depth -= 2.0 * size;
bestDepth = currentDepth;
currentDepth -= 2.0 * currentSize;
}

depth += size;
currentDepth += currentSize;
}

return bestDepth * ds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ uniform float u_EnvironmentInterpolation;

uniform float u_AlphaThreshold;
uniform vec3 u_ViewOrigin;
uniform float u_OffsetScale;
uniform float u_OffsetBias;
uniform float u_ParallaxDepthScale;
uniform float u_ParallaxOffsetBias;

uniform sampler3D u_LightGrid1;
uniform sampler3D u_LightGrid2;
Expand Down Expand Up @@ -95,7 +95,7 @@ void main()

#if defined(USE_PARALLAX_MAPPING)
// compute texcoords offset from heightmap
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_OffsetScale, u_OffsetBias, viewDir, tangentToWorldMatrix);
vec2 texOffset = ParallaxTexOffset(u_NormalMap, texNormal, u_ParallaxDepthScale, u_ParallaxOffsetBias, viewDir, tangentToWorldMatrix);

texDiffuse += texOffset;
texGlow += texOffset;
Expand Down
Loading

0 comments on commit 1b06c22

Please sign in to comment.