Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions examples/shaders/shaders_shadowmap_rendering.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,18 +57,18 @@ 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");
int lightColLoc = GetShaderLocation(shadowShader, "lightColor");
SetShaderValue(shadowShader, lightDirLoc, &lightDir, SHADER_UNIFORM_VEC3);
SetShaderValue(shadowShader, lightColLoc, &lightColorNormalized, SHADER_UNIFORM_VEC4);
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);
int lightVPLoc = GetShaderLocation(shadowShader, "lightVP");
int shadowMapLoc = GetShaderLocation(shadowShader, "shadowMap");
Expand All @@ -81,7 +81,7 @@ int main(void)
for (int i = 0; i < robot.materialCount; i++) robot.materials[i].shader = shadowShader;

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);

Expand Down Expand Up @@ -111,9 +111,11 @@ int main(void)
//----------------------------------------------------------------------------------
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;
SetShaderValue(shadowShader, shadowShader.locs[SHADER_LOC_VECTOR_VIEW], &cameraPos, SHADER_UNIFORM_VEC3);
UpdateCamera(&camera, CAMERA_ORBITAL);

frameCounter++;
frameCounter %= (anims[0].keyframeCount);
Expand All @@ -123,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);
Expand All @@ -151,34 +153,34 @@ 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();

Expand Down
Loading