Skip to content

Commit a918555

Browse files
authored
Merge pull request #298 from taysta/rend2-updates
Rend2 updates
2 parents ffd652d + 00eb627 commit a918555

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

shared/rd-rend2/glsl/lightall.glsl

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,23 @@ uniform vec4 u_VertColor;
8080
uniform vec4 u_Disintegration;
8181
uniform int u_ColorGen;
8282

83+
#if defined(PER_PIXEL_LIGHTING) && defined(USE_NORMALMAP) && defined(USE_PARALLAXMAP)
84+
uniform sampler2D u_NormalMap;
85+
#endif
86+
8387
out vec4 var_TexCoords;
8488
out vec4 var_Color;
8589

8690
#if defined(PER_PIXEL_LIGHTING)
8791
out vec4 var_Normal;
8892
out vec4 var_Tangent;
8993
out vec4 var_ViewDir;
90-
out vec4 var_TangentViewDir;
94+
out vec4 var_LightDir;
9195
#else
9296
out vec3 var_Position;
9397
out vec3 var_Normal;
9498
#endif
9599

96-
#if defined(PER_PIXEL_LIGHTING)
97-
out vec4 var_LightDir;
98-
#endif
99-
100100
vec4 CalcColor(vec3 position)
101101
{
102102
vec4 color = vec4(1.0);
@@ -327,15 +327,30 @@ void main()
327327

328328
#if defined(PER_PIXEL_LIGHTING)
329329
vec3 viewDir = u_ViewOrigin.xyz - position;
330-
331-
// store view direction in tangent space to save on outs
332-
var_Normal = vec4(normal, 0.0);
333330
var_Tangent = vec4(tangent, (attr_Tangent.w * 2.0 - 1.0));
334-
var_ViewDir = vec4(viewDir.xyz, 0.0);
335331

336-
vec3 bitangent = cross(normal, tangent) * var_Tangent.w;
337-
mat3 TBN = mat3(tangent, bitangent, normal);
338-
var_TangentViewDir = vec4(var_ViewDir.xyz * TBN, 0.0);
332+
#if defined(USE_NORMALMAP) && defined(USE_PARALLAXMAP)
333+
vec3 bitangent = cross(normal, tangent) * var_Tangent.w;
334+
mat3 TBN = mat3(tangent, bitangent, normal);
335+
vec3 tangentViewDir = viewDir * TBN;
336+
337+
// normal map aspect correction for parallax mapping
338+
vec2 normalSize = vec2(textureSize(u_NormalMap, 0));
339+
float normalMapAspect = normalSize.y / normalSize.x;
340+
341+
tangentViewDir *= vec3(
342+
max(1.0, normalMapAspect),
343+
max(1.0, 1.0 / normalMapAspect),
344+
1.0
345+
);
346+
#else
347+
vec3 tangentViewDir = vec3(0.0);
348+
#endif
349+
350+
// store tangent view direction in other outs to save space
351+
var_LightDir.w = tangentViewDir.x;
352+
var_Normal = vec4(normal, tangentViewDir.y);
353+
var_ViewDir = vec4(viewDir, tangentViewDir.z);
339354
#else
340355
var_Normal = normal;
341356
var_Position = position;
@@ -456,7 +471,6 @@ in vec4 var_Color;
456471
in vec4 var_Normal;
457472
in vec4 var_Tangent;
458473
in vec4 var_ViewDir;
459-
in vec4 var_TangentViewDir;
460474
in vec4 var_LightDir;
461475
#else
462476
in vec3 var_Position;
@@ -615,7 +629,7 @@ float RayIntersectDisplaceMap(in vec2 inDp, in vec2 ds, in sampler2D normalMap,
615629
const int linearSearchSteps = 16;
616630
const int binarySearchSteps = 8;
617631

618-
vec2 dp = inDp - parallaxBias * ds;
632+
vec2 dp = fract(inDp - parallaxBias * ds);
619633

620634
// current size of search window
621635
float size = 1.0 / float(linearSearchSteps);
@@ -629,8 +643,16 @@ float RayIntersectDisplaceMap(in vec2 inDp, in vec2 ds, in sampler2D normalMap,
629643
vec2 dx = dFdx(inDp);
630644
vec2 dy = dFdy(inDp);
631645

646+
// try sampling at least one border pixel
647+
vec2 tMin = (vec2(0.0) - dp) / ds;
648+
vec2 tMax = (vec2(1.0) - dp) / ds;
649+
vec2 t = max(tMin, tMax);
650+
float tExit = min(t.x, t.y);
651+
float stepFraction = fract(tExit / size) * size;
652+
depth -= size-stepFraction;
653+
632654
// search front to back for first point inside object
633-
for(int i = 0; i < linearSearchSteps - 1; ++i)
655+
for(int i = 0; i < linearSearchSteps; ++i)
634656
{
635657
depth += size;
636658

@@ -676,9 +698,7 @@ float RayIntersectDisplaceMap(in vec2 inDp, in vec2 ds, in sampler2D normalMap,
676698
vec2 GetParallaxOffset(in vec2 texCoords, in vec3 tangentDir)
677699
{
678700
#if defined(USE_PARALLAXMAP)
679-
ivec2 normalSize = textureSize(u_NormalMap, 0);
680-
vec3 nonSquareScale = mix(vec3(normalSize.y / normalSize.x, 1.0, 1.0), vec3(1.0, normalSize.x / normalSize.y, 1.0), float(normalSize.y <= normalSize.x));
681-
vec3 offsetDir = normalize(tangentDir * nonSquareScale);
701+
vec3 offsetDir = normalize(tangentDir);
682702
offsetDir.xy *= -u_NormalScale.a / offsetDir.z;
683703

684704
return offsetDir.xy * RayIntersectDisplaceMap(texCoords, offsetDir.xy, u_NormalMap, u_ParallaxBias);
@@ -1052,7 +1072,9 @@ void main()
10521072
vec2 texCoords = var_TexCoords.xy;
10531073
vec2 lmCoords = var_TexCoords.zw;
10541074
#if defined(PER_PIXEL_LIGHTING)
1055-
vec2 tex_offset = GetParallaxOffset(texCoords, var_TangentViewDir.xyz);
1075+
// Unpack tangent view direction
1076+
vec3 tangentViewDir = vec3(var_LightDir.w, var_Normal.w, var_ViewDir.w);
1077+
vec2 tex_offset = GetParallaxOffset(texCoords, tangentViewDir);
10561078
texCoords += tex_offset;
10571079
#endif
10581080

@@ -1127,10 +1149,6 @@ void main()
11271149
vec3 normalBias = vertexNormal * (1.0 - NPL);
11281150
float shadowValue = sunShadow(u_ViewOrigin, viewDir, normalBias, u_ShadowMap) * NPL;
11291151

1130-
// surfaces not facing the light are always shadowed
1131-
1132-
//shadowValue = mix(0.0, shadowValue, dot(N, primaryLightDir) > 0.0);
1133-
11341152
#if defined(SHADOWMAP_MODULATE)
11351153
vec3 ambientScale = mix(vec3(1.0), u_PrimaryLightAmbient, u_EnableTextures.z);
11361154
lightColor = mix(ambientScale * lightColor, lightColor, shadowValue);

shared/rd-rend2/glsl/surface_sprites.glsl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ void main()
132132
float distanceToCamera = length(V);
133133
float fadeScale = smoothstep(u_FadeStartDistance, u_FadeEndDistance,
134134
distanceToCamera);
135+
136+
if (fadeScale >= 1.0)
137+
{
138+
gl_Position = vec4(0.0);
139+
return;
140+
}
135141

136142
float sprite_time = u_frameTime * 1000.0;
137143
int vertex_id = gl_VertexID % 4;

shared/rd-rend2/tr_bsp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4150,7 +4150,6 @@ static void R_GenerateSurfaceSprites( const world_t *world, int worldIndex )
41504150
if (j > 0 && (stage->stateBits & GLS_DEPTHFUNC_EQUAL))
41514151
{
41524152
ri.Printf(PRINT_WARNING, "depthFunc equal is not supported on surface sprites in rend2. Skipping stage\n");
4153-
surf->numSurfaceSprites -= 1;
41544153
continue;
41554154
}
41564155

@@ -4180,6 +4179,8 @@ static void R_GenerateSurfaceSprites( const world_t *world, int worldIndex )
41804179

41814180
++surfaceSpriteNum;
41824181
}
4182+
surf->numSurfaceSprites = surfaceSpriteNum;
4183+
41834184
break;
41844185
}
41854186

0 commit comments

Comments
 (0)