Advertisement
Aslai

Untitled

Oct 25th, 2012
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.96 KB | None | 0 0
  1. struct mesh{
  2.     struct vertex{
  3.         float x, y, z;
  4.     };
  5.     struct normal{
  6.         float x, y, z;
  7.     };
  8.     struct texcoord{
  9.         float u, v;
  10.     };
  11.     struct face{
  12.         std::vector< int > vertices;
  13.         std::vector< int > normals;
  14.         std::vector< int > texcoords;
  15.     };
  16.     std::vector<vertex> verts;
  17.     std::vector<normal> norms;
  18.     std::vector<texcoord> tex;
  19.     std::vector<face> faces;
  20.     bool load_from_bzw( const char* fname ){
  21.         FILE *f = fopen( fname, "r" );
  22.         if( f <= 0 ) return 1;
  23.  
  24.         const int NUL = 0;
  25.         const int MATERIAL = 1;
  26.         const int MESH = 2;
  27.         const int MESHFACE = 3;
  28.  
  29.         int state = NUL;
  30.         char buf[1000];
  31.         fgets( buf, 999, f );
  32.  
  33.         while( !feof( f ) ){
  34.             char* buff = buf;
  35.             while( isspace( *buff ) ) buff++;
  36.             for( int i = 0; buff[i] != 0; i ++ ) if( buff[i] == '\r' ) buff[i] = 0;
  37.             char*tu[] = {"NUL", "MATERIAL", "MESH", "MESHFACE"};
  38.             //printf( "%s\n", tu[state] );
  39.             if( !( buff[0] == 0 || buff[0] == '#' ) )
  40.                 switch( state ){
  41.                     case NUL:
  42.                         if( strcmp( buff, "material" ) == 0 )
  43.                             state = MATERIAL;
  44.                         else if( strcmp( buff, "mesh" ) == 0 )
  45.                             state = MESH;
  46.                         else{
  47.                             printf( "A Unknown value \"%s\"\n", buff );
  48.                             break;
  49.                         }
  50.                         break;
  51.                     case MESH:{
  52.                             float a,b,c;
  53.                             if( strcmp( buff, "end" ) == 0  ){
  54.                                 state = NUL;
  55.                             } else if( sscanf( buff, "vertex %f %f %f", &a, &b, &c ) == 3 ){
  56.                                 verts.push_back( { a, b, c } );
  57.                             } else if( sscanf( buff, "normal %f %f %f", &a, &b, &c ) == 3 ){
  58.                                 norms.push_back( { a, b, c } );
  59.                                 //printf("joos");
  60.                             } else if( sscanf( buff, "texcoord %f %f", &a, &b) == 2 ){
  61.                                 tex.push_back( { a, b } );
  62.                             } else if( strcmp( buff, "face" ) == 0  ){
  63.                                 state = MESHFACE;
  64.                                 face a;
  65.                                 faces.push_back( a );
  66.                             }
  67.                             else{
  68.                                 printf( "Unknown value \"%s\"", buff );
  69.                                 break;
  70.                             }
  71.                         }break;
  72.                     case MESHFACE:{
  73.                         int off;
  74.                         int t;
  75.                         face* a = &(faces[faces.size()-1]);
  76.  
  77.                         //printf(" %i ",  ( sscanf( buff, "vertices%n", &off ) | off ) > 8 );
  78.                         if( sscanf( buff, "vertices%n", &off ) | off >= 8 ){
  79.                             buff += off;
  80.                             while( sscanf( buff, " %i%n", &t, &off ) >= 1 ){
  81.                                 buff += off;
  82.                                 a->vertices.push_back( t );
  83.                                 //printf(" HAHA" );
  84.                             }
  85.                         }
  86.                         else if( sscanf( buff, "normals%n", &off ) | off >= 7 ){
  87.                             buff += off;
  88.                             while( sscanf( buff, " %i%n", &t, &off ) >= 1 ){
  89.                                 buff += off;
  90.                                 a->normals.push_back( t );
  91.                                 //printf(" LOLO" );
  92.                             }
  93.                         }
  94.                         else if( sscanf( buff, "texcoords%n", &off ) | off >= 9 ){
  95.                             buff += off;
  96.                             while( sscanf( buff, " %i%n", &t, &off ) >= 1 ){
  97.                                 buff += off;
  98.                                 a->texcoords.push_back( t );
  99.                                 //printf(" YULU %i ", a->texcoords.size() );
  100.                             }
  101.                         }
  102.                         else if(strcmp( buff, "endface" ) == 0  ){
  103.                             state = MESH;
  104.  
  105.                             break;
  106.                         }
  107.  
  108.  
  109.                     } break;
  110.                     case MATERIAL:{
  111.                             float a,b,c;
  112.                             if( strcmp( buff, "end" ) == 0 ){
  113.                                 state = NUL;
  114.                             } else if( sscanf( buff, "vertex %f %f %f", &a, &b, &c ) == 3 ){
  115.                                 verts.push_back( { a, b, c } );
  116.                             } else if( sscanf( buff, "normal %f %f %f", &a, &b, &c ) == 3 ){
  117.                                 norms.push_back( { a, b, c } );
  118.                             } else if( sscanf( buff, "texcoord %f %f", &a, &b) == 2 ){
  119.                                 tex.push_back( { a, b } );
  120.                             }
  121.                             else{
  122.                                 printf( "Unknown value \"%s\"", buff );
  123.                                 break;
  124.                             }
  125.                         }break;
  126.  
  127.                 }
  128.             fgets( buf, 999, f );
  129.         }
  130.  
  131.     }
  132.     void render(){
  133.         for( int i = 0; i < faces.size(); ++i ){
  134.             //puts("a");
  135.             glBegin( GL_POLYGON );
  136.             for( int j = 0; j < faces[i].vertices.size(); ++j ){
  137.                 glNormal3f( (norms[faces[i].normals[j]]).x, (norms[faces[i].normals[j]]).y, (norms[faces[i].normals[j]]).z );
  138.                 glTexCoord2f( tex[ faces[i].texcoords[j] ].u, 1.0-tex[ faces[i].texcoords[j] ].v );
  139.                 glVertex3f( verts[faces[i].vertices[j]].x,verts[faces[i].vertices[j]].y,verts[faces[i].vertices[j]].z );
  140.             }
  141.             glEnd();
  142.         }
  143.     }
  144. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement