[examples] shaders shadowmap rendering camera fix (#6041)

* Update the camera before sending it to the shader

* tabs to spaces (oops)
This commit is contained in:
Jeffery Myers 2026-08-08 07:58:39 -07:00 • committed by GitHub
commit 388806f24d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -21,9 +21,9 @@
#include "rlgl.h" #include "rlgl.h"
#if defined(PLATFORM_DESKTOP) #if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION 330 #define GLSL_VERSION 330
#else // PLATFORM_ANDROID, PLATFORM_WEB #else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100 #define GLSL_VERSION 100
#endif #endif
#define SHADOWMAP_RESOLUTION 1024 #define SHADOWMAP_RESOLUTION 1024
@ -57,10 +57,10 @@ int main(void)
camera.fovy = 45.0f; camera.fovy = 45.0f;
Shader shadowShader = LoadShader(TextFormat("resources/shaders/glsl%i/shadowmap.vs", GLSL_VERSION), Shader shadowShader = LoadShader(TextFormat("resources/shaders/glsl%i/shadowmap.vs", GLSL_VERSION),
TextFormat("resources/shaders/glsl%i/shadowmap.fs", GLSL_VERSION)); TextFormat("resources/shaders/glsl%i/shadowmap.fs", GLSL_VERSION));
shadowShader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shadowShader, "viewPos"); shadowShader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shadowShader, "viewPos");
Vector3 lightDir = Vector3Normalize((Vector3){ 0.35f, -1.0f, -0.35f }); Vector3 lightDir = Vector3Normalize((Vector3) { 0.35f, -1.0f, -0.35f });
Color lightColor = WHITE; Color lightColor = WHITE;
Vector4 lightColorNormalized = ColorNormalize(lightColor); Vector4 lightColorNormalized = ColorNormalize(lightColor);
int lightDirLoc = GetShaderLocation(shadowShader, "lightDir"); int lightDirLoc = GetShaderLocation(shadowShader, "lightDir");
@ -68,7 +68,7 @@ int main(void)
SetShaderValue(shadowShader, lightDirLoc, &lightDir, SHADER_UNIFORM_VEC3); SetShaderValue(shadowShader, lightDirLoc, &lightDir, SHADER_UNIFORM_VEC3);
SetShaderValue(shadowShader, lightColLoc, &lightColorNormalized, SHADER_UNIFORM_VEC4); SetShaderValue(shadowShader, lightColLoc, &lightColorNormalized, SHADER_UNIFORM_VEC4);
int ambientLoc = GetShaderLocation(shadowShader, "ambient"); int ambientLoc = GetShaderLocation(shadowShader, "ambient");
float ambient[4] = {0.1f, 0.1f, 0.1f, 1.0f}; float ambient[4] = { 0.1f, 0.1f, 0.1f, 1.0f };
SetShaderValue(shadowShader, ambientLoc, ambient, SHADER_UNIFORM_VEC4); SetShaderValue(shadowShader, ambientLoc, ambient, SHADER_UNIFORM_VEC4);
int lightVPLoc = GetShaderLocation(shadowShader, "lightVP"); int lightVPLoc = GetShaderLocation(shadowShader, "lightVP");
int shadowMapLoc = GetShaderLocation(shadowShader, "shadowMap"); int shadowMapLoc = GetShaderLocation(shadowShader, "shadowMap");
@ -81,7 +81,7 @@ int main(void)
for (int i = 0; i < robot.materialCount; i++) robot.materials[i].shader = shadowShader; for (int i = 0; i < robot.materialCount; i++) robot.materials[i].shader = shadowShader;
int animCount = 0; int animCount = 0;
ModelAnimation *anims = LoadModelAnimations("resources/models/robot.glb", &animCount); ModelAnimation* anims = LoadModelAnimations("resources/models/robot.glb", &animCount);
RenderTexture2D shadowMap = LoadShadowmapRenderTexture(SHADOWMAP_RESOLUTION, SHADOWMAP_RESOLUTION); RenderTexture2D shadowMap = LoadShadowmapRenderTexture(SHADOWMAP_RESOLUTION, SHADOWMAP_RESOLUTION);
@ -111,9 +111,11 @@ int main(void)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
float deltaTime = GetFrameTime(); float deltaTime = GetFrameTime();
UpdateCamera(&camera, CAMERA_ORBITAL);
// send the updated camera position to the shader so that it can calculate the correct lighting for the scene
Vector3 cameraPos = camera.position; Vector3 cameraPos = camera.position;
SetShaderValue(shadowShader, shadowShader.locs[SHADER_LOC_VECTOR_VIEW], &cameraPos, SHADER_UNIFORM_VEC3); SetShaderValue(shadowShader, shadowShader.locs[SHADER_LOC_VECTOR_VIEW], &cameraPos, SHADER_UNIFORM_VEC3);
UpdateCamera(&camera, CAMERA_ORBITAL);
frameCounter++; frameCounter++;
frameCounter %= (anims[0].keyframeCount); frameCounter %= (anims[0].keyframeCount);
@ -123,19 +125,19 @@ int main(void)
const float cameraSpeed = 0.05f; const float cameraSpeed = 0.05f;
if (IsKeyDown(KEY_LEFT)) if (IsKeyDown(KEY_LEFT))
{ {
if (lightDir.x < 0.6f) lightDir.x += cameraSpeed*60.0f*deltaTime; if (lightDir.x < 0.6f) lightDir.x += cameraSpeed * 60.0f * deltaTime;
} }
if (IsKeyDown(KEY_RIGHT)) if (IsKeyDown(KEY_RIGHT))
{ {
if (lightDir.x > -0.6f) lightDir.x -= cameraSpeed*60.0f*deltaTime; if (lightDir.x > -0.6f) lightDir.x -= cameraSpeed * 60.0f * deltaTime;
} }
if (IsKeyDown(KEY_UP)) if (IsKeyDown(KEY_UP))
{ {
if (lightDir.z < 0.6f) lightDir.z += cameraSpeed*60.0f*deltaTime; if (lightDir.z < 0.6f) lightDir.z += cameraSpeed * 60.0f * deltaTime;
} }
if (IsKeyDown(KEY_DOWN)) if (IsKeyDown(KEY_DOWN))
{ {
if (lightDir.z > -0.6f) lightDir.z -= cameraSpeed*60.0f*deltaTime; if (lightDir.z > -0.6f) lightDir.z -= cameraSpeed * 60.0f * deltaTime;
} }
lightDir = Vector3Normalize(lightDir); lightDir = Vector3Normalize(lightDir);
@ -151,34 +153,34 @@ int main(void)
// We can later use the depth buffer when rendering everything from the player's point of view // We can later use the depth buffer when rendering everything from the player's point of view
// to determine whether a given point is "visible" to the light // to determine whether a given point is "visible" to the light
BeginTextureMode(shadowMap); BeginTextureMode(shadowMap);
ClearBackground(WHITE); ClearBackground(WHITE);
BeginMode3D(lightCamera); BeginMode3D(lightCamera);
lightView = rlGetMatrixModelview(); lightView = rlGetMatrixModelview();
lightProj = rlGetMatrixProjection(); lightProj = rlGetMatrixProjection();
DrawScene(cube, robot); DrawScene(cube, robot);
EndMode3D(); EndMode3D();
EndTextureMode(); EndTextureMode();
lightViewProj = MatrixMultiply(lightView, lightProj); lightViewProj = MatrixMultiply(lightView, lightProj);
// PASS 02: Draw the scene into main framebuffer, using the generated shadowmap // PASS 02: Draw the scene into main framebuffer, using the generated shadowmap
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
SetShaderValueMatrix(shadowShader, lightVPLoc, lightViewProj); SetShaderValueMatrix(shadowShader, lightVPLoc, lightViewProj);
rlEnableShader(shadowShader.id); rlEnableShader(shadowShader.id);
rlActiveTextureSlot(textureActiveSlot); rlActiveTextureSlot(textureActiveSlot);
rlEnableTexture(shadowMap.depth.id); rlEnableTexture(shadowMap.depth.id);
rlSetUniform(shadowMapLoc, &textureActiveSlot, SHADER_UNIFORM_INT, 1); rlSetUniform(shadowMapLoc, &textureActiveSlot, SHADER_UNIFORM_INT, 1);
BeginMode3D(camera); BeginMode3D(camera);
DrawScene(cube, robot); // Draw the same exact things as we drew in the shadowmap! DrawScene(cube, robot); // Draw the same exact things as we drew in the shadowmap!
EndMode3D(); EndMode3D();
DrawText("Use the arrow keys to rotate the light!", 10, 10, 30, RED); DrawText("Use the arrow keys to rotate the light!", 10, 10, 30, RED);
DrawText("Shadows in raylib using the shadowmapping algorithm!", screenWidth - 280, screenHeight - 20, 10, GRAY); DrawText("Shadows in raylib using the shadowmapping algorithm!", screenWidth - 280, screenHeight - 20, 10, GRAY);
EndDrawing(); EndDrawing();