Go to the documentation of this file.00001 #ifndef OPENTISSUE_KINEMATICS_SKINNING_LBS_SKINNING_LBS_GPU_H
00002 #define OPENTISSUE_KINEMATICS_SKINNING_LBS_SKINNING_LBS_GPU_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/containers/mesh/trimesh/trimesh.h>
00013 #include <OpenTissue/core/containers/mesh/trimesh/util/trimesh_util.h>
00014 #include <OpenTissue/kinematics/skinning/skinning_traits.h>
00015 #include <cassert>
00016 #include <vector>
00017
00018
00019 #include <OpenTissue/gpu/cg/cg_program.h>
00020
00021 namespace OpenTissue
00022 {
00023 namespace skinning
00024 {
00025
00026 template<typename types>
00027 class LBSGPU
00028 : public trimesh::TriMesh<types,SkinVertexTraits<types> ,SkinFaceTraits<types>, trimesh::TriMeshArrayKernel >
00029 {
00030 public:
00031
00032
00033 typedef int gpu_support;
00034
00035 typedef typename types::matrix3x3_type matrix3x3_type;
00036 typedef typename types::quaternion_type quaternion_type;
00037 typedef typename types::vector3_type vector3_type;
00038 typedef typename types::real_type real_type;
00039
00040
00041 typedef trimesh::TriMesh<types ,SkinVertexTraits<types> ,SkinFaceTraits<types>, OpenTissue::trimesh::TriMeshArrayKernel > base_mesh;
00042
00043 public:
00044 int m_material_idx;
00045
00046 public:
00047
00048 LBSGPU()
00049 : m_material_idx(-1)
00050 {}
00051
00052 public:
00053
00054 bool empty() const
00055 {
00056 return (!(base_mesh::size_faces()>0));
00057 }
00058
00059
00060 template<typename skin_container_type>
00061 static void init_skin_render( skin_container_type & scont )
00062 {
00063 gl::gl_check_errors( "init_rendering: called" );
00064
00065
00066 OpenTissue::cg::startup();
00067
00068
00069 std::string filename =
00070 opentissue_path +
00071 "/OpenTissue/kinematics/skinning/LBS/lbs_skin_shader.cg";
00072 scont.m_vp.load_from_file( cg::Program::vertex_program, filename );
00073 gl::gl_check_errors( "init_fragment_program: program loaded" );
00074 }
00075
00076 template<typename skin_container_type>
00077 static void cleanup_skin_render( skin_container_type & scont )
00078 {
00079 gl::gl_check_errors("cleanup_rendering: called");
00080 OpenTissue::cg::shutdown();
00081 }
00082
00083
00084
00085 template<typename skin_container_type>
00086 static void pre_render( skin_container_type & scont )
00087 {
00088 scont.m_vp.set_modelview_projection_matrix();
00089 scont.m_vp.set_modelview_inverse_transpose();
00090
00091 scont.m_vp.enable();
00092 scont.m_uploadBones = true;
00093 }
00094
00095 template<typename skin_container_type>
00096 static void post_render( skin_container_type & scont )
00097 {
00098 scont.m_vp.disable();
00099 }
00100
00110
00111 template<typename skeleton_type, typename skin_container_type>
00112 void update( skeleton_type & skeleton, skin_container_type & scont )
00113 {
00114 typedef typename skeleton_type::bone_traits bone_traits;
00115 typedef typename types::coordsys_type coordsys_type;
00116
00117
00118 if(this->empty())
00119 return;
00120
00121 if( scont.m_uploadBones )
00122 {
00123
00124 float *allBones = new float[16*skeleton.size()];
00125 unsigned int k=0;
00126
00127
00128 typename skeleton_type::bone_iterator abone = skeleton.begin();
00129 typename skeleton_type::bone_iterator boneEnd = skeleton.end();
00130
00131 for( ; abone!=boneEnd; ++abone )
00132 {
00133
00134 coordsys_type bone_space_transform = bone_traits::convert( abone->bone_space_transform() );
00135
00136 quaternion_type qt = bone_space_transform.Q();
00137 vector3_type trans = bone_space_transform.T();
00138
00139
00140 allBones[0+k*9] = qt.v()(0);
00141 allBones[1+k*9] = qt.v()(1);
00142 allBones[2+k*9] = qt.v()(2);
00143 allBones[3+k*9] = qt.s();
00144 allBones[4+k*9] = 0;
00145 allBones[5+k*9] = 0;
00146 allBones[6+k*9] = trans( 0 );
00147 allBones[7+k*9] = trans( 1 );
00148 allBones[8+k*9] = trans( 2 );
00149
00150
00151 ++k;
00152 }
00153
00154
00155 scont.m_vp.set_floatNxN_array_param( "bones", (int)skeleton.size(), allBones );
00156
00157
00158 delete[] allBones;
00159
00160 scont.m_uploadBones = false;
00161 }
00162 }
00163 };
00164
00165 }
00166 }
00167
00168
00169 #endif