frame.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/frame.hpp"
00028 #include "renderstack/matrix.hpp"
00029 #include "renderstack/quaternion.hpp"
00030 
00031 namespace renderstack {
00032 
00033 class frame frame::identity = frame();
00034 
00035 frame::frame()
00036 {
00037     m_local_to_world = matrix::identity();
00038     m_world_to_local = matrix::identity();
00039 }
00040 
00041 bool frame::connect(uniform_bindings &bindings, enum logical_uniform uniform, int slot)
00042 {
00043     switch(uniform)
00044     {
00045         case logical_uniform_local_to_world: return bindings.add(uniform_type_matrix_4, slot, 1, &m_local_to_world.m[0][0]);
00046         case logical_uniform_world_to_local: return bindings.add(uniform_type_matrix_4, slot, 1, &m_world_to_local.m[0][0]);
00047         default: return false;
00048     }
00049 }
00050 
00051 void frame::translate_in_world(vec3 const &t)
00052 {
00053     m_local_to_world.m[3][0] += t.x;
00054     m_local_to_world.m[3][1] += t.y;
00055     m_local_to_world.m[3][2] += t.z;
00056     m_world_to_local = inverse(m_local_to_world);
00057 }
00058 
00059 void frame::rotate_in_world(vec3 const &axis, float angle)
00060 {
00061     vec3    a = axis;
00062     float   half_angle     = 0.5f * angle;
00063     float   half_angle_sin = sinf(half_angle);
00064     float   half_angle_cos = cosf(half_angle);
00065     matrix  local_to_world;
00066 
00067     while(angle < 0)
00068     {
00069         angle += RS_TWO_PI;
00070     }
00071 
00072     while(angle >= RS_TWO_PI)
00073     {
00074         angle -= RS_TWO_PI;
00075     }
00076 
00077     quaternion q = quaternion(axis, half_angle_sin, half_angle_cos);
00078     m_local_to_world *= matrix(q);
00079     m_world_to_local = inverse(m_local_to_world);
00080 }
00081 
00082 void frame::set_hpb_xyz(
00083     vec3 const &hpb,
00084     vec3 const &translation
00085 )
00086 {
00087     make_matrix_hpb_xyz(
00088         m_local_to_world, 
00089         m_world_to_local,
00090         hpb_mod_2pi(hpb),
00091         translation
00092     );
00093 }
00094 
00095 }
00096 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3