Update shaders_shadowmap_rendering.c

This commit is contained in:
Ray
2026-08-08 17:03:55 +02:00
parent 388806f24d
commit 079cc2df09

View File

@@ -21,9 +21,9 @@
#include "rlgl.h"
#if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION 330
#define GLSL_VERSION 330
#else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#define GLSL_VERSION 100
#endif
#define SHADOWMAP_RESOLUTION 1024
@@ -57,10 +57,10 @@ int main(void)
camera.fovy = 45.0f;
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");
Vector3 lightDir = Vector3Normalize((Vector3) { 0.35f, -1.0f, -0.35f });
Vector3 lightDir = Vector3Normalize((Vector3){ 0.35f, -1.0f, -0.35f });
Color lightColor = WHITE;
Vector4 lightColorNormalized = ColorNormalize(lightColor);
int lightDirLoc = GetShaderLocation(shadowShader, "lightDir");
@@ -125,19 +125,19 @@ int main(void)
const float cameraSpeed = 0.05f;
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 (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 (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 (lightDir.z > -0.6f) lightDir.z -= cameraSpeed * 60.0f * deltaTime;
if (lightDir.z > -0.6f) lightDir.z -= cameraSpeed*60.0f*deltaTime;
}
lightDir = Vector3Normalize(lightDir);
@@ -153,34 +153,35 @@ int main(void)
// 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
BeginTextureMode(shadowMap);
ClearBackground(WHITE);
ClearBackground(WHITE);
BeginMode3D(lightCamera);
lightView = rlGetMatrixModelview();
lightProj = rlGetMatrixProjection();
DrawScene(cube, robot);
EndMode3D();
BeginMode3D(lightCamera);
lightView = rlGetMatrixModelview();
lightProj = rlGetMatrixProjection();
DrawScene(cube, robot);
EndMode3D();
EndTextureMode();
lightViewProj = MatrixMultiply(lightView, lightProj);
// PASS 02: Draw the scene into main framebuffer, using the generated shadowmap
BeginDrawing();
ClearBackground(RAYWHITE);
ClearBackground(RAYWHITE);
SetShaderValueMatrix(shadowShader, lightVPLoc, lightViewProj);
rlEnableShader(shadowShader.id);
SetShaderValueMatrix(shadowShader, lightVPLoc, lightViewProj);
rlEnableShader(shadowShader.id);
rlActiveTextureSlot(textureActiveSlot);
rlEnableTexture(shadowMap.depth.id);
rlSetUniform(shadowMapLoc, &textureActiveSlot, SHADER_UNIFORM_INT, 1);
rlActiveTextureSlot(textureActiveSlot);
rlEnableTexture(shadowMap.depth.id);
rlSetUniform(shadowMapLoc, &textureActiveSlot, SHADER_UNIFORM_INT, 1);
BeginMode3D(camera);
DrawScene(cube, robot); // Draw the same exact things as we drew in the shadowmap!
EndMode3D();
BeginMode3D(camera);
DrawScene(cube, robot); // Draw the same exact things as we drew in the shadowmap!
EndMode3D();
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("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);
EndDrawing();