Skip to content

Commit

Permalink
Added dead-simple culling
Browse files Browse the repository at this point in the history
Pixel-perfect for sprites and drop shadows
A bit lax for walls but that's alright
No spacial partitioning necessary because the amount of objects to cull is pretty small
  • Loading branch information
Ohmnivore committed Jul 7, 2019
1 parent c3d844a commit 9a416b6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions code/Renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ Renderer::SortedRenderList& Renderer::UpdateWallsAndSprites(Camera& cam, LvlData
glm::vec4 modelPos(wall.pos.x, wall.pos.y, wall.pos.z + wallZOffset, 1.0f);
glm::vec4 modelPosInViewSpace = cam.GetTransformInverse() * modelPos;

// Cull
float cullWidth = lvl.texSizes[wall.texIdx].x * MAP_AND_WALL_SCALE;
float cullHeight = lvl.texSizes[wall.texIdx].y * lvl.wallHeightMultiplier * MAP_AND_WALL_SCALE * 1.5f; // 1.5f to account for max slant
if (!RectangleIsInViewFrustum(cullWidth, cullHeight, modelPosInViewSpace))
{
continue;
}

// Compute transform matrix
glm::mat3 modelTranslate = glm::translate(glm::mat3(), glm::vec2(modelPosInViewSpace.x, modelPosInViewSpace.y));
glm::mat3 transform = modelTranslate * WallAffine[wall.dir];
Expand Down Expand Up @@ -199,6 +207,14 @@ Renderer::SortedRenderList& Renderer::UpdateWallsAndSprites(Camera& cam, LvlData
glm::vec4 modelPosInViewSpace = cam.GetTransformInverse() * modelPos;
modelPosInViewSpace.y += lvl.texSizes[sprite.texIdx].y / 2.0f; // Offset by half-height

// Cull
float cullWidth = lvl.texSizes[sprite.texIdx].x;
float cullHeight = lvl.texSizes[sprite.texIdx].y;
if (!RectangleIsInViewFrustum(cullWidth, cullHeight, modelPosInViewSpace))
{
continue;
}

// Compute transform matrix
glm::mat3 transform = glm::translate(glm::mat3(), glm::vec2(modelPosInViewSpace.x, modelPosInViewSpace.y)) * spriteFlip;

Expand Down Expand Up @@ -302,6 +318,16 @@ bool Renderer::RenderableDepthCompare(const Renderable& left, const Renderable&
}


bool Renderer::RectangleIsInViewFrustum(float width, float height, glm::vec4 position) {
bool leftSideOverRightBound = (position.x - width / 2.0f) > SCREEN_WIDTH / 2.0f;
bool rightSideOverLeftBound = (position.x + width / 2.0f) < -SCREEN_WIDTH / 2.0f;
bool downSideOverUpBound = (position.y - height / 2.0f) > SCREEN_HEIGHT / 2.0f;
bool upSideOverDownBound = (position.y + height / 2.0f) < -SCREEN_HEIGHT / 2.0f;

return !leftSideOverRightBound && !rightSideOverLeftBound && !downSideOverUpBound && !upSideOverDownBound;
}


// Implements https://gamedev.stackexchange.com/a/120897
bool Renderer::CollideCircleBox2D(glm::vec2 circlePos, float circleRadius, BoxCollider box) {
float r1x = box.pos.x - circleRadius;
Expand Down
2 changes: 2 additions & 0 deletions code/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ class Renderer {

static bool RenderableDepthCompare(const Renderable& left, const Renderable& right);

static bool RectangleIsInViewFrustum(float width, float height, glm::vec4 position);

static bool CollideCircleBox2D(glm::vec2 circlePos, float circleRadius, BoxCollider box);

glm::mat3 TileMapAffine;
Expand Down

0 comments on commit 9a416b6

Please sign in to comment.