-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #586 from voxelinc/glowutils_camera
Switch to glowutils::Camera
- Loading branch information
Showing
10 changed files
with
42 additions
and
202 deletions.
There are no files selected for viewing
Submodule glow
updated
2 files
+10 −5 | source/glowutils/include/glowutils/Camera.h | |
+18 −3 | source/glowutils/source/Camera.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,153 +1,41 @@ | ||
#include "camera.h" | ||
|
||
#include <glow/logging.h> | ||
|
||
#include <glm/gtc/matrix_transform.hpp> | ||
#include <glm/gtc/matrix_inverse.hpp> | ||
|
||
#include <glm/gtx/transform.hpp> | ||
|
||
|
||
Camera::Camera(int viewportWidth, int viewportHeight): | ||
m_fovy(glm::radians(60.f)), | ||
m_aspect(1.f), | ||
m_zNear(1), | ||
m_zFar(9999) | ||
m_orientationDirty(true) | ||
{ | ||
setViewport(glm::ivec2(viewportWidth, viewportHeight)); | ||
} | ||
|
||
Camera::~Camera(){ | ||
|
||
} | ||
|
||
void Camera::viewDirty() { | ||
m_view = glm::mat4_cast(glm::inverse(m_orientation)) * glm::translate(-m_position); | ||
m_viewProjection = m_projection * m_view; | ||
} | ||
|
||
void Camera::projectionDirty() { | ||
m_projection = glm::perspective(m_fovy, m_aspect, m_zNear, m_zFar); | ||
m_projection = glm::translate(m_projectionOffset) * m_projection; | ||
m_viewProjection = m_projection * m_view; | ||
} | ||
|
||
void Camera::move(glm::vec3 dist){ | ||
Transform::move(dist); | ||
viewDirty(); | ||
} | ||
|
||
void Camera::setPosition(glm::vec3 pos){ | ||
Transform::setPosition(pos); | ||
viewDirty(); | ||
} | ||
|
||
void Camera::rotateX(float rot){ | ||
Transform::rotate(glm::angleAxis(rot, glm::vec3(1,0,0))); | ||
viewDirty(); | ||
} | ||
|
||
void Camera::rotateY(float rot){ | ||
Transform::rotate(glm::angleAxis(rot, glm::vec3(0, 1, 0))); | ||
viewDirty(); | ||
} | ||
|
||
void Camera::rotateZ(float rot){ | ||
Transform::rotate(glm::angleAxis(rot, glm::vec3(0, 0, -1))); | ||
viewDirty(); | ||
} | ||
|
||
void Camera::setOrientation(glm::quat quat){ | ||
Transform::setOrientation(quat); | ||
viewDirty(); | ||
} | ||
|
||
const glm::mat4& Camera::view() const{ | ||
return m_view; | ||
} | ||
|
||
const glm::mat4& Camera::viewInverted() const { | ||
return glm::inverse(m_view); | ||
} | ||
|
||
const glm::quat& Camera::orientation() const { | ||
return Transform::orientation(); | ||
} | ||
|
||
const glm::vec3& Camera::position() const { | ||
return m_position; | ||
} | ||
|
||
float Camera::zNear() const { | ||
return m_zNear; | ||
} | ||
|
||
void Camera::setZNear(const float zNear) { | ||
if (zNear == m_zNear) { | ||
return; | ||
} | ||
|
||
m_zNear = zNear; | ||
assert(m_zNear > 0.0); | ||
|
||
projectionDirty(); | ||
} | ||
|
||
float Camera::zFar() const { | ||
return m_zFar; | ||
} | ||
|
||
void Camera::setZFar(const float zFar) { | ||
if (zFar == m_zFar) { | ||
return; | ||
} | ||
setViewport(viewportWidth, viewportHeight); | ||
|
||
m_zFar = zFar; | ||
assert(m_zFar > m_zNear); | ||
projectionDirty(); | ||
} | ||
setFovy(glm::radians(60.0f)); | ||
|
||
float Camera::fovy() const { | ||
return m_fovy; | ||
setZNear(1.0f); | ||
setZFar(10000.0f); | ||
} | ||
|
||
void Camera::setFovy(const float fovy) { | ||
if (fovy == m_fovy) { | ||
return; | ||
glm::quat Camera::orientation() const { | ||
if (m_orientationDirty) { | ||
const_cast<glm::quat&>(m_orientation) = glm::quat_cast(glm::inverse(view())); | ||
m_orientationDirty = false; | ||
} | ||
|
||
m_fovy = fovy; | ||
assert(m_fovy > 0.0); | ||
projectionDirty(); | ||
return m_orientation; | ||
} | ||
|
||
const glm::ivec2 Camera::viewport() const { | ||
return m_viewport; | ||
} | ||
void Camera::setOrientation(const glm::quat& orientation) { | ||
glowutils::Camera::setUp(orientation * glm::vec3(0, 1, 0)); | ||
glowutils::Camera::setCenter(eye() + orientation * glm::vec3(0, 0, -1)); | ||
|
||
void Camera::setViewport(const glm::ivec2& viewport) { | ||
if (viewport == m_viewport) { | ||
return; | ||
} | ||
|
||
m_aspect = viewport.x / glm::max(static_cast<float>(viewport.y), 1.f); | ||
m_viewport = viewport; | ||
projectionDirty(); | ||
// changed() will be called anyway, no need to set m_orientationDirty = true | ||
} | ||
|
||
const glm::vec3& Camera::projectionOffset() const { | ||
return m_projectionOffset; | ||
void Camera::changed() const { | ||
glowutils::Camera::changed(); | ||
m_orientationDirty = true; | ||
} | ||
|
||
void Camera::setProjectionOffset(const glm::vec3& projectionOffset) { | ||
m_projectionOffset = projectionOffset; | ||
projectionDirty(); | ||
} | ||
|
||
float Camera::aspectRatio() const { | ||
return m_aspect; | ||
} | ||
|
||
const glm::mat4& Camera::projection() const { | ||
return m_projection; | ||
} | ||
|
||
const glm::mat4& Camera::viewProjection() const { | ||
return m_viewProjection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,24 @@ | ||
#pragma once | ||
|
||
#include <glowutils/Camera.h> | ||
|
||
#include <glm/glm.hpp> | ||
|
||
#include "geometry/transform.h" | ||
|
||
/** | ||
* Represents the camera. matrix thus is the view matrix for all other objects | ||
*/ | ||
class Camera : protected Transform { //protected so we don't have matrix, because we want view | ||
|
||
class Camera : public glowutils::Camera { | ||
public: | ||
Camera(int viewportWidth, int viewportHeight); | ||
virtual ~Camera(); | ||
|
||
/* Overwrite WorldObject functions for performance: | ||
* WorldObject recalculates the matrix on every read access, | ||
* Camera should recalculate on every write access, as reads are more common */ | ||
|
||
void move(glm::vec3 dist); | ||
void setPosition(glm::vec3 pos); | ||
|
||
void rotateX(float rot); | ||
void rotateY(float rot); | ||
void rotateZ(float rot); | ||
void setOrientation(glm::quat quat); | ||
|
||
const glm::mat4& view() const; | ||
const glm::mat4& viewInverted() const; | ||
const glm::quat& orientation() const; | ||
const glm::vec3& position() const; | ||
|
||
/* Projection from glow::Camera */ | ||
|
||
float zNear() const; | ||
void setZNear(float zNear); | ||
float zFar() const; | ||
void setZFar(float zFar); | ||
|
||
float fovy() const; | ||
void setFovy(float fovy); | ||
glm::quat orientation() const; | ||
void setOrientation(const glm::quat& orientation); | ||
|
||
const glm::ivec2 viewport() const; | ||
void setViewport(const glm::ivec2 & viewport); | ||
|
||
const glm::vec3& projectionOffset() const; | ||
void setProjectionOffset(const glm::vec3& projectionOffset); | ||
|
||
float aspectRatio() const; | ||
|
||
const glm::mat4& projection() const; | ||
|
||
const glm::mat4& viewProjection() const; | ||
void changed() const override; | ||
|
||
|
||
protected: | ||
void viewDirty(); | ||
void projectionDirty(); | ||
|
||
float m_fovy; | ||
float m_aspect; | ||
float m_zNear; | ||
float m_zFar; | ||
|
||
glm::ivec2 m_viewport; | ||
|
||
glm::vec3 m_projectionOffset; | ||
|
||
glm::mat4 m_view; | ||
glm::mat4 m_projection; | ||
glm::mat4 m_viewProjection; | ||
mutable bool m_orientationDirty; | ||
glm::quat m_orientation; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
#include "eyeside.h" | ||
#include "property/property.h" | ||
|
||
|
||
namespace glow { | ||
class FrameBufferObject; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters