Fix files headers

This commit is contained in:
PanicTitan 2026-08-05 00:49:56 -03:00 • committed by GitHub
commit 2edbe305f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 1629 additions and 1610 deletions

View file

@ -1,220 +1,227 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [other] example - 3d stencil buffer portal masking * raylib [other] example - 3d stencil buffer portal masking
* *
* Example complexity rating: [★★★☆] 3/4 * Example demonstrates 3D stencil buffer portal masking.
* *
* Example originally created with raylib 6.0, last time updated with raylib 6.0 * Example complexity rating: [★★★☆] 3/4
* *
* Example contributed by PanicTitan (@PanicTitan) and reviewed by Ramon Santamaria (@raysan5) * Example originally created with raylib 6.0, last time updated with raylib 6.0
* *
********************************************************************************************/ * Example contributed by PanicTitan (@PanicTitan) and reviewed by Ramon Santamaria (@raysan5)
*
#include "raylib.h" * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
#include "rlgl.h" * BSD-like license that allows static linking with closed source software
#include <math.h> *
* Copyright (c) 2025 PanicTitan (@PanicTitan)
// Standard OpenGL defines for Stencil Buffer and Face Culling *
#ifndef GL_STENCIL_TEST ********************************************************************************************/
#define GL_STENCIL_TEST 0x0B90
#define GL_STENCIL_BUFFER_BIT 0x00000400 #include "raylib.h"
#define GL_DEPTH_BUFFER_BIT 0x00000100 #include "rlgl.h"
#define GL_CULL_FACE 0x0B44 #include <math.h>
#define GL_BACK 0x0405
#define GL_ALWAYS 0x0207 // Standard OpenGL defines for Stencil Buffer and Face Culling
#define GL_EQUAL 0x0202 #ifndef GL_STENCIL_TEST
#define GL_REPLACE 0x1E01 #define GL_STENCIL_TEST 0x0B90
#define GL_KEEP 0x1E00 #define GL_STENCIL_BUFFER_BIT 0x00000400
#define GL_FALSE 0 #define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_TRUE 1 #define GL_CULL_FACE 0x0B44
#endif #define GL_BACK 0x0405
#define GL_ALWAYS 0x0207
// External OpenGL function declarations #define GL_EQUAL 0x0202
#if defined(__cplusplus) #define GL_REPLACE 0x1E01
extern "C" { #define GL_KEEP 0x1E00
#endif #define GL_FALSE 0
RLAPI void glEnable(unsigned int cap); #define GL_TRUE 1
RLAPI void glDisable(unsigned int cap); #endif
RLAPI void glClear(unsigned int mask);
RLAPI void glStencilFunc(unsigned int func, int ref, unsigned int mask); // External OpenGL function declarations
RLAPI void glStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass); #if defined(__cplusplus)
RLAPI void glColorMask(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha); extern "C" {
RLAPI void glDepthMask(unsigned char flag); #endif
RLAPI void glCullFace(unsigned int mode); RLAPI void glEnable(unsigned int cap);
#if defined(__cplusplus) RLAPI void glDisable(unsigned int cap);
} RLAPI void glClear(unsigned int mask);
#endif RLAPI void glStencilFunc(unsigned int func, int ref, unsigned int mask);
RLAPI void glStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass);
//------------------------------------------------------------------------------------ RLAPI void glColorMask(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha);
// Module Functions Declaration RLAPI void glDepthMask(unsigned char flag);
//------------------------------------------------------------------------------------ RLAPI void glCullFace(unsigned int mode);
static void DrawPortalMaskPlane(float width, float height); #if defined(__cplusplus)
}
//------------------------------------------------------------------------------------ #endif
// Program main entry point
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
int main(void) // Module Functions Declaration
{ //------------------------------------------------------------------------------------
// Initialization static void DrawPortalMaskPlane(float width, float height);
//--------------------------------------------------------------------------------------
const int screenWidth = 1280; //------------------------------------------------------------------------------------
const int screenHeight = 720; // Program main entry point
//------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [rlgl] example - 3d stencil buffer portal"); int main(void)
{
// Camera to navigate the 3D world // Initialization
Camera3D camera = { 0 }; //--------------------------------------------------------------------------------------
camera.position = (Vector3){ 0.0f, 2.5f, 7.0f }; const int screenWidth = 1280;
camera.target = (Vector3){ 0.0f, 1.8f, 0.0f }; const int screenHeight = 720;
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 45.0f; InitWindow(screenWidth, screenHeight, "raylib [rlgl] example - 3d stencil buffer portal");
camera.projection = CAMERA_PERSPECTIVE;
// Camera to navigate the 3D world
DisableCursor(); // Lock cursor for first-person free camera controls Camera3D camera = { 0 };
SetTargetFPS(60); // Set target frame-rate camera.position = (Vector3){ 0.0f, 2.5f, 7.0f };
//-------------------------------------------------------------------------------------- camera.target = (Vector3){ 0.0f, 1.8f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
// Main game loop camera.fovy = 45.0f;
while (!WindowShouldClose()) // Detect window close button or ESC key camera.projection = CAMERA_PERSPECTIVE;
{
// Update DisableCursor(); // Lock cursor for first-person free camera controls
//---------------------------------------------------------------------------------- SetTargetFPS(60); // Set target frame-rate
UpdateCamera(&camera, CAMERA_FREE); //--------------------------------------------------------------------------------------
float time = (float)GetTime();
//---------------------------------------------------------------------------------- // Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
// Draw {
//---------------------------------------------------------------------------------- // Update
BeginDrawing(); //----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_FREE);
ClearBackground((Color){ 15, 18, 26, 255 }); float time = (float)GetTime();
//----------------------------------------------------------------------------------
// 1. Render Real World (Outside Portal)
//------------------------------------------------------------------------------ // Draw
BeginMode3D(camera); //----------------------------------------------------------------------------------
DrawGrid(20, 1.0f); BeginDrawing();
// Real world side pillars ClearBackground((Color){ 15, 18, 26, 255 });
DrawCube((Vector3){ -3.5f, 2.0f, 0.0f }, 0.8f, 4.0f, 0.8f, DARKGRAY);
DrawCubeWires((Vector3){ -3.5f, 2.0f, 0.0f }, 0.8f, 4.0f, 0.8f, ORANGE); // 1. Render Real World (Outside Portal)
//------------------------------------------------------------------------------
DrawCube((Vector3){ 3.5f, 2.0f, 0.0f }, 0.8f, 4.0f, 0.8f, DARKGRAY); BeginMode3D(camera);
DrawCubeWires((Vector3){ 3.5f, 2.0f, 0.0f }, 0.8f, 4.0f, 0.8f, ORANGE); DrawGrid(20, 1.0f);
// Golden archway front frame // Real world side pillars
DrawCubeWires((Vector3){ 0.0f, 2.0f, 0.0f }, 3.2f, 4.2f, 0.2f, GOLD); DrawCube((Vector3){ -3.5f, 2.0f, 0.0f }, 0.8f, 4.0f, 0.8f, DARKGRAY);
DrawCube((Vector3){ 0.0f, 0.05f, 0.0f }, 3.4f, 0.1f, 0.6f, MAROON); DrawCubeWires((Vector3){ -3.5f, 2.0f, 0.0f }, 0.8f, 4.0f, 0.8f, ORANGE);
// Solid back wall for the portal (visible when viewing from behind) DrawCube((Vector3){ 3.5f, 2.0f, 0.0f }, 0.8f, 4.0f, 0.8f, DARKGRAY);
DrawCube((Vector3){ 0.0f, 2.0f, -0.05f }, 3.1f, 4.1f, 0.05f, DARKBLUE); DrawCubeWires((Vector3){ 3.5f, 2.0f, 0.0f }, 0.8f, 4.0f, 0.8f, ORANGE);
EndMode3D();
// Golden archway front frame
// Flush pending render batch before modifying OpenGL stencil state DrawCubeWires((Vector3){ 0.0f, 2.0f, 0.0f }, 3.2f, 4.2f, 0.2f, GOLD);
rlDrawRenderBatchActive(); DrawCube((Vector3){ 0.0f, 0.05f, 0.0f }, 3.4f, 0.1f, 0.6f, MAROON);
// 2. Stencil Mask Pass (Write Portal Doorway to Stencil Buffer) // Solid back wall for the portal (visible when viewing from behind)
//------------------------------------------------------------------------------ DrawCube((Vector3){ 0.0f, 2.0f, -0.05f }, 3.1f, 4.1f, 0.05f, DARKBLUE);
glEnable(GL_STENCIL_TEST); EndMode3D();
glClear(GL_STENCIL_BUFFER_BIT); // Reset stencil mask buffer to 0
// Flush pending render batch before modifying OpenGL stencil state
// Enable backface culling so the back of the portal plane does not register rlDrawRenderBatchActive();
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); // 2. Stencil Mask Pass (Write Portal Doorway to Stencil Buffer)
//------------------------------------------------------------------------------
// Stencil settings: write '1' everywhere mask geometry is drawn glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 0xFF); glClear(GL_STENCIL_BUFFER_BIT); // Reset stencil mask buffer to 0
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
// Enable backface culling so the back of the portal plane does not register
// Disable color and depth writes (mask geometry remains invisible) glEnable(GL_CULL_FACE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); glCullFace(GL_BACK);
glDepthMask(GL_FALSE);
// Stencil settings: write '1' everywhere mask geometry is drawn
BeginMode3D(camera); glStencilFunc(GL_ALWAYS, 1, 0xFF);
// Draw single-sided 2D quad mask plane facing +Z glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
DrawPortalMaskPlane(3.0f, 4.0f);
EndMode3D(); // Disable color and depth writes (mask geometry remains invisible)
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
rlDrawRenderBatchActive(); glDepthMask(GL_FALSE);
// Restore culling state BeginMode3D(camera);
glDisable(GL_CULL_FACE); // Draw single-sided 2D quad mask plane facing +Z
DrawPortalMaskPlane(3.0f, 4.0f);
// 3. Render Secret World (Only where Stencil == 1) EndMode3D();
//------------------------------------------------------------------------------
// Restore color and depth writing rlDrawRenderBatchActive();
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE); // Restore culling state
glDisable(GL_CULL_FACE);
// Only render fragments where stencil buffer is equal to 1
glStencilFunc(GL_EQUAL, 1, 0xFF); // 3. Render Secret World (Only where Stencil == 1)
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); //------------------------------------------------------------------------------
// Restore color and depth writing
// Clear depth buffer in stencil region so inner geometry renders properly glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glClear(GL_DEPTH_BUFFER_BIT); glDepthMask(GL_TRUE);
BeginMode3D(camera); // Only render fragments where stencil buffer is equal to 1
// Secret world neon grid floor glStencilFunc(GL_EQUAL, 1, 0xFF);
DrawGrid(30, 0.8f); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
// Floating pulsing core sphere inside alternate dimension // Clear depth buffer in stencil region so inner geometry renders properly
Vector3 corePos = { 0.0f, 2.0f + sinf(time * 2.5f) * 0.4f, -4.0f }; glClear(GL_DEPTH_BUFFER_BIT);
DrawSphere(corePos, 1.2f, PURPLE);
DrawSphereWires(corePos, 1.25f, 16, 16, MAGENTA); BeginMode3D(camera);
// Secret world neon grid floor
// Orbiting green cubes in alternate dimension DrawGrid(30, 0.8f);
for (int i = 0; i < 4; i++)
{ // Floating pulsing core sphere inside alternate dimension
float angle = time * 1.5f + i * (PI / 2.0f); Vector3 corePos = { 0.0f, 2.0f + sinf(time * 2.5f) * 0.4f, -4.0f };
Vector3 pos = { DrawSphere(corePos, 1.2f, PURPLE);
sinf(angle) * 2.5f, DrawSphereWires(corePos, 1.25f, 16, 16, MAGENTA);
2.0f + cosf(time * 3.0f + i) * 0.5f,
-4.0f + cosf(angle) * 2.5f // Orbiting green cubes in alternate dimension
}; for (int i = 0; i < 4; i++)
DrawCube(pos, 0.5f, 0.5f, 0.5f, LIME); {
DrawCubeWires(pos, 0.52f, 0.52f, 0.52f, DARKGREEN); float angle = time * 1.5f + i * (PI / 2.0f);
} Vector3 pos = {
EndMode3D(); sinf(angle) * 2.5f,
2.0f + cosf(time * 3.0f + i) * 0.5f,
rlDrawRenderBatchActive(); -4.0f + cosf(angle) * 2.5f
};
// 4. Cleanup & 2D HUD Overlay DrawCube(pos, 0.5f, 0.5f, 0.5f, LIME);
//------------------------------------------------------------------------------ DrawCubeWires(pos, 0.52f, 0.52f, 0.52f, DARKGREEN);
glDisable(GL_STENCIL_TEST); }
EndMode3D();
// Overlay HUD
DrawRectangle(15, 15, 420, 85, Fade(BLACK, 0.75f)); rlDrawRenderBatchActive();
DrawRectangleLines(15, 15, 420, 85, GOLD);
DrawText("3D STENCIL PORTAL DEMO", 28, 25, 20, GOLD); // 4. Cleanup & 2D HUD Overlay
DrawText("Look through the golden arch into Dimension B", 28, 52, 14, RAYWHITE); //------------------------------------------------------------------------------
DrawText("Controls: Mouse to look | WASD / Keys to move", 28, 72, 12, GRAY); glDisable(GL_STENCIL_TEST);
EndDrawing(); // Overlay HUD
//---------------------------------------------------------------------------------- DrawRectangle(15, 15, 420, 85, Fade(BLACK, 0.75f));
} DrawRectangleLines(15, 15, 420, 85, GOLD);
DrawText("3D STENCIL PORTAL DEMO", 28, 25, 20, GOLD);
// De-Initialization DrawText("Look through the golden arch into Dimension B", 28, 52, 14, RAYWHITE);
//-------------------------------------------------------------------------------------- DrawText("Controls: Mouse to look | WASD / Keys to move", 28, 72, 12, GRAY);
CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- EndDrawing();
//----------------------------------------------------------------------------------
return 0; }
}
// De-Initialization
//---------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Module Functions Definition CloseWindow(); // Close window and OpenGL context
//---------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Draw a single 1-sided 2D quad plane facing +Z used for stencil masking return 0;
static void DrawPortalMaskPlane(float width, float height) }
{
float hw = width / 2.0f; //----------------------------------------------------------------------------------
// Module Functions Definition
rlBegin(RL_QUADS); //----------------------------------------------------------------------------------
rlNormal3f(0.0f, 0.0f, 1.0f);
// Counter-Clockwise (CCW) vertex order = Front Face // Draw a single 1-sided 2D quad plane facing +Z used for stencil masking
rlVertex3f(-hw, 0.0f, 0.0f); static void DrawPortalMaskPlane(float width, float height)
rlVertex3f( hw, 0.0f, 0.0f); {
rlVertex3f( hw, height, 0.0f); float hw = width / 2.0f;
rlVertex3f(-hw, height, 0.0f);
rlEnd(); rlBegin(RL_QUADS);
rlNormal3f(0.0f, 0.0f, 1.0f);
// Counter-Clockwise (CCW) vertex order = Front Face
rlVertex3f(-hw, 0.0f, 0.0f);
rlVertex3f( hw, 0.0f, 0.0f);
rlVertex3f( hw, height, 0.0f);
rlVertex3f(-hw, height, 0.0f);
rlEnd();
} }

