Guest User

Untitled

a guest
Jul 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1.  
  2.  
  3. /* GLMgroup: Structure that defines a group in a model.
  4. */
  5. typedef struct _GRRLIB_Group {
  6. char* name; /* name of this group */
  7. u32 numtriangles; /* number of triangles in this group */
  8. u32* triangles; /* array of triangle indices */
  9. u32 material; /* index to material for group */
  10. struct _GRRLIB_Group* next; /* pointer to next group in model */
  11. } GRRLIB_Group;
  12.  
  13.  
  14. /* GLMmodel: Structure that defines a model.
  15. */
  16. typedef struct {
  17. char* pathname; /* path to this model */
  18. char* mtllibname; /* name of the material library */
  19.  
  20. u32 numvertices; /* number of vertices in model */
  21. guVector* vertices; /* array of vertices */
  22.  
  23. u32 numnormals; /* number of normals in model */
  24. guVector* normals; /* array of normals */
  25.  
  26. u32 numtexcoords; /* number of texcoords in model */
  27. guVector* texcoords; /* array of texture coordinates */
  28.  
  29. u32 numfacetnorms; /* number of facetnorms in model */
  30. guVector* facetnorms; /* array of facetnorms */
  31.  
  32. u32 numtriangles; /* number of triangles in model */
  33. //GLMtriangle* triangles; /* array of triangles */
  34.  
  35. //u32 nummaterials; /* number of materials in model */
  36. //GLMmaterial* materials; /* array of materials */
  37.  
  38. u32 numgroups; /* number of groups in model */
  39. GRRLIB_Group* groups; /* linked list of groups */
  40.  
  41. //GLfloat position[3]; /* position of the model */
  42.  
  43. } GRRLIB_Model;
  44.  
  45. static void GRRLIB_FirstPass(GRRLIB_Model* model, FILE* file)
  46. {
  47. u32 numvertices; /* number of vertices in model */
  48. u32 numnormals; /* number of normals in model */
  49. u32 numtexcoords; /* number of texcoords in model */
  50. u32 numtriangles; /* number of triangles in model */
  51. GRRLIB_Group* group; /* current group */
  52. unsigned v, n, t;
  53. char buf[128];
  54.  
  55. numvertices = numnormals = numtexcoords = numtriangles = 0;
  56.  
  57. while(fscanf(file, "%s", buf) != EOF) {
  58. switch(buf[0]) {
  59. case '#': /* comment */
  60. fgets(buf, sizeof(buf), file);
  61. break;
  62. case 'v': /* v, vn, vt */
  63. switch(buf[1]) {
  64. case '\0': /* vertex */
  65. fgets(buf, sizeof(buf), file);
  66. numvertices++;
  67. break;
  68. case 'n': /* normal */
  69. fgets(buf, sizeof(buf), file);
  70. numnormals++;
  71. break;
  72. case 't': /* texcoord */
  73. fgets(buf, sizeof(buf), file);
  74. numtexcoords++;
  75. break;
  76. default:
  77. exit(1);
  78. break;
  79. }
  80. break;
  81. case 'm':
  82. fgets(buf, sizeof(buf), file);
  83. sscanf(buf, "%s %s", buf, buf);
  84. model->mtllibname = strdup(buf);
  85. //_glmReadMTL(model, buf);
  86. break;
  87. case 'u':
  88. fgets(buf, sizeof(buf), file);
  89. break;
  90. case 'g': /* group */
  91. fgets(buf, sizeof(buf), file);
  92. sscanf(buf, "%s", buf);
  93. //group = _glmAddGroup(model, buf);
  94. break;
  95. case 'f': /* face */
  96. v = n = t = 0;
  97. fscanf(file, "%s", buf);
  98. /* can be one of %d, %d//%d, %d/%d, %d/%d/%d %d//%d */
  99. if (strstr(buf, "//")) {
  100. /* v//n */
  101. sscanf(buf, "%d//%d", &v, &n);
  102. fscanf(file, "%d//%d", &v, &n);
  103. fscanf(file, "%d//%d", &v, &n);
  104. numtriangles++;
  105. /*
  106. group->numtriangles++;
  107. while(fscanf(file, "%d//%d", &v, &n) > 0) {
  108. numtriangles++;
  109. group->numtriangles++;
  110. }*/
  111. } else if (sscanf(buf, "%d/%d/%d", &v, &t, &n) == 3) {
  112. /* v/t/n */
  113. fscanf(file, "%d/%d/%d", &v, &t, &n);
  114. fscanf(file, "%d/%d/%d", &v, &t, &n);
  115. numtriangles++;
  116. /*
  117. group->numtriangles++;
  118. while(fscanf(file, "%d/%d/%d", &v, &t, &n) > 0) {
  119. numtriangles++;
  120. group->numtriangles++;
  121. }*/
  122. } else if (sscanf(buf, "%d/%d", &v, &t) == 2) {
  123. /* v/t */
  124. fscanf(file, "%d/%d", &v, &t);
  125. fscanf(file, "%d/%d", &v, &t);
  126. numtriangles++;
  127. /*
  128. group->numtriangles++;
  129. while(fscanf(file, "%d/%d", &v, &t) > 0) {
  130. numtriangles++;
  131. group->numtriangles++;
  132. }*/
  133. } else {
  134. /* v */
  135. fscanf(file, "%d", &v);
  136. fscanf(file, "%d", &v);
  137. numtriangles++;
  138. /*
  139. group->numtriangles++;
  140. while(fscanf(file, "%d", &v) > 0) {
  141. numtriangles++;
  142. group->numtriangles++;
  143. }*/
  144. }
  145. break;
  146. default:
  147. fgets(buf, sizeof(buf), file);
  148. break;
  149. }
  150. }
  151.  
  152. model->numvertices = numvertices;
  153. model->numnormals = numnormals;
  154. model->numtexcoords = numtexcoords;
  155. model->numtriangles = numtriangles;
  156.  
  157. }
  158.  
  159. GRRLIB_Model* GRRLIB_ReadOBJ(char* filename)
  160. {
  161. GRRLIB_Model* model;
  162. FILE* file;
  163.  
  164. file = fopen(filename, "r");
  165. if (!file) {
  166. exit(1);
  167. }
  168.  
  169. model = (GRRLIB_Model*)malloc(sizeof(GRRLIB_Model));
  170. model->pathname = strdup(filename);
  171. model->mtllibname = NULL;
  172. model->numvertices = 0;
  173. model->vertices = NULL;
  174. model->numnormals = 0;
  175. model->normals = NULL;
  176. model->numtexcoords = 0;
  177. model->texcoords = NULL;
  178. model->numfacetnorms = 0;
  179. model->facetnorms = NULL;
  180. model->numtriangles = 0;
  181.  
  182. // Get a count of the number of stuff
  183. GRRLIB_FirstPass(model, file);
  184.  
  185. // allocate memory
  186. model->vertices = (guVector*)malloc(sizeof(guVector) * 3 * (model->numvertices + 1));
  187. //model->triangles = (GLMtriangle*)malloc(sizeof(GLMtriangle) * model->numtriangles);
  188. if (model->numnormals) {
  189. model->normals = (guVector*)malloc(sizeof(guVector) * 3 * (model->numnormals + 1));
  190. }
  191. if (model->numtexcoords) {
  192. model->texcoords = (guVector*)malloc(sizeof(guVector) * 2 * (model->numtexcoords + 1));
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199. fclose(file);
  200.  
  201. return model;
  202. }
Add Comment
Please, Sign In to add comment