Skip to content

Commit

Permalink
Fix orthogonal camera over rendering
Browse files Browse the repository at this point in the history
also enable skip rendering for it
  • Loading branch information
enginmanap committed Nov 5, 2024
1 parent 70e51b3 commit 2ea450c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
10 changes: 1 addition & 9 deletions Engine/Options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,7 @@
</Parameter>
<Parameter>
<RequestType>FreeNumber</RequestType>
<Description>lightOrthogonalProjectionTopZ</Description>
<Value>5000</Value>
<valueType>Double</valueType>
<IsSet>True</IsSet>
<Index>2</Index>
</Parameter>
<Parameter>
<RequestType>FreeNumber</RequestType>
<Description>lightOrthogonalProjectionZBottom</Description>
<Description>lightOrthogonalProjectionBackOff</Description>
<Value>-5000</Value>
<valueType>Double</valueType>
<IsSet>True</IsSet>
Expand Down
23 changes: 16 additions & 7 deletions src/Camera/OrthographicCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ class OrthographicCamera : public Camera {
std::vector<glm::vec4> frustumPlanes;
std::vector<glm::vec4> frustumCorners;
uint32_t cascadeIndex;
float lightOrthogonalProjectionTopZ, lightOrthogonalProjectionZBottom;
float lightOrthogonalProjectionZBottom;
Options *options;
uint32_t drawLineBufferId = 0;
uint32_t playerDrawLineBufferId = 0;
long frameCounter = 0;
mutable bool dirty = true;

Expand All @@ -40,8 +41,7 @@ class OrthographicCamera : public Camera {

frustumPlanes.resize(6);
this->frustumCorners.resize(8);
options->getOptionOrDefault("lightOrthogonalProjectionTopZ", lightOrthogonalProjectionTopZ, 5000.0f);
options->getOptionOrDefault("lightOrthogonalProjectionZBottom", lightOrthogonalProjectionZBottom, -5000.0f);
options->getOptionOrDefault("lightOrthogonalProjectionBackOff", lightOrthogonalProjectionZBottom, -5000.0f);
}

CameraTypes getType() const override {
Expand Down Expand Up @@ -88,12 +88,10 @@ class OrthographicCamera : public Camera {
}

const glm::mat4 &getCameraMatrixConst() const override {
std::cerr << "Orthogonal Camera can't provide single camera matrix" << std::endl;
return cameraTransformMatrix;
}

const glm::mat4& getProjectionMatrix() const override {
std::cerr << "Orthogonal Camera can't provide single projection matrix" << std::endl;
return orthogonalProjectionMatrix;
}

Expand All @@ -120,13 +118,15 @@ class OrthographicCamera : public Camera {
if(cascadeIndex == 0) {
color = glm::vec3(255, 0, 0);
} else if(cascadeIndex == 1) {
color = glm::vec3(0, 255, 255);
color = glm::vec3(255, 255, 0);
} else if(cascadeIndex == 2) {
color = glm::vec3(0, 0, 255);
} else if(cascadeIndex == 3) {
color = glm::vec3(255, 255, 255);
}
debugDrawFrustum(playerFrustumCorners[cascadeIndex], color, playerDrawLineBufferId, frameCounter);
debugDrawFrustum(frustumCorners, color, drawLineBufferId, frameCounter);

frameCounter++;
}

Expand Down Expand Up @@ -171,14 +171,23 @@ class OrthographicCamera : public Camera {
float maxX = std::numeric_limits<float>::lowest();
float minY = std::numeric_limits<float>::max();
float maxY = std::numeric_limits<float>::lowest();
float maxZ = std::numeric_limits<float>::lowest();
for (const auto& corner : playerFrustumCascadeCorners) {
const auto trf = lightViewMatrix * corner;
minX = std::min(minX, trf.x);
maxX = std::max(maxX, trf.x);
minY = std::min(minY, trf.y);
maxY = std::max(maxY, trf.y);
maxZ = std::max(maxZ, trf.z);
}

constexpr float zMultiplier = 3.0f;
if (maxZ < 0) {
maxZ /= zMultiplier;
} else {
maxZ *= zMultiplier;
}
return glm::ortho(minX, maxX, minY, maxY, lightOrthogonalProjectionZBottom, lightOrthogonalProjectionTopZ);
return glm::ortho(minX, maxX, minY, maxY, lightOrthogonalProjectionZBottom, maxZ);
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ void* fillVisibleObjectPerCamera(const void* visibilityRequestRaw) {
float skipRenderDistance = 0, skipRenderSize = 0, maxSkipRenderSize = 0;
visibilityRequest->options->getOption("LodDistanceList", lodDistances);
glm::mat4 viewMatrix;
if(visibilityRequest->camera->getType() == Camera::CameraTypes::PERSPECTIVE) {
if(visibilityRequest->camera->getType() == Camera::CameraTypes::PERSPECTIVE ||
visibilityRequest->camera->getType() == Camera::CameraTypes::ORTHOGRAPHIC) {
visibilityRequest->options->getOption("SkipRenderDistance", skipRenderDistance);
visibilityRequest->options->getOption("SkipRenderSize", skipRenderSize);
visibilityRequest->options->getOption("MaxSkipRenderSize", maxSkipRenderSize);
Expand Down

0 comments on commit 2ea450c

Please sign in to comment.