Skip to content

Commit

Permalink
Merge pull request #586 from voxelinc/glowutils_camera
Browse files Browse the repository at this point in the history
Switch to glowutils::Camera
  • Loading branch information
Moritz Eyssen committed Apr 27, 2014
2 parents 9ed3223 + 2c16c45 commit 05a4327
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 202 deletions.
2 changes: 1 addition & 1 deletion lib/glow
154 changes: 21 additions & 133 deletions src/camera/camera.cpp
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;
}
66 changes: 9 additions & 57 deletions src/camera/camera.h
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;
};

2 changes: 1 addition & 1 deletion src/display/monoview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void MonoView::draw(const Scene& scene, const CameraHead& cameraHead) {
glViewport(m_viewport.x(), m_viewport.y(), sampleWidth, sampleHeight);

m_camera->setViewport(glm::ivec2(sampleWidth, sampleHeight));
m_camera->setPosition(cameraHead.position());
m_camera->setEye(cameraHead.position());
m_camera->setOrientation(cameraHead.orientation());

scene.draw(*m_camera, glow::FrameBufferObject::defaultFBO(), m_viewport);
Expand Down
2 changes: 1 addition & 1 deletion src/display/rendering/skybox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void Skybox::initialize() {
glm::vec3(-1, -1, 0),
glm::vec3(1, -1, 0),
glm::vec3(1, 1, 0),
glm::vec3(-1, 1, 0)
glm::vec3(-1, 1, 0)
};

m_vertexArrayObject = new glow::VertexArrayObject();
Expand Down
4 changes: 2 additions & 2 deletions src/display/rendering/starfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void Starfield::createBinding(int index, std::string name, int offset, int size)
}

void Starfield::addLocation(const Camera& camera, int side) {
CameraLocation location = CameraLocation{ m_time, camera.position(), camera.orientation() };
CameraLocation location = CameraLocation{ m_time, camera.eye(), camera.orientation() };
m_locations[side].push_back(location);
}

Expand Down Expand Up @@ -172,7 +172,7 @@ glm::mat4 Starfield::getMatrixFromPast(const Camera& camera, int side) {
assert(interpolation >= 0 && interpolation <= 1);

Camera c(camera);
c.setPosition(glm::mix(after.position, before.position, interpolation));
c.setEye(glm::mix(after.position, before.position, interpolation));
c.setOrientation(glm::slerp(after.orientation, before.orientation, interpolation));

return c.viewProjection();
Expand Down
11 changes: 5 additions & 6 deletions src/display/stereovieweye.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ StereoViewEye::StereoViewEye(const glm::ivec2& viewportResolution, const StereoR
m_camera(new Camera(viewportResolution.x, viewportResolution.y)),
m_distortionScale(stereoRenderInfo.distortionScale()),
m_fbo(new FrameBuffer()),
m_antialiasing("vfx.antialiasing")
m_antialiasing("vfx.antialiasing")
{
setViewportResolution(viewportResolution);

Expand All @@ -35,18 +35,16 @@ StereoViewEye::StereoViewEye(const glm::ivec2& viewportResolution, const StereoR
}
}

StereoViewEye::~StereoViewEye() = default;

const Camera& StereoViewEye::camera() const {
return *m_camera;
}

StereoViewEye::~StereoViewEye() = default;

FrameBuffer& StereoViewEye::fbo() {
return *m_fbo;
}



void StereoViewEye::draw(const Scene& scene, const CameraHead& cameraHead) {
int samplingFactor = 1;
if (m_antialiasing.get() == Antialiasing::SSAA) {
Expand All @@ -56,7 +54,7 @@ void StereoViewEye::draw(const Scene& scene, const CameraHead& cameraHead) {
int sampleHeight = static_cast<int>(m_textureSize.y * samplingFactor);

m_camera->setViewport(glm::ivec2(sampleWidth, sampleHeight));
m_camera->setPosition(cameraHead.position() + cameraHead.orientation() * m_offset);
m_camera->setEye(cameraHead.position() + cameraHead.orientation() * m_offset);
m_camera->setOrientation(cameraHead.orientation());

m_fbo->bind();
Expand All @@ -76,3 +74,4 @@ void StereoViewEye::setViewportResolution(const glm::ivec2& viewportResolution)

m_fbo->setResolution(m_textureSize);
}

1 change: 1 addition & 0 deletions src/display/stereovieweye.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "eyeside.h"
#include "property/property.h"


namespace glow {
class FrameBufferObject;
}
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ int main(int argc, char* argv[]) {
game->viewer().switchToStereoView(StereoRenderInfo::dummy());
}
}
game->gamePlay().updateView();

delete loadingScreen;
delete initTimer;
Expand Down
1 change: 0 additions & 1 deletion src/utils/inertiafollower.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* Follows a position and orientation with a given inertia between
* 0 = instant follow and 100 = never moves
*/

class InertiaFollower {
public:
InertiaFollower(float directionalInertia, float angularInertia);
Expand Down

0 comments on commit 05a4327

Please sign in to comment.