Go to the documentation of this file.00001 #ifndef OPENTISSUE_KINEMATICS_SKINNING_GL_SKIN_RENDER_H
00002 #define OPENTISSUE_KINEMATICS_SKINNING_GL_SKIN_RENDER_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/utility/gl/gl_util.h>
00013 #include <OpenTissue/kinematics/skinning/skinning_gpu_vertex.h>
00014 #include <OpenTissue/kinematics/skinning/sbs/skinning_create_key.h>
00015
00016 namespace OpenTissue
00017 {
00018
00019 namespace gl
00020 {
00021
00022 class SkinRender
00023 {
00024 private:
00025 GLuint* m_BufferID;
00026 size_t m_bufferSz;
00027 size_t* m_vertexCount;
00028
00029 public:
00030 template<typename skin_type>
00031 void init( skin_type & skin,
00032 typename skin_type::skin_part_type::no_gpu_support* p=0 )
00033 {
00034 }
00035
00036 template<typename skin_type>
00037 void init( skin_type & skin,
00038 typename skin_type::skin_part_type::gpu_support* p=0 )
00039 {
00040 createGPUBuffers( skin );
00041 }
00042
00043 template <typename skin_type,typename skeleton_type>
00044 void render( skin_type & skin,
00045 skeleton_type & skeleton,
00046 typename skin_type::skin_part_type::no_gpu_support* p=0 )
00047 {
00048 typedef typename skin_type::skin_part_type skin_part_type;
00049
00050
00051
00052
00053 skin_type::skin_part_type::pre_render( skin );
00054
00055
00056 skin.update( skeleton );
00057
00058
00059 for(unsigned int i=0;i<skin.m_sz;++i)
00060 {
00061 if(skin.m_skin_parts[i].m_material_idx!=-1)
00062 skin.m_material[skin.m_skin_parts[i].m_material_idx].use();
00063
00064 gl::DrawMesh( skin.m_skin_parts[i] );
00065 }
00066
00067
00068 skin_type::skin_part_type::post_render( skin );
00069 }
00070
00071
00072 template <typename skin_type,typename skeleton_type>
00073 void render( skin_type & skin,
00074 skeleton_type & skeleton,
00075 typename skin_type::skin_part_type::gpu_support* p=0 )
00076 {
00077 typedef typename skin_type::skin_part_type skin_part_type;
00078
00079
00080 skin_type::skin_part_type::pre_render( skin );
00081
00082
00083 skin.update( skeleton );
00084
00085 unsigned int stride = sizeof(OpenTissue::skinning::gpu_vertex);
00086
00087 GLvoid * ptr_vert = BUFFER_OFFSET(0);
00088 GLvoid * ptr_norm = BUFFER_OFFSET(4*sizeof(float));
00089 GLvoid * ptr_weig = BUFFER_OFFSET(7*sizeof(float));
00090 GLvoid * ptr_bidx = BUFFER_OFFSET(11*sizeof(float));
00091
00092
00093
00094 glEnableClientState( GL_VERTEX_ARRAY );
00095 glEnableClientState( GL_NORMAL_ARRAY );
00096 glEnableClientState( GL_COLOR_ARRAY );
00097 glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00098
00099 for( size_t i=0; i<m_bufferSz; ++i )
00100 {
00101 if(skin.m_skin_parts[i].m_material_idx!=-1)
00102 skin.m_material[skin.m_skin_parts[i].m_material_idx].use();
00103
00104 glBindBuffer( GL_ARRAY_BUFFER, m_BufferID[i] );
00105
00106
00107 glVertexPointer ( 4, GL_FLOAT, stride, ptr_vert );
00108 glNormalPointer ( GL_FLOAT, stride, ptr_norm );
00109 glColorPointer ( 4, GL_FLOAT, stride, ptr_weig );
00110 glTexCoordPointer( 4, GL_FLOAT, stride, ptr_bidx );
00111
00112
00113 glDrawArrays( GL_TRIANGLES, 0, (GLsizei)m_vertexCount[i] );
00114 }
00115
00116
00117 glDisableClientState( GL_VERTEX_ARRAY );
00118 glDisableClientState( GL_NORMAL_ARRAY );
00119 glDisableClientState( GL_COLOR_ARRAY );
00120 glDisableClientState( GL_TEXTURE_COORD_ARRAY );
00121
00122
00123 skin_type::skin_part_type::post_render( skin );
00124 }
00125
00126 void cleanup()
00127 {
00128 glDeleteBuffers( (GLsizei)m_bufferSz, m_BufferID );
00129 delete[] m_BufferID;
00130 }
00131
00132 protected:
00133 template<typename skin_type>
00134 void createGPUBuffers( skin_type & skin )
00135 {
00136 typedef typename skin_type::skin_part_type skin_part_type;
00137
00138 skin_part_type* meshArr = skin.m_skin_parts;
00139
00140
00141 m_bufferSz = skin.m_sz;
00142 m_BufferID = new GLuint[m_bufferSz];
00143 m_vertexCount = new size_t[m_bufferSz];
00144 for(size_t i=0; i<m_bufferSz; ++i)
00145 {
00146 m_BufferID[i] = 0;
00147 m_vertexCount[i] = 0;
00148 }
00149
00150
00151 glGenBuffers( (GLsizei)m_bufferSz, m_BufferID );
00152
00153
00154 for(size_t i=0; i<m_bufferSz; ++i)
00155 m_vertexCount[i] = 3*meshArr[i].size_faces();
00156
00157 OpenTissue::skinning::gpu_vertex* vertexData = NULL;
00158
00159
00160 for(size_t i=0; i<m_bufferSz; ++i)
00161 {
00162
00163 vertexData = new OpenTissue::skinning::gpu_vertex[m_vertexCount[i]];
00164
00165
00166 OpenTissue::skinning::gpu_vertex* data = vertexData;
00167
00168 typename skin_part_type::const_face_iterator end = meshArr[i].face_end();
00169 typename skin_part_type::const_face_iterator f = meshArr[i].face_begin();
00170
00171 for(;f!=end;++f)
00172 {
00173 typename skin_part_type::const_face_vertex_circulator v( *f );
00174
00175 for( int j=0; j<3; ++j )
00176 {
00177 data->vertex[0] = v->m_coord(0);
00178 data->vertex[1] = v->m_coord(1);
00179 data->vertex[2] = v->m_coord(2);
00180
00181
00182 data->vertex[3] = skin.m_rc_lut[v->m_key];
00183
00184 data->normal[0] = v->m_normal(0);
00185 data->normal[1] = v->m_normal(1);
00186 data->normal[2] = v->m_normal(2);
00187
00188 data->weight[0] = v->m_weight[0];
00189 data->weight[1] = v->m_weight[1];
00190 data->weight[2] = v->m_weight[2];
00191 data->weight[3] = v->m_weight[3];
00192
00193 data->boneIdx[0] = v->m_bone[0];
00194 data->boneIdx[1] = v->m_bone[1];
00195 data->boneIdx[2] = v->m_bone[2];
00196 data->boneIdx[3] = v->m_bone[3];
00197
00198 ++v;
00199 ++data;
00200 }
00201 }
00202
00203
00204 glBindBuffer( GL_ARRAY_BUFFER, m_BufferID[i] );
00205
00206
00207 glBufferData( GL_ARRAY_BUFFER, m_vertexCount[i]*sizeof(OpenTissue::skinning::gpu_vertex), vertexData, GL_STATIC_DRAW );
00208
00209 delete[] vertexData;
00210 }
00211 }
00212 };
00213
00214 }
00215
00216 }
00217
00218
00219 #endif