|
|
|
@ -6,7 +6,7 @@ |
|
|
|
* |
|
|
|
* Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev |
|
|
|
* |
|
|
|
* Example contributed by Luís Almeida (@luis605) |
|
|
|
* Example contributed by Luís Almeida (@luis605) and reviewed by Ramon Santamaria (@raysan5) |
|
|
|
* |
|
|
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
|
|
|
* BSD-like license that allows static linking with closed source software |
|
|
|
@ -16,6 +16,7 @@ |
|
|
|
********************************************************************************************/ |
|
|
|
|
|
|
|
#include "raylib.h" |
|
|
|
|
|
|
|
#include "rlgl.h" |
|
|
|
|
|
|
|
#if defined(PLATFORM_DESKTOP) |
|
|
|
@ -24,7 +25,14 @@ |
|
|
|
#define GLSL_VERSION 100 |
|
|
|
#endif |
|
|
|
|
|
|
|
RenderTexture2D LoadRenderTextureWithDepth(int width, int height); |
|
|
|
//-------------------------------------------------------------------------------------- |
|
|
|
// Module Functions Declaration |
|
|
|
//-------------------------------------------------------------------------------------- |
|
|
|
// Load custom render texture with depth texture attached |
|
|
|
static RenderTexture2D LoadRenderTextureDepthTex(int width, int height); |
|
|
|
|
|
|
|
// Unload render texture from GPU memory (VRAM) |
|
|
|
static void UnloadRenderTextureDepthTex(RenderTexture2D target); |
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------ |
|
|
|
// Program main entry point |
|
|
|
@ -38,7 +46,7 @@ int main(void) |
|
|
|
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - depth rendering"); |
|
|
|
|
|
|
|
// Init camera |
|
|
|
// Define the camera to look into our 3d world |
|
|
|
Camera camera = { 0 }; |
|
|
|
camera.position = (Vector3){ 4.0f, 1.0f, 5.0f }; |
|
|
|
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; |
|
|
|
@ -46,8 +54,8 @@ int main(void) |
|
|
|
camera.fovy = 45.0f; |
|
|
|
camera.projection = CAMERA_PERSPECTIVE; |
|
|
|
|
|
|
|
// Load an empty render texture with a depth texture |
|
|
|
RenderTexture2D target = LoadRenderTextureWithDepth(screenWidth, screenHeight); |
|
|
|
// Load render texture with a depth texture attached |
|
|
|
RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight); |
|
|
|
|
|
|
|
// Load depth shader and get depth texture shader location |
|
|
|
Shader depthShader = LoadShader(0, TextFormat("resources/shaders/glsl%i/depth.fs", GLSL_VERSION)); |
|
|
|
@ -55,7 +63,7 @@ int main(void) |
|
|
|
int flipTextureLoc = GetShaderLocation(depthShader, "flipY"); |
|
|
|
SetShaderValue(depthShader, flipTextureLoc, (int[]){ 1 }, SHADER_UNIFORM_INT); // Flip Y texture |
|
|
|
|
|
|
|
// Load models |
|
|
|
// Load scene models |
|
|
|
Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); |
|
|
|
Model floor = LoadModelFromMesh(GenMeshPlane(20.0f, 20.0f, 1, 1)); |
|
|
|
|
|
|
|
@ -78,12 +86,14 @@ int main(void) |
|
|
|
ClearBackground(WHITE); |
|
|
|
|
|
|
|
BeginMode3D(camera); |
|
|
|
DrawModel(cube, (Vector3){ 0.0f, 0.0f, 0.0f }, 3.0f, YELLOW); |
|
|
|
DrawModel(cube, (Vector3){ 0.0f, 0.0f, 0.0f }, 3.0f, YELLOW); |
|
|
|
DrawModel(floor, (Vector3){ 10.0f, 0.0f, 2.0f }, 2.0f, RED); |
|
|
|
EndMode3D(); |
|
|
|
EndTextureMode(); |
|
|
|
|
|
|
|
// Draw into screen (main framebuffer) |
|
|
|
BeginDrawing(); |
|
|
|
ClearBackground(RAYWHITE); |
|
|
|
|
|
|
|
BeginShaderMode(depthShader); |
|
|
|
SetShaderValueTexture(depthShader, depthLoc, target.depth); |
|
|
|
@ -106,19 +116,24 @@ int main(void) |
|
|
|
//-------------------------------------------------------------------------------------- |
|
|
|
UnloadModel(cube); // Unload model |
|
|
|
UnloadModel(floor); // Unload model |
|
|
|
UnloadRenderTexture(target); // Unload render texture |
|
|
|
UnloadRenderTextureDepthTex(target); |
|
|
|
UnloadShader(depthShader); // Unload shader |
|
|
|
|
|
|
|
CloseWindow(); // Close window and OpenGL context |
|
|
|
//-------------------------------------------------------------------------------------- |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
RenderTexture2D LoadRenderTextureWithDepth(int width, int height) |
|
|
|
//-------------------------------------------------------------------------------------- |
|
|
|
// Module Functions Definition |
|
|
|
//-------------------------------------------------------------------------------------- |
|
|
|
// Load custom render texture, create a writable depth texture buffer |
|
|
|
static RenderTexture2D LoadRenderTextureDepthTex(int width, int height) |
|
|
|
{ |
|
|
|
RenderTexture2D target = {0}; |
|
|
|
RenderTexture2D target = { 0 }; |
|
|
|
|
|
|
|
target.id = rlLoadFramebuffer(); // Load an empty framebuffer |
|
|
|
target.id = rlLoadFramebuffer(); // Load an empty framebuffer |
|
|
|
|
|
|
|
if (target.id > 0) |
|
|
|
{ |
|
|
|
@ -131,11 +146,11 @@ RenderTexture2D LoadRenderTextureWithDepth(int width, int height) |
|
|
|
target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8; |
|
|
|
target.texture.mipmaps = 1; |
|
|
|
|
|
|
|
// Create depth texture |
|
|
|
// Create depth texture buffer (instead of raylib default renderbuffer) |
|
|
|
target.depth.id = rlLoadTextureDepth(width, height, false); |
|
|
|
target.depth.width = width; |
|
|
|
target.depth.height = height; |
|
|
|
target.depth.format = 19; //DEPTH_COMPONENT_24BIT? THIS DOESN'T EXIST IN RAYLIB |
|
|
|
target.depth.format = 19; // DEPTH_COMPONENT_24BIT: Not defined in raylib |
|
|
|
target.depth.mipmaps = 1; |
|
|
|
|
|
|
|
// Attach color texture and depth texture to FBO |
|
|
|
@ -150,4 +165,19 @@ RenderTexture2D LoadRenderTextureWithDepth(int width, int height) |
|
|
|
else TRACELOG(LOG_WARNING, "FBO: Framebuffer object can not be created"); |
|
|
|
|
|
|
|
return target; |
|
|
|
} |
|
|
|
|
|
|
|
// Unload render texture from GPU memory (VRAM) |
|
|
|
void UnloadRenderTextureDepthTex(RenderTexture2D target) |
|
|
|
{ |
|
|
|
if (target.id > 0) |
|
|
|
{ |
|
|
|
// Color texture attached to FBO is deleted |
|
|
|
rlUnloadTexture(target.texture.id); |
|
|
|
rlUnloadTexture(target.depth.id); |
|
|
|
|
|
|
|
// NOTE: Depth texture is automatically |
|
|
|
// queried and deleted before deleting framebuffer |
|
|
|
rlUnloadFramebuffer(target.id); |
|
|
|
} |
|
|
|
} |