[examples] Reviewed shader view depth
This commit is contained in:
parent
8ec52e3b25
commit
4960cc74e0
4 changed files with 55 additions and 20 deletions
|
@ -1,19 +1,34 @@
|
||||||
#version 100
|
#version 100
|
||||||
|
|
||||||
in vec2 fragTexCoord;
|
// Input vertex attributes (from vertex shader)
|
||||||
out vec4 finalColor;
|
varying vec2 fragTexCoord;
|
||||||
uniform sampler2D depthTexture;
|
|
||||||
|
|
||||||
|
// Input uniform values
|
||||||
|
uniform sampler2D depthTexture;
|
||||||
|
uniform bool flipY;
|
||||||
|
|
||||||
|
float nearPlane = 0.1;
|
||||||
|
float farPlane = 100.0;
|
||||||
|
|
||||||
|
// Function to linearize depth from non-linear depth buffer
|
||||||
float linearizeDepth(float depth)
|
float linearizeDepth(float depth)
|
||||||
{
|
{
|
||||||
float n = 0.1; // near plane
|
return (2.0 * nearPlane) / (farPlane + nearPlane - depth * (farPlane - nearPlane));
|
||||||
float f = 100.0; // far plane
|
|
||||||
return (2.0 * n) / (f + n - depth * (f - n));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main()
|
||||||
vec2 flippedTexCoord = vec2(fragTexCoord.x, 1.0 - fragTexCoord.y);
|
{
|
||||||
float depth = texture(depthTexture, flippedTexCoord).r;
|
// Handle potential Y-flipping
|
||||||
|
vec2 texCoord = fragTexCoord;
|
||||||
|
if (flipY)
|
||||||
|
texCoord.y = 1.0 - texCoord.y;
|
||||||
|
|
||||||
|
// Sample depth texture
|
||||||
|
float depth = texture2D(depthTexture, texCoord).r;
|
||||||
|
|
||||||
|
// Linearize depth
|
||||||
float linearDepth = linearizeDepth(depth);
|
float linearDepth = linearizeDepth(depth);
|
||||||
finalColor = vec4(vec3(linearDepth), 1.0);
|
|
||||||
|
// Output final color
|
||||||
|
gl_FragColor = vec4(vec3(linearDepth), 1.0);
|
||||||
}
|
}
|
|
@ -1,19 +1,37 @@
|
||||||
#version 330
|
#version 330
|
||||||
|
|
||||||
|
// Input vertex attributes (from vertex shader)
|
||||||
in vec2 fragTexCoord;
|
in vec2 fragTexCoord;
|
||||||
out vec4 finalColor;
|
|
||||||
uniform sampler2D depthTexture;
|
|
||||||
|
|
||||||
|
// Input uniform values
|
||||||
|
uniform sampler2D depthTexture;
|
||||||
|
uniform bool flipY;
|
||||||
|
|
||||||
|
const float nearPlane = 0.1;
|
||||||
|
const float farPlane = 100.0;
|
||||||
|
|
||||||
|
// Output fragment color
|
||||||
|
out vec4 finalColor;
|
||||||
|
|
||||||
|
// Linearizes the depth buffer value
|
||||||
float linearizeDepth(float depth)
|
float linearizeDepth(float depth)
|
||||||
{
|
{
|
||||||
float n = 0.1; // near plane
|
return (2.0 * nearPlane) / (farPlane + nearPlane - depth * (farPlane - nearPlane));
|
||||||
float f = 100.0; // far plane
|
|
||||||
return (2.0 * n) / (f + n - depth * (f - n));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main()
|
||||||
vec2 flippedTexCoord = vec2(fragTexCoord.x, 1.0 - fragTexCoord.y);
|
{
|
||||||
float depth = texture(depthTexture, flippedTexCoord).r;
|
// Handle potential Y-flipping
|
||||||
|
vec2 texCoord = fragTexCoord;
|
||||||
|
if (flipY)
|
||||||
|
texCoord.y = 1.0 - texCoord.y;
|
||||||
|
|
||||||
|
// Sample depth
|
||||||
|
float depth = texture(depthTexture, texCoord).r;
|
||||||
|
|
||||||
|
// Linearize depth value
|
||||||
float linearDepth = linearizeDepth(depth);
|
float linearDepth = linearizeDepth(depth);
|
||||||
|
|
||||||
|
// Output final color
|
||||||
finalColor = vec4(vec3(linearDepth), 1.0);
|
finalColor = vec4(vec3(linearDepth), 1.0);
|
||||||
}
|
}
|
|
@ -52,6 +52,8 @@ int main(void)
|
||||||
// Load depth shader and get depth texture shader location
|
// Load depth shader and get depth texture shader location
|
||||||
Shader depthShader = LoadShader(0, TextFormat("resources/shaders/glsl%i/depth.fs", GLSL_VERSION));
|
Shader depthShader = LoadShader(0, TextFormat("resources/shaders/glsl%i/depth.fs", GLSL_VERSION));
|
||||||
int depthLoc = GetShaderLocation(depthShader, "depthTexture");
|
int depthLoc = GetShaderLocation(depthShader, "depthTexture");
|
||||||
|
int flipTextureLoc = GetShaderLocation(depthShader, "flipY");
|
||||||
|
SetShaderValue(depthShader, flipTextureLoc, (int[]){ 1 }, SHADER_UNIFORM_INT); // Flip Y texture
|
||||||
|
|
||||||
// Load models
|
// Load models
|
||||||
Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
|
Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 7.6 KiB After Width: | Height: | Size: 11 KiB |
Loading…
Add table
Add a link
Reference in a new issue