- Timestamp:
- 01/02/14 20:52:34 (11 years ago)
- Location:
- trunk/src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/formats/md2_loader.cc
r204 r224 241 241 } 242 242 243 mesh_data* nv::md2_loader::release_mesh_data() 244 { 245 mesh_data_creator m; 246 247 load_positions( m.get_positions() ); 248 load_normals( m.get_normals() ); 249 load_texcoords( m.get_texcoords() ); 250 load_indicies( m.get_indices() ); 251 252 m_size = m.get_indices().size(); 253 return m.release(); 254 } 255 256 mesh_data* nv::md2_loader::get_frame( sint32 frame ) 257 { 258 mesh_data_creator m; 259 260 load_positions( m.get_positions(), frame ); 261 load_normals( m.get_normals(), frame ); 262 load_texcoords( m.get_texcoords() ); 263 load_indicies( m.get_indices() ); 264 265 m_size = m.get_indices().size(); 266 return m.release(); 267 } 268 269 270 /* 243 271 mesh* nv::md2_loader::release_mesh() 244 272 { … … 255 283 vertex_attribute< vec3 >* normal = m->add_attribute<vec3>("nv_normal"); 256 284 vertex_attribute< vec2 >* texcoord = m->add_attribute<vec2>("nv_texcoord"); 257 vertex_attribute< uint 16 >* indices = m->add_indices<uint16>();285 vertex_attribute< uint32 >* indices = m->add_indices<uint32>(); 258 286 259 287 load_positions( position->get(), frame ); … … 266 294 return m; 267 295 } 296 */ 268 297 269 298 size_t md2_loader::get_max_frames() const … … 344 373 } 345 374 346 void md2_loader::load_indicies( std::vector<uint 16>& idx )375 void md2_loader::load_indicies( std::vector<uint32>& idx ) 347 376 { 348 377 idx.assign( m_new_indexes.begin(), m_new_indexes.end() ); -
trunk/src/formats/md3_loader.cc
r200 r224 286 286 } 287 287 288 /* 288 289 mesh* nv::md3_loader::release_mesh() 289 290 { … … 314 315 vertex_attribute< vec3 >* normal = m->add_attribute<vec3>("nv_normal"); 315 316 vertex_attribute< vec2 >* texcoord = m->add_attribute<vec2>("nv_texcoord"); 316 vertex_attribute< uint 16 >* indices = m->add_indices<uint16>();317 vertex_attribute< uint32 >* indices = m->add_indices<uint32>(); 317 318 318 319 load_positions( position->get(), frame ); … … 325 326 return m; 326 327 } 328 */ 327 329 328 330 void nv::md3_loader::load_tags( std::vector<mat4>& t, const std::string& tag ) … … 349 351 } 350 352 353 mesh_data* nv::md3_loader::release_mesh_data() 354 { 355 mesh_data_creator m; 356 357 load_positions( m.get_positions() ); 358 load_normals( m.get_normals() ); 359 load_texcoords( m.get_texcoords() ); 360 load_indicies( m.get_indices() ); 361 362 std::vector< std::string > names; 363 load_tag_names( names ); 364 365 for ( auto& name : names ) 366 { 367 load_tags( m.get_tag_map()[ name ], name ); 368 } 369 370 return m.release(); 371 } 372 373 mesh_data* nv::md3_loader::get_frame( sint32 frame ) 374 { 375 mesh_data_creator m; 376 377 load_positions( m.get_positions(), frame ); 378 load_normals( m.get_normals(), frame ); 379 load_texcoords( m.get_texcoords() ); 380 load_indicies( m.get_indices() ); 381 382 std::vector< std::string > names; 383 load_tag_names( names ); 384 385 for ( auto& name : names ) 386 { 387 load_tags( m.get_tag_map()[ name ], name ); 388 } 389 return m.release(); 390 } 391 351 392 size_t md3_loader::get_max_frames() const 352 393 { … … 465 506 } 466 507 467 void md3_loader::load_indicies( std::vector<uint 16>& idx )508 void md3_loader::load_indicies( std::vector<uint32>& idx ) 468 509 { 469 510 md3_t* md3 = (md3_t*)m_md3; … … 480 521 { 481 522 const md3_triangle_t& t = surface.triangles[j]; 482 idx.push_back( static_cast< uint 16>( index_base + t.indexes[0] ) );483 idx.push_back( static_cast< uint 16>( index_base + t.indexes[1] ) );484 idx.push_back( static_cast< uint 16>( index_base + t.indexes[2] ) );523 idx.push_back( static_cast< uint32 >( index_base + t.indexes[0] ) ); 524 idx.push_back( static_cast< uint32 >( index_base + t.indexes[1] ) ); 525 idx.push_back( static_cast< uint32 >( index_base + t.indexes[2] ) ); 485 526 } 486 527 … … 490 531 } 491 532 492 keyframed_mesh_data::keyframed_mesh_data( keyframed_loader* loader )493 {494 loader->load_positions( m_positions );495 loader->load_normals( m_normals );496 loader->load_texcoords( m_texcoords );497 loader->load_indicies( m_indices );498 499 std::vector< std::string > names;500 501 md3_loader* md3loader = dynamic_cast< md3_loader* >( loader );502 if ( md3loader != nullptr )503 {504 md3loader->load_tag_names( names );505 for ( auto& name : names )506 {507 md3loader->load_tags( m_tags[ name ], name );508 }509 }510 511 m_frames = loader->get_max_frames();512 }513 -
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 -
trunk/src/formats/obj_loader.cc
r204 r224 119 119 struct mesh_obj_reader : public obj_reader 120 120 { 121 mesh_obj_reader( mesh * m ) : m_position( nullptr ), m_normal( nullptr ), m_tex_coord( nullptr ), m_tangent( nullptr ),m_mesh( m ) {}121 mesh_obj_reader( mesh_data_creator* m ) : m_mesh( m ) {} 122 122 virtual std::size_t add_face( uint32* v, uint32* t, uint32* n, size_t count ); 123 123 virtual void calculate_tangents(); 124 124 125 vertex_attribute< vec3 >* m_position; 126 vertex_attribute< vec3 >* m_normal; 127 vertex_attribute< vec2 >* m_tex_coord; 128 vertex_attribute< vec4 >* m_tangent; 129 mesh* m_mesh; 125 mesh_data_creator* m_mesh; 130 126 }; 131 127 … … 138 134 } 139 135 140 if ( m_position == nullptr )141 {142 m_position = m_mesh->add_attribute< vec3 >( "nv_position" );143 }144 if ( m_tex_coord == nullptr )145 {146 m_tex_coord = m_mesh->add_attribute< vec2 >( "nv_texcoord" );147 }148 if ( m_normal == nullptr && ni != nullptr )149 {150 m_normal = m_mesh->add_attribute< vec3 >( "nv_normal" );151 }152 153 136 // TODO : support if normals not present; 154 137 155 std::vector< vec3 >& vp = m_ position->get();156 std::vector< vec 2 >& vt = m_tex_coord->get();157 std::vector< vec 3 >& vn = m_normal->get();138 std::vector< vec3 >& vp = m_mesh->get_positions(); 139 std::vector< vec3 >& vn = m_mesh->get_normals(); 140 std::vector< vec2 >& vt = m_mesh->get_texcoords(); 158 141 159 142 std::size_t result = 0; … … 174 157 void mesh_obj_reader::calculate_tangents() 175 158 { 176 m_tangent = m_mesh->add_attribute< vec4 >( "nv_tangent" ); 177 178 std::vector< vec3 >& vp = m_position->get(); 179 std::vector< vec2 >& vt = m_tex_coord->get(); 180 std::vector< vec3 >& vn = m_normal->get(); 181 std::vector< vec4 >& tg = m_tangent->get(); 159 const std::vector< vec3 >& vp = m_mesh->get_positions(); 160 const std::vector< vec2 >& vt = m_mesh->get_texcoords(); 161 const std::vector< vec3 >& vn = m_mesh->get_normals(); 162 std::vector< vec3 >& tg = m_mesh->get_tangents(); 182 163 183 164 size_t count = vp.size(); … … 225 206 tan1[i3] += sdir; 226 207 208 // tan2 not needed anymore?? 227 209 tan2[i1] += tdir; 228 210 tan2[i2] += tdir; … … 235 217 const vec3& t = tan1[a]; 236 218 237 tg[a] = vec 4( glm::normalize(t - n * glm::dot( n, t )),238 (glm::dot(glm::cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f );219 tg[a] = vec3( glm::normalize(t - n * glm::dot( n, t )) ); 220 //tg[a][3] = (glm::dot(glm::cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f; 239 221 } 240 222 … … 258 240 delete m_mesh; 259 241 } 260 m _mesh = new mesh();261 mesh_obj_reader reader( m_mesh);242 mesh_data_creator creator; 243 mesh_obj_reader reader( &creator ); 262 244 std_stream sstream( &source ); 263 245 reader.read_stream( sstream ); … … 267 249 reader.calculate_tangents(); 268 250 } 251 m_mesh = creator.release(); 269 252 return true; 270 253 } -
trunk/src/gfx/keyframed_mesh.cc
r223 r224 15 15 using namespace nv; 16 16 17 keyframed_mesh::keyframed_mesh( context* a_context, keyframed_mesh_data* a_data, program* a_program )17 keyframed_mesh::keyframed_mesh( context* a_context, mesh_data* a_data, program* a_program ) 18 18 : m_context( a_context ) 19 19 , m_data( a_data ) … … 112 112 } 113 113 114 keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, keyframed_mesh_data* a_data, program* a_program )114 keyframed_mesh_gpu::keyframed_mesh_gpu( context* a_context, mesh_data* a_data, program* a_program ) 115 115 : keyframed_mesh( a_context, a_data, a_program ) 116 116 , m_loc_next_position( 0 ) … … 133 133 vb = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec2 ), (void*)m_data->get_texcoords().data() ); 134 134 m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 ); 135 nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint 16), (void*)m_data->get_indices().data() );136 m_va->set_index_buffer( ib, nv::U SHORT, true );135 nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() ); 136 m_va->set_index_buffer( ib, nv::UINT, true ); 137 137 } 138 138 … … 156 156 157 157 158 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, keyframed_mesh_data* a_data, program* a_program )158 nv::keyframed_mesh_cpu::keyframed_mesh_cpu( context* a_context, mesh_data* a_data, program* a_program ) 159 159 : keyframed_mesh( a_context, a_data, a_program ) 160 160 { 161 m_vb_position = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_position_data(0) );161 m_vb_position = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_position_frame(0) ); 162 162 m_va->add_vertex_buffer( nv::slot::POSITION, m_vb_position, nv::FLOAT, 3 ); 163 163 164 m_vb_normal = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ) * m_data->get_frame_count(), (void*)m_data->get_normal_data(0) );164 m_vb_normal = m_context->get_device()->create_vertex_buffer( nv::STATIC_DRAW, m_data->get_vertex_count() * sizeof( nv::vec3 ), (void*)m_data->get_normal_frame(0) ); 165 165 m_va->add_vertex_buffer( nv::slot::NORMAL, m_vb_normal, nv::FLOAT, 3 ); 166 166 … … 169 169 m_va->add_vertex_buffer( nv::slot::TEXCOORD, vb, nv::FLOAT, 2 ); 170 170 171 nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint 16), (void*)m_data->get_indices().data() );172 m_va->set_index_buffer( ib, nv::U SHORT, true );171 nv::index_buffer* ib = m_context->get_device()->create_index_buffer( nv::STATIC_DRAW, m_data->get_index_count() * sizeof( nv::uint32 ), (void*)m_data->get_indices().data() ); 172 m_va->set_index_buffer( ib, nv::UINT, true ); 173 173 174 174 m_position.resize( m_data->get_vertex_count() ); … … 181 181 182 182 size_t vtx_count = m_data->get_vertex_count(); 183 const vec3* prev_position = m_data->get_position_ data( m_last_frame );184 const vec3* next_position = m_data->get_position_ data( m_next_frame );185 const vec3* prev_normal = m_data->get_normal_ data( m_last_frame );186 const vec3* next_normal = m_data->get_normal_ data( m_next_frame );183 const vec3* prev_position = m_data->get_position_frame( m_last_frame ); 184 const vec3* next_position = m_data->get_position_frame( m_next_frame ); 185 const vec3* prev_normal = m_data->get_normal_frame( m_last_frame ); 186 const vec3* next_normal = m_data->get_normal_frame( m_next_frame ); 187 187 188 188 for ( size_t i = 0; i < vtx_count; ++i )
Note: See TracChangeset
for help on using the changeset viewer.