Camera now returns a viewing vector.

This commit is contained in:
Fabian Klemp 2014-11-17 13:29:41 +01:00
parent 38b6d33aec
commit a19039426c
3 changed files with 22 additions and 7 deletions

View File

@ -19,6 +19,7 @@ float Camera::getDistance() {
void Camera::setDistance(float distance) { void Camera::setDistance(float distance) {
this->distance = distance; this->distance = distance;
updatePosition();
} }
glm::vec2 Camera::getRotation() { glm::vec2 Camera::getRotation() {
@ -27,8 +28,23 @@ glm::vec2 Camera::getRotation() {
void Camera::setRotation(glm::vec2 rotation) { void Camera::setRotation(glm::vec2 rotation) {
this->rotation = rotation; this->rotation = rotation;
updatePosition();
} }
void Camera::updateRotation(glm::vec2 rotation) { 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<float>(rotation[1], glm::vec3(0.0f, 1.0f, 0.0f)) * glm::rotate<float>(rotation[0], glm::vec3(1.0f, 0.0f, 0.0f));
this->vector = glm::vec3(rotationMatrix * cameraVector);
}
glm::vec3 Camera::getVector() {
return vector;
}

View File

@ -13,9 +13,12 @@ class Camera {
glm::vec2 getRotation(); glm::vec2 getRotation();
void setRotation(glm::vec2 rotation); void setRotation(glm::vec2 rotation);
void updateRotation(glm::vec2 rotation); //adds to current rotation void updateRotation(glm::vec2 rotation); //adds to current rotation
glm::vec3 getVector();
private: private:
void updatePosition();
float distance; float distance;
glm::vec2 rotation; glm::vec2 rotation;
glm::vec3 vector;
}; };
#endif #endif

View File

@ -161,11 +161,7 @@ glm::mat4 Graphics::buildFrustum( float phiInDegree, float _near, float _far, fl
} }
glm::mat4 Graphics::buildViewMatrix(Level* level) { 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<float>(level->getCamera().getRotation()[1], glm::vec3(0.0f, 1.0f, 0.0f)) *glm::rotate<float>(level->getCamera().getRotation()[0], glm::vec3(1.0f, 0.0f, 0.0f));
cameraVector = rotationMatrix * cameraVector;
//construct lookAt (cameraPosition = cameraCenter + 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));
} }