View file

@ -1,311 +1,315 @@
/******************************************************************************************* /*******************************************************************************************
* *
* raylib [shaders] example - forward multi-lighting with bloom * raylib [shaders] example - forward multi-lighting with bloom
* *
* Example complexity rating: [★★★☆] 3/4 * Example demonstrates forward multi-point lighting with bloom post-processing.
* *
* Example originally created with raylib 6.0, last time updated with raylib 6.0 * Example complexity rating: [★★★☆] 3/4
* *
* NOTE: Forward multi-point lighting combined with specular highlights and post-processing * Example originally created with raylib 6.0, last time updated with raylib 6.0
* tone-mapped bloom shader pass using offscreen render textures. *
* * Example contributed by PanicTitan (@PanicTitan) and reviewed by Ramon Santamaria (@raysan5)
* Example contributed by PanicTitan 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
*
#include "raylib.h" * Copyright (c) 2025 PanicTitan (@PanicTitan)
#include "raymath.h" *
********************************************************************************************/
#include <stdlib.h> // Required for: NULL
#include <math.h> // Required for: sinf(), cosf() #include "raylib.h"
#include "raymath.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h> #include <stdlib.h> // Required for: NULL
#endif #include <math.h> // Required for: sinf(), cosf()
#if defined(PLATFORM_DESKTOP) #if defined(PLATFORM_WEB)
#define GLSL_VERSION "#version 330\n" #include <emscripten/emscripten.h>
#else // PLATFORM_ANDROID, PLATFORM_WEB #endif
#define GLSL_VERSION "#version 300 es\nprecision mediump float;\n"
#endif #if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION "#version 330\n"
//-------------------------------------------------------------------------------------- #else // PLATFORM_ANDROID, PLATFORM_WEB
// Global Definitions #define GLSL_VERSION "#version 300 es\nprecision mediump float;\n"
//-------------------------------------------------------------------------------------- #endif
#define MAX_LIGHTS 8
//--------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------- // Global Definitions
// Embedded Shader Sources //--------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------- #define MAX_LIGHTS 8
// Forward Vertex Shader //--------------------------------------------------------------------------------------
static const char *multiLightVS = GLSL_VERSION // Embedded Shader Sources
"in vec3 vertexPosition;\n" //--------------------------------------------------------------------------------------
"in vec2 vertexTexCoord;\n"
"in vec3 vertexNormal;\n" // Forward Vertex Shader
"\n" static const char *multiLightVS = GLSL_VERSION
"out vec3 fragPosition;\n" "in vec3 vertexPosition;\n"
"out vec3 fragNormal;\n" "in vec2 vertexTexCoord;\n"
"out vec2 fragTexCoord;\n" "in vec3 vertexNormal;\n"
"\n" "\n"
"uniform mat4 mvp;\n" "out vec3 fragPosition;\n"
"uniform mat4 matModel;\n" "out vec3 fragNormal;\n"
"\n" "out vec2 fragTexCoord;\n"
"void main() {\n" "\n"
" fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));\n" "uniform mat4 mvp;\n"
" fragNormal = normalize(vec3(matModel * vec4(vertexNormal, 0.0)));\n" "uniform mat4 matModel;\n"
" fragTexCoord = vertexTexCoord;\n" "\n"
" gl_Position = mvp * vec4(vertexPosition, 1.0);\n" "void main() {\n"
"}\n"; " fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));\n"
" fragNormal = normalize(vec3(matModel * vec4(vertexNormal, 0.0)));\n"
// Forward Fragment Shader (Point Light Falloff & Blinn-Phong Specular) " fragTexCoord = vertexTexCoord;\n"
static const char *multiLightFS = GLSL_VERSION " gl_Position = mvp * vec4(vertexPosition, 1.0);\n"
"in vec3 fragPosition;\n" "}\n";
"in vec3 fragNormal;\n"
"in vec2 fragTexCoord;\n" // Forward Fragment Shader (Point Light Falloff & Blinn-Phong Specular)
"\n" static const char *multiLightFS = GLSL_VERSION
"out vec4 finalColor;\n" "in vec3 fragPosition;\n"
"\n" "in vec3 fragNormal;\n"
"uniform sampler2D texture0;\n" "in vec2 fragTexCoord;\n"
"uniform vec3 viewPos;\n" "\n"
"uniform vec3 lightPositions[8];\n" "out vec4 finalColor;\n"
"uniform vec3 lightColors[8];\n" "\n"
"\n" "uniform sampler2D texture0;\n"
"void main() {\n" "uniform vec3 viewPos;\n"
" vec4 texColor = texture(texture0, fragTexCoord);\n" "uniform vec3 lightPositions[8];\n"
" vec3 norm = normalize(fragNormal);\n" "uniform vec3 lightColors[8];\n"
" vec3 viewDir = normalize(viewPos - fragPosition);\n" "\n"
"\n" "void main() {\n"
" // Dark ambient baseline\n" " vec4 texColor = texture(texture0, fragTexCoord);\n"
" vec3 ambient = 0.05 * texColor.rgb;\n" " vec3 norm = normalize(fragNormal);\n"
" vec3 totalDiffuse = vec3(0.0);\n" " vec3 viewDir = normalize(viewPos - fragPosition);\n"
" vec3 totalSpecular = vec3(0.0);\n" "\n"
"\n" " // Dark ambient baseline\n"
" for (int i = 0; i < 8; i++) {\n" " vec3 ambient = 0.05 * texColor.rgb;\n"
" vec3 lightDir = normalize(lightPositions[i] - fragPosition);\n" " vec3 totalDiffuse = vec3(0.0);\n"
" float dist = length(lightPositions[i] - fragPosition);\n" " vec3 totalSpecular = vec3(0.0);\n"
" \n" "\n"
" // Attenuation calculation\n" " for (int i = 0; i < 8; i++) {\n"
" float attenuation = 1.0 / (1.0 + 0.8 * dist + 0.4 * dist * dist);\n" " vec3 lightDir = normalize(lightPositions[i] - fragPosition);\n"
"\n" " float dist = length(lightPositions[i] - fragPosition);\n"
" // Diffuse Shading\n" " \n"
" float diff = max(dot(norm, lightDir), 0.0);\n" " // Attenuation calculation\n"
" totalDiffuse += diff * lightColors[i] * attenuation * 1.2;\n" " float attenuation = 1.0 / (1.0 + 0.8 * dist + 0.4 * dist * dist);\n"
"\n" "\n"
" // Blinn-Phong Specular\n" " // Diffuse Shading\n"
" vec3 halfwayDir = normalize(lightDir + viewDir);\n" " float diff = max(dot(norm, lightDir), 0.0);\n"
" float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0);\n" " totalDiffuse += diff * lightColors[i] * attenuation * 1.2;\n"
" totalSpecular += spec * lightColors[i] * attenuation * 0.8;\n" "\n"
" }\n" " // Blinn-Phong Specular\n"
"\n" " vec3 halfwayDir = normalize(lightDir + viewDir);\n"
" vec3 result = ambient + (totalDiffuse * texColor.rgb) + totalSpecular;\n" " float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0);\n"
" finalColor = vec4(result, texColor.a);\n" " totalSpecular += spec * lightColors[i] * attenuation * 0.8;\n"
"}\n"; " }\n"
"\n"
// Tone-Mapped Kawase Bloom Fragment Shader " vec3 result = ambient + (totalDiffuse * texColor.rgb) + totalSpecular;\n"
static const char *bloomFS = GLSL_VERSION " finalColor = vec4(result, texColor.a);\n"
"in vec2 fragTexCoord;\n" "}\n";
"out vec4 finalColor;\n"
"\n" // Tone-Mapped Kawase Bloom Fragment Shader
"uniform sampler2D texture0;\n" static const char *bloomFS = GLSL_VERSION
"\n" "in vec2 fragTexCoord;\n"
"void main() {\n" "out vec4 finalColor;\n"
" vec2 uv = fragTexCoord;\n" "\n"
" vec4 color = texture(texture0, uv);\n" "uniform sampler2D texture0;\n"
"\n" "\n"
" // High-pass threshold check (pixels brighter than 0.75)\n" "void main() {\n"
" vec4 bloom = vec4(0.0);\n" " vec2 uv = fragTexCoord;\n"
" vec2 texel = vec2(1.0 / 1280.0, 1.0 / 720.0) * 2.5;\n" " vec4 color = texture(texture0, uv);\n"
"\n" "\n"
" for (int x = -2; x <= 2; x++) {\n" " // High-pass threshold check (pixels brighter than 0.75)\n"
" for (int y = -2; y <= 2; y++) {\n" " vec4 bloom = vec4(0.0);\n"
" vec4 smp = texture(texture0, uv + vec2(x, y) * texel);\n" " vec2 texel = vec2(1.0 / 1280.0, 1.0 / 720.0) * 2.5;\n"
" float brightness = max(smp.r, max(smp.g, smp.b));\n" "\n"
" if (brightness > 0.75) bloom += smp;\n" " for (int x = -2; x <= 2; x++) {\n"
" }\n" " for (int y = -2; y <= 2; y++) {\n"
" }\n" " vec4 smp = texture(texture0, uv + vec2(x, y) * texel);\n"
" bloom /= 25.0;\n" " float brightness = max(smp.r, max(smp.g, smp.b));\n"
"\n" " if (brightness > 0.75) bloom += smp;\n"
" // Combine original with soft bloom glow\n" " }\n"
" vec3 HDR = color.rgb + bloom.rgb * 1.2;\n" " }\n"
"\n" " bloom /= 25.0;\n"
" // Reinhard Tone Mapping\n" "\n"
" vec3 LDR = HDR / (HDR + vec3(1.0));\n" " // Combine original with soft bloom glow\n"
"\n" " vec3 HDR = color.rgb + bloom.rgb * 1.2;\n"
" finalColor = vec4(LDR, color.a);\n" "\n"
"}\n"; " // Reinhard Tone Mapping\n"
" vec3 LDR = HDR / (HDR + vec3(1.0));\n"
//-------------------------------------------------------------------------------------- "\n"
// Types and Structures Definition " finalColor = vec4(LDR, color.a);\n"
//-------------------------------------------------------------------------------------- "}\n";
typedef struct AppContext {
Camera camera; // 3D scene camera //--------------------------------------------------------------------------------------
RenderTexture2D fbo; // Offscreen render target // Types and Structures Definition
Shader lightShader; // Multi-light forward shader //--------------------------------------------------------------------------------------
Shader bloomShader; // Bloom post-processing shader typedef struct AppContext {
Model cubeModel; // Center cube mesh model Camera camera; // 3D scene camera
Model floorModel; // Ground plane mesh model RenderTexture2D fbo; // Offscreen render target
Shader lightShader; // Multi-light forward shader
int lightPosLoc; // Uniform location: light positions Shader bloomShader; // Bloom post-processing shader
int lightColLoc; // Uniform location: light colors Model cubeModel; // Center cube mesh model
int viewPosLoc; // Uniform location: view position Model floorModel; // Ground plane mesh model
Vector3 lightPositions[MAX_LIGHTS]; // Array of point light positions int lightPosLoc; // Uniform location: light positions
Vector3 lightColors[MAX_LIGHTS]; // Array of point light RGB colors int lightColLoc; // Uniform location: light colors
} AppContext; int viewPosLoc; // Uniform location: view position
//-------------------------------------------------------------------------------------- Vector3 lightPositions[MAX_LIGHTS]; // Array of point light positions
// Module Global Variables Vector3 lightColors[MAX_LIGHTS]; // Array of point light RGB colors
//-------------------------------------------------------------------------------------- } AppContext;
static AppContext g_App = { 0 };
//--------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------- // Module Global Variables
// Module Functions Declaration //--------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------- static AppContext g_App = { 0 };
void UpdateDrawFrame(void); // Main frame loop (web & desktop)
//--------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------ // Module Functions Declaration
// Program main entry point //--------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------ void UpdateDrawFrame(void); // Main frame loop (web & desktop)
int main(void)
{ //------------------------------------------------------------------------------------
// Initialization // Program main entry point
//-------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------
const int screenWidth = 1280; int main(void)
const int screenHeight = 720; {
// Initialization
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT); //--------------------------------------------------------------------------------------
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - forward multi-lighting bloom"); const int screenWidth = 1280;
const int screenHeight = 720;
// Camera setup
g_App.camera.position = (Vector3){ 0.0f, 5.0f, 9.0f }; SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT);
g_App.camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; InitWindow(screenWidth, screenHeight, "raylib [shaders] example - forward multi-lighting bloom");
g_App.camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
g_App.camera.fovy = 45.0f; // Camera setup
g_App.camera.projection = CAMERA_PERSPECTIVE; g_App.camera.position = (Vector3){ 0.0f, 5.0f, 9.0f };
g_App.camera.target = (Vector3){ 0.0f, 0.5f, 0.0f };
// Load render target for multi-pass bloom g_App.camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
g_App.fbo = LoadRenderTexture(screenWidth, screenHeight); g_App.camera.fovy = 45.0f;
g_App.camera.projection = CAMERA_PERSPECTIVE;
// Load custom shaders from memory strings
g_App.lightShader = LoadShaderFromMemory(multiLightVS, multiLightFS); // Load render target for multi-pass bloom
g_App.bloomShader = LoadShaderFromMemory(NULL, bloomFS); g_App.fbo = LoadRenderTexture(screenWidth, screenHeight);
// Create models and attach custom lighting shader // Load custom shaders from memory strings
g_App.cubeModel = LoadModelFromMesh(GenMeshCube(2.0f, 2.0f, 2.0f)); g_App.lightShader = LoadShaderFromMemory(multiLightVS, multiLightFS);
g_App.floorModel = LoadModelFromMesh(GenMeshPlane(14.0f, 14.0f, 1, 1)); g_App.bloomShader = LoadShaderFromMemory(NULL, bloomFS);
g_App.cubeModel.materials[0].shader = g_App.lightShader;
g_App.floorModel.materials[0].shader = g_App.lightShader; // Create models and attach custom lighting shader
g_App.cubeModel = LoadModelFromMesh(GenMeshCube(2.0f, 2.0f, 2.0f));
// Fetch shader uniform locations g_App.floorModel = LoadModelFromMesh(GenMeshPlane(14.0f, 14.0f, 1, 1));
g_App.lightPosLoc = GetShaderLocation(g_App.lightShader, "lightPositions"); g_App.cubeModel.materials[0].shader = g_App.lightShader;
g_App.lightColLoc = GetShaderLocation(g_App.lightShader, "lightColors"); g_App.floorModel.materials[0].shader = g_App.lightShader;
g_App.viewPosLoc = GetShaderLocation(g_App.lightShader, "viewPos");
// Fetch shader uniform locations
// Initialize 8 vivid light colors g_App.lightPosLoc = GetShaderLocation(g_App.lightShader, "lightPositions");
g_App.lightColors[0] = (Vector3){ 1.0f, 0.2f, 0.2f }; // Red g_App.lightColLoc = GetShaderLocation(g_App.lightShader, "lightColors");
g_App.lightColors[1] = (Vector3){ 0.2f, 1.0f, 0.3f }; // Green g_App.viewPosLoc = GetShaderLocation(g_App.lightShader, "viewPos");
g_App.lightColors[2] = (Vector3){ 0.2f, 0.5f, 1.0f }; // Blue
g_App.lightColors[3] = (Vector3){ 1.0f, 0.8f, 0.1f }; // Yellow // Initialize 8 vivid light colors
g_App.lightColors[4] = (Vector3){ 1.0f, 0.1f, 0.8f }; // Magenta g_App.lightColors[0] = (Vector3){ 1.0f, 0.2f, 0.2f }; // Red
g_App.lightColors[5] = (Vector3){ 0.1f, 1.0f, 1.0f }; // Cyan g_App.lightColors[1] = (Vector3){ 0.2f, 1.0f, 0.3f }; // Green
g_App.lightColors[6] = (Vector3){ 1.0f, 0.4f, 0.1f }; // Orange g_App.lightColors[2] = (Vector3){ 0.2f, 0.5f, 1.0f }; // Blue
g_App.lightColors[7] = (Vector3){ 0.7f, 0.2f, 1.0f }; // Purple g_App.lightColors[3] = (Vector3){ 1.0f, 0.8f, 0.1f }; // Yellow
g_App.lightColors[4] = (Vector3){ 1.0f, 0.1f, 0.8f }; // Magenta
SetShaderValueV(g_App.lightShader, g_App.lightColLoc, g_App.lightColors, SHADER_UNIFORM_VEC3, MAX_LIGHTS); g_App.lightColors[5] = (Vector3){ 0.1f, 1.0f, 1.0f }; // Cyan
g_App.lightColors[6] = (Vector3){ 1.0f, 0.4f, 0.1f }; // Orange
SetTargetFPS(60); g_App.lightColors[7] = (Vector3){ 0.7f, 0.2f, 1.0f }; // Purple
//--------------------------------------------------------------------------------------
SetShaderValueV(g_App.lightShader, g_App.lightColLoc, g_App.lightColors, SHADER_UNIFORM_VEC3, MAX_LIGHTS);
// Main game loop
#if defined(PLATFORM_WEB) SetTargetFPS(60);
emscripten_set_main_loop(UpdateDrawFrame, 0, 1); //--------------------------------------------------------------------------------------
#else
while (!WindowShouldClose()) // Main game loop
{ #if defined(PLATFORM_WEB)
UpdateDrawFrame(); emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
} #else
while (!WindowShouldClose())
// De-Initialization {
//-------------------------------------------------------------------------------------- UpdateDrawFrame();
UnloadModel(g_App.cubeModel); }
UnloadModel(g_App.floorModel);
UnloadShader(g_App.lightShader); // De-Initialization
UnloadShader(g_App.bloomShader); //--------------------------------------------------------------------------------------
UnloadRenderTexture(g_App.fbo); UnloadModel(g_App.cubeModel);
CloseWindow(); UnloadModel(g_App.floorModel);
//-------------------------------------------------------------------------------------- UnloadShader(g_App.lightShader);
#endif UnloadShader(g_App.bloomShader);
UnloadRenderTexture(g_App.fbo);
return 0; CloseWindow();
} //--------------------------------------------------------------------------------------
#endif
//--------------------------------------------------------------------------------------
// Frame Update and Render Logic return 0;
//-------------------------------------------------------------------------------------- }
void UpdateDrawFrame(void)
{ //--------------------------------------------------------------------------------------
// Update // Frame Update and Render Logic
//---------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UpdateCamera(&g_App.camera, CAMERA_ORBITAL); void UpdateDrawFrame(void)
float time = (float)GetTime(); {
// Update
// Orbital path calculation for point lights //----------------------------------------------------------------------------------
for (int i = 0; i < MAX_LIGHTS; i++) UpdateCamera(&g_App.camera, CAMERA_ORBITAL);
{ float time = (float)GetTime();
float angle = (i / (float)MAX_LIGHTS) * 2.0f * PI + time * 0.6f;
float radius = 4.2f + sinf(time * 1.2f + i) * 0.4f; // Orbital path calculation for point lights
float height = 1.0f + sinf(time * 1.8f + i) * 0.6f; for (int i = 0; i < MAX_LIGHTS; i++)
g_App.lightPositions[i] = (Vector3){ sinf(angle) * radius, height, cosf(angle) * radius }; {
} float angle = (i / (float)MAX_LIGHTS) * 2.0f * PI + time * 0.6f;
float radius = 4.2f + sinf(time * 1.2f + i) * 0.4f;
SetShaderValueV(g_App.lightShader, g_App.lightPosLoc, g_App.lightPositions, SHADER_UNIFORM_VEC3, MAX_LIGHTS); float height = 1.0f + sinf(time * 1.8f + i) * 0.6f;
SetShaderValue(g_App.lightShader, g_App.viewPosLoc, &g_App.camera.position, SHADER_UNIFORM_VEC3); g_App.lightPositions[i] = (Vector3){ sinf(angle) * radius, height, cosf(angle) * radius };
//---------------------------------------------------------------------------------- }
// Draw SetShaderValueV(g_App.lightShader, g_App.lightPosLoc, g_App.lightPositions, SHADER_UNIFORM_VEC3, MAX_LIGHTS);
//---------------------------------------------------------------------------------- SetShaderValue(g_App.lightShader, g_App.viewPosLoc, &g_App.camera.position, SHADER_UNIFORM_VEC3);
// PASS 1: RENDER SCENE TO OFFSCREEN FBO //----------------------------------------------------------------------------------
BeginTextureMode(g_App.fbo);
// Draw
ClearBackground((Color){ 12, 12, 18, 255 }); //----------------------------------------------------------------------------------
// PASS 1: RENDER SCENE TO OFFSCREEN FBO
BeginMode3D(g_App.camera); BeginTextureMode(g_App.fbo);
DrawModel(g_App.cubeModel, (Vector3){ 0.0f, 1.0f, 0.0f }, 1.0f, GRAY); ClearBackground((Color){ 12, 12, 18, 255 });
DrawModel(g_App.floorModel, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, DARKGRAY);
DrawGrid(10, 1.0f); BeginMode3D(g_App.camera);
// Draw glowing orb bulbs at light origins DrawModel(g_App.cubeModel, (Vector3){ 0.0f, 1.0f, 0.0f }, 1.0f, GRAY);
for (int i = 0; i < MAX_LIGHTS; i++) DrawModel(g_App.floorModel, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, DARKGRAY);
{ DrawGrid(10, 1.0f);
Color bulbColor = (Color){
(unsigned char)(g_App.lightColors[i].x * 255), // Draw glowing orb bulbs at light origins
(unsigned char)(g_App.lightColors[i].y * 255), for (int i = 0; i < MAX_LIGHTS; i++)
(unsigned char)(g_App.lightColors[i].z * 255), {
255 Color bulbColor = (Color){
}; (unsigned char)(g_App.lightColors[i].x * 255),
DrawSphere(g_App.lightPositions[i], 0.15f, bulbColor); (unsigned char)(g_App.lightColors[i].y * 255),
} (unsigned char)(g_App.lightColors[i].z * 255),
255
EndMode3D(); };
DrawSphere(g_App.lightPositions[i], 0.15f, bulbColor);
EndTextureMode(); }
// PASS 2: PRESENT WITH TONE-MAPPED BLOOM EndMode3D();
BeginDrawing();
EndTextureMode();
ClearBackground(BLACK);
// PASS 2: PRESENT WITH TONE-MAPPED BLOOM
Rectangle srcRec = (Rectangle){ 0, 0, (float)g_App.fbo.texture.width, (float)-g_App.fbo.texture.height }; BeginDrawing();
BeginShaderMode(g_App.bloomShader); ClearBackground(BLACK);
DrawTextureRec(g_App.fbo.texture, srcRec, (Vector2){ 0, 0 }, WHITE);
EndShaderMode(); Rectangle srcRec = (Rectangle){ 0, 0, (float)g_App.fbo.texture.width, (float)-g_App.fbo.texture.height };
DrawText("BALANCED MULTI-LIGHT + REINHARD TONE MAPPED BLOOM", 20, 20, 20, GREEN); BeginShaderMode(g_App.bloomShader);
DrawFPS(10, 10); DrawTextureRec(g_App.fbo.texture, srcRec, (Vector2){ 0, 0 }, WHITE);
EndShaderMode();
EndDrawing();
//---------------------------------------------------------------------------------- DrawText("BALANCED MULTI-LIGHT + REINHARD TONE MAPPED BLOOM", 20, 20, 20, GREEN);
} DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff