This commit is contained in:
Jeffery Myers 2022-01-16 08:31:51 -08:00
parent a5d33a6116
commit 3b7389e2a3

View File

@ -52,13 +52,13 @@ typedef struct
Vector2 Position; Vector2 Position;
// alpha mask for the light // alpha mask for the light
RenderTexture Mask; RenderTexture Mask;
// the distance the light touches // the distance the light touches
float OuterRadius; float OuterRadius;
// a cached rectangle of the light bounds to help with culling // a cached rectangle of the light bounds to help with culling
Rectangle Bounds; Rectangle Bounds;
ShadowGeometry Shadows[MAX_SHADOWS]; ShadowGeometry Shadows[MAX_SHADOWS];
int ShadowCount; int ShadowCount;
@ -86,17 +86,17 @@ void ComputeShadowVolumeForEdge(int slot, Vector2 sp, Vector2 ep)
if (Lights[slot].ShadowCount >= MAX_SHADOWS) if (Lights[slot].ShadowCount >= MAX_SHADOWS)
return; return;
float extension = Lights[slot].OuterRadius*2; float extension = Lights[slot].OuterRadius*2;
Vector2 spVector = Vector2Normalize(Vector2Subtract(sp, Lights[slot].Position)); Vector2 spVector = Vector2Normalize(Vector2Subtract(sp, Lights[slot].Position));
Vector2 spProjection = Vector2Add(sp, Vector2Scale(spVector, extension)); Vector2 spProjection = Vector2Add(sp, Vector2Scale(spVector, extension));
Vector2 epVector = Vector2Normalize(Vector2Subtract(ep, Lights[slot].Position)); Vector2 epVector = Vector2Normalize(Vector2Subtract(ep, Lights[slot].Position));
Vector2 epProjection = Vector2Add(ep, Vector2Scale(epVector, extension)); Vector2 epProjection = Vector2Add(ep, Vector2Scale(epVector, extension));
Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[0] = sp; Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[0] = sp;
Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[1] = ep; Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[1] = ep;
Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[2] = epProjection; Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[2] = epProjection;
Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[3] = spProjection; Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[3] = spProjection;
Lights[slot].ShadowCount++; Lights[slot].ShadowCount++;
@ -106,49 +106,49 @@ void ComputeShadowVolumeForEdge(int slot, Vector2 sp, Vector2 ep)
void UpdateLightMask(int slot) void UpdateLightMask(int slot)
{ {
// use the light mask // use the light mask
BeginTextureMode(Lights[slot].Mask); BeginTextureMode(Lights[slot].Mask);
ClearBackground(WHITE); ClearBackground(WHITE);
// force the blend mode to only set the alpha of the destination // force the blend mode to only set the alpha of the destination
rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MIN); rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MIN);
rlSetBlendMode(BLEND_CUSTOM); rlSetBlendMode(BLEND_CUSTOM);
// if we are valid, then draw the light radius to the alpha mask // if we are valid, then draw the light radius to the alpha mask
if (Lights[slot].Valid) if (Lights[slot].Valid)
DrawCircleGradient((int)Lights[slot].Position.x, (int)Lights[slot].Position.y, Lights[slot].OuterRadius, ColorAlpha(WHITE, 0), WHITE); DrawCircleGradient((int)Lights[slot].Position.x, (int)Lights[slot].Position.y, Lights[slot].OuterRadius, ColorAlpha(WHITE, 0), WHITE);
rlDrawRenderBatchActive(); rlDrawRenderBatchActive();
// cut out the shadows from the light radius by forcing the alpha to maximum // cut out the shadows from the light radius by forcing the alpha to maximum
rlSetBlendMode(BLEND_ALPHA); rlSetBlendMode(BLEND_ALPHA);
rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MAX); rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MAX);
rlSetBlendMode(BLEND_CUSTOM); rlSetBlendMode(BLEND_CUSTOM);
// draw the shadows to the alpha mask // draw the shadows to the alpha mask
for (int i = 0; i < Lights[slot].ShadowCount; i++) for (int i = 0; i < Lights[slot].ShadowCount; i++)
{ {
DrawTriangleFan(Lights[slot].Shadows[i].Vertecies, 4, WHITE); DrawTriangleFan(Lights[slot].Shadows[i].Vertecies, 4, WHITE);
} }
rlDrawRenderBatchActive(); rlDrawRenderBatchActive();
// go back to normal blend mode // go back to normal blend mode
rlSetBlendMode(BLEND_ALPHA); rlSetBlendMode(BLEND_ALPHA);
EndTextureMode(); EndTextureMode();
} }
// setup a light // setup a light
void SetUpLight(int slot, float x, float y, float radius) void SetUpLight(int slot, float x, float y, float radius)
{ {
Lights[slot].Active = true; Lights[slot].Active = true;
Lights[slot].Valid = false; // the light must prove it is valid Lights[slot].Valid = false; // the light must prove it is valid
Lights[slot].Mask = LoadRenderTexture(GetScreenWidth(), GetScreenHeight()); Lights[slot].Mask = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
Lights[slot].OuterRadius = radius; Lights[slot].OuterRadius = radius;
Lights[slot].Bounds.width = radius * 2; Lights[slot].Bounds.width = radius * 2;
Lights[slot].Bounds.height = radius * 2; Lights[slot].Bounds.height = radius * 2;
MoveLight(slot, x, y); MoveLight(slot, x, y);
// force the render texture to have something in it // force the render texture to have something in it
UpdateLightMask(slot); UpdateLightMask(slot);
@ -164,50 +164,50 @@ bool UpdateLight(int slot, Rectangle* boxes, int count)
Lights[slot].ShadowCount = 0; Lights[slot].ShadowCount = 0;
Lights[slot].Valid = false; Lights[slot].Valid = false;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
// are we in a box, if so we are not valid // are we in a box, if so we are not valid
if (CheckCollisionPointRec(Lights[slot].Position, boxes[i])) if (CheckCollisionPointRec(Lights[slot].Position, boxes[i]))
return false; return false;
// if this box is outside our bounds, we can skip it // if this box is outside our bounds, we can skip it
if (!CheckCollisionRecs(Lights[slot].Bounds, boxes[i])) if (!CheckCollisionRecs(Lights[slot].Bounds, boxes[i]))
continue; continue;
// check the edges that are on the same side we are, and cast shadow volumes out from them. // check the edges that are on the same side we are, and cast shadow volumes out from them.
// top // top
Vector2 sp = (Vector2){ boxes[i].x, boxes[i].y }; Vector2 sp = (Vector2){ boxes[i].x, boxes[i].y };
Vector2 ep = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y }; Vector2 ep = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y };
if (Lights[slot].Position.y > ep.y) if (Lights[slot].Position.y > ep.y)
ComputeShadowVolumeForEdge(slot, sp, ep); ComputeShadowVolumeForEdge(slot, sp, ep);
// right // right
sp = ep; sp = ep;
ep.y += boxes[i].height; ep.y += boxes[i].height;
if (Lights[slot].Position.x < ep.x) if (Lights[slot].Position.x < ep.x)
ComputeShadowVolumeForEdge(slot, sp, ep); ComputeShadowVolumeForEdge(slot, sp, ep);
// bottom // bottom
sp = ep; sp = ep;
ep.x -= boxes[i].width; ep.x -= boxes[i].width;
if (Lights[slot].Position.y < ep.y) if (Lights[slot].Position.y < ep.y)
ComputeShadowVolumeForEdge(slot, sp, ep); ComputeShadowVolumeForEdge(slot, sp, ep);
// left // left
sp = ep; sp = ep;
ep.y -= boxes[i].height; ep.y -= boxes[i].height;
if (Lights[slot].Position.x > ep.x) if (Lights[slot].Position.x > ep.x)
ComputeShadowVolumeForEdge(slot, sp, ep); ComputeShadowVolumeForEdge(slot, sp, ep);
// the box itself // the box itself
Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[0] = (Vector2){ boxes[i].x, boxes[i].y }; Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[0] = (Vector2){ boxes[i].x, boxes[i].y };
Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[1] = (Vector2){ boxes[i].x, boxes[i].y + boxes[i].height }; Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[1] = (Vector2){ boxes[i].x, boxes[i].y + boxes[i].height };
Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[2] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y + boxes[i].height }; Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[2] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y + boxes[i].height };
Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[3] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y }; Lights[slot].Shadows[Lights[slot].ShadowCount].Vertecies[3] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y };
Lights[slot].ShadowCount++; Lights[slot].ShadowCount++;
} }
Lights[slot].Valid = true; Lights[slot].Valid = true;
@ -219,16 +219,16 @@ bool UpdateLight(int slot, Rectangle* boxes, int count)
// set up some boxes // set up some boxes
void SetupBoxes(Rectangle* boxes, int *count) void SetupBoxes(Rectangle* boxes, int *count)
{ {
boxes[0] = (Rectangle){ 150,80, 40, 40 }; boxes[0] = (Rectangle){ 150,80, 40, 40 };
boxes[1] = (Rectangle){ 1200, 700, 40, 40 }; boxes[1] = (Rectangle){ 1200, 700, 40, 40 };
boxes[2] = (Rectangle){ 200, 600, 40, 40 }; boxes[2] = (Rectangle){ 200, 600, 40, 40 };
boxes[3] = (Rectangle){ 1000, 50, 40, 40 }; boxes[3] = (Rectangle){ 1000, 50, 40, 40 };
boxes[4] = (Rectangle){ 500, 350, 40, 40 }; boxes[4] = (Rectangle){ 500, 350, 40, 40 };
for (int i = 5; i < MAX_BOXES; i++) for (int i = 5; i < MAX_BOXES; i++)
{ {
boxes[i] = (Rectangle){(float)GetRandomValue(0,GetScreenWidth()), (float)GetRandomValue(0,GetScreenHeight()), (float)GetRandomValue(10,100), (float)GetRandomValue(10,100) }; boxes[i] = (Rectangle){(float)GetRandomValue(0,GetScreenWidth()), (float)GetRandomValue(0,GetScreenHeight()), (float)GetRandomValue(10,100), (float)GetRandomValue(10,100) };
} }
*count = MAX_BOXES; *count = MAX_BOXES;
} }
@ -243,23 +243,23 @@ int main(void)
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
// initialize our 'world' of boxes // initialize our 'world' of boxes
Rectangle boxes[MAX_BOXES]; Rectangle boxes[MAX_BOXES];
int boxCount = 0; int boxCount = 0;
SetupBoxes(boxes, &boxCount); SetupBoxes(boxes, &boxCount);
// create a checkerboard ground texture // create a checkerboard ground texture
Image img = GenImageChecked(64, 64, 32, 32, DARKBROWN, DARKGRAY); Image img = GenImageChecked(64, 64, 32, 32, DARKBROWN, DARKGRAY);
Texture2D backgroundTexture = LoadTextureFromImage(img); Texture2D backgroundTexture = LoadTextureFromImage(img);
UnloadImage(img); UnloadImage(img);
// create a global light mask to hold all the blended lights // create a global light mask to hold all the blended lights
RenderTexture lightMask = LoadRenderTexture(GetScreenWidth(), GetScreenHeight()); RenderTexture lightMask = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
// setup initial light // setup initial light
SetUpLight(0, 600, 400, 300); SetUpLight(0, 600, 400, 300);
int nextLight = 1; int nextLight = 1;
bool showLines = false; bool showLines = false;
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -268,52 +268,52 @@ int main(void)
{ {
// Update // Update
// drag light 0 // drag light 0
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
MoveLight(0, GetMousePosition().x, GetMousePosition().y); MoveLight(0, GetMousePosition().x, GetMousePosition().y);
// make a new light // make a new light
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) && nextLight < MAX_LIGHTS) if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT) && nextLight < MAX_LIGHTS)
{ {
SetUpLight(nextLight, GetMousePosition().x, GetMousePosition().y, 200); SetUpLight(nextLight, GetMousePosition().x, GetMousePosition().y, 200);
nextLight++; nextLight++;
} }
// toggle debug info // toggle debug info
if (IsKeyPressed(KEY_F1)) if (IsKeyPressed(KEY_F1))
showLines = !showLines; showLines = !showLines;
// update the lights and keep track if any were dirty so we know if we need to update the master light mask // update the lights and keep track if any were dirty so we know if we need to update the master light mask
bool dirtyLights = false; bool dirtyLights = false;
for (int i = 0; i < MAX_LIGHTS; i++) for (int i = 0; i < MAX_LIGHTS; i++)
{ {
if (UpdateLight(i, boxes, boxCount)) if (UpdateLight(i, boxes, boxCount))
dirtyLights = true; dirtyLights = true;
} }
// update the light mask // update the light mask
if (dirtyLights) if (dirtyLights)
{ {
// build up the light mask // build up the light mask
BeginTextureMode(lightMask); BeginTextureMode(lightMask);
ClearBackground(BLACK); ClearBackground(BLACK);
// force the blend mode to only set the alpha of the destination // force the blend mode to only set the alpha of the destination
rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MIN); rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MIN);
rlSetBlendMode(BLEND_CUSTOM); rlSetBlendMode(BLEND_CUSTOM);
// merge in all the light masks // merge in all the light masks
for (int i = 0; i < MAX_LIGHTS; i++) for (int i = 0; i < MAX_LIGHTS; i++)
{ {
if (Lights[i].Active) if (Lights[i].Active)
DrawTextureRec(Lights[i].Mask.texture, (Rectangle){ 0, 0, (float)GetScreenWidth(), -(float)GetScreenHeight() }, Vector2Zero(), WHITE); DrawTextureRec(Lights[i].Mask.texture, (Rectangle){ 0, 0, (float)GetScreenWidth(), -(float)GetScreenHeight() }, Vector2Zero(), WHITE);
} }
rlDrawRenderBatchActive(); rlDrawRenderBatchActive();
// go back to normal // go back to normal
rlSetBlendMode(BLEND_ALPHA); rlSetBlendMode(BLEND_ALPHA);
EndTextureMode(); EndTextureMode();
} }
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -321,40 +321,40 @@ int main(void)
ClearBackground(BLACK); ClearBackground(BLACK);
// draw the tile background // draw the tile background
DrawTextureRec(backgroundTexture, (Rectangle){ 0,0,(float)GetScreenWidth(),(float)GetScreenHeight() }, Vector2Zero(), WHITE); DrawTextureRec(backgroundTexture, (Rectangle){ 0,0,(float)GetScreenWidth(),(float)GetScreenHeight() }, Vector2Zero(), WHITE);
// overlay the shadows from all the lights // overlay the shadows from all the lights
DrawTextureRec(lightMask.texture, (Rectangle){ 0, 0, (float)GetScreenWidth(), -(float)GetScreenHeight() }, Vector2Zero(), ColorAlpha(WHITE, showLines ? 0.75f : 1.0f)); DrawTextureRec(lightMask.texture, (Rectangle){ 0, 0, (float)GetScreenWidth(), -(float)GetScreenHeight() }, Vector2Zero(), ColorAlpha(WHITE, showLines ? 0.75f : 1.0f));
// draw the lights // draw the lights
for (int i = 0; i < MAX_LIGHTS; i++) for (int i = 0; i < MAX_LIGHTS; i++)
{ {
if (Lights[i].Active) if (Lights[i].Active)
DrawCircle((int)Lights[i].Position.x, (int)Lights[i].Position.y, 10, i == 0 ? YELLOW : WHITE); DrawCircle((int)Lights[i].Position.x, (int)Lights[i].Position.y, 10, i == 0 ? YELLOW : WHITE);
} }
if (showLines) if (showLines)
{ {
for (int s = 0; s < Lights[0].ShadowCount; s++) for (int s = 0; s < Lights[0].ShadowCount; s++)
{ {
DrawTriangleFan(Lights[0].Shadows[s].Vertecies, 4, DARKPURPLE); DrawTriangleFan(Lights[0].Shadows[s].Vertecies, 4, DARKPURPLE);
} }
for (int b = 0; b < boxCount; b++) for (int b = 0; b < boxCount; b++)
{ {
if (CheckCollisionRecs(boxes[b],Lights[0].Bounds)) if (CheckCollisionRecs(boxes[b],Lights[0].Bounds))
DrawRectangleRec(boxes[b], PURPLE); DrawRectangleRec(boxes[b], PURPLE);
DrawRectangleLines((int)boxes[b].x, (int)boxes[b].y, (int)boxes[b].width, (int)boxes[b].height, DARKBLUE); DrawRectangleLines((int)boxes[b].x, (int)boxes[b].y, (int)boxes[b].width, (int)boxes[b].height, DARKBLUE);
} }
DrawText("(F1) Hide Shadow Volumes", 0, 60, 20, GREEN); DrawText("(F1) Hide Shadow Volumes", 0, 60, 20, GREEN);
} }
else else
{ {
DrawText("(F1) Show Shadow Volumes", 0, 60, 20, GREEN); DrawText("(F1) Show Shadow Volumes", 0, 60, 20, GREEN);
} }
DrawFPS(0, 0); DrawFPS(0, 0);
DrawText("Drag to move light #1", 0, 20, 20, DARKGREEN); DrawText("Drag to move light #1", 0, 20, 20, DARKGREEN);
@ -366,12 +366,12 @@ int main(void)
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadTexture(backgroundTexture); UnloadTexture(backgroundTexture);
UnloadRenderTexture(lightMask); UnloadRenderTexture(lightMask);
for (int i = 0; i < MAX_LIGHTS; i++) for (int i = 0; i < MAX_LIGHTS; i++)
{ {
if (Lights[i].Active) if (Lights[i].Active)
UnloadRenderTexture(Lights[i].Mask); UnloadRenderTexture(Lights[i].Mask);
} }
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------