Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "tango-gl/camera.h"
00018 #include "tango-gl/util.h"
00019
00020 namespace tango_gl {
00021
00022 Camera::Camera() {
00023 field_of_view_ = 45.0f * DEGREE_2_RADIANS;
00024 aspect_ratio_ = 4.0f / 3.0f;
00025 width_ = 800.0f;
00026 height_ = 600.0f;
00027 near_clip_plane_ = 0.2f;
00028 far_clip_plane_ = 1000.0f;
00029 ortho_ = false;
00030 orthoScale_ = 2.0f;
00031 orthoCropFactor_ = -1.0f;
00032 }
00033
00034 glm::mat4 Camera::GetViewMatrix() {
00035 return glm::inverse(GetTransformationMatrix());
00036 }
00037
00038 glm::mat4 Camera::GetProjectionMatrix() {
00039 if(ortho_)
00040 {
00041 return glm::ortho(-orthoScale_*aspect_ratio_, orthoScale_*aspect_ratio_, -orthoScale_, orthoScale_, orthoScale_ + orthoCropFactor_, far_clip_plane_);
00042 }
00043 return glm::perspective(field_of_view_, aspect_ratio_, near_clip_plane_, far_clip_plane_);
00044 }
00045
00046 void Camera::SetWindowSize(float width, float height) {
00047 width_ = width;
00048 height_ = height;
00049 aspect_ratio_ = width/height;
00050 }
00051
00052 void Camera::SetFieldOfView(float fov) {
00053 field_of_view_ = fov * DEGREE_2_RADIANS;
00054 }
00055
00056 void Camera::SetNearFarClipPlanes(const float near, const float far)
00057 {
00058 near_clip_plane_ = near;
00059 far_clip_plane_ = far;
00060 }
00061
00062 Camera::~Camera() {
00063 }
00064
00065 glm::mat4 Camera::ProjectionMatrixForCameraIntrinsics(float width, float height,
00066 float fx, float fy,
00067 float cx, float cy,
00068 float near, float far) {
00069 const float xscale = near / fx;
00070 const float yscale = near / fy;
00071
00072 const float xoffset = (cx - (width / 2.0)) * xscale;
00073
00074 const float yoffset = -(cy - (height / 2.0)) * yscale;
00075
00076 return glm::frustum(xscale * -width / 2.0f - xoffset,
00077 xscale * width / 2.0f - xoffset,
00078 yscale * -height / 2.0f - yoffset,
00079 yscale * height / 2.0f - yoffset,
00080 near, far);
00081 }
00082
00083 }