#include "mesh.h" #include "renderer.h" #include "resource.h" #include "Scanner.h" #include "parser.h" //===================================================================================================================================== // Load = //===================================================================================================================================== bool mesh_t::Load( const char* filename ) { Scanner scanner; if( !scanner.loadFile( filename ) ) return false; const Scanner::Token* token; //** MATERIAL ** token = &scanner.getNextToken(); if( token->code != Scanner::TC_STRING ) { PARSE_ERR_EXPECTED( "string" ); return false; } material_name = token->value.string; //** DP_MATERIAL ** /*token = &scanner.getNextToken(); if( token->code != Scanner::TC_STRING ) { PARSE_ERR_EXPECTED( "string" ); return false; } dp_material_name = token->value.string;*/ //** VERTS ** // verts num token = &scanner.getNextToken(); if( token->code != Scanner::TC_NUMBER || token->type != Scanner::DT_INT ) { PARSE_ERR_EXPECTED( "integer" ); return false; } vert_coords.resize( token->value.int_ ); // read the verts for( uint i=0; i( scanner, false, true, 3, &vert_coords[i][0] ) ) return false; } //** FACES ** // faces num token = &scanner.getNextToken(); if( token->code != Scanner::TC_NUMBER || token->type != Scanner::DT_INT ) { PARSE_ERR_EXPECTED( "integer" ); return false; } tris.resize( token->value.int_ ); // read the faces for( uint i=0; i( scanner, false, true, 3, tris[i].vert_ids ) ) return false; } //** UVS ** // UVs num token = &scanner.getNextToken(); if( token->code != Scanner::TC_NUMBER || token->type != Scanner::DT_INT ) { PARSE_ERR_EXPECTED( "integer" ); return false; } tex_coords.resize( token->value.int_ ); // read the tex_coords for( uint i=0; icode != Scanner::TC_NUMBER || token->type != Scanner::DT_INT ) { PARSE_ERR_EXPECTED( "integer" ); return false; } vert_weights.resize( token->value.int_ ); for( uint i=0; icode != Scanner::TC_NUMBER || token->type != Scanner::DT_INT ) { PARSE_ERR_EXPECTED( "integer" ); return false; } // we treat as error if one vert doesnt have a bone if( token->value.int_ < 1 ) { ERROR( "Vert \"" << i << "\" doesnt have at least one bone" ); return false; } // and here is another possible error if( token->value.int_ > vertex_weight_t::MAX_BONES_PER_VERT ) { ERROR( "Cannot have more than " << vertex_weight_t::MAX_BONES_PER_VERT << " bones per vertex" ); return false; } vert_weights[i].bones_num = token->value.int_; // for all the weights of the current vertes for( uint j=0; jcode != Scanner::TC_NUMBER || token->type != Scanner::DT_INT ) { PARSE_ERR_EXPECTED( "integer" ); return false; } vert_weights[i].bone_ids[j] = token->value.int_; // read the weight of that bone token = &scanner.getNextToken(); if( token->code != Scanner::TC_NUMBER || token->type != Scanner::DT_FLOAT ) { PARSE_ERR_EXPECTED( "float" ); return false; } vert_weights[i].weights[j] = token->value.float_; } } // Sanity checks if( vert_coords.size()<1 || tris.size()<1 ) { ERROR( "Vert coords and tris must be filled \"" << filename << "\"" ); return false; } if( tex_coords.size()!=0 && tex_coords.size()!=vert_coords.size() ) { ERROR( "Tex coords num must be zero or equal to vertex coords num \"" << filename << "\"" ); return false; } if( vert_weights.size()!=0 && vert_weights.size()!=vert_coords.size() ) { ERROR( "Vert weights num must be zero or equal to vertex coords num \"" << filename << "\"" ); return false; } CreateAllNormals(); if( tex_coords.size() > 0 ) CreateVertTangents(); CreateVertIndeces(); CreateVBOs(); CalcBSphere(); return true; } //===================================================================================================================================== // Unload = //===================================================================================================================================== void mesh_t::Unload() { // ToDo: add when finalized } //===================================================================================================================================== // CreateFaceNormals = //===================================================================================================================================== void mesh_t::CreateVertIndeces() { DEBUG_ERR( vert_indeces.size() > 0 ); vert_indeces.resize( tris.size() * 3 ); for( uint i=0; i bitagents( vert_coords.size() ); for( uint i=0; i 1 ) vbos.vert_tangents.Create( GL_ARRAY_BUFFER, vert_tangents.GetSizeInBytes(), &vert_tangents[0], GL_STATIC_DRAW ); if( tex_coords.size() > 1 ) vbos.tex_coords.Create( GL_ARRAY_BUFFER, tex_coords.GetSizeInBytes(), &tex_coords[0], GL_STATIC_DRAW ); if( vert_weights.size() > 1 ) vbos.vert_weights.Create( GL_ARRAY_BUFFER, vert_weights.GetSizeInBytes(), &vert_weights[0], GL_STATIC_DRAW ); } //===================================================================================================================================== // CalcBSphere = //===================================================================================================================================== void mesh_t::CalcBSphere() { bsphere.Set( &vert_coords[0], 0, vert_coords.size() ); }