2023-01-01 20:17:28 +03:00
|
|
|
|
/*******************************************************************************************
|
|
|
|
|
*
|
2023-01-01 20:31:03 +03:00
|
|
|
|
* raylib [shaders] example - Depth buffer writing
|
2023-01-01 20:17:28 +03:00
|
|
|
|
*
|
|
|
|
|
* Example originally created with raylib 4.2, last time updated with raylib 4.2
|
|
|
|
|
*
|
|
|
|
|
* Example contributed by Buğra Alptekin Sarı (@BugraAlptekinSari) 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
|
|
|
|
|
*
|
2023-01-01 20:31:03 +03:00
|
|
|
|
* Copyright (c) 2022-2023 Buğra Alptekin Sarı (@BugraAlptekinSari)
|
2023-01-01 20:17:28 +03:00
|
|
|
|
*
|
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "raylib.h"
|
2023-02-14 22:12:53 +03:00
|
|
|
|
|
2023-01-01 20:17:28 +03:00
|
|
|
|
#include "rlgl.h"
|
|
|
|
|
|
|
|
|
|
#if defined(PLATFORM_DESKTOP)
|
|
|
|
|
#define GLSL_VERSION 330
|
2023-09-07 18:42:28 +03:00
|
|
|
|
#else // PLATFORM_ANDROID, PLATFORM_WEB
|
2023-01-01 20:17:28 +03:00
|
|
|
|
#define GLSL_VERSION 100
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-01-01 20:31:03 +03:00
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
|
// Declare custom functions required for the example
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
|
// Load custom render texture, create a writable depth texture buffer
|
|
|
|
|
static RenderTexture2D LoadRenderTextureDepthTex(int width, int height);
|
2023-02-14 22:12:53 +03:00
|
|
|
|
|
2023-01-01 20:17:28 +03:00
|
|
|
|
// Unload render texture from GPU memory (VRAM)
|
2023-01-01 20:31:03 +03:00
|
|
|
|
static void UnloadRenderTextureDepthTex(RenderTexture2D target);
|
|
|
|
|
|
2023-01-01 20:17:28 +03:00
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
|
// Program main entry point
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
// Initialization
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
const int screenWidth = 800;
|
|
|
|
|
const int screenHeight = 450;
|
|
|
|
|
|
2023-01-01 20:31:03 +03:00
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - write depth buffer");
|
2023-01-01 20:17:28 +03:00
|
|
|
|
|
2023-01-01 20:31:03 +03:00
|
|
|
|
// The shader inverts the depth buffer by writing into it by `gl_FragDepth = 1 - gl_FragCoord.z;`
|
2023-01-01 20:17:28 +03:00
|
|
|
|
Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/write_depth.fs", GLSL_VERSION));
|
|
|
|
|
|
2023-01-01 20:31:03 +03:00
|
|
|
|
// Use Customized function to create writable depth texture buffer
|
|
|
|
|
RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
|
2023-01-01 20:17:28 +03:00
|
|
|
|
|
2023-01-01 20:31:03 +03:00
|
|
|
|
// Define the camera to look into our 3d world
|
|
|
|
|
Camera camera = {
|
2023-01-01 20:17:28 +03:00
|
|
|
|
.position = (Vector3){ 2.0f, 2.0f, 3.0f }, // Camera position
|
|
|
|
|
.target = (Vector3){ 0.0f, 0.5f, 0.0f }, // Camera looking at point
|
|
|
|
|
.up = (Vector3){ 0.0f, 1.0f, 0.0f }, // Camera up vector (rotation towards target)
|
|
|
|
|
.fovy = 45.0f, // Camera field-of-view Y
|
2023-02-14 22:12:53 +03:00
|
|
|
|
.projection = CAMERA_PERSPECTIVE // Camera projection type
|
2023-01-01 20:17:28 +03:00
|
|
|
|
};
|
2023-01-01 20:31:03 +03:00
|
|
|
|
|
2023-02-14 22:12:53 +03:00
|
|
|
|
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
2023-01-01 20:17:28 +03:00
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// Main game loop
|
2023-02-14 22:12:53 +03:00
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
2023-01-01 20:17:28 +03:00
|
|
|
|
{
|
|
|
|
|
// Update
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-02-14 22:12:53 +03:00
|
|
|
|
UpdateCamera(&camera, CAMERA_ORBITAL);
|
2023-01-01 20:31:03 +03:00
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
2023-01-01 20:17:28 +03:00
|
|
|
|
// Draw
|
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-01-01 20:31:03 +03:00
|
|
|
|
|
|
|
|
|
// Draw into our custom render texture (framebuffer)
|
2023-01-01 20:17:28 +03:00
|
|
|
|
BeginTextureMode(target);
|
2023-01-01 20:31:03 +03:00
|
|
|
|
ClearBackground(WHITE);
|
|
|
|
|
|
|
|
|
|
BeginMode3D(camera);
|
|
|
|
|
BeginShaderMode(shader);
|
|
|
|
|
DrawCubeWiresV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, RED);
|
|
|
|
|
DrawCubeV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, PURPLE);
|
|
|
|
|
DrawCubeWiresV((Vector3){ 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, DARKGREEN);
|
|
|
|
|
DrawCubeV((Vector3) { 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, YELLOW);
|
|
|
|
|
DrawGrid(10, 1.0f);
|
|
|
|
|
EndShaderMode();
|
|
|
|
|
EndMode3D();
|
2023-01-01 20:17:28 +03:00
|
|
|
|
EndTextureMode();
|
|
|
|
|
|
2023-01-01 20:31:03 +03:00
|
|
|
|
// Draw into screen our custom render texture
|
2023-01-01 20:17:28 +03:00
|
|
|
|
BeginDrawing();
|
2023-01-01 20:31:03 +03:00
|
|
|
|
ClearBackground(RAYWHITE);
|
|
|
|
|
|
|
|
|
|
DrawTextureRec(target.texture, (Rectangle) { 0, 0, screenWidth, -screenHeight }, (Vector2) { 0, 0 }, WHITE);
|
|
|
|
|
DrawFPS(10, 10);
|
2023-01-01 20:17:28 +03:00
|
|
|
|
EndDrawing();
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
2023-01-01 20:31:03 +03:00
|
|
|
|
UnloadRenderTextureDepthTex(target);
|
2023-01-01 20:17:28 +03:00
|
|
|
|
UnloadShader(shader);
|
|
|
|
|
|
|
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-01-01 20:31:03 +03:00
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
|
// Define custom functions required for the example
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
|
// Load custom render texture, create a writable depth texture buffer
|
|
|
|
|
RenderTexture2D LoadRenderTextureDepthTex(int width, int height)
|
|
|
|
|
{
|
|
|
|
|
RenderTexture2D target = { 0 };
|
|
|
|
|
|
|
|
|
|
target.id = rlLoadFramebuffer(width, height); // Load an empty framebuffer
|
|
|
|
|
|
|
|
|
|
if (target.id > 0)
|
|
|
|
|
{
|
|
|
|
|
rlEnableFramebuffer(target.id);
|
|
|
|
|
|
|
|
|
|
// Create color texture (default to RGBA)
|
|
|
|
|
target.texture.id = rlLoadTexture(0, width, height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
|
|
|
|
|
target.texture.width = width;
|
|
|
|
|
target.texture.height = height;
|
|
|
|
|
target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
|
|
|
|
target.texture.mipmaps = 1;
|
|
|
|
|
|
|
|
|
|
// 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?
|
|
|
|
|
target.depth.mipmaps = 1;
|
|
|
|
|
|
|
|
|
|
// Attach color texture and depth texture to FBO
|
|
|
|
|
rlFramebufferAttach(target.id, target.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
|
|
|
|
|
rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
|
|
|
|
|
|
|
|
|
|
// Check if fbo is complete with attachments (valid)
|
|
|
|
|
if (rlFramebufferComplete(target.id)) TRACELOG(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", target.id);
|
|
|
|
|
|
|
|
|
|
rlDisableFramebuffer();
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|