/ Published in: C
Expand |
Embed | Plain Text
/* Пример "cube.obj" v 1 1 1 v 1 1 -1 v 1 -1 1 v 1 -1 -1 v -1 1 1 v -1 1 -1 v -1 -1 1 v -1 -1 -1 f 1 3 4 2 f 5 7 8 6 f 1 5 6 2 f 3 7 8 4 f 1 5 7 3 f 2 6 8 4 */ . . . int v_count = 0; int vt_count = 0; int f_count = 0; int current_f_count = 0; typedef struct { struct { float x, y, z; } vertexes[V_COUNT]; struct { float u, v; } text_coords[VT_COUNT]; struct { int v1, v2, v3, vt1, vt2 , vt3; } faces[F_COUNT]; } model; model mdl; . . . int read_obj_file () { char filename[] = "soldat.obj"; char buffer[100]; FILE* obj_file = NULL; obj_file = fopen ( filename, "r" ); if ( obj_file == NULL ) return 1; while ( fgets (buffer, 100, obj_file) ) { /* vertex coords */ if ( buffer[0] == 'v' && buffer [1] == ' ' ) { sscanf ( buffer, "v %f %f %f", &mdl.vertexes[v_count].x, &mdl.vertexes[v_count].y, &mdl.vertexes[v_count].z ); v_count++; } /* texture coords */ else if ( buffer[0] == 'v' && buffer[1] == 't' ) { sscanf ( buffer, "vt %f %f", &mdl.text_coords[vt_count].v, &mdl.text_coords[vt_count].u ); vt_count++; } /* faces */ else if ( buffer[0] == 'f' && buffer [1] == ' ' ) { //sscanf ( buffer, "f %i/%i %i/%i %i/%i", sscanf ( buffer, "f %i %i %i", &mdl.faces[f_count].v1, //&mdl.faces[f_count].vt1, &mdl.faces[f_count].v2, //&mdl.faces[f_count].vt2, &mdl.faces[f_count].v3 ); //&mdl.faces[f_count].vt3 ); f_count++; } } fclose ( obj_file ); return 0; }
You need to login to post a comment.
