Skip to content

Commit a5639bb

Browse files
authored
Merge pull request #4861 from luis605/view-depth-texture
[examples] Added new shader example: `shader_depth_texture`
2 parents 5aa3f0c + 4960cc7 commit a5639bb

File tree

10 files changed

+784
-30
lines changed

10 files changed

+784
-30
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,14 @@ if (${PLATFORM} MATCHES "Android")
9090

9191
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_custom_uniform.c)
9292
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_model_shader.c)
93+
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_view_depth.c)
9394
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_postprocessing.c)
9495
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_raymarching.c)
9596
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_palette_switch.c)
9697
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_basic_lighting.c)
9798

9899
elseif (${PLATFORM} MATCHES "Web")
99-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os")
100+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os")
100101
# Since WASM is used, ALLOW_MEMORY_GROWTH has no extra overheads
101102
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s WASM=1 -s ASYNCIFY -s ALLOW_MEMORY_GROWTH=1 --shell-file ${CMAKE_SOURCE_DIR}/src/shell.html")
102103
set(CMAKE_EXECUTABLE_SUFFIX ".html")

examples/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ CFLAGS = -Wall -std=c99 -D_DEFAULT_SOURCE -Wno-missing-braces -Wunused-result
232232

233233
ifeq ($(BUILD_MODE),DEBUG)
234234
CFLAGS += -g -D_DEBUG
235-
else
235+
else
236236
ifeq ($(TARGET_PLATFORM),$(filter $(TARGET_PLATFORM),PLATFORM_WEB PLATFORM_WEB_RGFW))
237237
ifeq ($(BUILD_WEB_ASYNCIFY),TRUE)
238238
CFLAGS += -O3
@@ -341,12 +341,12 @@ ifeq ($(TARGET_PLATFORM),$(filter $(TARGET_PLATFORM),PLATFORM_WEB PLATFORM_WEB_R
341341
# --source-map-base # allow debugging in browser with source map
342342
# --shell-file shell.html # define a custom shell .html and output extension
343343
LDFLAGS += -sTOTAL_MEMORY=$(BUILD_WEB_HEAP_SIZE) -sFORCE_FILESYSTEM=1 -sMINIFY_HTML=0
344-
344+
345345
# Using GLFW3 library (instead of RGFW)
346346
ifeq ($(TARGET_PLATFORM),PLATFORM_WEB)
347347
LDFLAGS += -sUSE_GLFW=3
348348
endif
349-
349+
350350
# Build using asyncify
351351
ifeq ($(BUILD_WEB_ASYNCIFY),TRUE)
352352
LDFLAGS += -sASYNCIFY
@@ -642,6 +642,7 @@ SHADERS = \
642642
shaders/shaders_texture_outline \
643643
shaders/shaders_texture_tiling \
644644
shaders/shaders_texture_waves \
645+
shaders/shaders_view_depth \
645646
shaders/shaders_write_depth \
646647
shaders/shaders_vertex_displacement
647648

examples/Makefile.Web

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ ifeq ($(PLATFORM),$(filter $(PLATFORM),PLATFORM_WEB PLATFORM_WEB_RGFW))
278278
# --source-map-base # allow debugging in browser with source map
279279
# --shell-file shell.html # define a custom shell .html and output extension
280280
LDFLAGS += -sTOTAL_MEMORY=$(BUILD_WEB_HEAP_SIZE) -sFORCE_FILESYSTEM=1 -sEXPORTED_RUNTIME_METHODS=ccall -sMINIFY_HTML=0
281-
281+
282282
# Using GLFW3 library (instead of RGFW)
283283
ifeq ($(PLATFORM),PLATFORM_WEB)
284284
LDFLAGS += -sUSE_GLFW=3
@@ -525,6 +525,7 @@ SHADERS = \
525525
shaders/shaders_texture_tiling \
526526
shaders/shaders_texture_waves \
527527
shaders/shaders_vertex_displacement \
528+
shaders/shaders_view_depth \
528529
shaders/shaders_write_depth
529530

530531
AUDIO = \
@@ -1150,6 +1151,10 @@ shaders/shaders_texture_waves: shaders/shaders_texture_waves.c
11501151
--preload-file shaders/resources/space.png@resources/space.png \
11511152
--preload-file shaders/resources/shaders/glsl100/wave.fs@resources/shaders/glsl100/wave.fs
11521153

1154+
shaders/shaders_view_depth: shaders/shaders_view_depth.c
1155+
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
1156+
--preload-file shaders/resources/shaders/glsl100/write_depth.fs@resources/shaders/glsl100/write_depth.fs
1157+
11531158
shaders/shaders_write_depth: shaders/shaders_write_depth.c
11541159
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \
11551160
--preload-file shaders/resources/shaders/glsl100/write_depth.fs@resources/shaders/glsl100/write_depth.fs
@@ -1235,4 +1240,3 @@ ifeq ($(PLATFORM),$(filter $(PLATFORM),PLATFORM_WEB PLATFORM_WEB_RGFW))
12351240
del *.o *.html *.js
12361241
endif
12371242
@echo Cleaning done
1238-

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Examples using raylib shaders functionality, including shaders loading, paramete
201201
| 139 | [shaders_basic_pbr](shaders/shaders_basic_pbr.c) | <img src="shaders/shaders_basic_pbr.png" alt="shaders_basic_pbr" width="80"> | ⭐️⭐️⭐️⭐️ | 5.0 | 5.1-dev | [Afan OLOVCIC](https://github.com/_DevDad) |
202202
| 140 | [shaders_lightmap](shaders/shaders_lightmap.c) | <img src="shaders/shaders_lightmap.png" alt="shaders_lightmap" width="80"> | ⭐️⭐️⭐️☆ | 4.5 | 4.5 | [Jussi Viitala](https://github.com/nullstare) |
203203
| 141 | [shaders_rounded_rectangle](shaders/shaders_rounded_rectangle.c) | <img src="shaders/shaders_rounded_rectangle.png" alt="shaders_rounded_rectangle" width=80> | ⭐️⭐️⭐️☆ | 5.5 | 5.5 | [Anstro Pleuton](https://github.com/anstropleuton) |
204+
| 142 | [shaders_view_depth](shaders/shaders_view_depth.c) | <img src="shaders/shaders_view_depth.png" alt="shaders_view_depth" width="80"> | ⭐️⭐️⭐️☆ | 5.6-dev | 5.6-dev | [Luís Almeida](https://github.com/luis605) |
204205

205206
### category: audio
206207

@@ -230,4 +231,3 @@ Examples showing raylib misc functionality that does not fit in other categories
230231
| 154 | [raymath_vector_angle](others/raymath_vector_angle.c) | <img src="others/raymath_vector_angle.png" alt="raymath_vector_angle" width="80"> | ⭐️⭐️☆☆ | 1.0 | 4.6 | [Ray](https://github.com/raysan5) |
231232

232233
As always contributions are welcome, feel free to send new examples! Here is an [examples template](examples_template.c) to start with!
233-
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
#version 100
22

3-
precision mediump float;
4-
53
// Input vertex attributes (from vertex shader)
64
varying vec2 fragTexCoord;
7-
varying vec4 fragColor;
85

96
// Input uniform values
10-
uniform sampler2D texture0; // Depth texture
11-
uniform vec4 colDiffuse;
7+
uniform sampler2D depthTexture;
8+
uniform bool flipY;
9+
10+
float nearPlane = 0.1;
11+
float farPlane = 100.0;
1212

13-
// NOTE: Add your custom variables here
13+
// Function to linearize depth from non-linear depth buffer
14+
float linearizeDepth(float depth)
15+
{
16+
return (2.0 * nearPlane) / (farPlane + nearPlane - depth * (farPlane - nearPlane));
17+
}
1418

1519
void main()
1620
{
17-
float zNear = 0.01; // camera z near
18-
float zFar = 10.0; // camera z far
19-
float z = texture2D(texture0, fragTexCoord).x;
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;
2028

21-
// Linearize depth value
22-
float depth = (2.0*zNear)/(zFar + zNear - z*(zFar - zNear));
29+
// Linearize depth
30+
float linearDepth = linearizeDepth(depth);
2331

24-
// Calculate final fragment color
25-
gl_FragColor = vec4(depth, depth, depth, 1.0);
32+
// Output final color
33+
gl_FragColor = vec4(vec3(linearDepth), 1.0);
2634
}

examples/shaders/resources/shaders/glsl330/depth.fs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,36 @@
22

33
// Input vertex attributes (from vertex shader)
44
in vec2 fragTexCoord;
5-
in vec4 fragColor;
65

76
// Input uniform values
8-
uniform sampler2D texture0; // Depth texture
9-
uniform vec4 colDiffuse;
7+
uniform sampler2D depthTexture;
8+
uniform bool flipY;
9+
10+
const float nearPlane = 0.1;
11+
const float farPlane = 100.0;
1012

1113
// Output fragment color
1214
out vec4 finalColor;
1315

14-
// NOTE: Add your custom variables here
16+
// Linearizes the depth buffer value
17+
float linearizeDepth(float depth)
18+
{
19+
return (2.0 * nearPlane) / (farPlane + nearPlane - depth * (farPlane - nearPlane));
20+
}
1521

1622
void main()
1723
{
18-
float zNear = 0.01; // camera z near
19-
float zFar = 10.0; // camera z far
20-
float z = texture(texture0, fragTexCoord).x;
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;
2131

2232
// Linearize depth value
23-
float depth = (2.0*zNear)/(zFar + zNear - z*(zFar - zNear));
33+
float linearDepth = linearizeDepth(depth);
2434

25-
// Calculate final fragment color
26-
finalColor = vec4(depth, depth, depth, 1.0);
35+
// Output final color
36+
finalColor = vec4(vec3(linearDepth), 1.0);
2737
}

examples/shaders/shaders_view_depth.c

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [shader] example - render depth texture
4+
*
5+
* Example complexity rating: [★★★☆] 3/4
6+
*
7+
* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
8+
*
9+
* Example contributed by Luís Almeida (@luis605)
10+
*
11+
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
12+
* BSD-like license that allows static linking with closed source software
13+
*
14+
* Copyright (c) 2025 Luís Almeida (@luis605)
15+
*
16+
********************************************************************************************/
17+
18+
#include "raylib.h"
19+
#include "rlgl.h"
20+
21+
#if defined(PLATFORM_DESKTOP)
22+
#define GLSL_VERSION 330
23+
#else // PLATFORM_ANDROID, PLATFORM_WEB
24+
#define GLSL_VERSION 100
25+
#endif
26+
27+
RenderTexture2D LoadRenderTextureWithDepth(int width, int height);
28+
29+
//------------------------------------------------------------------------------------
30+
// Program main entry point
31+
//------------------------------------------------------------------------------------
32+
int main(void)
33+
{
34+
// Initialization
35+
//--------------------------------------------------------------------------------------
36+
const int screenWidth = 800;
37+
const int screenHeight = 600;
38+
39+
InitWindow(screenWidth, screenHeight, "raylib [shader] example - render depth texture");
40+
41+
// Load camera
42+
Camera camera = { 0 };
43+
camera.position = (Vector3){ 4.0f, 1.0f, 5.0f };
44+
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
45+
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
46+
camera.fovy = 45.0f;
47+
camera.projection = CAMERA_PERSPECTIVE;
48+
49+
// Load an empty render texture with a depth texture
50+
RenderTexture2D target = LoadRenderTextureWithDepth(screenWidth, screenHeight);
51+
52+
// Load depth shader and get depth texture shader location
53+
Shader depthShader = LoadShader(0, TextFormat("resources/shaders/glsl%i/depth.fs", GLSL_VERSION));
54+
int depthLoc = GetShaderLocation(depthShader, "depthTexture");
55+
int flipTextureLoc = GetShaderLocation(depthShader, "flipY");
56+
SetShaderValue(depthShader, flipTextureLoc, (int[]){ 1 }, SHADER_UNIFORM_INT); // Flip Y texture
57+
58+
// Load models
59+
Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
60+
Model floor = LoadModelFromMesh(GenMeshPlane(20.0f, 20.0f, 1, 1));
61+
62+
DisableCursor(); // Limit cursor to relative movement inside the window
63+
64+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
65+
//--------------------------------------------------------------------------------------
66+
67+
// Main game loop
68+
while (!WindowShouldClose()) // Detect window close button or ESC key
69+
{
70+
// Update
71+
//----------------------------------------------------------------------------------
72+
UpdateCamera(&camera, CAMERA_FREE);
73+
//----------------------------------------------------------------------------------
74+
75+
// Draw
76+
//----------------------------------------------------------------------------------
77+
BeginTextureMode(target);
78+
79+
ClearBackground(WHITE);
80+
81+
BeginMode3D(camera);
82+
83+
DrawModel(cube, (Vector3){ 0.0f, 0.0f, 0.0f }, 3.0f, YELLOW);
84+
DrawModel(floor, (Vector3){ 10.0f, 0.0f, 2.0f }, 2.0f, RED);
85+
86+
EndMode3D();
87+
88+
EndTextureMode();
89+
90+
BeginDrawing();
91+
92+
BeginShaderMode(depthShader);
93+
94+
SetShaderValueTexture(depthShader, depthLoc, target.depth);
95+
DrawTexture(target.depth, 0, 0, WHITE);
96+
97+
EndShaderMode();
98+
99+
DrawRectangle( 10, 10, 320, 93, Fade(SKYBLUE, 0.5f));
100+
DrawRectangleLines( 10, 10, 320, 93, BLUE);
101+
102+
DrawText("Camera Controls:", 20, 20, 10, BLACK);
103+
DrawText("- WASD to move", 40, 40, 10, DARKGRAY);
104+
DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, DARKGRAY);
105+
DrawText("- Z to zoom to (0, 0, 0)", 40, 80, 10, DARKGRAY);
106+
107+
EndDrawing();
108+
//----------------------------------------------------------------------------------
109+
}
110+
111+
// De-Initialization
112+
//--------------------------------------------------------------------------------------
113+
UnloadModel(cube); // Unload model
114+
UnloadModel(floor); // Unload model
115+
UnloadRenderTexture(target); // Unload render texture
116+
UnloadShader(depthShader); // Unload shader
117+
118+
CloseWindow(); // Close window and OpenGL context
119+
//--------------------------------------------------------------------------------------
120+
return 0;
121+
}
122+
123+
RenderTexture2D LoadRenderTextureWithDepth(int width, int height)
124+
{
125+
RenderTexture2D target = {0};
126+
127+
target.id = rlLoadFramebuffer(); // Load an empty framebuffer
128+
129+
if (target.id > 0)
130+
{
131+
rlEnableFramebuffer(target.id);
132+
133+
// Create color texture (default to RGBA)
134+
target.texture.id = rlLoadTexture(0, width, height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
135+
target.texture.width = width;
136+
target.texture.height = height;
137+
target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
138+
target.texture.mipmaps = 1;
139+
140+
// Create depth texture
141+
target.depth.id = rlLoadTextureDepth(width, height, false);
142+
target.depth.width = width;
143+
target.depth.height = height;
144+
target.depth.format = 19; //DEPTH_COMPONENT_24BIT? THIS DOESN'T EXIST IN RAYLIB
145+
target.depth.mipmaps = 1;
146+
147+
// Attach color texture and depth texture to FBO
148+
rlFramebufferAttach(target.id, target.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
149+
rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
150+
151+
// Check if fbo is complete with attachments (valid)
152+
if (rlFramebufferComplete(target.id)) TRACELOG(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", target.id);
153+
154+
rlDisableFramebuffer();
155+
}
156+
else TRACELOG(LOG_WARNING, "FBO: Framebuffer object can not be created");
157+
158+
return target;
159+
}
11.1 KB
Loading

0 commit comments

Comments
 (0)