camera.cpp

Go to the documentation of this file.
00001 /*
00002     RenderStack  Support library for OpenGL 3+
00003     Copyright (C) 2010  Timo Suoranta
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00027 #include "renderstack/camera.hpp"
00028 #include "renderstack/matrix.hpp"
00029 #include "renderstack/viewport.hpp"
00030 
00031 namespace renderstack {
00032 
00033 camera::camera()
00034 {
00035     m_near_clip       = 0.1f;
00036     m_far_clip        = 100.0f;
00037     m_projection_type = camera_projection_perspective_vertical;
00038     m_fov             = 90.0f * RS_PI / 180.0f;
00039 }
00040 
00041 bool camera::connect(uniform_bindings &bindings, enum logical_uniform uniform, int slot)
00042 {
00043     switch(uniform)
00044     {
00045         case logical_uniform_local_to_clip         : return bindings.add(uniform_type_matrix_4, slot, 1, &frame_to_clip().m[0][0]);
00046         case logical_uniform_clip_to_local         : return bindings.add(uniform_type_matrix_4, slot, 1, &clip_to_local().m[0][0]);
00047         case logical_uniform_world_to_clip         : return bindings.add(uniform_type_matrix_4, slot, 1, &world_to_clip().m[0][0]);
00048         case logical_uniform_clip_to_world         : return bindings.add(uniform_type_matrix_4, slot, 1, &clip_to_world().m[0][0]);
00049         case logical_uniform_view_position_in_world: return bindings.add(uniform_type_vec3,     slot, 1, &local_to_world().m[3][0]);
00050         default: 
00051             return false;
00052     }
00053 }
00054 
00055 void camera::update(viewport const &viewport)
00056 {
00057     switch(m_projection_type)
00058     {
00059         case camera_projection_perspective_horizontal:
00060         {
00061             m_local_to_clip = make_matrix_perspective_horizontal(
00062                 m_fov,
00063                 viewport.aspect_ratio(),
00064                 m_near_clip,
00065                 m_far_clip
00066             );
00067             break;
00068         }
00069         case camera_projection_perspective_vertical:
00070         {
00071             m_local_to_clip = make_matrix_perspective_vertical(
00072                 m_fov,
00073                 viewport.aspect_ratio(),
00074                 m_near_clip,
00075                 m_far_clip
00076             );
00077             break;
00078         }
00079         case camera_projection_orthogonal_horizontal:
00080         {
00081             m_local_to_clip = make_matrix_orthogonal_centered(
00082                 m_ortho_width,
00083                 m_ortho_width / viewport.aspect_ratio(),
00084                 m_near_clip,
00085                 m_far_clip
00086             );
00087             break;
00088         }
00089         case camera_projection_orthogonal_vertical:
00090         {
00091             m_local_to_clip = make_matrix_orthogonal_centered(
00092                 m_ortho_height * viewport.aspect_ratio(),
00093                 m_ortho_height,
00094                 m_near_clip,
00095                 m_far_clip
00096             );
00097             break;
00098         }
00099         case camera_projection_orthogonal:
00100         {
00101             m_local_to_clip = make_matrix_orthogonal(
00102                 0,
00103                 m_ortho_width,
00104                 0,
00105                 m_ortho_height,
00106                 m_near_clip, 
00107                 m_far_clip
00108             );
00109             break;
00110         }
00111         default:
00112         {
00113             throw invalid_camera_projection_exception();
00114         }
00115     }
00116 
00117     {
00118         m_clip_to_local = inverse(m_local_to_clip);
00119     }
00120 
00121     m_world_to_clip = local_to_clip() * world_to_local();
00122     m_clip_to_world = local_to_world() * clip_to_local();
00123 }
00124 
00125 void camera::update_frame(frame const &frame)
00126 {
00127     m_frame_to_clip   = this->world_to_clip() * frame.local_to_world();
00128     m_frame_to_camera = this->world_to_local() * frame.local_to_world();
00129 }
00130 
00131 }
00132 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3