|
@@ -20,14 +20,17 @@
|
|
|
#include "rlgl.h"
|
|
|
|
|
|
// Custom Blend Modes
|
|
|
-#define RLGL_SRC_ALPHA 0x0302
|
|
|
-#define RLGL_MIN 0x8007
|
|
|
-#define RLGL_MAX 0x8008
|
|
|
+#define RLGL_SRC_ALPHA 0x0302
|
|
|
+#define RLGL_MIN 0x8007
|
|
|
+#define RLGL_MAX 0x8008
|
|
|
|
|
|
-#define MAX_BOXES 20
|
|
|
-#define MAX_SHADOWS MAX_BOXES*3 // MAX_BOXES *3. Each box can cast up to two shadow volumes for the edges it is away from, and one for the box itself
|
|
|
-#define MAX_LIGHTS 16
|
|
|
+#define MAX_BOXES 20
|
|
|
+#define MAX_SHADOWS MAX_BOXES*3 // MAX_BOXES*3 - Each box can cast up to two shadow volumes for the edges it is away from, and one for the box itself
|
|
|
+#define MAX_LIGHTS 16
|
|
|
|
|
|
+//----------------------------------------------------------------------------------
|
|
|
+// Types and Structures Definition
|
|
|
+//----------------------------------------------------------------------------------
|
|
|
// Shadow geometry type
|
|
|
typedef struct ShadowGeometry {
|
|
|
Vector2 vertices[4];
|
|
@@ -48,167 +51,26 @@ typedef struct LightInfo {
|
|
|
int shadowCount;
|
|
|
} LightInfo;
|
|
|
|
|
|
+//------------------------------------------------------------------------------------
|
|
|
+// Global Variables Definition
|
|
|
+//------------------------------------------------------------------------------------
|
|
|
+static LightInfo lights[MAX_LIGHTS] = { 0 };
|
|
|
|
|
|
-LightInfo lights[MAX_LIGHTS] = { 0 };
|
|
|
-
|
|
|
+//------------------------------------------------------------------------------------
|
|
|
+// Module Functions Declaration
|
|
|
+//------------------------------------------------------------------------------------
|
|
|
// Move a light and mark it as dirty so that we update it's mask next frame
|
|
|
-void MoveLight(int slot, float x, float y)
|
|
|
-{
|
|
|
- lights[slot].dirty = true;
|
|
|
- lights[slot].position.x = x;
|
|
|
- lights[slot].position.y = y;
|
|
|
-
|
|
|
- // update the cached bounds
|
|
|
- lights[slot].bounds.x = x - lights[slot].outerRadius;
|
|
|
- lights[slot].bounds.y = y - lights[slot].outerRadius;
|
|
|
-}
|
|
|
-
|
|
|
+static void MoveLight(int slot, float x, float y);
|
|
|
// Compute a shadow volume for the edge
|
|
|
-// It takes the edge and projects it back by the light radius and turns it into a quad
|
|
|
-void ComputeShadowVolumeForEdge(int slot, Vector2 sp, Vector2 ep)
|
|
|
-{
|
|
|
- if (lights[slot].shadowCount >= MAX_SHADOWS) return;
|
|
|
-
|
|
|
- float extension = lights[slot].outerRadius*2;
|
|
|
-
|
|
|
- Vector2 spVector = Vector2Normalize(Vector2Subtract(sp, lights[slot].position));
|
|
|
- Vector2 spProjection = Vector2Add(sp, Vector2Scale(spVector, extension));
|
|
|
-
|
|
|
- Vector2 epVector = Vector2Normalize(Vector2Subtract(ep, lights[slot].position));
|
|
|
- Vector2 epProjection = Vector2Add(ep, Vector2Scale(epVector, extension));
|
|
|
-
|
|
|
- lights[slot].shadows[lights[slot].shadowCount].vertices[0] = sp;
|
|
|
- lights[slot].shadows[lights[slot].shadowCount].vertices[1] = ep;
|
|
|
- lights[slot].shadows[lights[slot].shadowCount].vertices[2] = epProjection;
|
|
|
- lights[slot].shadows[lights[slot].shadowCount].vertices[3] = spProjection;
|
|
|
-
|
|
|
- lights[slot].shadowCount++;
|
|
|
-}
|
|
|
-
|
|
|
+static void ComputeShadowVolumeForEdge(int slot, Vector2 sp, Vector2 ep);
|
|
|
// Draw the light and shadows to the mask for a light
|
|
|
-void DrawLightMask(int slot)
|
|
|
-{
|
|
|
- // Use the light mask
|
|
|
- BeginTextureMode(lights[slot].mask);
|
|
|
-
|
|
|
- ClearBackground(WHITE);
|
|
|
-
|
|
|
- // Force the blend mode to only set the alpha of the destination
|
|
|
- rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MIN);
|
|
|
- rlSetBlendMode(BLEND_CUSTOM);
|
|
|
-
|
|
|
- // If we are valid, then draw the light radius to the alpha mask
|
|
|
- if (lights[slot].valid) DrawCircleGradient((int)lights[slot].position.x, (int)lights[slot].position.y, lights[slot].outerRadius, ColorAlpha(WHITE, 0), WHITE);
|
|
|
-
|
|
|
- rlDrawRenderBatchActive();
|
|
|
-
|
|
|
- // Cut out the shadows from the light radius by forcing the alpha to maximum
|
|
|
- rlSetBlendMode(BLEND_ALPHA);
|
|
|
- rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MAX);
|
|
|
- rlSetBlendMode(BLEND_CUSTOM);
|
|
|
-
|
|
|
- // Draw the shadows to the alpha mask
|
|
|
- for (int i = 0; i < lights[slot].shadowCount; i++)
|
|
|
- {
|
|
|
- DrawTriangleFan(lights[slot].shadows[i].vertices, 4, WHITE);
|
|
|
- }
|
|
|
-
|
|
|
- rlDrawRenderBatchActive();
|
|
|
-
|
|
|
- // Go back to normal blend mode
|
|
|
- rlSetBlendMode(BLEND_ALPHA);
|
|
|
-
|
|
|
- EndTextureMode();
|
|
|
-}
|
|
|
-
|
|
|
+static void DrawLightMask(int slot);
|
|
|
// Setup a light
|
|
|
-void SetupLight(int slot, float x, float y, float radius)
|
|
|
-{
|
|
|
- lights[slot].active = true;
|
|
|
- lights[slot].valid = false; // The light must prove it is valid
|
|
|
- lights[slot].mask = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
|
|
|
- lights[slot].outerRadius = radius;
|
|
|
-
|
|
|
- lights[slot].bounds.width = radius*2;
|
|
|
- lights[slot].bounds.height = radius*2;
|
|
|
-
|
|
|
- MoveLight(slot, x, y);
|
|
|
-
|
|
|
- // Force the render texture to have something in it
|
|
|
- DrawLightMask(slot);
|
|
|
-}
|
|
|
-
|
|
|
+static void SetupLight(int slot, float x, float y, float radius);
|
|
|
// See if a light needs to update it's mask
|
|
|
-bool UpdateLight(int slot, Rectangle* boxes, int count)
|
|
|
-{
|
|
|
- if (!lights[slot].active || !lights[slot].dirty) return false;
|
|
|
-
|
|
|
- lights[slot].dirty = false;
|
|
|
- lights[slot].shadowCount = 0;
|
|
|
- lights[slot].valid = false;
|
|
|
-
|
|
|
- for (int i = 0; i < count; i++)
|
|
|
- {
|
|
|
- // Are we in a box? if so we are not valid
|
|
|
- if (CheckCollisionPointRec(lights[slot].position, boxes[i])) return false;
|
|
|
-
|
|
|
- // If this box is outside our bounds, we can skip it
|
|
|
- if (!CheckCollisionRecs(lights[slot].bounds, boxes[i])) continue;
|
|
|
-
|
|
|
- // Check the edges that are on the same side we are, and cast shadow volumes out from them
|
|
|
-
|
|
|
- // Top
|
|
|
- Vector2 sp = (Vector2){ boxes[i].x, boxes[i].y };
|
|
|
- Vector2 ep = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y };
|
|
|
-
|
|
|
- if (lights[slot].position.y > ep.y) ComputeShadowVolumeForEdge(slot, sp, ep);
|
|
|
-
|
|
|
- // Right
|
|
|
- sp = ep;
|
|
|
- ep.y += boxes[i].height;
|
|
|
- if (lights[slot].position.x < ep.x) ComputeShadowVolumeForEdge(slot, sp, ep);
|
|
|
-
|
|
|
- // Bottom
|
|
|
- sp = ep;
|
|
|
- ep.x -= boxes[i].width;
|
|
|
- if (lights[slot].position.y < ep.y) ComputeShadowVolumeForEdge(slot, sp, ep);
|
|
|
-
|
|
|
- // Left
|
|
|
- sp = ep;
|
|
|
- ep.y -= boxes[i].height;
|
|
|
- if (lights[slot].position.x > ep.x) ComputeShadowVolumeForEdge(slot, sp, ep);
|
|
|
-
|
|
|
- // The box itself
|
|
|
- lights[slot].shadows[lights[slot].shadowCount].vertices[0] = (Vector2){ boxes[i].x, boxes[i].y };
|
|
|
- lights[slot].shadows[lights[slot].shadowCount].vertices[1] = (Vector2){ boxes[i].x, boxes[i].y + boxes[i].height };
|
|
|
- lights[slot].shadows[lights[slot].shadowCount].vertices[2] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y + boxes[i].height };
|
|
|
- lights[slot].shadows[lights[slot].shadowCount].vertices[3] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y };
|
|
|
- lights[slot].shadowCount++;
|
|
|
- }
|
|
|
-
|
|
|
- lights[slot].valid = true;
|
|
|
-
|
|
|
- DrawLightMask(slot);
|
|
|
-
|
|
|
- return true;
|
|
|
-}
|
|
|
-
|
|
|
+static bool UpdateLight(int slot, Rectangle* boxes, int count);
|
|
|
// Set up some boxes
|
|
|
-void SetupBoxes(Rectangle *boxes, int *count)
|
|
|
-{
|
|
|
- boxes[0] = (Rectangle){ 150,80, 40, 40 };
|
|
|
- boxes[1] = (Rectangle){ 1200, 700, 40, 40 };
|
|
|
- boxes[2] = (Rectangle){ 200, 600, 40, 40 };
|
|
|
- boxes[3] = (Rectangle){ 1000, 50, 40, 40 };
|
|
|
- boxes[4] = (Rectangle){ 500, 350, 40, 40 };
|
|
|
-
|
|
|
- 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) };
|
|
|
- }
|
|
|
-
|
|
|
- *count = MAX_BOXES;
|
|
|
-}
|
|
|
+static void SetupBoxes(Rectangle *boxes, int *count);
|
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
// Program main entry point
|
|
@@ -356,3 +218,165 @@ int main(void)
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
+
|
|
|
+//------------------------------------------------------------------------------------
|
|
|
+// Module Functions Definition
|
|
|
+//------------------------------------------------------------------------------------
|
|
|
+// Move a light and mark it as dirty so that we update it's mask next frame
|
|
|
+static void MoveLight(int slot, float x, float y)
|
|
|
+{
|
|
|
+ lights[slot].dirty = true;
|
|
|
+ lights[slot].position.x = x;
|
|
|
+ lights[slot].position.y = y;
|
|
|
+
|
|
|
+ // update the cached bounds
|
|
|
+ lights[slot].bounds.x = x - lights[slot].outerRadius;
|
|
|
+ lights[slot].bounds.y = y - lights[slot].outerRadius;
|
|
|
+}
|
|
|
+
|
|
|
+// Compute a shadow volume for the edge
|
|
|
+// It takes the edge and projects it back by the light radius and turns it into a quad
|
|
|
+static void ComputeShadowVolumeForEdge(int slot, Vector2 sp, Vector2 ep)
|
|
|
+{
|
|
|
+ if (lights[slot].shadowCount >= MAX_SHADOWS) return;
|
|
|
+
|
|
|
+ float extension = lights[slot].outerRadius*2;
|
|
|
+
|
|
|
+ Vector2 spVector = Vector2Normalize(Vector2Subtract(sp, lights[slot].position));
|
|
|
+ Vector2 spProjection = Vector2Add(sp, Vector2Scale(spVector, extension));
|
|
|
+
|
|
|
+ Vector2 epVector = Vector2Normalize(Vector2Subtract(ep, lights[slot].position));
|
|
|
+ Vector2 epProjection = Vector2Add(ep, Vector2Scale(epVector, extension));
|
|
|
+
|
|
|
+ lights[slot].shadows[lights[slot].shadowCount].vertices[0] = sp;
|
|
|
+ lights[slot].shadows[lights[slot].shadowCount].vertices[1] = ep;
|
|
|
+ lights[slot].shadows[lights[slot].shadowCount].vertices[2] = epProjection;
|
|
|
+ lights[slot].shadows[lights[slot].shadowCount].vertices[3] = spProjection;
|
|
|
+
|
|
|
+ lights[slot].shadowCount++;
|
|
|
+}
|
|
|
+
|
|
|
+// Setup a light
|
|
|
+static void SetupLight(int slot, float x, float y, float radius)
|
|
|
+{
|
|
|
+ lights[slot].active = true;
|
|
|
+ lights[slot].valid = false; // The light must prove it is valid
|
|
|
+ lights[slot].mask = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
|
|
|
+ lights[slot].outerRadius = radius;
|
|
|
+
|
|
|
+ lights[slot].bounds.width = radius*2;
|
|
|
+ lights[slot].bounds.height = radius*2;
|
|
|
+
|
|
|
+ MoveLight(slot, x, y);
|
|
|
+
|
|
|
+ // Force the render texture to have something in it
|
|
|
+ DrawLightMask(slot);
|
|
|
+}
|
|
|
+
|
|
|
+// See if a light needs to update it's mask
|
|
|
+static bool UpdateLight(int slot, Rectangle* boxes, int count)
|
|
|
+{
|
|
|
+ if (!lights[slot].active || !lights[slot].dirty) return false;
|
|
|
+
|
|
|
+ lights[slot].dirty = false;
|
|
|
+ lights[slot].shadowCount = 0;
|
|
|
+ lights[slot].valid = false;
|
|
|
+
|
|
|
+ for (int i = 0; i < count; i++)
|
|
|
+ {
|
|
|
+ // Are we in a box? if so we are not valid
|
|
|
+ if (CheckCollisionPointRec(lights[slot].position, boxes[i])) return false;
|
|
|
+
|
|
|
+ // If this box is outside our bounds, we can skip it
|
|
|
+ if (!CheckCollisionRecs(lights[slot].bounds, boxes[i])) continue;
|
|
|
+
|
|
|
+ // Check the edges that are on the same side we are, and cast shadow volumes out from them
|
|
|
+
|
|
|
+ // Top
|
|
|
+ Vector2 sp = (Vector2){ boxes[i].x, boxes[i].y };
|
|
|
+ Vector2 ep = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y };
|
|
|
+
|
|
|
+ if (lights[slot].position.y > ep.y) ComputeShadowVolumeForEdge(slot, sp, ep);
|
|
|
+
|
|
|
+ // Right
|
|
|
+ sp = ep;
|
|
|
+ ep.y += boxes[i].height;
|
|
|
+ if (lights[slot].position.x < ep.x) ComputeShadowVolumeForEdge(slot, sp, ep);
|
|
|
+
|
|
|
+ // Bottom
|
|
|
+ sp = ep;
|
|
|
+ ep.x -= boxes[i].width;
|
|
|
+ if (lights[slot].position.y < ep.y) ComputeShadowVolumeForEdge(slot, sp, ep);
|
|
|
+
|
|
|
+ // Left
|
|
|
+ sp = ep;
|
|
|
+ ep.y -= boxes[i].height;
|
|
|
+ if (lights[slot].position.x > ep.x) ComputeShadowVolumeForEdge(slot, sp, ep);
|
|
|
+
|
|
|
+ // The box itself
|
|
|
+ lights[slot].shadows[lights[slot].shadowCount].vertices[0] = (Vector2){ boxes[i].x, boxes[i].y };
|
|
|
+ lights[slot].shadows[lights[slot].shadowCount].vertices[1] = (Vector2){ boxes[i].x, boxes[i].y + boxes[i].height };
|
|
|
+ lights[slot].shadows[lights[slot].shadowCount].vertices[2] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y + boxes[i].height };
|
|
|
+ lights[slot].shadows[lights[slot].shadowCount].vertices[3] = (Vector2){ boxes[i].x + boxes[i].width, boxes[i].y };
|
|
|
+ lights[slot].shadowCount++;
|
|
|
+ }
|
|
|
+
|
|
|
+ lights[slot].valid = true;
|
|
|
+
|
|
|
+ DrawLightMask(slot);
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+// Draw the light and shadows to the mask for a light
|
|
|
+static void DrawLightMask(int slot)
|
|
|
+{
|
|
|
+ // Use the light mask
|
|
|
+ BeginTextureMode(lights[slot].mask);
|
|
|
+
|
|
|
+ ClearBackground(WHITE);
|
|
|
+
|
|
|
+ // Force the blend mode to only set the alpha of the destination
|
|
|
+ rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MIN);
|
|
|
+ rlSetBlendMode(BLEND_CUSTOM);
|
|
|
+
|
|
|
+ // If we are valid, then draw the light radius to the alpha mask
|
|
|
+ if (lights[slot].valid) DrawCircleGradient((int)lights[slot].position.x, (int)lights[slot].position.y, lights[slot].outerRadius, ColorAlpha(WHITE, 0), WHITE);
|
|
|
+
|
|
|
+ rlDrawRenderBatchActive();
|
|
|
+
|
|
|
+ // Cut out the shadows from the light radius by forcing the alpha to maximum
|
|
|
+ rlSetBlendMode(BLEND_ALPHA);
|
|
|
+ rlSetBlendFactors(RLGL_SRC_ALPHA, RLGL_SRC_ALPHA, RLGL_MAX);
|
|
|
+ rlSetBlendMode(BLEND_CUSTOM);
|
|
|
+
|
|
|
+ // Draw the shadows to the alpha mask
|
|
|
+ for (int i = 0; i < lights[slot].shadowCount; i++)
|
|
|
+ {
|
|
|
+ DrawTriangleFan(lights[slot].shadows[i].vertices, 4, WHITE);
|
|
|
+ }
|
|
|
+
|
|
|
+ rlDrawRenderBatchActive();
|
|
|
+
|
|
|
+ // Go back to normal blend mode
|
|
|
+ rlSetBlendMode(BLEND_ALPHA);
|
|
|
+
|
|
|
+ EndTextureMode();
|
|
|
+}
|
|
|
+
|
|
|
+// Set up some boxes
|
|
|
+static void SetupBoxes(Rectangle *boxes, int *count)
|
|
|
+{
|
|
|
+ boxes[0] = (Rectangle){ 150,80, 40, 40 };
|
|
|
+ boxes[1] = (Rectangle){ 1200, 700, 40, 40 };
|
|
|
+ boxes[2] = (Rectangle){ 200, 600, 40, 40 };
|
|
|
+ boxes[3] = (Rectangle){ 1000, 50, 40, 40 };
|
|
|
+ boxes[4] = (Rectangle){ 500, 350, 40, 40 };
|
|
|
+
|
|
|
+ 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) };
|
|
|
+ }
|
|
|
+
|
|
|
+ *count = MAX_BOXES;
|
|
|
+}
|