camera.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2014 Google Inc. All Rights Reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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   // Color camera's coordinates has y pointing downwards so we negate this term.
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 }  // namespace tango_gl


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:18