Posted By

ozkriff on 07/25/09


Tagged


Versions (?)

хмм


 / Published in: C
 

  1. /*
  2. Пример "cube.obj"
  3.  
  4. v 1 1 1
  5. v 1 1 -1
  6. v 1 -1 1
  7. v 1 -1 -1
  8. v -1 1 1
  9. v -1 1 -1
  10. v -1 -1 1
  11. v -1 -1 -1
  12. f 1 3 4 2
  13. f 5 7 8 6
  14. f 1 5 6 2
  15. f 3 7 8 4
  16. f 1 5 7 3
  17. f 2 6 8 4
  18.  
  19.  
  20. */
  21.  
  22. .
  23. .
  24. .
  25.  
  26. int v_count = 0;
  27. int vt_count = 0;
  28. int f_count = 0;
  29. int current_f_count = 0;
  30.  
  31. typedef struct
  32. {
  33. struct { float x, y, z; } vertexes[V_COUNT];
  34. struct { float u, v; } text_coords[VT_COUNT];
  35. struct { int v1, v2, v3, vt1, vt2 , vt3; } faces[F_COUNT];
  36. } model;
  37. model mdl;
  38.  
  39. .
  40. .
  41. .
  42.  
  43. int read_obj_file ()
  44. {
  45. char filename[] = "soldat.obj";
  46. char buffer[100];
  47. FILE* obj_file = NULL;
  48.  
  49. obj_file = fopen ( filename, "r" );
  50.  
  51. if ( obj_file == NULL )
  52. return 1;
  53.  
  54. while ( fgets (buffer, 100, obj_file) ) {
  55. /* vertex coords */
  56. if ( buffer[0] == 'v' && buffer [1] == ' ' ) {
  57. sscanf ( buffer, "v %f %f %f",
  58. &mdl.vertexes[v_count].x,
  59. &mdl.vertexes[v_count].y,
  60. &mdl.vertexes[v_count].z );
  61. v_count++;
  62. }
  63.  
  64. /* texture coords */
  65. else if ( buffer[0] == 'v' && buffer[1] == 't' ) {
  66. sscanf ( buffer, "vt %f %f",
  67. &mdl.text_coords[vt_count].v,
  68. &mdl.text_coords[vt_count].u );
  69. vt_count++;
  70. }
  71.  
  72. /* faces */
  73. else if ( buffer[0] == 'f' && buffer [1] == ' ' ) {
  74. //sscanf ( buffer, "f %i/%i %i/%i %i/%i",
  75. sscanf ( buffer, "f %i %i %i",
  76. &mdl.faces[f_count].v1,
  77. //&mdl.faces[f_count].vt1,
  78. &mdl.faces[f_count].v2,
  79. //&mdl.faces[f_count].vt2,
  80. &mdl.faces[f_count].v3 );
  81. //&mdl.faces[f_count].vt3 );
  82. f_count++;
  83. }
  84. }
  85.  
  86. fclose ( obj_file );
  87. return 0;
  88. }

Report this snippet  

You need to login to post a comment.