/* Copyright 2005 Nicholas Bishop * * This file is part of SharpConstruct. * * SharpConstruct is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * SharpConstruct is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with SharpConstruct; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "Create.hh" #include "MeshHistory.h" #include using SharpConstruct::Create::File; File::File( const std::string& file ) : file_( file ) {} bool File::operator()() const { Mesh& mesh( MeshHistory::Instance().GetCurrentMesh() ); using std::string; string extension( file_.substr ( file_.rfind( '.', string::npos ) + 1 ) ); transform( extension.begin(), extension.end(), extension.begin(), toupper ); if( extension == "OBJ" ) load_obj_(); /*else if( extension == "" ) load__();*/ else load_obj_(); /*_reset_visibles(); _visible_elements.RestoreOrder.Reset(); _visible_elements.RestoreOrder.Enabled = true;*/ mesh.SetFileName( file_ ); return false; } void File::load_obj_() const { Mesh& mesh( MeshHistory::Instance().GetCurrentMesh() ); std::ifstream file( file_.c_str(), std::ios::in ); //std::istringstream line; std::string token, subtoken; char buffer[ 256 ]; unsigned polygon_count = 0; unsigned ndx; bool hascolor = false; std::vector< Polygon3D > polygons; while( !file.eof() ) { std::istringstream line; file.getline( buffer, 256 ); line.str( buffer ); line >> token; if( token == "v" ) { float x, y, z; line >> x >> y >> z; mesh.VertexLocations().push_back( Optimized::Point3D( x, y, z ) ); } else if( token == "f" ) { polygons.resize( polygon_count + 1 ); while( line >> token ) { int loc = token.find( '/' ); subtoken = token.substr( 0, loc ); polygons[ polygon_count ].VertexIndices.push_back( ToInt( subtoken ) - 1 ); } polygon_count++; } else if( token == "#vertexcolor" ) { line >> ndx; mesh.VertexColors().push_back( SharpConstruct::Color() ); line >> mesh.VertexColors()[ ndx ].Red >> mesh.VertexColors()[ ndx ].Green >> mesh.VertexColors()[ ndx ].Blue; hascolor = true; } } file.close(); for( unsigned i = 0; i < polygons.size(); ++i ) { if( polygons[ i ].VertexIndices.size() == 3 ) { mesh.Triangles().push_back( Triangle( polygons[ i ].VertexIndices[ 0 ], polygons[ i ].VertexIndices[ 1 ], polygons[ i ].VertexIndices[ 2 ] ) ); } else if( polygons[ i ].VertexIndices.size() == 4 ) { mesh.Quads().push_back( Quad( polygons[ i ].VertexIndices[ 0 ], polygons[ i ].VertexIndices[ 1 ], polygons[ i ].VertexIndices[ 2 ], polygons[ i ].VertexIndices[ 3 ] ) ); } else { for( unsigned j = 1; j < polygons[ i ].VertexIndices.size() - 1; ++j ) { mesh.Triangles().push_back( Triangle( polygons[ i ].VertexIndices[ 0 ], polygons[ i ].VertexIndices[ j ], polygons[ i ].VertexIndices[ j + 1 ] ) ); } } } /*_recalculate_vertex_users(); RecalculateAllNormals();*/ if( !hascolor ) { mesh.VertexColors().resize( mesh.VertexLocations().size() ); mesh.Flood( Color( 1, 1, 1 ), 1 ); } //_saved = true; }