00001 #ifndef OPENTISSUE_KINEMATICS_SKINNING_SKINNING_ANIMATED_SKIN_H 00002 #define OPENTISSUE_KINEMATICS_SKINNING_SKINNING_ANIMATED_SKIN_H 00003 // 00004 // OpenTissue Template Library 00005 // - A generic toolbox for physics-based modeling and simulation. 00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen. 00007 // 00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php 00009 // 00010 #include <OpenTissue/configuration.h> 00011 00012 #include <OpenTissue/utility/gl/gl_util.h> 00013 #include <OpenTissue/core/containers/mesh/trimesh/trimesh.h> 00014 #include <OpenTissue/core/containers/mesh/trimesh/util/trimesh_util.h> 00015 #include <OpenTissue/kinematics/skinning/skinning_traits.h> 00016 #include <OpenTissue/utility/gl/gl_draw_mesh.h> 00017 #include <cassert> 00018 #include <vector> 00019 00020 // Shader program header includes 00021 #include <OpenTissue/gpu/cg/cg_program.h> 00022 00023 namespace OpenTissue 00024 { 00025 namespace skinning 00026 { 00027 00028 template<typename character_types> 00029 class AnimatedSkin 00030 { 00031 public: 00032 typedef typename character_types::skin_part_type skin_part_type; 00033 typedef typename character_types::key_type key_type; 00034 typedef typename character_types::math_types::vector3_type vector3_type; 00035 00036 // Comtainiers for rotation center lookup 00037 typedef std::vector<vector3_type> rc_cached_type; 00038 typedef std::map<key_type, size_t> lut_type; 00039 00040 //--- Shader program 00041 typedef OpenTissue::cg::Program cg_program_type; 00042 00043 public: 00044 static const size_t m_sz = 20u; 00045 skin_part_type m_skin_parts[m_sz]; 00046 OpenTissue::gl::Material m_material[m_sz]; 00047 00048 bool m_uploadBones; 00049 bool m_uploadRc; 00050 size_t m_num_keys; 00051 00052 cg_program_type m_vp; 00053 00054 rc_cached_type m_rc_cached; 00055 lut_type m_rc_lut; 00056 00057 public: 00058 AnimatedSkin() 00059 : m_uploadBones( true ), 00060 m_uploadRc( true ), 00061 m_num_keys( 0u ) 00062 { 00063 } 00064 00065 virtual ~AnimatedSkin() 00066 { 00067 } 00068 00069 void init() 00070 { 00071 skin_part_type::init_skin_render( *this ); 00072 00073 // Create a unique key for each vertex 00074 // Actually only used for sbs gpu skinning 00075 unsigned int idx = 0; 00076 00077 for(size_t i=0u; i<m_sz; ++i) 00078 { 00079 typename skin_part_type::vertex_iterator vertex = m_skin_parts[i].vertex_begin(); 00080 typename skin_part_type::vertex_iterator end = m_skin_parts[i].vertex_end(); 00081 00082 for(;vertex!=end;++vertex) 00083 { 00084 vertex->m_key = create_key( vertex ); 00085 00086 if( vertex->m_influences > 1 ) 00087 { 00088 typename lut_type::iterator lut_it = m_rc_lut.find( vertex->m_key ); 00089 if( lut_it == m_rc_lut.end() ) 00090 { 00091 // Key not in lut... 00092 m_rc_lut[vertex->m_key] = idx; 00093 ++idx; 00094 } 00095 } 00096 } 00097 } 00098 00099 // Tells sbs_gpu how many keys have been created 00100 m_num_keys = m_rc_lut.size(); 00101 } 00102 00103 template <typename skeleton_type> 00104 void update( skeleton_type & skeleton ) 00105 { 00106 for(size_t i=0u; i<m_sz; ++i) 00107 { 00108 m_skin_parts[i].update( skeleton, *this ); 00109 } 00110 } 00111 00112 // Create a software_update and a gpu_update function, use SFINAE to distinguish 00113 00114 void cleanup() 00115 { 00116 skin_part_type::cleanup_skin_render( *this ); 00117 } 00118 }; 00119 00120 } // namespace skinning 00121 } // namespace OpenTissue 00122 00123 //OPENTISSUE_KINEMATICS_SKINNING_SKINNING_ANIMATED_SKIN_H 00124 #endif