9
3

MeshLoaderObj.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include "MeshLoaderObj.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <cstring>
  22. #define _USE_MATH_DEFINES
  23. #include <math.h>
  24. rcMeshLoaderObj::rcMeshLoaderObj() :
  25. m_scale(1.0f),
  26. m_verts(0),
  27. m_tris(0),
  28. m_normals(0),
  29. m_vertCount(0),
  30. m_triCount(0)
  31. {
  32. }
  33. rcMeshLoaderObj::~rcMeshLoaderObj()
  34. {
  35. delete [] m_verts;
  36. delete [] m_normals;
  37. delete [] m_tris;
  38. }
  39. void rcMeshLoaderObj::addVertex(float x, float y, float z, int& cap)
  40. {
  41. if (m_vertCount+1 > cap)
  42. {
  43. cap = !cap ? 8 : cap*2;
  44. float* nv = new float[cap*3];
  45. if (m_vertCount)
  46. memcpy(nv, m_verts, m_vertCount*3*sizeof(float));
  47. delete [] m_verts;
  48. m_verts = nv;
  49. }
  50. float* dst = &m_verts[m_vertCount*3];
  51. *dst++ = x*m_scale;
  52. *dst++ = y*m_scale;
  53. *dst++ = z*m_scale;
  54. m_vertCount++;
  55. }
  56. void rcMeshLoaderObj::addTriangle(int a, int b, int c, int& cap)
  57. {
  58. if (m_triCount+1 > cap)
  59. {
  60. cap = !cap ? 8 : cap*2;
  61. int* nv = new int[cap*3];
  62. if (m_triCount)
  63. memcpy(nv, m_tris, m_triCount*3*sizeof(int));
  64. delete [] m_tris;
  65. m_tris = nv;
  66. }
  67. int* dst = &m_tris[m_triCount*3];
  68. *dst++ = a;
  69. *dst++ = b;
  70. *dst++ = c;
  71. m_triCount++;
  72. }
  73. static char* parseRow(char* buf, char* bufEnd, char* row, int len)
  74. {
  75. bool start = true;
  76. bool done = false;
  77. int n = 0;
  78. while (!done && buf < bufEnd)
  79. {
  80. char c = *buf;
  81. buf++;
  82. // multirow
  83. switch (c)
  84. {
  85. case '\\':
  86. break;
  87. case '\n':
  88. if (start) break;
  89. done = true;
  90. break;
  91. case '\r':
  92. break;
  93. case '\t':
  94. case ' ':
  95. if (start) break;
  96. // else falls through
  97. default:
  98. start = false;
  99. row[n++] = c;
  100. if (n >= len-1)
  101. done = true;
  102. break;
  103. }
  104. }
  105. row[n] = '\0';
  106. return buf;
  107. }
  108. static int parseFace(char* row, int* data, int n, int vcnt)
  109. {
  110. int j = 0;
  111. while (*row != '\0')
  112. {
  113. // Skip initial white space
  114. while (*row != '\0' && (*row == ' ' || *row == '\t'))
  115. row++;
  116. char* s = row;
  117. // Find vertex delimiter and terminated the string there for conversion.
  118. while (*row != '\0' && *row != ' ' && *row != '\t')
  119. {
  120. if (*row == '/') *row = '\0';
  121. row++;
  122. }
  123. if (*s == '\0')
  124. continue;
  125. int vi = atoi(s);
  126. data[j++] = vi < 0 ? vi+vcnt : vi-1;
  127. if (j >= n) return j;
  128. }
  129. return j;
  130. }
  131. bool rcMeshLoaderObj::load(const std::string& filename)
  132. {
  133. char* buf = 0;
  134. FILE* fp = fopen(filename.c_str(), "rb");
  135. if (!fp)
  136. return false;
  137. if (fseek(fp, 0, SEEK_END) != 0)
  138. {
  139. fclose(fp);
  140. return false;
  141. }
  142. long bufSize = ftell(fp);
  143. if (bufSize < 0)
  144. {
  145. fclose(fp);
  146. return false;
  147. }
  148. if (fseek(fp, 0, SEEK_SET) != 0)
  149. {
  150. fclose(fp);
  151. return false;
  152. }
  153. buf = new char[bufSize];
  154. if (!buf)
  155. {
  156. fclose(fp);
  157. return false;
  158. }
  159. size_t readLen = fread(buf, bufSize, 1, fp);
  160. fclose(fp);
  161. if (readLen != 1)
  162. {
  163. delete[] buf;
  164. return false;
  165. }
  166. char* src = buf;
  167. char* srcEnd = buf + bufSize;
  168. char row[512];
  169. int face[32];
  170. float x,y,z;
  171. int nv;
  172. int vcap = 0;
  173. int tcap = 0;
  174. while (src < srcEnd)
  175. {
  176. // Parse one row
  177. row[0] = '\0';
  178. src = parseRow(src, srcEnd, row, sizeof(row)/sizeof(char));
  179. // Skip comments
  180. if (row[0] == '#') continue;
  181. if (row[0] == 'v' && row[1] != 'n' && row[1] != 't')
  182. {
  183. // Vertex pos
  184. sscanf(row+1, "%f %f %f", &x, &y, &z);
  185. addVertex(x, y, z, vcap);
  186. }
  187. if (row[0] == 'f')
  188. {
  189. // Faces
  190. nv = parseFace(row+1, face, 32, m_vertCount);
  191. for (int i = 2; i < nv; ++i)
  192. {
  193. const int a = face[0];
  194. const int b = face[i-1];
  195. const int c = face[i];
  196. if (a < 0 || a >= m_vertCount || b < 0 || b >= m_vertCount || c < 0 || c >= m_vertCount)
  197. continue;
  198. addTriangle(a, b, c, tcap);
  199. }
  200. }
  201. }
  202. delete [] buf;
  203. // Calculate normals.
  204. m_normals = new float[m_triCount*3];
  205. for (int i = 0; i < m_triCount*3; i += 3)
  206. {
  207. const float* v0 = &m_verts[m_tris[i]*3];
  208. const float* v1 = &m_verts[m_tris[i+1]*3];
  209. const float* v2 = &m_verts[m_tris[i+2]*3];
  210. float e0[3], e1[3];
  211. for (int j = 0; j < 3; ++j)
  212. {
  213. e0[j] = v1[j] - v0[j];
  214. e1[j] = v2[j] - v0[j];
  215. }
  216. float* n = &m_normals[i];
  217. n[0] = e0[1]*e1[2] - e0[2]*e1[1];
  218. n[1] = e0[2]*e1[0] - e0[0]*e1[2];
  219. n[2] = e0[0]*e1[1] - e0[1]*e1[0];
  220. float d = sqrtf(n[0]*n[0] + n[1]*n[1] + n[2]*n[2]);
  221. if (d > 0)
  222. {
  223. d = 1.0f/d;
  224. n[0] *= d;
  225. n[1] *= d;
  226. n[2] *= d;
  227. }
  228. }
  229. m_filename = filename;
  230. return true;
  231. }