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
+ }
0 commit comments