Changeset 224 for trunk/src/formats/md5_loader.cc
- Timestamp:
- 01/02/14 20:52:34 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/formats/md5_loader.cc
r200 r224 90 90 { 91 91 // TODO : efficiency dammit 92 md5_mesh 92 md5_mesh* mesh = new md5_mesh; 93 93 int num_verts, num_tris, num_weights; 94 94 … … 99 99 if ( command == "shader" ) 100 100 { 101 sstream >> mesh .shader;102 remove_quotes( mesh .shader );101 sstream >> mesh->shader; 102 remove_quotes( mesh->shader ); 103 103 // texturePath.replace_extension( ".tga" ); 104 104 next_line( sstream ); … … 120 120 next_line( sstream ); 121 121 122 mesh .verts.push_back(vert);123 mesh .texcoord_buffer.push_back( vert.texcoord );122 mesh->verts.push_back(vert); 123 mesh->texcoord_buffer.push_back( vert.texcoord ); 124 124 } 125 125 } … … 136 136 next_line( sstream ); 137 137 138 mesh .tris.push_back( tri );139 mesh .index_buffer.push_back( (uint32)tri.indices[0] );140 mesh .index_buffer.push_back( (uint32)tri.indices[1] );141 mesh .index_buffer.push_back( (uint32)tri.indices[2] );138 mesh->tris.push_back( tri ); 139 mesh->index_buffer.push_back( (uint32)tri.indices[0] ); 140 mesh->index_buffer.push_back( (uint32)tri.indices[1] ); 141 mesh->index_buffer.push_back( (uint32)tri.indices[2] ); 142 142 } 143 143 } … … 156 156 discard( sstream, ")" ); 157 157 next_line( sstream ); 158 mesh .weights.push_back(weight);158 mesh->weights.push_back(weight); 159 159 } 160 160 } … … 180 180 } 181 181 182 bool md5_loader::prepare_mesh( md5_mesh &mesh )183 { 184 mesh .position_buffer.clear();185 mesh .texcoord_buffer.clear();186 187 for ( uint32 i = 0; i < mesh .verts.size(); ++i )188 { 189 md5_vertex& vert = mesh .verts[i];182 bool md5_loader::prepare_mesh( md5_mesh* mesh ) 183 { 184 mesh->position_buffer.clear(); 185 mesh->texcoord_buffer.clear(); 186 187 for ( uint32 i = 0; i < mesh->verts.size(); ++i ) 188 { 189 md5_vertex& vert = mesh->verts[i]; 190 190 191 191 vert.position = glm::vec3(0); … … 195 195 for ( size_t j = 0; j < vert.weight_count; ++j ) 196 196 { 197 md5_weight& weight = mesh .weights[vert.start_weight + j];197 md5_weight& weight = mesh->weights[vert.start_weight + j]; 198 198 md5_joint& joint = m_joints[weight.joint_id]; 199 199 … … 203 203 } 204 204 205 mesh .position_buffer.push_back(vert.position);206 mesh .texcoord_buffer.push_back(vert.texcoord);205 mesh->position_buffer.push_back(vert.position); 206 mesh->texcoord_buffer.push_back(vert.texcoord); 207 207 } 208 208 … … 210 210 } 211 211 212 bool md5_loader::prepare_normals( md5_mesh &mesh )213 { 214 mesh .normal_buffer.clear();215 216 for ( unsigned int i = 0; i < mesh .tris.size(); ++i )217 { 218 const md5_triangle& tri = mesh .tris[i];219 glm::vec3 v1 = mesh .verts[ tri.indices[0] ].position;220 glm::vec3 v2 = mesh .verts[ tri.indices[1] ].position;221 glm::vec3 v3 = mesh .verts[ tri.indices[2] ].position;212 bool md5_loader::prepare_normals( md5_mesh* mesh ) 213 { 214 mesh->normal_buffer.clear(); 215 216 for ( unsigned int i = 0; i < mesh->tris.size(); ++i ) 217 { 218 const md5_triangle& tri = mesh->tris[i]; 219 glm::vec3 v1 = mesh->verts[ tri.indices[0] ].position; 220 glm::vec3 v2 = mesh->verts[ tri.indices[1] ].position; 221 glm::vec3 v3 = mesh->verts[ tri.indices[2] ].position; 222 222 glm::vec3 xyz1 = v3 - v1; 223 223 glm::vec3 xyz2 = v2 - v1; … … 225 225 glm::vec3 normal = glm::cross( xyz1, xyz2 ); 226 226 227 mesh .verts[ tri.indices[0] ].normal += normal;228 mesh .verts[ tri.indices[1] ].normal += normal;229 mesh .verts[ tri.indices[2] ].normal += normal;230 231 const vec2& w1 = mesh .verts[ tri.indices[0] ].texcoord;232 const vec2& w2 = mesh .verts[ tri.indices[1] ].texcoord;233 const vec2& w3 = mesh .verts[ tri.indices[2] ].texcoord;227 mesh->verts[ tri.indices[0] ].normal += normal; 228 mesh->verts[ tri.indices[1] ].normal += normal; 229 mesh->verts[ tri.indices[2] ].normal += normal; 230 231 const vec2& w1 = mesh->verts[ tri.indices[0] ].texcoord; 232 const vec2& w2 = mesh->verts[ tri.indices[1] ].texcoord; 233 const vec2& w3 = mesh->verts[ tri.indices[2] ].texcoord; 234 234 235 235 vec2 st1 = w3 - w1; … … 240 240 vec3 tangent = (( xyz1 * st2.y ) - ( xyz2 * st1.y )) * coef; 241 241 242 mesh .verts[ tri.indices[0] ].tangent += tangent;243 mesh .verts[ tri.indices[1] ].tangent += tangent;244 mesh .verts[ tri.indices[2] ].tangent += tangent;245 } 246 247 for ( size_t i = 0; i < mesh .verts.size(); ++i )248 { 249 md5_vertex& vert = mesh .verts[i];242 mesh->verts[ tri.indices[0] ].tangent += tangent; 243 mesh->verts[ tri.indices[1] ].tangent += tangent; 244 mesh->verts[ tri.indices[2] ].tangent += tangent; 245 } 246 247 for ( size_t i = 0; i < mesh->verts.size(); ++i ) 248 { 249 md5_vertex& vert = mesh->verts[i]; 250 250 251 251 glm::vec3 normal = glm::normalize( vert.normal ); 252 252 glm::vec3 tangent = glm::normalize( vert.tangent ); 253 mesh .normal_buffer.push_back( normal );254 mesh .tangent_buffer.push_back( tangent );253 mesh->normal_buffer.push_back( normal ); 254 mesh->tangent_buffer.push_back( tangent ); 255 255 256 256 vert.normal = glm::vec3(0); … … 259 259 for ( size_t j = 0; j < vert.weight_count; ++j ) 260 260 { 261 const md5_weight& weight = mesh .weights[vert.start_weight + j];261 const md5_weight& weight = mesh->weights[vert.start_weight + j]; 262 262 const md5_joint& joint = m_joints[weight.joint_id]; 263 263 vert.normal += ( normal * joint.orient ) * weight.bias; … … 269 269 } 270 270 271 mesh_data* nv::md5_loader::release_submesh_data( uint32 mesh_id ) 272 { 273 mesh_data_creator m; 274 m.get_positions().assign( m_meshes[mesh_id]->position_buffer.begin(), m_meshes[mesh_id]->position_buffer.begin() ); 275 m.get_normals() .assign( m_meshes[mesh_id]->normal_buffer.begin(), m_meshes[mesh_id]->normal_buffer.begin() ); 276 m.get_tangents() .assign( m_meshes[mesh_id]->tangent_buffer.begin(), m_meshes[mesh_id]->tangent_buffer.begin() ); 277 m.get_texcoords().assign( m_meshes[mesh_id]->texcoord_buffer.begin(), m_meshes[mesh_id]->texcoord_buffer.begin() ); 278 m.get_indices() .assign( m_meshes[mesh_id]->index_buffer.begin(), m_meshes[mesh_id]->index_buffer.begin() ); 279 280 return m.release(); 281 } 282 283 /* 271 284 mesh* md5_loader::release_mesh() 272 285 { … … 287 300 return m; 288 301 } 302 */ 289 303 290 304 md5_animation::md5_animation() … … 539 553 } 540 554 541 bool md5_loader::prepare_animated_mesh( md5_mesh &mesh, const md5_animation::md5_frame_skeleton& skel )542 { 543 for ( unsigned int i = 0; i < mesh .verts.size(); ++i )544 { 545 const md5_vertex& vert = mesh .verts[i];546 glm::vec3& pos = mesh .position_buffer[i];547 glm::vec3& normal = mesh .normal_buffer[i];548 glm::vec3& tangent = mesh .tangent_buffer[i];555 bool md5_loader::prepare_animated_mesh( md5_mesh* mesh, const md5_animation::md5_frame_skeleton& skel ) 556 { 557 for ( unsigned int i = 0; i < mesh->verts.size(); ++i ) 558 { 559 const md5_vertex& vert = mesh->verts[i]; 560 glm::vec3& pos = mesh->position_buffer[i]; 561 glm::vec3& normal = mesh->normal_buffer[i]; 562 glm::vec3& tangent = mesh->tangent_buffer[i]; 549 563 550 564 pos = glm::vec3(0); … … 554 568 for ( size_t j = 0; j < vert.weight_count; ++j ) 555 569 { 556 const md5_weight& weight = mesh .weights[vert.start_weight + j];570 const md5_weight& weight = mesh->weights[vert.start_weight + j]; 557 571 const md5_animation::md5_skeleton_joint& joint = skel.joints[weight.joint_id]; 558 572
Note: See TracChangeset
for help on using the changeset viewer.