Skip to content

Commit

Permalink
Implement configurable frustum parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
craigrmccown committed Apr 1, 2023
1 parent 4691910 commit 2116b0c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ int main()
mouse::listenForMovement(window);

orc::Scene scene;
scene.GetCamera().SetPerspective(glm::radians(45.0f), (float)windowWidth/(float)windowHeight);
scene.GetCamera().SetAspectRatio((float)windowWidth/(float)windowHeight);
scene.GetCamera().Translate(0, 0, 10);

// This model is not checked into source control for practical reasons
Expand Down
2 changes: 1 addition & 1 deletion libs/orc/examples/basic_usage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main(int argc, char *argv[])

// Create scene and position camera
orc::Scene scene;
scene.GetCamera().SetPerspective(M_PI_4, windowWidth/windowHeight);
scene.GetCamera().SetAspectRatio(windowWidth/windowHeight);
scene.GetCamera().Translate(6, 6, 12);
scene.GetCamera().Rotate(M_PI_4/1.25, -M_PI_4/2.5, 0);

Expand Down
41 changes: 34 additions & 7 deletions libs/orc/src/orc/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#include "camera.hpp"
#include "visitor.hpp"

const float defaultFieldOfView = glm::radians(45.0f), defaultAspectRatio = 16.0f/9.0f;
const float
defaultFieldOfView = glm::radians(45.0f),
defaultAspectRatio = 16.0f/9.0f,
defaultNearClip = 0.1f,
defaultFarClip = 200.0f;

static void copyVecToRow(const glm::vec3 &v, glm::mat4 &m, int r)
{
Expand All @@ -20,8 +24,11 @@ namespace orc
}

Camera::Camera()
: fieldOfView(defaultFieldOfView)
, aspectRatio(defaultAspectRatio)
, nearClip(defaultNearClip)
, farClip(defaultFarClip)
{
SetPerspective(defaultFieldOfView, defaultAspectRatio);
ComputeMxs();
}

Expand All @@ -30,23 +37,38 @@ namespace orc
visitor.VisitCamera(this);
}

void Camera::SetPerspective(float fieldOfView, float aspectRatio)
void Camera::SetFieldOfView(float fieldOfView)
{
// TODO: Configurable frustum distances
projectionMx = glm::perspective(fieldOfView, aspectRatio, 0.1f, 100.0f);
this->fieldOfView = fieldOfView;
}

void Camera::SetAspectRatio(float aspectRatio)
{
this->aspectRatio = aspectRatio;
}

void Camera::SetClippingDistance(float near, float far)
{
if (near <= 0)
{
throw std::logic_error("Near clipping distance must be greater than 0");
}

nearClip = near;
farClip = far;
}

void Camera::ComputeMxs()
{
Node::ComputeMxs();
viewProjectionMx = projectionMx * GetViewMx();
viewProjectionMx = ComputeProjectionMx() * ComputeViewMx();
}

glm::mat4 Camera::GetViewProjectionMx() const {
return viewProjectionMx;
}

glm::mat4 Camera::GetViewMx() const
glm::mat4 Camera::ComputeViewMx() const
{
glm::vec3 pos = GetPosition();
glm::vec3 up = GetUp();
Expand Down Expand Up @@ -80,4 +102,9 @@ namespace orc

return viewMx;
}

glm::mat4 Camera::ComputeProjectionMx() const
{
return glm::perspective(fieldOfView, aspectRatio, nearClip, farClip);
}
}
19 changes: 16 additions & 3 deletions libs/orc/src/orc/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ namespace orc

void Dispatch(NodeVisitor &visitor) override;

void SetPerspective(float fieldOfView, float aspectRatio);
// Sets the camera's field of view angle, expressed in radians. Defaults
// to pi/4 (45 deg).
void SetFieldOfView(float fieldOfView);

// Sets the aspect ratio of the perspective projection. Usually this
// should be set to the screen's width/height. Defaults to 16/9,
// although this is rarely correct.
void SetAspectRatio(float aspectRatio);

// Sets the frustum distances of the camera. Distances that fall out of
// this range will be clipped. Near distance must be a non-zero,
// positive number
void SetClippingDistance(float near, float far);

// See Node.ComputeMxs. Also computes the view-projection matrix.
void ComputeMxs() override;
Expand All @@ -26,9 +38,10 @@ namespace orc
Camera();

private:
glm::mat4 projectionMx;
glm::mat4 viewProjectionMx;
float fieldOfView, aspectRatio, nearClip, farClip;

glm::mat4 GetViewMx() const;
glm::mat4 ComputeViewMx() const;
glm::mat4 ComputeProjectionMx() const;
};
}

0 comments on commit 2116b0c

Please sign in to comment.