REVIEWED: shaders_write_depth
example
This commit is contained in:
parent
3cfb9a6e83
commit
5ba41e4f7f
@ -7,9 +7,11 @@ varying vec4 fragColor;
|
|||||||
|
|
||||||
uniform sampler2D texture0;
|
uniform sampler2D texture0;
|
||||||
uniform vec4 colDiffuse;
|
uniform vec4 colDiffuse;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec4 texelColor = texture2D(texture0, fragTexCoord);
|
vec4 texelColor = texture2D(texture0, fragTexCoord);
|
||||||
|
|
||||||
gl_FragColor = texelColor*colDiffuse*fragColor;
|
gl_FragColor = texelColor*colDiffuse*fragColor;
|
||||||
gl_FragDepthEXT = 1.0 - gl_FragCoord.z;
|
gl_FragDepthEXT = 1.0 - gl_FragCoord.z;
|
||||||
}
|
}
|
@ -5,9 +5,11 @@ in vec4 fragColor;
|
|||||||
|
|
||||||
uniform sampler2D texture0;
|
uniform sampler2D texture0;
|
||||||
uniform vec4 colDiffuse;
|
uniform vec4 colDiffuse;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec4 texelColor = texture2D(texture0, fragTexCoord);
|
vec4 texelColor = texture2D(texture0, fragTexCoord);
|
||||||
|
|
||||||
gl_FragColor = texelColor*colDiffuse*fragColor;
|
gl_FragColor = texelColor*colDiffuse*fragColor;
|
||||||
gl_FragDepth = 1.0 - gl_FragCoord.z;
|
gl_FragDepth = 1.0 - gl_FragCoord.z;
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
*
|
*
|
||||||
* raylib [core] example - Basic window
|
* raylib [shaders] example - Depth buffer writing
|
||||||
*
|
*
|
||||||
* Example originally created with raylib 4.2, last time updated with raylib 4.2
|
* Example originally created with raylib 4.2, last time updated with raylib 4.2
|
||||||
*
|
*
|
||||||
@ -9,7 +9,7 @@
|
|||||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||||
* BSD-like license that allows static linking with closed source software
|
* BSD-like license that allows static linking with closed source software
|
||||||
*
|
*
|
||||||
* Copyright (c) 2022 Buğra Alptekin Sarı (@BugraAlptekinSari)
|
* Copyright (c) 2022-2023 Buğra Alptekin Sarı (@BugraAlptekinSari)
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
@ -21,9 +21,100 @@
|
|||||||
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
|
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
|
||||||
#define GLSL_VERSION 100
|
#define GLSL_VERSION 100
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Customized render texture function to create a writable render buffer
|
// Declare custom functions required for the example
|
||||||
RenderTexture2D LoadRenderTextureMOD(int width, int height)
|
//------------------------------------------------------------------------------------
|
||||||
|
// Load custom render texture, create a writable depth texture buffer
|
||||||
|
static RenderTexture2D LoadRenderTextureDepthTex(int width, int height);
|
||||||
|
// Unload render texture from GPU memory (VRAM)
|
||||||
|
static void UnloadRenderTextureDepthTex(RenderTexture2D target);
|
||||||
|
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Program main entry point
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
const int screenWidth = 800;
|
||||||
|
const int screenHeight = 450;
|
||||||
|
|
||||||
|
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - write depth buffer");
|
||||||
|
|
||||||
|
// The shader inverts the depth buffer by writing into it by `gl_FragDepth = 1 - gl_FragCoord.z;`
|
||||||
|
Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/write_depth.fs", GLSL_VERSION));
|
||||||
|
|
||||||
|
// Use Customized function to create writable depth texture buffer
|
||||||
|
RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
|
||||||
|
|
||||||
|
// Define the camera to look into our 3d world
|
||||||
|
Camera camera = {
|
||||||
|
.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
|
||||||
|
.projection = CAMERA_PERSPECTIVE // Camera mode type
|
||||||
|
};
|
||||||
|
|
||||||
|
SetCameraMode(camera, CAMERA_ORBITAL);
|
||||||
|
|
||||||
|
SetTargetFPS(60);
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Main game loop
|
||||||
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||||
|
{
|
||||||
|
// Update
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
UpdateCamera(&camera);
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Draw into our custom render texture (framebuffer)
|
||||||
|
BeginTextureMode(target);
|
||||||
|
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();
|
||||||
|
EndTextureMode();
|
||||||
|
|
||||||
|
// Draw into screen our custom render texture
|
||||||
|
BeginDrawing();
|
||||||
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
|
DrawTextureRec(target.texture, (Rectangle) { 0, 0, screenWidth, -screenHeight }, (Vector2) { 0, 0 }, WHITE);
|
||||||
|
DrawFPS(10, 10);
|
||||||
|
EndDrawing();
|
||||||
|
//----------------------------------------------------------------------------------
|
||||||
|
}
|
||||||
|
|
||||||
|
// De-Initialization
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
UnloadRenderTextureDepthTex(target);
|
||||||
|
UnloadShader(shader);
|
||||||
|
|
||||||
|
CloseWindow(); // Close window and OpenGL context
|
||||||
|
//--------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// 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 };
|
RenderTexture2D target = { 0 };
|
||||||
|
|
||||||
@ -40,14 +131,14 @@ RenderTexture2D LoadRenderTextureMOD(int width, int height)
|
|||||||
target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
|
||||||
target.texture.mipmaps = 1;
|
target.texture.mipmaps = 1;
|
||||||
|
|
||||||
// Create depth rendertexture
|
// Create depth texture buffer (instead of raylib default renderbuffer)
|
||||||
target.depth.id = rlLoadTextureDepth(width, height, false);
|
target.depth.id = rlLoadTextureDepth(width, height, false);
|
||||||
target.depth.width = width;
|
target.depth.width = width;
|
||||||
target.depth.height = height;
|
target.depth.height = height;
|
||||||
target.depth.format = 19; //DEPTH_COMPONENT_24BIT?
|
target.depth.format = 19; //DEPTH_COMPONENT_24BIT?
|
||||||
target.depth.mipmaps = 1;
|
target.depth.mipmaps = 1;
|
||||||
|
|
||||||
// Attach color texture and depth renderbuffer/texture to FBO
|
// 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.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
|
||||||
rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
|
rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
|
||||||
|
|
||||||
@ -62,7 +153,7 @@ RenderTexture2D LoadRenderTextureMOD(int width, int height)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Unload render texture from GPU memory (VRAM)
|
// Unload render texture from GPU memory (VRAM)
|
||||||
void UnloadRenderTextureMOD(RenderTexture2D target)
|
void UnloadRenderTextureDepthTex(RenderTexture2D target)
|
||||||
{
|
{
|
||||||
if (target.id > 0)
|
if (target.id > 0)
|
||||||
{
|
{
|
||||||
@ -70,80 +161,8 @@ void UnloadRenderTextureMOD(RenderTexture2D target)
|
|||||||
rlUnloadTexture(target.texture.id);
|
rlUnloadTexture(target.texture.id);
|
||||||
rlUnloadTexture(target.depth.id);
|
rlUnloadTexture(target.depth.id);
|
||||||
|
|
||||||
// NOTE: Depth texture/renderbuffer is automatically
|
// NOTE: Depth texture is automatically
|
||||||
// queried and deleted before deleting framebuffer
|
// queried and deleted before deleting framebuffer
|
||||||
rlUnloadFramebuffer(target.id);
|
rlUnloadFramebuffer(target.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//------------------------------------------------------------------------------------
|
|
||||||
// Program main entry point
|
|
||||||
//------------------------------------------------------------------------------------
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
// Initialization
|
|
||||||
//--------------------------------------------------------------------------------------
|
|
||||||
const int screenWidth = 800;
|
|
||||||
const int screenHeight = 450;
|
|
||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
|
|
||||||
|
|
||||||
// The shader inverst the depth buffer by writing into it by `gl_FragDepth = 1 - gl_FragCoord.z;`
|
|
||||||
Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/write_depth.fs", GLSL_VERSION));
|
|
||||||
|
|
||||||
//Use Customized function to create writable depth buffer
|
|
||||||
RenderTexture2D target = LoadRenderTextureMOD(screenWidth, screenHeight);
|
|
||||||
|
|
||||||
Camera camera = { // Define the camera to look into our 3d world
|
|
||||||
.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
|
|
||||||
.projection = CAMERA_PERSPECTIVE // Camera mode type
|
|
||||||
};
|
|
||||||
SetCameraMode(camera, CAMERA_ORBITAL);
|
|
||||||
SetTargetFPS(60);
|
|
||||||
//--------------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Main game loop
|
|
||||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
||||||
{
|
|
||||||
// Update
|
|
||||||
//----------------------------------------------------------------------------------
|
|
||||||
UpdateCamera(&camera);
|
|
||||||
// Draw
|
|
||||||
//----------------------------------------------------------------------------------
|
|
||||||
// Draw FBO
|
|
||||||
BeginTextureMode(target);
|
|
||||||
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();
|
|
||||||
EndTextureMode();
|
|
||||||
|
|
||||||
// Draw Screen
|
|
||||||
BeginDrawing();
|
|
||||||
|
|
||||||
ClearBackground(RAYWHITE);
|
|
||||||
DrawTextureRec(target.texture, (Rectangle) { 0, 0, screenWidth, -screenHeight }, (Vector2) { 0, 0 }, WHITE);
|
|
||||||
DrawFPS(0, 0);
|
|
||||||
|
|
||||||
EndDrawing();
|
|
||||||
//----------------------------------------------------------------------------------
|
|
||||||
}
|
|
||||||
|
|
||||||
// De-Initialization
|
|
||||||
//--------------------------------------------------------------------------------------
|
|
||||||
UnloadRenderTextureMOD(target);
|
|
||||||
UnloadShader(shader);
|
|
||||||
|
|
||||||
CloseWindow(); // Close window and OpenGL context
|
|
||||||
//--------------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user