Skip to content

Commit 4960cc7

Browse files
committed
[examples] Reviewed shader view depth
1 parent 8ec52e3 commit 4960cc7

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
lines changed
Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
#version 100
22

3-
in vec2 fragTexCoord;
4-
out vec4 finalColor;
3+
// Input vertex attributes (from vertex shader)
4+
varying vec2 fragTexCoord;
5+
6+
// Input uniform values
57
uniform sampler2D depthTexture;
8+
uniform bool flipY;
9+
10+
float nearPlane = 0.1;
11+
float farPlane = 100.0;
612

13+
// Function to linearize depth from non-linear depth buffer
714
float linearizeDepth(float depth)
815
{
9-
float n = 0.1; // near plane
10-
float f = 100.0; // far plane
11-
return (2.0 * n) / (f + n - depth * (f - n));
16+
return (2.0 * nearPlane) / (farPlane + nearPlane - depth * (farPlane - nearPlane));
1217
}
1318

14-
void main() {
15-
vec2 flippedTexCoord = vec2(fragTexCoord.x, 1.0 - fragTexCoord.y);
16-
float depth = texture(depthTexture, flippedTexCoord).r;
19+
void main()
20+
{
21+
// Handle potential Y-flipping
22+
vec2 texCoord = fragTexCoord;
23+
if (flipY)
24+
texCoord.y = 1.0 - texCoord.y;
25+
26+
// Sample depth texture
27+
float depth = texture2D(depthTexture, texCoord).r;
28+
29+
// Linearize depth
1730
float linearDepth = linearizeDepth(depth);
18-
finalColor = vec4(vec3(linearDepth), 1.0);
19-
}
31+
32+
// Output final color
33+
gl_FragColor = vec4(vec3(linearDepth), 1.0);
34+
}
Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
#version 330
22

3+
// Input vertex attributes (from vertex shader)
34
in vec2 fragTexCoord;
4-
out vec4 finalColor;
5+
6+
// Input uniform values
57
uniform sampler2D depthTexture;
8+
uniform bool flipY;
9+
10+
const float nearPlane = 0.1;
11+
const float farPlane = 100.0;
12+
13+
// Output fragment color
14+
out vec4 finalColor;
615

16+
// Linearizes the depth buffer value
717
float linearizeDepth(float depth)
818
{
9-
float n = 0.1; // near plane
10-
float f = 100.0; // far plane
11-
return (2.0 * n) / (f + n - depth * (f - n));
19+
return (2.0 * nearPlane) / (farPlane + nearPlane - depth * (farPlane - nearPlane));
1220
}
1321

14-
void main() {
15-
vec2 flippedTexCoord = vec2(fragTexCoord.x, 1.0 - fragTexCoord.y);
16-
float depth = texture(depthTexture, flippedTexCoord).r;
22+
void main()
23+
{
24+
// Handle potential Y-flipping
25+
vec2 texCoord = fragTexCoord;
26+
if (flipY)
27+
texCoord.y = 1.0 - texCoord.y;
28+
29+
// Sample depth
30+
float depth = texture(depthTexture, texCoord).r;
31+
32+
// Linearize depth value
1733
float linearDepth = linearizeDepth(depth);
34+
35+
// Output final color
1836
finalColor = vec4(vec3(linearDepth), 1.0);
19-
}
37+
}

examples/shaders/shaders_view_depth.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ int main(void)
5252
// Load depth shader and get depth texture shader location
5353
Shader depthShader = LoadShader(0, TextFormat("resources/shaders/glsl%i/depth.fs", GLSL_VERSION));
5454
int depthLoc = GetShaderLocation(depthShader, "depthTexture");
55+
int flipTextureLoc = GetShaderLocation(depthShader, "flipY");
56+
SetShaderValue(depthShader, flipTextureLoc, (int[]){ 1 }, SHADER_UNIFORM_INT); // Flip Y texture
5557

5658
// Load models
5759
Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
3.51 KB
Loading

0 commit comments

Comments
 (0)