#include "../util/typedef.h" #include "../util/string.h" #include "../util/vector.h" #include "../util/file.h" #include struct MODEL { }; namespace OBJ { enum LINE_TYPES : U16 { comment = ( '#' | ( ' ' << 8 ) ), f = ( 'f' | ( ' ' << 8 ) ), mtllib = ( 'm' | ( 't' << 8 ) ), o = ( 'o' | ( ' ' << 8 ) ), s = ( 's' | ( ' ' << 8 ) ), v = ( 'v' | ( ' ' << 8 ) ), vn = ( 'v' | ( 'n' << 8 ) ), vt = ( 'v' | ( 't' << 8 ) ), usemtl = ( 'u' | ( 's' << 8 ) ) }; enum FACE_TYPES : U8 { V = 0, VVT = 1, VVN = 2, VVTVN = 3 }; static inline U32 face_stride( FACE_TYPES t ) { switch( t ) { case FACE_TYPES::V: return 1; case FACE_TYPES::VVT: return 2; case FACE_TYPES::VVN: return 2; case FACE_TYPES::VVTVN: return 3; } return 0; } constexpr U32 STR_LEN = 0x40; static inline char* skip_space( char* s ) { while( *s == ' ' || *s == '\t' ) ++s; return s; } static inline char* read_f32( char* s, F32* out ) { *out = strtof( s, &s ); return s; } static inline char* read_u32( char* s, U32* out ) { *out = strtoul( s, &s, 10 ); return s; } struct MTLDATA { STR name; }; struct OBJDATA { LIST uvs; LIST normals; LIST vertices; LIST mtllib; struct UseMtl { LIST indices; LIST data; } usemtl; struct Indices { LIST data; LIST counts; LIST types; } indices; }; inline OBJDATA obj_from_file( const char* path, const char* name ) { char* f_obj = nullptr; STR obj_path( path ); U64 obj_size = 0; obj_path += name; obj_path += ".obj"; f_obj = (char*)file_read( obj_path.data, &obj_size ); if( !f_obj ) { dlog( "obj_from_file() : failed to read obj file %s\n", obj_path.data ); return {}; } dlog( "___modelfile___\n%s\n", f_obj ); OBJDATA obj; for( U64 i = 0; i < obj_size - 1 && f_obj[i]; ++i ) { if( f_obj[i] == (I32)'#' ) { while( f_obj[i] && f_obj[i] != '\n' ) ++i; continue; } I32 c0 = f_obj[i]; U16 c1 = f_obj[i + 1]; I32 first_two = c0 | c1 << 8; switch( first_two ) { case LINE_TYPES::f: { FACE_TYPES type = FACE_TYPES::V; char* s = f_obj + i + 2; bool type_set = 0; U32 count = 0; s = skip_space( s ); while( *s && *s != '\n' ) { bool has_vt = 0, has_vn = 0; U32 v = 0, vt = 0, vn = 0; s = read_u32( s, &v ); obj.indices.data.push( --v ); if( *s == '/' ) { ++s; if( *s != '/' ) { s = read_u32( s, &vt ); obj.indices.data.push( --vt ); has_vt = 1; } if( *s == '/' ) { s = read_u32( ++s, &vn ); obj.indices.data.push( --vn ); has_vn = 1; } } if( !type_set ) { if( has_vt && has_vn ) type = FACE_TYPES::VVTVN; else if( has_vt ) type = FACE_TYPES::VVT; else if( has_vn ) type = FACE_TYPES::VVN; else type = FACE_TYPES::V; type_set = 1; } ++count; while( *s && *s != ' ' && *s != '\n' ) ++s; s = skip_space( s ); } obj.indices.counts.push( count ); obj.indices.types.push( type ); } break; case LINE_TYPES::v: case LINE_TYPES::vn: { bool is_v = first_two == LINE_TYPES::v; U32 start = i + ( is_v ? 2 : 3 ); char* s = f_obj + start; VEC3 temp{}; s = skip_space( s ); s = read_f32( s, &temp.x ); s = skip_space( s ); s = read_f32( s, &temp.y ); s = skip_space( s ); s = read_f32( s, &temp.z ); if( is_v ) obj.vertices.push( temp ); else obj.normals.push( temp ); } break; case LINE_TYPES::vt: { char* s = f_obj + i + 3; VEC2 temp{}; s = skip_space( s ); s = read_f32( s, &temp.x ); s = skip_space( s ); s = read_f32( s, &temp.y ); obj.uvs.push( temp ); } break; case LINE_TYPES::mtllib: case LINE_TYPES::usemtl: { bool is_mtllib = first_two == LINE_TYPES::mtllib; char* start = f_obj + i + 7; char* end = start; while( *end && *end != '\n' ) ++end; U32 len = end - start; STR temp; temp.reserve( len + 1 ); memcpy( temp, start, len ); temp.data[len] = 0; if( !is_mtllib ) { obj.usemtl.data.push( temp ); obj.usemtl.indices.push( obj.indices.data.size ); } else { U64 mtl_size = 0; STR mtl_path( path ); mtl_path += temp.data; char* f_mtl = (char*)file_read( mtl_path.data, &mtl_size ); if( !f_mtl ) { dlog( "obj_from_file() : failed to read mtl file %s\n", mtl_path.data ); return {}; } dlog( "___mtlfile___\n%s\n", f_mtl ); // TODO: finish mtl parsing here into obj.mtllib obj.mtllib.push({ temp }); free( (void*)f_mtl ); } } break; case LINE_TYPES::comment: case LINE_TYPES::o: case LINE_TYPES::s: break; default: dlog( "unhandled char combo %c%c\n", c0, c1 ); break; } while( f_obj[i] && f_obj[i] != '\n' ) ++i; } free( (void*)f_obj ); return obj; } inline MODEL obj_to_model( OBJDATA obj ) { MODEL model; return model; } }; inline MODEL model_from_obj( const I8* path, const I8* name ) { OBJ::OBJDATA obj = OBJ::obj_from_file( path, name ); return OBJ::obj_to_model( obj ); }