Accessing depth component of RenderTexture2D #1579
-
Hey, I'm trying to access the depth component of a RenderTexture2D called with LoadRenderTexture as part of a shadow mapping technique - while the texture component displays fine, the depth component is completely blank. I can't find much info outside the actual source itself that shows how to use the depth texture, is the setup here correct? (Truncated for brevity and written using Raylib-CS, but the underlying functionality should be the same - using Raylib 3.5) static unsafe void Main(string[] args)
{
[...]
while (!WindowShouldClose())
{
[...]
RenderTexture2D shadowTex = LoadRenderTexture(256, 256);
Shader depthshader = LoadShader(null,
"assets/shaders/depth.fs"); // The default depth linearisation shader provided with Raylib
[...]
BeginDrawing();
BeginTextureMode(shadowTex);
ClearBackground(WHITE);
BeginMode3D(shadowCamera); // Just an orthographic camera set up where the light is
DrawModel(model, Vector3.Zero, 0.0005f, WHITE);
EndMode3D();
EndTextureMode();
[...]
DrawTextureRec(shadowTex.texture, new Rectangle(0, 0, shadowTex.texture.width, -shadowTex.texture.height), new Vector2(0, 0), PINK);
BeginShaderMode(depthshader);
DrawTextureRec(shadowTex.depth, new Rectangle(0, 0, shadowTex.depth.width, -shadowTex.depth.height), new Vector2(256, 0), BLUE);
EndShaderMode();
EndDrawing();
}
[...]
} The trace log isn't giving me any useful or unusual output, it says everything is clear sailing The attached screenshot demonstrates what I'm getting - the left (pink-tinted) square at the top is the texture component, the right is the depth component. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi. There is https://github.com/WEREMSOFT/c99-raylib-shadowmap But when I did shadow mapping on my own engines, I sometimes forgot that near-plane should be quite big (ie 1.0) and far-plane as minimum as possible so depth precision isnt lost (used keyboard keys to change values in real time and saw which values are good). Of course light camera is different than user camera. Your shadowTex is quite small.. |
Beta Was this translation helpful? Give feedback.
-
By default raylib loads RenderTexture depth as a RenderBuffer instead of a Texture2D for optimization, that's the reason it can not be drawn. To be able to draw it, you should load it as a Texture2D instead... at this point there is no mechanism in raylib that allows that, you should modify source code: // textures.c -> LoadRenderTexture()
target.depth.id = rlLoadTextureDepth(width, height, false); raylib is intended for basic usage, for advance 3d usage you should use In any case, I take note for a future update to allow changing that behaviour from the main API. |
Beta Was this translation helpful? Give feedback.
By default raylib loads RenderTexture depth as a RenderBuffer instead of a Texture2D for optimization, that's the reason it can not be drawn.
To be able to draw it, you should load it as a Texture2D instead... at this point there is no mechanism in raylib that allows that, you should modify source code:
raylib is intended for basic usage, for advance 3d usage you should use
rlgl
functionality directly... but as you are using a binding that's not exposed...In any case, I take note for a future update to allow changing that behaviour from the main API.