From a19039426c1b7668df0269187b60c0ebcc89a56f Mon Sep 17 00:00:00 2001 From: Fabian Klemp Date: Mon, 17 Nov 2014 13:29:41 +0100 Subject: [PATCH] Camera now returns a viewing vector. --- camera.cc | 18 +++++++++++++++++- camera.hh | 3 +++ graphics.cc | 8 ++------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/camera.cc b/camera.cc index c5b3c77..647ac2a 100644 --- a/camera.cc +++ b/camera.cc @@ -19,6 +19,7 @@ float Camera::getDistance() { void Camera::setDistance(float distance) { this->distance = distance; + updatePosition(); } glm::vec2 Camera::getRotation() { @@ -27,8 +28,23 @@ glm::vec2 Camera::getRotation() { void Camera::setRotation(glm::vec2 rotation) { this->rotation = rotation; + updatePosition(); } void Camera::updateRotation(glm::vec2 rotation) { - this->rotation += rotation;; + this->rotation += rotation; + updatePosition(); } + +void Camera::updatePosition() { + glm::vec4 cameraVector = glm::vec4(0.0f, 0.0f, distance, 0.0f); + // rotate vector + glm::mat4 rotationMatrix = + glm::rotate(rotation[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate(rotation[0], glm::vec3(1.0f, 0.0f, 0.0f)); + this->vector = glm::vec3(rotationMatrix * cameraVector); +} + +glm::vec3 Camera::getVector() { + return vector; +} + diff --git a/camera.hh b/camera.hh index 7391fa3..c76014f 100644 --- a/camera.hh +++ b/camera.hh @@ -13,9 +13,12 @@ class Camera { glm::vec2 getRotation(); void setRotation(glm::vec2 rotation); void updateRotation(glm::vec2 rotation); //adds to current rotation + glm::vec3 getVector(); private: + void updatePosition(); float distance; glm::vec2 rotation; + glm::vec3 vector; }; #endif diff --git a/graphics.cc b/graphics.cc index 651fe00..1a10797 100644 --- a/graphics.cc +++ b/graphics.cc @@ -161,11 +161,7 @@ glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, fl } glm::mat4 Graphics::buildViewMatrix(Level* level) { - glm::vec4 cameraVector = glm::vec4(0.0f, 0.0f, level->getCamera().getDistance(), 0.0f); - // rotate vector - glm::mat4 rotationMatrix = - glm::rotate(level->getCamera().getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) *glm::rotate(level->getCamera().getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f)); - cameraVector = rotationMatrix * cameraVector; //construct lookAt (cameraPosition = cameraCenter + cameraVector - return glm::lookAt(level->getCameraCenter()->getPosition() + glm::vec3(cameraVector), level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f)); + return glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()), + level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f)); }