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
*
* Example complexity rating: [★★★☆] 3/4
*
* 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"
#include "rlgl.h"
#include <math.h>
// Standard OpenGL defines for Stencil Buffer and Face Culling
#ifndef GL_STENCIL_TEST
#define GL_STENCIL_TEST 0x0B90
#define GL_STENCIL_BUFFER_BIT 0x00000400
#define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_CULL_FACE 0x0B44
#define GL_BACK 0x0405
#define GL_ALWAYS 0x0207
#define GL_EQUAL 0x0202
#define GL_REPLACE 0x1E01
#define GL_KEEP 0x1E00
#define GL_FALSE 0
#define GL_TRUE 1
#endif
// External OpenGL function declarations
#if defined(__cplusplus)
extern "C" {
#endif
RLAPI void glEnable(unsigned int cap);
RLAPI void glDisable(unsigned int cap);
RLAPI void glClear(unsigned int mask);
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);
RLAPI void glDepthMask(unsigned char flag);
RLAPI void glCullFace(unsigned int mode);
#if defined(__cplusplus)
}
#endif
//------------------------------------------------------------------------------------
// Module Functions Declaration
//------------------------------------------------------------------------------------
static void DrawPortalMaskPlane(float width, float height);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 1280;
const int screenHeight = 720;
InitWindow(screenWidth, screenHeight, "raylib [rlgl] example - 3d stencil buffer portal");
// Camera to navigate the 3D world
Camera3D camera = { 0 };
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 };
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
DisableCursor(); // Lock cursor for first-person free camera controls
SetTargetFPS(60); // Set target frame-rate
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_FREE);
float time = (float)GetTime();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground((Color){ 15, 18, 26, 255 });
// 1. Render Real World (Outside Portal)
//------------------------------------------------------------------------------
BeginMode3D(camera);
DrawGrid(20, 1.0f);
// Real world side pillars
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);
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);
// Golden archway front frame
DrawCubeWires((Vector3){ 0.0f, 2.0f, 0.0f }, 3.2f, 4.2f, 0.2f, GOLD);
DrawCube((Vector3){ 0.0f, 0.05f, 0.0f }, 3.4f, 0.1f, 0.6f, MAROON);
// 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);
EndMode3D();
// Flush pending render batch before modifying OpenGL stencil state
rlDrawRenderBatchActive();
// 2. Stencil Mask Pass (Write Portal Doorway to Stencil Buffer)
//------------------------------------------------------------------------------
glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT); // Reset stencil mask buffer to 0
// Enable backface culling so the back of the portal plane does not register
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// Stencil settings: write '1' everywhere mask geometry is drawn
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
// Disable color and depth writes (mask geometry remains invisible)
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
BeginMode3D(camera);
// Draw single-sided 2D quad mask plane facing +Z
DrawPortalMaskPlane(3.0f, 4.0f);
EndMode3D();
rlDrawRenderBatchActive();
// Restore culling state
glDisable(GL_CULL_FACE);
// 3. Render Secret World (Only where Stencil == 1)
//------------------------------------------------------------------------------
// Restore color and depth writing
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
// Only render fragments where stencil buffer is equal to 1
glStencilFunc(GL_EQUAL, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
// Clear depth buffer in stencil region so inner geometry renders properly
glClear(GL_DEPTH_BUFFER_BIT);
BeginMode3D(camera);
// Secret world neon grid floor
DrawGrid(30, 0.8f);
// Floating pulsing core sphere inside alternate dimension
Vector3 corePos = { 0.0f, 2.0f + sinf(time * 2.5f) * 0.4f, -4.0f };
DrawSphere(corePos, 1.2f, PURPLE);
DrawSphereWires(corePos, 1.25f, 16, 16, MAGENTA);
// Orbiting green cubes in alternate dimension
for (int i = 0; i < 4; i++)
{
float angle = time * 1.5f + i * (PI / 2.0f);
Vector3 pos = {
sinf(angle) * 2.5f,
2.0f + cosf(time * 3.0f + i) * 0.5f,
-4.0f + cosf(angle) * 2.5f
};
DrawCube(pos, 0.5f, 0.5f, 0.5f, LIME);
DrawCubeWires(pos, 0.52f, 0.52f, 0.52f, DARKGREEN);
}
EndMode3D();
rlDrawRenderBatchActive();
// 4. Cleanup & 2D HUD Overlay
//------------------------------------------------------------------------------
glDisable(GL_STENCIL_TEST);
// 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);
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);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
// Draw a single 1-sided 2D quad plane facing +Z used for stencil masking
static void DrawPortalMaskPlane(float width, float height)
{
float hw = width / 2.0f;
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();
/*******************************************************************************************
*
* raylib [other] example - 3d stencil buffer portal masking
*
* Example demonstrates 3D stencil buffer portal masking.
*
* Example complexity rating: [★★★☆] 3/4
*
* 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)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2025 PanicTitan (@PanicTitan)
*
********************************************************************************************/
#include "raylib.h"
#include "rlgl.h"
#include <math.h>
// Standard OpenGL defines for Stencil Buffer and Face Culling
#ifndef GL_STENCIL_TEST
#define GL_STENCIL_TEST 0x0B90
#define GL_STENCIL_BUFFER_BIT 0x00000400
#define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_CULL_FACE 0x0B44
#define GL_BACK 0x0405
#define GL_ALWAYS 0x0207
#define GL_EQUAL 0x0202
#define GL_REPLACE 0x1E01
#define GL_KEEP 0x1E00
#define GL_FALSE 0
#define GL_TRUE 1
#endif
// External OpenGL function declarations
#if defined(__cplusplus)
extern "C" {
#endif
RLAPI void glEnable(unsigned int cap);
RLAPI void glDisable(unsigned int cap);
RLAPI void glClear(unsigned int mask);
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);
RLAPI void glDepthMask(unsigned char flag);
RLAPI void glCullFace(unsigned int mode);
#if defined(__cplusplus)
}
#endif
//------------------------------------------------------------------------------------
// Module Functions Declaration
//------------------------------------------------------------------------------------
static void DrawPortalMaskPlane(float width, float height);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 1280;
const int screenHeight = 720;
InitWindow(screenWidth, screenHeight, "raylib [rlgl] example - 3d stencil buffer portal");
// Camera to navigate the 3D world
Camera3D camera = { 0 };
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 };
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
DisableCursor(); // Lock cursor for first-person free camera controls
SetTargetFPS(60); // Set target frame-rate
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_FREE);
float time = (float)GetTime();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground((Color){ 15, 18, 26, 255 });
// 1. Render Real World (Outside Portal)
//------------------------------------------------------------------------------
BeginMode3D(camera);
DrawGrid(20, 1.0f);
// Real world side pillars
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);
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);
// Golden archway front frame
DrawCubeWires((Vector3){ 0.0f, 2.0f, 0.0f }, 3.2f, 4.2f, 0.2f, GOLD);
DrawCube((Vector3){ 0.0f, 0.05f, 0.0f }, 3.4f, 0.1f, 0.6f, MAROON);
// 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);
EndMode3D();
// Flush pending render batch before modifying OpenGL stencil state
rlDrawRenderBatchActive();
// 2. Stencil Mask Pass (Write Portal Doorway to Stencil Buffer)
//------------------------------------------------------------------------------
glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT); // Reset stencil mask buffer to 0
// Enable backface culling so the back of the portal plane does not register
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// Stencil settings: write '1' everywhere mask geometry is drawn
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
// Disable color and depth writes (mask geometry remains invisible)
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
BeginMode3D(camera);
// Draw single-sided 2D quad mask plane facing +Z
DrawPortalMaskPlane(3.0f, 4.0f);
EndMode3D();
rlDrawRenderBatchActive();
// Restore culling state
glDisable(GL_CULL_FACE);
// 3. Render Secret World (Only where Stencil == 1)
//------------------------------------------------------------------------------
// Restore color and depth writing
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
// Only render fragments where stencil buffer is equal to 1
glStencilFunc(GL_EQUAL, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
// Clear depth buffer in stencil region so inner geometry renders properly
glClear(GL_DEPTH_BUFFER_BIT);
BeginMode3D(camera);
// Secret world neon grid floor
DrawGrid(30, 0.8f);
// Floating pulsing core sphere inside alternate dimension
Vector3 corePos = { 0.0f, 2.0f + sinf(time * 2.5f) * 0.4f, -4.0f };
DrawSphere(corePos, 1.2f, PURPLE);
DrawSphereWires(corePos, 1.25f, 16, 16, MAGENTA);
// Orbiting green cubes in alternate dimension
for (int i = 0; i < 4; i++)
{
float angle = time * 1.5f + i * (PI / 2.0f);
Vector3 pos = {
sinf(angle) * 2.5f,
2.0f + cosf(time * 3.0f + i) * 0.5f,
-4.0f + cosf(angle) * 2.5f
};
DrawCube(pos, 0.5f, 0.5f, 0.5f, LIME);
DrawCubeWires(pos, 0.52f, 0.52f, 0.52f, DARKGREEN);
}
EndMode3D();
rlDrawRenderBatchActive();
// 4. Cleanup & 2D HUD Overlay
//------------------------------------------------------------------------------
glDisable(GL_STENCIL_TEST);
// 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);
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);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
// Draw a single 1-sided 2D quad plane facing +Z used for stencil masking
static void DrawPortalMaskPlane(float width, float height)
{
float hw = width / 2.0f;
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
*
* Example complexity rating: [★★★☆] 3/4
*
* Example originally created with raylib 6.0, last time updated with raylib 6.0
*
* NOTE: Forward multi-point lighting combined with specular highlights and post-processing
* tone-mapped bloom shader pass using offscreen render textures.
*
* Example contributed by PanicTitan and reviewed by Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#include <stdlib.h> // Required for: NULL
#include <math.h> // Required for: sinf(), cosf()
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
#if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION "#version 330\n"
#else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION "#version 300 es\nprecision mediump float;\n"
#endif
//--------------------------------------------------------------------------------------
// Global Definitions
//--------------------------------------------------------------------------------------
#define MAX_LIGHTS 8
//--------------------------------------------------------------------------------------
// Embedded Shader Sources
//--------------------------------------------------------------------------------------
// Forward Vertex Shader
static const char *multiLightVS = GLSL_VERSION
"in vec3 vertexPosition;\n"
"in vec2 vertexTexCoord;\n"
"in vec3 vertexNormal;\n"
"\n"
"out vec3 fragPosition;\n"
"out vec3 fragNormal;\n"
"out vec2 fragTexCoord;\n"
"\n"
"uniform mat4 mvp;\n"
"uniform mat4 matModel;\n"
"\n"
"void main() {\n"
" fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));\n"
" fragNormal = normalize(vec3(matModel * vec4(vertexNormal, 0.0)));\n"
" fragTexCoord = vertexTexCoord;\n"
" gl_Position = mvp * vec4(vertexPosition, 1.0);\n"
"}\n";
// Forward Fragment Shader (Point Light Falloff & Blinn-Phong Specular)
static const char *multiLightFS = GLSL_VERSION
"in vec3 fragPosition;\n"
"in vec3 fragNormal;\n"
"in vec2 fragTexCoord;\n"
"\n"
"out vec4 finalColor;\n"
"\n"
"uniform sampler2D texture0;\n"
"uniform vec3 viewPos;\n"
"uniform vec3 lightPositions[8];\n"
"uniform vec3 lightColors[8];\n"
"\n"
"void main() {\n"
" vec4 texColor = texture(texture0, fragTexCoord);\n"
" vec3 norm = normalize(fragNormal);\n"
" vec3 viewDir = normalize(viewPos - fragPosition);\n"
"\n"
" // Dark ambient baseline\n"
" vec3 ambient = 0.05 * texColor.rgb;\n"
" vec3 totalDiffuse = vec3(0.0);\n"
" vec3 totalSpecular = vec3(0.0);\n"
"\n"
" for (int i = 0; i < 8; i++) {\n"
" vec3 lightDir = normalize(lightPositions[i] - fragPosition);\n"
" float dist = length(lightPositions[i] - fragPosition);\n"
" \n"
" // Attenuation calculation\n"
" float attenuation = 1.0 / (1.0 + 0.8 * dist + 0.4 * dist * dist);\n"
"\n"
" // Diffuse Shading\n"
" float diff = max(dot(norm, lightDir), 0.0);\n"
" totalDiffuse += diff * lightColors[i] * attenuation * 1.2;\n"
"\n"
" // Blinn-Phong Specular\n"
" vec3 halfwayDir = normalize(lightDir + viewDir);\n"
" float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0);\n"
" totalSpecular += spec * lightColors[i] * attenuation * 0.8;\n"
" }\n"
"\n"
" vec3 result = ambient + (totalDiffuse * texColor.rgb) + totalSpecular;\n"
" finalColor = vec4(result, texColor.a);\n"
"}\n";
// Tone-Mapped Kawase Bloom Fragment Shader
static const char *bloomFS = GLSL_VERSION
"in vec2 fragTexCoord;\n"
"out vec4 finalColor;\n"
"\n"
"uniform sampler2D texture0;\n"
"\n"
"void main() {\n"
" vec2 uv = fragTexCoord;\n"
" vec4 color = texture(texture0, uv);\n"
"\n"
" // High-pass threshold check (pixels brighter than 0.75)\n"
" vec4 bloom = vec4(0.0);\n"
" vec2 texel = vec2(1.0 / 1280.0, 1.0 / 720.0) * 2.5;\n"
"\n"
" for (int x = -2; x <= 2; x++) {\n"
" for (int y = -2; y <= 2; y++) {\n"
" vec4 smp = texture(texture0, uv + vec2(x, y) * texel);\n"
" float brightness = max(smp.r, max(smp.g, smp.b));\n"
" if (brightness > 0.75) bloom += smp;\n"
" }\n"
" }\n"
" bloom /= 25.0;\n"
"\n"
" // Combine original with soft bloom glow\n"
" vec3 HDR = color.rgb + bloom.rgb * 1.2;\n"
"\n"
" // Reinhard Tone Mapping\n"
" vec3 LDR = HDR / (HDR + vec3(1.0));\n"
"\n"
" finalColor = vec4(LDR, color.a);\n"
"}\n";
//--------------------------------------------------------------------------------------
// Types and Structures Definition
//--------------------------------------------------------------------------------------
typedef struct AppContext {
Camera camera; // 3D scene camera
RenderTexture2D fbo; // Offscreen render target
Shader lightShader; // Multi-light forward shader
Shader bloomShader; // Bloom post-processing shader
Model cubeModel; // Center cube mesh model
Model floorModel; // Ground plane mesh model
int lightPosLoc; // Uniform location: light positions
int lightColLoc; // Uniform location: light colors
int viewPosLoc; // Uniform location: view position
Vector3 lightPositions[MAX_LIGHTS]; // Array of point light positions
Vector3 lightColors[MAX_LIGHTS]; // Array of point light RGB colors
} AppContext;
//--------------------------------------------------------------------------------------
// Module Global Variables
//--------------------------------------------------------------------------------------
static AppContext g_App = { 0 };
//--------------------------------------------------------------------------------------
// Module Functions Declaration
//--------------------------------------------------------------------------------------
void UpdateDrawFrame(void); // Main frame loop (web & desktop)
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 1280;
const int screenHeight = 720;
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT);
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - forward multi-lighting bloom");
// Camera setup
g_App.camera.position = (Vector3){ 0.0f, 5.0f, 9.0f };
g_App.camera.target = (Vector3){ 0.0f, 0.5f, 0.0f };
g_App.camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
g_App.camera.fovy = 45.0f;
g_App.camera.projection = CAMERA_PERSPECTIVE;
// Load render target for multi-pass bloom
g_App.fbo = LoadRenderTexture(screenWidth, screenHeight);
// Load custom shaders from memory strings
g_App.lightShader = LoadShaderFromMemory(multiLightVS, multiLightFS);
g_App.bloomShader = LoadShaderFromMemory(NULL, bloomFS);
// Create models and attach custom lighting shader
g_App.cubeModel = LoadModelFromMesh(GenMeshCube(2.0f, 2.0f, 2.0f));
g_App.floorModel = LoadModelFromMesh(GenMeshPlane(14.0f, 14.0f, 1, 1));
g_App.cubeModel.materials[0].shader = g_App.lightShader;
g_App.floorModel.materials[0].shader = g_App.lightShader;
// Fetch shader uniform locations
g_App.lightPosLoc = GetShaderLocation(g_App.lightShader, "lightPositions");
g_App.lightColLoc = GetShaderLocation(g_App.lightShader, "lightColors");
g_App.viewPosLoc = GetShaderLocation(g_App.lightShader, "viewPos");
// Initialize 8 vivid light colors
g_App.lightColors[0] = (Vector3){ 1.0f, 0.2f, 0.2f }; // Red
g_App.lightColors[1] = (Vector3){ 0.2f, 1.0f, 0.3f }; // Green
g_App.lightColors[2] = (Vector3){ 0.2f, 0.5f, 1.0f }; // Blue
g_App.lightColors[3] = (Vector3){ 1.0f, 0.8f, 0.1f }; // Yellow
g_App.lightColors[4] = (Vector3){ 1.0f, 0.1f, 0.8f }; // Magenta
g_App.lightColors[5] = (Vector3){ 0.1f, 1.0f, 1.0f }; // Cyan
g_App.lightColors[6] = (Vector3){ 1.0f, 0.4f, 0.1f }; // Orange
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);
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
while (!WindowShouldClose())
{
UpdateDrawFrame();
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadModel(g_App.cubeModel);
UnloadModel(g_App.floorModel);
UnloadShader(g_App.lightShader);
UnloadShader(g_App.bloomShader);
UnloadRenderTexture(g_App.fbo);
CloseWindow();
//--------------------------------------------------------------------------------------
#endif
return 0;
}
//--------------------------------------------------------------------------------------
// Frame Update and Render Logic
//--------------------------------------------------------------------------------------
void UpdateDrawFrame(void)
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&g_App.camera, CAMERA_ORBITAL);
float time = (float)GetTime();
// Orbital path calculation for point lights
for (int i = 0; i < MAX_LIGHTS; i++)
{
float angle = (i / (float)MAX_LIGHTS) * 2.0f * PI + time * 0.6f;
float radius = 4.2f + sinf(time * 1.2f + i) * 0.4f;
float height = 1.0f + sinf(time * 1.8f + i) * 0.6f;
g_App.lightPositions[i] = (Vector3){ sinf(angle) * radius, height, cosf(angle) * radius };
}
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);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
// PASS 1: RENDER SCENE TO OFFSCREEN FBO
BeginTextureMode(g_App.fbo);
ClearBackground((Color){ 12, 12, 18, 255 });
BeginMode3D(g_App.camera);
DrawModel(g_App.cubeModel, (Vector3){ 0.0f, 1.0f, 0.0f }, 1.0f, GRAY);
DrawModel(g_App.floorModel, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, DARKGRAY);
DrawGrid(10, 1.0f);
// Draw glowing orb bulbs at light origins
for (int i = 0; i < MAX_LIGHTS; i++)
{
Color bulbColor = (Color){
(unsigned char)(g_App.lightColors[i].x * 255),
(unsigned char)(g_App.lightColors[i].y * 255),
(unsigned char)(g_App.lightColors[i].z * 255),
255
};
DrawSphere(g_App.lightPositions[i], 0.15f, bulbColor);
}
EndMode3D();
EndTextureMode();
// PASS 2: PRESENT WITH TONE-MAPPED BLOOM
BeginDrawing();
ClearBackground(BLACK);
Rectangle srcRec = (Rectangle){ 0, 0, (float)g_App.fbo.texture.width, (float)-g_App.fbo.texture.height };
BeginShaderMode(g_App.bloomShader);
DrawTextureRec(g_App.fbo.texture, srcRec, (Vector2){ 0, 0 }, WHITE);
EndShaderMode();
DrawText("BALANCED MULTI-LIGHT + REINHARD TONE MAPPED BLOOM", 20, 20, 20, GREEN);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
/*******************************************************************************************
*
* raylib [shaders] example - forward multi-lighting with bloom
*
* Example demonstrates forward multi-point lighting with bloom post-processing.
*
* Example complexity rating: [★★★☆] 3/4
*
* 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)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2025 PanicTitan (@PanicTitan)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#include <stdlib.h> // Required for: NULL
#include <math.h> // Required for: sinf(), cosf()
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
#if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION "#version 330\n"
#else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION "#version 300 es\nprecision mediump float;\n"
#endif
//--------------------------------------------------------------------------------------
// Global Definitions
//--------------------------------------------------------------------------------------
#define MAX_LIGHTS 8
//--------------------------------------------------------------------------------------
// Embedded Shader Sources
//--------------------------------------------------------------------------------------
// Forward Vertex Shader
static const char *multiLightVS = GLSL_VERSION
"in vec3 vertexPosition;\n"
"in vec2 vertexTexCoord;\n"
"in vec3 vertexNormal;\n"
"\n"
"out vec3 fragPosition;\n"
"out vec3 fragNormal;\n"
"out vec2 fragTexCoord;\n"
"\n"
"uniform mat4 mvp;\n"
"uniform mat4 matModel;\n"
"\n"
"void main() {\n"
" fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));\n"
" fragNormal = normalize(vec3(matModel * vec4(vertexNormal, 0.0)));\n"
" fragTexCoord = vertexTexCoord;\n"
" gl_Position = mvp * vec4(vertexPosition, 1.0);\n"
"}\n";
// Forward Fragment Shader (Point Light Falloff & Blinn-Phong Specular)
static const char *multiLightFS = GLSL_VERSION
"in vec3 fragPosition;\n"
"in vec3 fragNormal;\n"
"in vec2 fragTexCoord;\n"
"\n"
"out vec4 finalColor;\n"
"\n"
"uniform sampler2D texture0;\n"
"uniform vec3 viewPos;\n"
"uniform vec3 lightPositions[8];\n"
"uniform vec3 lightColors[8];\n"
"\n"
"void main() {\n"
" vec4 texColor = texture(texture0, fragTexCoord);\n"
" vec3 norm = normalize(fragNormal);\n"
" vec3 viewDir = normalize(viewPos - fragPosition);\n"
"\n"
" // Dark ambient baseline\n"
" vec3 ambient = 0.05 * texColor.rgb;\n"
" vec3 totalDiffuse = vec3(0.0);\n"
" vec3 totalSpecular = vec3(0.0);\n"
"\n"
" for (int i = 0; i < 8; i++) {\n"
" vec3 lightDir = normalize(lightPositions[i] - fragPosition);\n"
" float dist = length(lightPositions[i] - fragPosition);\n"
" \n"
" // Attenuation calculation\n"
" float attenuation = 1.0 / (1.0 + 0.8 * dist + 0.4 * dist * dist);\n"
"\n"
" // Diffuse Shading\n"
" float diff = max(dot(norm, lightDir), 0.0);\n"
" totalDiffuse += diff * lightColors[i] * attenuation * 1.2;\n"
"\n"
" // Blinn-Phong Specular\n"
" vec3 halfwayDir = normalize(lightDir + viewDir);\n"
" float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0);\n"
" totalSpecular += spec * lightColors[i] * attenuation * 0.8;\n"
" }\n"
"\n"
" vec3 result = ambient + (totalDiffuse * texColor.rgb) + totalSpecular;\n"
" finalColor = vec4(result, texColor.a);\n"
"}\n";
// Tone-Mapped Kawase Bloom Fragment Shader
static const char *bloomFS = GLSL_VERSION
"in vec2 fragTexCoord;\n"
"out vec4 finalColor;\n"
"\n"
"uniform sampler2D texture0;\n"
"\n"
"void main() {\n"
" vec2 uv = fragTexCoord;\n"
" vec4 color = texture(texture0, uv);\n"
"\n"
" // High-pass threshold check (pixels brighter than 0.75)\n"
" vec4 bloom = vec4(0.0);\n"
" vec2 texel = vec2(1.0 / 1280.0, 1.0 / 720.0) * 2.5;\n"
"\n"
" for (int x = -2; x <= 2; x++) {\n"
" for (int y = -2; y <= 2; y++) {\n"
" vec4 smp = texture(texture0, uv + vec2(x, y) * texel);\n"
" float brightness = max(smp.r, max(smp.g, smp.b));\n"
" if (brightness > 0.75) bloom += smp;\n"
" }\n"
" }\n"
" bloom /= 25.0;\n"
"\n"
" // Combine original with soft bloom glow\n"
" vec3 HDR = color.rgb + bloom.rgb * 1.2;\n"
"\n"
" // Reinhard Tone Mapping\n"
" vec3 LDR = HDR / (HDR + vec3(1.0));\n"
"\n"
" finalColor = vec4(LDR, color.a);\n"
"}\n";
//--------------------------------------------------------------------------------------
// Types and Structures Definition
//--------------------------------------------------------------------------------------
typedef struct AppContext {
Camera camera; // 3D scene camera
RenderTexture2D fbo; // Offscreen render target
Shader lightShader; // Multi-light forward shader
Shader bloomShader; // Bloom post-processing shader
Model cubeModel; // Center cube mesh model
Model floorModel; // Ground plane mesh model
int lightPosLoc; // Uniform location: light positions
int lightColLoc; // Uniform location: light colors
int viewPosLoc; // Uniform location: view position
Vector3 lightPositions[MAX_LIGHTS]; // Array of point light positions
Vector3 lightColors[MAX_LIGHTS]; // Array of point light RGB colors
} AppContext;
//--------------------------------------------------------------------------------------
// Module Global Variables
//--------------------------------------------------------------------------------------
static AppContext g_App = { 0 };
//--------------------------------------------------------------------------------------
// Module Functions Declaration
//--------------------------------------------------------------------------------------
void UpdateDrawFrame(void); // Main frame loop (web & desktop)
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 1280;
const int screenHeight = 720;
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT);
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - forward multi-lighting bloom");
// Camera setup
g_App.camera.position = (Vector3){ 0.0f, 5.0f, 9.0f };
g_App.camera.target = (Vector3){ 0.0f, 0.5f, 0.0f };
g_App.camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
g_App.camera.fovy = 45.0f;
g_App.camera.projection = CAMERA_PERSPECTIVE;
// Load render target for multi-pass bloom
g_App.fbo = LoadRenderTexture(screenWidth, screenHeight);
// Load custom shaders from memory strings
g_App.lightShader = LoadShaderFromMemory(multiLightVS, multiLightFS);
g_App.bloomShader = LoadShaderFromMemory(NULL, bloomFS);
// Create models and attach custom lighting shader
g_App.cubeModel = LoadModelFromMesh(GenMeshCube(2.0f, 2.0f, 2.0f));
g_App.floorModel = LoadModelFromMesh(GenMeshPlane(14.0f, 14.0f, 1, 1));
g_App.cubeModel.materials[0].shader = g_App.lightShader;
g_App.floorModel.materials[0].shader = g_App.lightShader;
// Fetch shader uniform locations
g_App.lightPosLoc = GetShaderLocation(g_App.lightShader, "lightPositions");
g_App.lightColLoc = GetShaderLocation(g_App.lightShader, "lightColors");
g_App.viewPosLoc = GetShaderLocation(g_App.lightShader, "viewPos");
// Initialize 8 vivid light colors
g_App.lightColors[0] = (Vector3){ 1.0f, 0.2f, 0.2f }; // Red
g_App.lightColors[1] = (Vector3){ 0.2f, 1.0f, 0.3f }; // Green
g_App.lightColors[2] = (Vector3){ 0.2f, 0.5f, 1.0f }; // Blue
g_App.lightColors[3] = (Vector3){ 1.0f, 0.8f, 0.1f }; // Yellow
g_App.lightColors[4] = (Vector3){ 1.0f, 0.1f, 0.8f }; // Magenta
g_App.lightColors[5] = (Vector3){ 0.1f, 1.0f, 1.0f }; // Cyan
g_App.lightColors[6] = (Vector3){ 1.0f, 0.4f, 0.1f }; // Orange
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);
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
while (!WindowShouldClose())
{
UpdateDrawFrame();
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadModel(g_App.cubeModel);
UnloadModel(g_App.floorModel);
UnloadShader(g_App.lightShader);
UnloadShader(g_App.bloomShader);
UnloadRenderTexture(g_App.fbo);
CloseWindow();
//--------------------------------------------------------------------------------------
#endif
return 0;
}
//--------------------------------------------------------------------------------------
// Frame Update and Render Logic
//--------------------------------------------------------------------------------------
void UpdateDrawFrame(void)
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&g_App.camera, CAMERA_ORBITAL);
float time = (float)GetTime();
// Orbital path calculation for point lights
for (int i = 0; i < MAX_LIGHTS; i++)
{
float angle = (i / (float)MAX_LIGHTS) * 2.0f * PI + time * 0.6f;
float radius = 4.2f + sinf(time * 1.2f + i) * 0.4f;
float height = 1.0f + sinf(time * 1.8f + i) * 0.6f;
g_App.lightPositions[i] = (Vector3){ sinf(angle) * radius, height, cosf(angle) * radius };
}
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);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
// PASS 1: RENDER SCENE TO OFFSCREEN FBO
BeginTextureMode(g_App.fbo);
ClearBackground((Color){ 12, 12, 18, 255 });
BeginMode3D(g_App.camera);
DrawModel(g_App.cubeModel, (Vector3){ 0.0f, 1.0f, 0.0f }, 1.0f, GRAY);
DrawModel(g_App.floorModel, (Vector3){ 0.0f, 0.0f, 0.0f }, 1.0f, DARKGRAY);
DrawGrid(10, 1.0f);
// Draw glowing orb bulbs at light origins
for (int i = 0; i < MAX_LIGHTS; i++)
{
Color bulbColor = (Color){
(unsigned char)(g_App.lightColors[i].x * 255),
(unsigned char)(g_App.lightColors[i].y * 255),
(unsigned char)(g_App.lightColors[i].z * 255),
255
};
DrawSphere(g_App.lightPositions[i], 0.15f, bulbColor);
}
EndMode3D();
EndTextureMode();
// PASS 2: PRESENT WITH TONE-MAPPED BLOOM
BeginDrawing();
ClearBackground(BLACK);
Rectangle srcRec = (Rectangle){ 0, 0, (float)g_App.fbo.texture.width, (float)-g_App.fbo.texture.height };
BeginShaderMode(g_App.bloomShader);
DrawTextureRec(g_App.fbo.texture, srcRec, (Vector2){ 0, 0 }, WHITE);
EndShaderMode();
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