geometryc.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /*
  2. * Copyright 2011-2023 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include <algorithm>
  6. #include <bx/string.h>
  7. #include <bgfx/bgfx.h>
  8. #include "../../src/vertexlayout.h"
  9. #include <tinystl/allocator.h>
  10. #include <tinystl/string.h>
  11. #include <tinystl/vector.h>
  12. namespace stl = tinystl;
  13. #include <meshoptimizer/src/meshoptimizer.h>
  14. #define CGLTF_VALIDATE_ENABLE_ASSERTS BX_CONFIG_DEBUG
  15. #define CGLTF_IMPLEMENTATION
  16. #include <cgltf/cgltf.h>
  17. #define BGFX_GEOMETRYC_VERSION_MAJOR 1
  18. #define BGFX_GEOMETRYC_VERSION_MINOR 0
  19. #if 0
  20. # define BX_TRACE(_format, ...) \
  21. do { \
  22. bx::printf(BX_FILE_LINE_LITERAL "BGFX " _format "\n", ##__VA_ARGS__); \
  23. } while(0)
  24. # define BX_WARN(_condition, _format, ...) \
  25. do { \
  26. if (!(_condition) ) \
  27. { \
  28. BX_TRACE(BX_FILE_LINE_LITERAL "WARN " _format, ##__VA_ARGS__); \
  29. } \
  30. } while(0)
  31. # define BX_ASSERT(_condition, _format, ...) \
  32. do { \
  33. if (!(_condition) ) \
  34. { \
  35. BX_TRACE(BX_FILE_LINE_LITERAL "CHECK " _format, ##__VA_ARGS__); \
  36. bx::debugBreak(); \
  37. } \
  38. } while(0)
  39. #endif // 0
  40. #include <bx/bx.h>
  41. #include <bx/bounds.h>
  42. #include <bx/commandline.h>
  43. #include <bx/debug.h>
  44. #include <bx/file.h>
  45. #include <bx/hash.h>
  46. #include <bx/math.h>
  47. #include <bx/timer.h>
  48. #include <bx/uint32_t.h>
  49. typedef stl::vector<bx::Vec3> Vec3Array;
  50. struct Index3
  51. {
  52. int32_t m_position;
  53. int32_t m_texcoord;
  54. int32_t m_normal;
  55. int32_t m_vbc; // Barycentric ID. Holds either 0, 1 or 2.
  56. };
  57. struct TriIndices
  58. {
  59. Index3 m_index[3];
  60. };
  61. typedef stl::vector<TriIndices> TriangleArray;
  62. struct Group
  63. {
  64. uint32_t m_startTriangle;
  65. uint32_t m_numTriangles;
  66. stl::string m_name;
  67. stl::string m_material;
  68. };
  69. typedef stl::vector<Group> GroupArray;
  70. struct Primitive
  71. {
  72. uint32_t m_startVertex;
  73. uint32_t m_startIndex;
  74. uint32_t m_numVertices;
  75. uint32_t m_numIndices;
  76. stl::string m_name;
  77. };
  78. typedef stl::vector<Primitive> PrimitiveArray;
  79. struct Axis
  80. {
  81. enum Enum
  82. {
  83. NegativeX,
  84. PositiveX,
  85. NegativeY,
  86. PositiveY,
  87. NegativeZ,
  88. PositiveZ,
  89. };
  90. };
  91. static bx::Vec3 s_axisVectors[6] =
  92. {
  93. bx::Vec3(-1.0f, 0.0f, 0.0f),
  94. bx::Vec3( 1.0f, 0.0f, 0.0f),
  95. bx::Vec3( 0.0f,-1.0f, 0.0f),
  96. bx::Vec3( 0.0f, 1.0f, 0.0f),
  97. bx::Vec3( 0.0f, 0.0f,-1.0f),
  98. bx::Vec3( 0.0f, 0.0f, 1.0f),
  99. };
  100. struct CoordinateSystem
  101. {
  102. bx::Handedness::Enum m_handedness;
  103. Axis::Enum m_up;
  104. Axis::Enum m_forward;
  105. };
  106. struct CoordinateSystemMapping
  107. {
  108. const char* m_param;
  109. CoordinateSystem m_coordinateSystem;
  110. };
  111. static const CoordinateSystemMapping s_coordinateSystemMappings[] =
  112. {
  113. { "lh-up+y", { bx::Handedness::Left, Axis::PositiveY, Axis::PositiveZ } },
  114. { "lh-up+z", { bx::Handedness::Left, Axis::PositiveZ, Axis::PositiveY } },
  115. { "rh-up+y", { bx::Handedness::Right, Axis::PositiveY, Axis::PositiveZ } },
  116. { "rh-up+z", { bx::Handedness::Right, Axis::PositiveZ, Axis::PositiveY } },
  117. };
  118. struct Mesh
  119. {
  120. Vec3Array m_positions;
  121. Vec3Array m_normals;
  122. Vec3Array m_texcoords;
  123. TriangleArray m_triangles;
  124. GroupArray m_groups;
  125. CoordinateSystem m_coordinateSystem;
  126. };
  127. static uint32_t s_obbSteps = 17;
  128. constexpr uint32_t kChunkVertexBuffer = BX_MAKEFOURCC('V', 'B', ' ', 0x1);
  129. constexpr uint32_t kChunkVertexBufferCompressed = BX_MAKEFOURCC('V', 'B', 'C', 0x0);
  130. constexpr uint32_t kChunkIndexBuffer = BX_MAKEFOURCC('I', 'B', ' ', 0x0);
  131. constexpr uint32_t kChunkIndexBufferCompressed = BX_MAKEFOURCC('I', 'B', 'C', 0x1);
  132. constexpr uint32_t kChunkPrimitive = BX_MAKEFOURCC('P', 'R', 'I', 0x0);
  133. void optimizeVertexCache(uint16_t* _indices, uint32_t _numIndices, uint32_t _numVertices)
  134. {
  135. uint16_t* newIndexList = new uint16_t[_numIndices];
  136. meshopt_optimizeVertexCache(newIndexList, _indices, _numIndices, _numVertices);
  137. bx::memCopy(_indices, newIndexList, _numIndices * 2);
  138. delete[] newIndexList;
  139. }
  140. uint32_t optimizeVertexFetch(
  141. uint16_t* _indices
  142. , uint32_t _numIndices
  143. , uint8_t* _vertexData
  144. , uint32_t _numVertices
  145. , uint16_t _stride
  146. )
  147. {
  148. unsigned char* newVertices = (unsigned char*)malloc(_numVertices * _stride );
  149. size_t vertexCount = meshopt_optimizeVertexFetch(newVertices, _indices, _numIndices, _vertexData, _numVertices, _stride);
  150. bx::memCopy(_vertexData, newVertices, _numVertices * _stride);
  151. free(newVertices);
  152. return uint32_t(vertexCount);
  153. }
  154. void writeCompressedIndices(
  155. bx::WriterI* _writer
  156. , const uint16_t* _indices
  157. , uint32_t _numIndices
  158. , uint32_t _numVertices
  159. , bx::Error* _err
  160. )
  161. {
  162. size_t maxSize = meshopt_encodeIndexBufferBound(_numIndices, _numVertices);
  163. unsigned char* compressedIndices = (unsigned char*)malloc(maxSize);
  164. size_t compressedSize = meshopt_encodeIndexBuffer(compressedIndices, maxSize, _indices, _numIndices);
  165. bx::printf("Indices uncompressed: %10d, compressed: %10d, ratio: %0.2f%%\n"
  166. , _numIndices*2
  167. , (uint32_t)compressedSize
  168. , 100.0f - float(compressedSize ) / float(_numIndices*2)*100.0f
  169. );
  170. bx::write(_writer, (uint32_t)compressedSize, _err);
  171. bx::write(_writer, compressedIndices, (uint32_t)compressedSize, _err);
  172. free(compressedIndices);
  173. }
  174. void writeCompressedVertices(
  175. bx::WriterI* _writer
  176. , const uint8_t* _vertices
  177. , uint32_t _numVertices
  178. , uint16_t _stride
  179. , bx::Error* _err
  180. )
  181. {
  182. size_t maxSize = meshopt_encodeVertexBufferBound(_numVertices, _stride);
  183. unsigned char* compressedVertices = (unsigned char*)malloc(maxSize);
  184. size_t compressedSize = meshopt_encodeVertexBuffer(compressedVertices, maxSize, _vertices, _numVertices, _stride);
  185. bx::printf("Vertices uncompressed: %10d, compressed: %10d, ratio: %0.2f%%\n"
  186. , _numVertices * _stride
  187. , (uint32_t)compressedSize
  188. , 100.0f - float(compressedSize) / float(_numVertices * _stride)*100.0f
  189. );
  190. bx::write(_writer, (uint32_t)compressedSize, _err);
  191. bx::write(_writer, compressedVertices, (uint32_t)compressedSize, _err);
  192. free(compressedVertices);
  193. }
  194. void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexLayout _layout, const uint16_t* _indices, uint32_t _numIndices)
  195. {
  196. struct PosTexcoord
  197. {
  198. float m_x;
  199. float m_y;
  200. float m_z;
  201. float m_pad0;
  202. float m_u;
  203. float m_v;
  204. float m_pad1;
  205. float m_pad2;
  206. };
  207. float* tangents = new float[6*_numVertices];
  208. bx::memSet(tangents, 0, 6*_numVertices*sizeof(float) );
  209. PosTexcoord v0;
  210. PosTexcoord v1;
  211. PosTexcoord v2;
  212. for (uint32_t ii = 0, num = _numIndices/3; ii < num; ++ii)
  213. {
  214. const uint16_t* indices = &_indices[ii*3];
  215. uint32_t i0 = indices[0];
  216. uint32_t i1 = indices[1];
  217. uint32_t i2 = indices[2];
  218. bgfx::vertexUnpack(&v0.m_x, bgfx::Attrib::Position, _layout, _vertices, i0);
  219. bgfx::vertexUnpack(&v0.m_u, bgfx::Attrib::TexCoord0, _layout, _vertices, i0);
  220. bgfx::vertexUnpack(&v1.m_x, bgfx::Attrib::Position, _layout, _vertices, i1);
  221. bgfx::vertexUnpack(&v1.m_u, bgfx::Attrib::TexCoord0, _layout, _vertices, i1);
  222. bgfx::vertexUnpack(&v2.m_x, bgfx::Attrib::Position, _layout, _vertices, i2);
  223. bgfx::vertexUnpack(&v2.m_u, bgfx::Attrib::TexCoord0, _layout, _vertices, i2);
  224. const float bax = v1.m_x - v0.m_x;
  225. const float bay = v1.m_y - v0.m_y;
  226. const float baz = v1.m_z - v0.m_z;
  227. const float bau = v1.m_u - v0.m_u;
  228. const float bav = v1.m_v - v0.m_v;
  229. const float cax = v2.m_x - v0.m_x;
  230. const float cay = v2.m_y - v0.m_y;
  231. const float caz = v2.m_z - v0.m_z;
  232. const float cau = v2.m_u - v0.m_u;
  233. const float cav = v2.m_v - v0.m_v;
  234. const float det = (bau * cav - bav * cau);
  235. const float invDet = 1.0f / det;
  236. const float tx = (bax * cav - cax * bav) * invDet;
  237. const float ty = (bay * cav - cay * bav) * invDet;
  238. const float tz = (baz * cav - caz * bav) * invDet;
  239. const float bx = (cax * bau - bax * cau) * invDet;
  240. const float by = (cay * bau - bay * cau) * invDet;
  241. const float bz = (caz * bau - baz * cau) * invDet;
  242. for (uint32_t jj = 0; jj < 3; ++jj)
  243. {
  244. float* tanu = &tangents[indices[jj]*6];
  245. float* tanv = &tanu[3];
  246. tanu[0] += tx;
  247. tanu[1] += ty;
  248. tanu[2] += tz;
  249. tanv[0] += bx;
  250. tanv[1] += by;
  251. tanv[2] += bz;
  252. }
  253. }
  254. for (uint32_t ii = 0; ii < _numVertices; ++ii)
  255. {
  256. const bx::Vec3 tanu = bx::load<bx::Vec3>(&tangents[ii*6]);
  257. const bx::Vec3 tanv = bx::load<bx::Vec3>(&tangents[ii*6 + 3]);
  258. float nxyzw[4];
  259. bgfx::vertexUnpack(nxyzw, bgfx::Attrib::Normal, _layout, _vertices, ii);
  260. const bx::Vec3 normal = bx::load<bx::Vec3>(nxyzw);
  261. const float ndt = bx::dot(normal, tanu);
  262. const bx::Vec3 nxt = bx::cross(normal, tanu);
  263. const bx::Vec3 tmp = bx::sub(tanu, bx::mul(normal, ndt) );
  264. float tangent[4];
  265. bx::store(tangent, bx::normalize(tmp) );
  266. tangent[3] = bx::dot(nxt, tanv) < 0.0f ? -1.0f : 1.0f;
  267. bgfx::vertexPack(tangent, true, bgfx::Attrib::Tangent, _layout, _vertices, ii);
  268. }
  269. delete [] tangents;
  270. }
  271. void write(
  272. bx::WriterI* _writer
  273. , const void* _vertices
  274. , uint32_t _numVertices
  275. , uint32_t _stride
  276. , bx::Error* _err
  277. )
  278. {
  279. bx::Sphere maxSphere;
  280. bx::calcMaxBoundingSphere(maxSphere, _vertices, _numVertices, _stride);
  281. bx::Sphere minSphere;
  282. bx::calcMinBoundingSphere(minSphere, _vertices, _numVertices, _stride);
  283. if (minSphere.radius > maxSphere.radius)
  284. {
  285. bx::write(_writer, maxSphere, _err);
  286. }
  287. else
  288. {
  289. bx::write(_writer, minSphere, _err);
  290. }
  291. bx::Aabb aabb;
  292. bx::toAabb(aabb, _vertices, _numVertices, _stride);
  293. bx::write(_writer, aabb, _err);
  294. bx::Obb obb;
  295. bx::calcObb(obb, _vertices, _numVertices, _stride, s_obbSteps);
  296. bx::write(_writer, obb, _err);
  297. }
  298. void write(
  299. bx::WriterI* _writer
  300. , const uint8_t* _vertices
  301. , uint32_t _numVertices
  302. , const bgfx::VertexLayout& _layout
  303. , const uint16_t* _indices
  304. , uint32_t _numIndices
  305. , bool _compress
  306. , const stl::string& _material
  307. , const PrimitiveArray& _primitives
  308. , bx::Error* _err
  309. )
  310. {
  311. using namespace bx;
  312. using namespace bgfx;
  313. uint32_t stride = _layout.getStride();
  314. if (_compress)
  315. {
  316. write(_writer, kChunkVertexBufferCompressed, _err);
  317. write(_writer, _vertices, _numVertices, stride, _err);
  318. write(_writer, _layout);
  319. write(_writer, uint16_t(_numVertices), _err);
  320. writeCompressedVertices(_writer, _vertices, _numVertices, uint16_t(stride), _err);
  321. }
  322. else
  323. {
  324. write(_writer, kChunkVertexBuffer, _err);
  325. write(_writer, _vertices, _numVertices, stride, _err);
  326. write(_writer, _layout, _err);
  327. write(_writer, uint16_t(_numVertices), _err);
  328. write(_writer, _vertices, _numVertices*stride, _err);
  329. }
  330. if (_compress)
  331. {
  332. write(_writer, kChunkIndexBufferCompressed, _err);
  333. write(_writer, _numIndices, _err);
  334. writeCompressedIndices(_writer, _indices, _numIndices, _numVertices, _err);
  335. }
  336. else
  337. {
  338. write(_writer, kChunkIndexBuffer, _err);
  339. write(_writer, _numIndices, _err);
  340. write(_writer, _indices, _numIndices*2, _err);
  341. }
  342. write(_writer, kChunkPrimitive, _err);
  343. uint16_t nameLen = uint16_t(_material.size() );
  344. write(_writer, nameLen, _err);
  345. write(_writer, _material.c_str(), nameLen, _err);
  346. write(_writer, uint16_t(_primitives.size() ), _err);
  347. for (PrimitiveArray::const_iterator primIt = _primitives.begin(); primIt != _primitives.end(); ++primIt)
  348. {
  349. const Primitive& prim = *primIt;
  350. nameLen = uint16_t(prim.m_name.size() );
  351. write(_writer, nameLen, _err);
  352. write(_writer, prim.m_name.c_str(), nameLen, _err);
  353. write(_writer, prim.m_startIndex, _err);
  354. write(_writer, prim.m_numIndices, _err);
  355. write(_writer, prim.m_startVertex, _err);
  356. write(_writer, prim.m_numVertices, _err);
  357. write(_writer, &_vertices[prim.m_startVertex*stride], prim.m_numVertices, stride, _err);
  358. }
  359. }
  360. inline uint32_t rgbaToAbgr(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a)
  361. {
  362. return (uint32_t(_r)<<0)
  363. | (uint32_t(_g)<<8)
  364. | (uint32_t(_b)<<16)
  365. | (uint32_t(_a)<<24)
  366. ;
  367. }
  368. struct GroupSortByMaterial
  369. {
  370. bool operator()(const Group& _lhs, const Group& _rhs)
  371. {
  372. return 0 < bx::strCmp(_lhs.m_material.c_str(), _rhs.m_material.c_str() );
  373. }
  374. };
  375. void mtxCoordinateTransform(float* _result, const CoordinateSystem& _cs)
  376. {
  377. bx::Vec3 up = s_axisVectors[_cs.m_up];
  378. bx::Vec3 forward = s_axisVectors[_cs.m_forward];
  379. bx::Vec3 right = cross(forward,up);
  380. if (_cs.m_handedness == bx::Handedness::Left)
  381. {
  382. right = bx::mul(right, -1.0f);
  383. }
  384. bx::mtxIdentity(_result);
  385. bx::store(&_result[0], right);
  386. bx::store(&_result[4], forward);
  387. bx::store(&_result[8], up);
  388. }
  389. float mtxDeterminant(const float* _a)
  390. {
  391. const float xx = _a[ 0];
  392. const float xy = _a[ 1];
  393. const float xz = _a[ 2];
  394. const float xw = _a[ 3];
  395. const float yx = _a[ 4];
  396. const float yy = _a[ 5];
  397. const float yz = _a[ 6];
  398. const float yw = _a[ 7];
  399. const float zx = _a[ 8];
  400. const float zy = _a[ 9];
  401. const float zz = _a[10];
  402. const float zw = _a[11];
  403. const float wx = _a[12];
  404. const float wy = _a[13];
  405. const float wz = _a[14];
  406. const float ww = _a[15];
  407. float det = 0.0f;
  408. det += xx * (yy*(zz*ww - zw*wz) - yz*(zy*ww - zw*wy) + yw*(zy*wz - zz*wy) );
  409. det -= xy * (yx*(zz*ww - zw*wz) - yz*(zx*ww - zw*wx) + yw*(zx*wz - zz*wx) );
  410. det += xz * (yx*(zy*ww - zw*wy) - yy*(zx*ww - zw*wx) + yw*(zx*wy - zy*wx) );
  411. det -= xw * (yx*(zy*wz - zz*wy) - yy*(zx*wz - zz*wx) + yz*(zx*wy - zy*wx) );
  412. return det;
  413. }
  414. void parseObj(char* _data, uint32_t _size, Mesh* _mesh, bool _hasBc)
  415. {
  416. // Reference(s):
  417. // - Wavefront .obj file
  418. // https://en.wikipedia.org/wiki/Wavefront_.obj_file
  419. // Coordinate system is right-handed, but up/forward is not defined, but +Y Up, +Z Forward seems to be a common default
  420. _mesh->m_coordinateSystem.m_handedness = bx::Handedness::Right;
  421. _mesh->m_coordinateSystem.m_up = Axis::PositiveY;
  422. _mesh->m_coordinateSystem.m_forward = Axis::PositiveZ;
  423. uint32_t num = 0;
  424. Group group;
  425. group.m_startTriangle = 0;
  426. group.m_numTriangles = 0;
  427. char commandLine[2048];
  428. uint32_t len = sizeof(commandLine);
  429. int argc;
  430. char* argv[64];
  431. for (bx::StringView next(_data, _size); !next.isEmpty(); )
  432. {
  433. next = bx::tokenizeCommandLine(next, commandLine, len, argc, argv, BX_COUNTOF(argv), '\n');
  434. if (0 < argc)
  435. {
  436. if (0 == bx::strCmp(argv[0], "#") )
  437. {
  438. if (2 < argc
  439. && 0 == bx::strCmp(argv[2], "polygons") )
  440. {
  441. }
  442. }
  443. else if (0 == bx::strCmp(argv[0], "f") )
  444. {
  445. TriIndices triangle;
  446. bx::memSet(&triangle, 0, sizeof(TriIndices) );
  447. const int numNormals = (int)_mesh->m_normals.size();
  448. const int numTexcoords = (int)_mesh->m_texcoords.size();
  449. const int numPositions = (int)_mesh->m_positions.size();
  450. for (uint32_t edge = 0, numEdges = argc-1; edge < numEdges; ++edge)
  451. {
  452. Index3 index;
  453. index.m_texcoord = -1;
  454. index.m_normal = -1;
  455. if (_hasBc)
  456. {
  457. index.m_vbc = edge < 3 ? edge : (1+(edge+1) )&1;
  458. }
  459. else
  460. {
  461. index.m_vbc = 0;
  462. }
  463. {
  464. bx::StringView triplet(argv[edge + 1]);
  465. bx::StringView vertex(triplet);
  466. bx::StringView texcoord = bx::strFind(triplet, '/');
  467. if (!texcoord.isEmpty() )
  468. {
  469. vertex.set(vertex.getPtr(), texcoord.getPtr() );
  470. const bx::StringView normal = bx::strFind(bx::StringView(texcoord.getPtr() + 1, triplet.getTerm() ), '/');
  471. if (!normal.isEmpty() )
  472. {
  473. int32_t nn;
  474. bx::fromString(&nn, bx::StringView(normal.getPtr() + 1, triplet.getTerm() ) );
  475. index.m_normal = (nn < 0) ? nn + numNormals : nn - 1;
  476. }
  477. texcoord.set(texcoord.getPtr() + 1, normal.getPtr() );
  478. // Reference(s):
  479. // - Wavefront .obj file / Vertex normal indices without texture coordinate indices
  480. // https://en.wikipedia.org/wiki/Wavefront_.obj_file#Vertex_Normal_Indices_Without_Texture_Coordinate_Indices
  481. if (!texcoord.isEmpty() )
  482. {
  483. int32_t tex;
  484. bx::fromString(&tex, texcoord);
  485. index.m_texcoord = (tex < 0) ? tex + numTexcoords : tex - 1;
  486. }
  487. }
  488. int32_t pos;
  489. bx::fromString(&pos, vertex);
  490. index.m_position = (pos < 0) ? pos + numPositions : pos - 1;
  491. }
  492. switch (edge)
  493. {
  494. case 0: case 1: case 2:
  495. triangle.m_index[edge] = index;
  496. if (2 == edge)
  497. {
  498. _mesh->m_triangles.push_back(triangle);
  499. }
  500. break;
  501. default:
  502. triangle.m_index[1] = triangle.m_index[2];
  503. triangle.m_index[2] = index;
  504. _mesh->m_triangles.push_back(triangle);
  505. break;
  506. }
  507. }
  508. }
  509. else if (0 == bx::strCmp(argv[0], "g") )
  510. {
  511. group.m_name = argv[1];
  512. }
  513. else if (*argv[0] == 'v')
  514. {
  515. group.m_numTriangles = (uint32_t)(_mesh->m_triangles.size() ) - group.m_startTriangle;
  516. if (0 < group.m_numTriangles)
  517. {
  518. _mesh->m_groups.push_back(group);
  519. group.m_startTriangle = (uint32_t)(_mesh->m_triangles.size() );
  520. group.m_numTriangles = 0;
  521. }
  522. if (0 == bx::strCmp(argv[0], "vn") )
  523. {
  524. bx::Vec3 normal(bx::InitNone);
  525. bx::fromString(&normal.x, argv[1]);
  526. bx::fromString(&normal.y, argv[2]);
  527. bx::fromString(&normal.z, argv[3]);
  528. _mesh->m_normals.push_back(normal);
  529. }
  530. else if (0 == bx::strCmp(argv[0], "vp") )
  531. {
  532. static bool once = true;
  533. if (once)
  534. {
  535. once = false;
  536. bx::printf("warning: 'parameter space vertices' are unsupported.\n");
  537. }
  538. }
  539. else if (0 == bx::strCmp(argv[0], "vt") )
  540. {
  541. bx::Vec3 texcoord(bx::InitNone);
  542. texcoord.y = 0.0f;
  543. texcoord.z = 0.0f;
  544. bx::fromString(&texcoord.x, argv[1]);
  545. switch (argc)
  546. {
  547. case 4:
  548. bx::fromString(&texcoord.z, argv[3]);
  549. [[fallthrough]];
  550. case 3:
  551. bx::fromString(&texcoord.y, argv[2]);
  552. break;
  553. default:
  554. break;
  555. }
  556. _mesh->m_texcoords.push_back(texcoord);
  557. }
  558. else
  559. {
  560. float px, py, pz, pw;
  561. bx::fromString(&px, argv[1]);
  562. bx::fromString(&py, argv[2]);
  563. bx::fromString(&pz, argv[3]);
  564. if (argc == 5 || argc == 8)
  565. {
  566. bx::fromString(&pw, argv[4]);
  567. }
  568. else
  569. {
  570. pw = 1.0f;
  571. }
  572. bx::Vec3 pos(px, py, pz);
  573. const float invW = bx::rcp(pw);
  574. pos = bx::mul(pos, invW);
  575. _mesh->m_positions.push_back(pos);
  576. }
  577. }
  578. else if (0 == bx::strCmp(argv[0], "usemtl") )
  579. {
  580. stl::string material(argv[1]);
  581. if (0 != bx::strCmp(material.c_str(), group.m_material.c_str() ) )
  582. {
  583. group.m_numTriangles = (uint32_t)(_mesh->m_triangles.size() ) - group.m_startTriangle;
  584. if (0 < group.m_numTriangles)
  585. {
  586. _mesh->m_groups.push_back(group);
  587. group.m_startTriangle = (uint32_t)(_mesh->m_triangles.size() );
  588. group.m_numTriangles = 0;
  589. }
  590. }
  591. group.m_material = material;
  592. }
  593. }
  594. ++num;
  595. }
  596. group.m_numTriangles = (uint32_t)(_mesh->m_triangles.size() ) - group.m_startTriangle;
  597. if (0 < group.m_numTriangles)
  598. {
  599. _mesh->m_groups.push_back(group);
  600. group.m_startTriangle = (uint32_t)(_mesh->m_triangles.size() );
  601. group.m_numTriangles = 0;
  602. }
  603. bx::printf("obj parser # %d\n", num);
  604. }
  605. void gltfReadFloat(const float* _accessorData, cgltf_size _accessorNumComponents, cgltf_size _index, cgltf_float* _out, cgltf_size _outElementSize)
  606. {
  607. const float* input = &_accessorData[_accessorNumComponents * _index];
  608. for (cgltf_size ii = 0; ii < _outElementSize; ++ii)
  609. {
  610. _out[ii] = (ii < _accessorNumComponents) ? input[ii] : 0.0f;
  611. }
  612. }
  613. void processGltfNode(cgltf_node* _node, Mesh* _mesh, Group* _group, bool _hasBc)
  614. {
  615. cgltf_mesh* mesh = _node->mesh;
  616. if (NULL != mesh)
  617. {
  618. float nodeToWorld[16];
  619. cgltf_node_transform_world(_node, nodeToWorld);
  620. float nodeToWorldNormal[16];
  621. bx::mtxCofactor(nodeToWorldNormal, nodeToWorld);
  622. for (cgltf_size primitiveIndex = 0; primitiveIndex < mesh->primitives_count; ++primitiveIndex)
  623. {
  624. cgltf_primitive* primitive = &mesh->primitives[primitiveIndex];
  625. cgltf_size numVertex = primitive->attributes[0].data->count;
  626. int32_t basePositionIndex = (int32_t)_mesh->m_positions.size();
  627. int32_t baseNormalIndex = (int32_t)_mesh->m_normals.size();
  628. int32_t baseTexcoordIndex = (int32_t)_mesh->m_texcoords.size();
  629. bool hasNormal = false;
  630. bool hasTexcoord = false;
  631. for (cgltf_size attributeIndex = 0; attributeIndex < primitive->attributes_count; ++attributeIndex)
  632. {
  633. cgltf_attribute* attribute = &primitive->attributes[attributeIndex];
  634. cgltf_accessor* accessor = attribute->data;
  635. cgltf_size accessorCount = accessor->count;
  636. BX_ASSERT(numVertex == accessorCount, "Invalid attribute count");
  637. cgltf_size floatCount = cgltf_accessor_unpack_floats(accessor, NULL, 0);
  638. float* accessorData = (float*)malloc(floatCount * sizeof(float) );
  639. cgltf_accessor_unpack_floats(accessor, accessorData, floatCount);
  640. cgltf_size numComponents = cgltf_num_components(accessor->type);
  641. if (attribute->type == cgltf_attribute_type_position && attribute->index == 0)
  642. {
  643. _mesh->m_positions.reserve(_mesh->m_positions.size() + accessorCount);
  644. bx::Vec3 pos(bx::InitNone);
  645. for (cgltf_size v = 0; v < accessorCount; ++v)
  646. {
  647. gltfReadFloat(accessorData, numComponents, v, &pos.x, 3);
  648. pos = mul(pos, nodeToWorld);
  649. _mesh->m_positions.push_back(pos);
  650. }
  651. }
  652. else if (attribute->type == cgltf_attribute_type_normal && attribute->index == 0)
  653. {
  654. _mesh->m_normals.reserve(_mesh->m_normals.size() + accessorCount);
  655. hasNormal = true;
  656. bx::Vec3 normal(bx::InitNone);
  657. for (cgltf_size v = 0; v < accessorCount; ++v)
  658. {
  659. gltfReadFloat(accessorData, numComponents, v, &normal.x, 3);
  660. normal = mul(normal, nodeToWorldNormal);
  661. _mesh->m_normals.push_back(normal);
  662. }
  663. }
  664. else if (attribute->type == cgltf_attribute_type_texcoord && attribute->index == 0)
  665. {
  666. _mesh->m_texcoords.reserve(_mesh->m_texcoords.size() + accessorCount);
  667. hasTexcoord = true;
  668. bx::Vec3 texcoord(bx::InitNone);
  669. for (cgltf_size v = 0; v < accessorCount; ++v)
  670. {
  671. gltfReadFloat(accessorData, numComponents, v, &texcoord.x, 3);
  672. _mesh->m_texcoords.push_back(texcoord);
  673. }
  674. }
  675. free(accessorData);
  676. }
  677. if (primitive->indices != NULL)
  678. {
  679. cgltf_accessor* accessor = primitive->indices;
  680. for (cgltf_size v = 0; v < accessor->count; v += 3)
  681. {
  682. TriIndices triangle;
  683. for (int i = 0; i < 3; ++i)
  684. {
  685. Index3 index;
  686. int32_t vertexIndex = int32_t(cgltf_accessor_read_index(accessor, v+i) );
  687. index.m_position = basePositionIndex + vertexIndex;
  688. index.m_normal = hasNormal ? baseNormalIndex + vertexIndex : -1;
  689. index.m_texcoord = hasTexcoord ? baseTexcoordIndex + vertexIndex : -1;
  690. index.m_vbc = _hasBc ? i : 0;
  691. triangle.m_index[i] = index;
  692. }
  693. _mesh->m_triangles.push_back(triangle);
  694. }
  695. }
  696. else
  697. {
  698. for (cgltf_size v = 0; v < numVertex; v += 3)
  699. {
  700. TriIndices triangle;
  701. for (int i = 0; i < 3; ++i)
  702. {
  703. Index3 index;
  704. int32_t vertexIndex = int32_t(v * 3 + i);
  705. index.m_position = basePositionIndex + vertexIndex;
  706. index.m_normal = hasNormal ? baseNormalIndex + vertexIndex : -1;
  707. index.m_texcoord = hasTexcoord ? baseTexcoordIndex + vertexIndex : -1;
  708. index.m_vbc = _hasBc ? i : 0;
  709. triangle.m_index[i] = index;
  710. }
  711. _mesh->m_triangles.push_back(triangle);
  712. }
  713. }
  714. _group->m_numTriangles = (uint32_t)(_mesh->m_triangles.size() ) - _group->m_startTriangle;
  715. if (0 < _group->m_numTriangles)
  716. {
  717. _mesh->m_groups.push_back(*_group);
  718. _group->m_startTriangle = (uint32_t)(_mesh->m_triangles.size() );
  719. _group->m_numTriangles = 0;
  720. }
  721. }
  722. }
  723. for (cgltf_size childIndex = 0; childIndex < _node->children_count; ++childIndex)
  724. {
  725. processGltfNode(_node->children[childIndex], _mesh, _group, _hasBc);
  726. }
  727. }
  728. void parseGltf(char* _data, uint32_t _size, Mesh* _mesh, bool _hasBc, const bx::StringView& _path)
  729. {
  730. // Reference(s):
  731. // - Gltf 2.0 specification
  732. // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0
  733. _mesh->m_coordinateSystem.m_handedness = bx::Handedness::Right;
  734. _mesh->m_coordinateSystem.m_forward = Axis::PositiveZ;
  735. _mesh->m_coordinateSystem.m_up = Axis::PositiveY;
  736. Group group;
  737. group.m_startTriangle = 0;
  738. group.m_numTriangles = 0;
  739. cgltf_options options = { };
  740. cgltf_data* data = NULL;
  741. cgltf_result result = cgltf_parse(&options, _data, _size, &data);
  742. if (result == cgltf_result_success)
  743. {
  744. char* path = (char*)malloc(_path.getLength()+1);
  745. bx::memCopy(path, _path.getPtr(), _path.getLength() );
  746. path[_path.getLength()] = 0;
  747. result = cgltf_load_buffers(&options, data, path);
  748. free(path);
  749. if (result == cgltf_result_success)
  750. {
  751. for (cgltf_size sceneIndex = 0; sceneIndex < data->scenes_count; ++sceneIndex)
  752. {
  753. cgltf_scene* scene = &data->scenes[sceneIndex];
  754. for (cgltf_size nodeIndex = 0; nodeIndex < scene->nodes_count; ++nodeIndex)
  755. {
  756. cgltf_node* node = scene->nodes[nodeIndex];
  757. processGltfNode(node, _mesh, &group, _hasBc);
  758. }
  759. }
  760. }
  761. cgltf_free(data);
  762. }
  763. }
  764. void help(const char* _error = NULL)
  765. {
  766. if (NULL != _error)
  767. {
  768. bx::printf("Error:\n%s\n\n", _error);
  769. }
  770. bx::printf(
  771. "geometryc, bgfx geometry compiler tool, version %d.%d.%d.\n"
  772. "Copyright 2011-2023 Branimir Karadzic. All rights reserved.\n"
  773. "License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE\n\n"
  774. , BGFX_GEOMETRYC_VERSION_MAJOR
  775. , BGFX_GEOMETRYC_VERSION_MINOR
  776. , BGFX_API_VERSION
  777. );
  778. bx::printf(
  779. "Usage: geometryc -f <in> -o <out>\n"
  780. "\n"
  781. "Supported input file types:\n"
  782. " *.obj Wavefront\n"
  783. " *.gltf,*.glb glTF 2.0\n"
  784. "\n"
  785. "Options:\n"
  786. " -h, --help Display this help and exit.\n"
  787. " -v, --version Output version information and exit.\n"
  788. " -f <file path> Input's file path.\n"
  789. " -o <file path> Output's file path.\n"
  790. " -s, --scale <num> Scale factor.\n"
  791. " --ccw Front face is counter-clockwise winding order.\n"
  792. " --flipv Flip texture coordinate V.\n"
  793. " --obb <num> Number of steps for calculating oriented bounding box.\n"
  794. " Defaults to 17.\n"
  795. " Less steps = less precise OBB.\n"
  796. " More steps = slower calculation.\n"
  797. " --packnormal <num> Normal packing.\n"
  798. " 0 - unpacked 12 bytes. (default)\n"
  799. " 1 - packed 4 bytes.\n"
  800. " --packuv <num> Texture coordinate packing.\n"
  801. " 0 - unpacked 8 bytes. (default)\n"
  802. " 1 - packed 4 bytes.\n"
  803. " --tangent Calculate tangent vectors. (packing mode is the same as normal)\n"
  804. " --barycentric Adds barycentric vertex attribute. (Packed in bgfx::Attrib::Color1)\n"
  805. " -c, --compress Compress indices.\n"
  806. " --[l/r]h-up+[y/z] Coordinate system. Defaults to '--lh-up+y' — Left-Handed +Y is up.\n"
  807. "\n"
  808. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  809. );
  810. }
  811. int main(int _argc, const char* _argv[])
  812. {
  813. bx::CommandLine cmdLine(_argc, _argv);
  814. if (cmdLine.hasArg('v', "version") )
  815. {
  816. bx::printf(
  817. "geometryc, bgfx geometry compiler tool, version %d.%d.%d.\n"
  818. , BGFX_GEOMETRYC_VERSION_MAJOR
  819. , BGFX_GEOMETRYC_VERSION_MINOR
  820. , BGFX_API_VERSION
  821. );
  822. return bx::kExitSuccess;
  823. }
  824. if (cmdLine.hasArg('h', "help") )
  825. {
  826. help();
  827. return bx::kExitFailure;
  828. }
  829. const char* filePath = cmdLine.findOption('f');
  830. if (NULL == filePath)
  831. {
  832. help("Input file name must be specified.");
  833. return bx::kExitFailure;
  834. }
  835. const char* outFilePath = cmdLine.findOption('o');
  836. if (NULL == outFilePath)
  837. {
  838. help("Output file name must be specified.");
  839. return bx::kExitFailure;
  840. }
  841. float scale = 1.0f;
  842. const char* scaleArg = cmdLine.findOption('s', "scale");
  843. if (NULL != scaleArg)
  844. {
  845. if (!bx::fromString(&scale, scaleArg) )
  846. {
  847. scale = 1.0f;
  848. }
  849. }
  850. bool compress = cmdLine.hasArg('c', "compress");
  851. cmdLine.hasArg(s_obbSteps, '\0', "obb");
  852. s_obbSteps = bx::uint32_min(bx::uint32_max(s_obbSteps, 1), 90);
  853. uint32_t packNormal = 0;
  854. cmdLine.hasArg(packNormal, '\0', "packnormal");
  855. uint32_t packUv = 0;
  856. cmdLine.hasArg(packUv, '\0', "packuv");
  857. bool ccw = cmdLine.hasArg("ccw");
  858. bool flipV = cmdLine.hasArg("flipv");
  859. bool hasTangent = cmdLine.hasArg("tangent");
  860. bool hasBc = cmdLine.hasArg("barycentric");
  861. CoordinateSystem outputCoordinateSystem;
  862. outputCoordinateSystem.m_handedness = bx::Handedness::Left;
  863. outputCoordinateSystem.m_forward = Axis::PositiveZ;
  864. outputCoordinateSystem.m_up = Axis::PositiveY;
  865. for (uint32_t ii = 0; ii < BX_COUNTOF(s_coordinateSystemMappings); ++ii)
  866. {
  867. if (cmdLine.hasArg(s_coordinateSystemMappings[ii].m_param) )
  868. {
  869. outputCoordinateSystem = s_coordinateSystemMappings[ii].m_coordinateSystem;
  870. }
  871. }
  872. bx::FileReader fr;
  873. if (!bx::open(&fr, filePath) )
  874. {
  875. bx::printf("Unable to open input file '%s'.", filePath);
  876. return bx::kExitFailure;
  877. }
  878. int64_t parseElapsed = -bx::getHPCounter();
  879. int64_t triReorderElapsed = 0;
  880. uint32_t size = (uint32_t)bx::getSize(&fr);
  881. char* data = new char[size+1];
  882. size = bx::read(&fr, data, size, bx::ErrorAssert{});
  883. data[size] = '\0';
  884. bx::close(&fr);
  885. Mesh mesh;
  886. bx::StringView ext = bx::FilePath(filePath).getExt();
  887. if (0 == bx::strCmpI(ext, ".obj") )
  888. {
  889. parseObj(data, size, &mesh, hasBc);
  890. }
  891. else if (0 == bx::strCmpI(ext, ".gltf") || 0 == bx::strCmpI(ext, ".glb") )
  892. {
  893. parseGltf(data, size, &mesh, hasBc, bx::FilePath(filePath).getPath() );
  894. }
  895. else
  896. {
  897. bx::printf("Unsupported input file format '%s'.", filePath);
  898. exit(bx::kExitFailure);
  899. }
  900. delete [] data;
  901. int64_t now = bx::getHPCounter();
  902. parseElapsed += now;
  903. int64_t convertElapsed = -now;
  904. std::sort(mesh.m_groups.begin(), mesh.m_groups.end(), GroupSortByMaterial() );
  905. bool changeWinding = ccw;
  906. if (scale != 1.0f)
  907. {
  908. for (Vec3Array::iterator it = mesh.m_positions.begin(), itEnd = mesh.m_positions.end(); it != itEnd; ++it)
  909. {
  910. it->x *= scale;
  911. it->y *= scale;
  912. it->z *= scale;
  913. }
  914. }
  915. {
  916. float meshTransform[16];
  917. mtxCoordinateTransform(meshTransform, mesh.m_coordinateSystem);
  918. float meshInvTranform[16];
  919. bx::mtxTranspose(meshInvTranform, meshTransform);
  920. float outTransform[16];
  921. mtxCoordinateTransform(outTransform, outputCoordinateSystem);
  922. float transform[16];
  923. bx::mtxMul(transform, meshInvTranform, outTransform);
  924. if (mtxDeterminant(transform) < 0.0f )
  925. {
  926. changeWinding = !changeWinding;
  927. }
  928. float identity[16];
  929. bx::mtxIdentity(identity);
  930. if (0 != bx::memCmp(identity, transform, sizeof(transform) ) )
  931. {
  932. for (Vec3Array::iterator it = mesh.m_positions.begin(), itEnd = mesh.m_positions.end(); it != itEnd; ++it)
  933. {
  934. *it = bx::mul(*it, transform);
  935. }
  936. for (Vec3Array::iterator it = mesh.m_normals.begin(), itEnd = mesh.m_normals.end(); it != itEnd; ++it)
  937. {
  938. *it = bx::mul(*it, transform);
  939. }
  940. }
  941. }
  942. bool hasColor = false;
  943. bool hasNormal = false;
  944. bool hasTexcoord = false;
  945. {
  946. for (TriangleArray::iterator it = mesh.m_triangles.begin(), itEnd = mesh.m_triangles.end(); it != itEnd && !hasTexcoord; ++it)
  947. {
  948. for (uint32_t i = 0; i < 3; ++i)
  949. {
  950. hasTexcoord |= -1 != it->m_index[i].m_texcoord;
  951. }
  952. }
  953. for (TriangleArray::iterator it = mesh.m_triangles.begin(), itEnd = mesh.m_triangles.end(); it != itEnd && !hasNormal; ++it)
  954. {
  955. for (uint32_t i = 0; i < 3; ++i)
  956. {
  957. hasNormal |= -1 != it->m_index[i].m_normal;
  958. }
  959. }
  960. if (changeWinding)
  961. {
  962. for (TriangleArray::iterator it = mesh.m_triangles.begin(), itEnd = mesh.m_triangles.end(); it != itEnd; ++it)
  963. {
  964. bx::swap(it->m_index[1], it->m_index[2]);
  965. }
  966. }
  967. }
  968. bgfx::VertexLayout layout;
  969. layout.begin();
  970. layout.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  971. if (hasColor)
  972. {
  973. layout.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  974. }
  975. if (hasBc)
  976. {
  977. layout.add(bgfx::Attrib::Color1, 4, bgfx::AttribType::Uint8, true);
  978. }
  979. if (hasTexcoord)
  980. {
  981. switch (packUv)
  982. {
  983. default:
  984. case 0:
  985. layout.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  986. break;
  987. case 1:
  988. layout.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Half);
  989. break;
  990. }
  991. }
  992. if (hasNormal)
  993. {
  994. hasTangent &= hasTexcoord;
  995. switch (packNormal)
  996. {
  997. default:
  998. case 0:
  999. layout.add(bgfx::Attrib::Normal, 3, bgfx::AttribType::Float);
  1000. if (hasTangent)
  1001. {
  1002. layout.add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Float);
  1003. }
  1004. break;
  1005. case 1:
  1006. layout.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true);
  1007. if (hasTangent)
  1008. {
  1009. layout.add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true);
  1010. }
  1011. break;
  1012. }
  1013. }
  1014. layout.end();
  1015. uint32_t stride = layout.getStride();
  1016. uint8_t* vertexData = new uint8_t[mesh.m_triangles.size() * 3 * stride];
  1017. uint16_t* indexData = new uint16_t[mesh.m_triangles.size() * 3];
  1018. int32_t numVertices = 0;
  1019. int32_t numIndices = 0;
  1020. int32_t writtenPrimitives = 0;
  1021. int32_t writtenVertices = 0;
  1022. int32_t writtenIndices = 0;
  1023. uint8_t* vertices = vertexData;
  1024. uint16_t* indices = indexData;
  1025. const uint32_t tableSize = 65536 * 2;
  1026. const uint32_t hashmod = tableSize - 1;
  1027. uint32_t* table = new uint32_t[tableSize];
  1028. bx::memSet(table, 0xff, tableSize * sizeof(uint32_t) );
  1029. stl::string material = mesh.m_groups.empty() ? "" : mesh.m_groups.begin()->m_material;
  1030. PrimitiveArray primitives;
  1031. bx::FileWriter writer;
  1032. if (!bx::open(&writer, outFilePath) )
  1033. {
  1034. bx::printf("Unable to open output file '%s'.", outFilePath);
  1035. exit(bx::kExitFailure);
  1036. }
  1037. Primitive prim;
  1038. prim.m_startVertex = 0;
  1039. prim.m_startIndex = 0;
  1040. uint32_t positionOffset = layout.getOffset(bgfx::Attrib::Position);
  1041. uint32_t color0Offset = layout.getOffset(bgfx::Attrib::Color0);
  1042. Group sentinelGroup;
  1043. sentinelGroup.m_startTriangle = 0;
  1044. sentinelGroup.m_numTriangles = UINT32_MAX;
  1045. mesh.m_groups.push_back(sentinelGroup);
  1046. bx::Error err;
  1047. uint32_t ii = 0;
  1048. for (GroupArray::const_iterator groupIt = mesh.m_groups.begin(); groupIt != mesh.m_groups.end(); ++groupIt, ++ii)
  1049. {
  1050. const bool sentinel = groupIt->m_startTriangle == 0 && groupIt->m_numTriangles == UINT32_MAX;
  1051. for (uint32_t tri = groupIt->m_startTriangle, end = tri + groupIt->m_numTriangles; tri < end; ++tri)
  1052. {
  1053. if (0 != bx::strCmp(material.c_str(), groupIt->m_material.c_str() )
  1054. || sentinel
  1055. || 65533 <= numVertices)
  1056. {
  1057. prim.m_numVertices = numVertices - prim.m_startVertex;
  1058. prim.m_numIndices = numIndices - prim.m_startIndex;
  1059. if (0 < prim.m_numVertices)
  1060. {
  1061. primitives.push_back(prim);
  1062. }
  1063. if (hasTangent)
  1064. {
  1065. calcTangents(vertexData, uint16_t(numVertices), layout, indexData, numIndices);
  1066. }
  1067. triReorderElapsed -= bx::getHPCounter();
  1068. for (PrimitiveArray::const_iterator primIt = primitives.begin(); primIt != primitives.end(); ++primIt)
  1069. {
  1070. const Primitive& prim1 = *primIt;
  1071. optimizeVertexCache(indexData + prim1.m_startIndex, prim1.m_numIndices, numVertices);
  1072. }
  1073. numVertices = optimizeVertexFetch(indexData, numIndices, vertexData, numVertices, uint16_t(stride) );
  1074. triReorderElapsed += bx::getHPCounter();
  1075. if (0 < numVertices
  1076. && 0 < numIndices)
  1077. {
  1078. write(&writer
  1079. , vertexData
  1080. , numVertices
  1081. , layout
  1082. , indexData
  1083. , numIndices
  1084. , compress
  1085. , material
  1086. , primitives
  1087. , &err
  1088. );
  1089. }
  1090. primitives.clear();
  1091. bx::memSet(table, 0xff, tableSize * sizeof(uint32_t) );
  1092. ++writtenPrimitives;
  1093. writtenVertices += numVertices;
  1094. writtenIndices += numIndices;
  1095. vertices = vertexData;
  1096. indices = indexData;
  1097. numVertices = 0;
  1098. numIndices = 0;
  1099. prim.m_startVertex = 0;
  1100. prim.m_startIndex = 0;
  1101. material = groupIt->m_material;
  1102. if (sentinel)
  1103. {
  1104. break;
  1105. }
  1106. }
  1107. TriIndices& triangle = mesh.m_triangles[tri];
  1108. for (uint32_t edge = 0; edge < 3; ++edge)
  1109. {
  1110. Index3& index = triangle.m_index[edge];
  1111. float* position = (float*)(vertices + positionOffset);
  1112. bx::memCopy(position, &mesh.m_positions[index.m_position], 3*sizeof(float) );
  1113. if (hasColor)
  1114. {
  1115. uint32_t* color0 = (uint32_t*)(vertices + color0Offset);
  1116. *color0 = rgbaToAbgr(numVertices%255, numIndices%255, 0, 0xff);
  1117. }
  1118. if (hasBc)
  1119. {
  1120. const float bc[4] =
  1121. {
  1122. (index.m_vbc == 0) ? 1.0f : 0.0f,
  1123. (index.m_vbc == 1) ? 1.0f : 0.0f,
  1124. (index.m_vbc == 2) ? 1.0f : 0.0f,
  1125. 0.0f
  1126. };
  1127. bgfx::vertexPack(bc, true, bgfx::Attrib::Color1, layout, vertices);
  1128. }
  1129. if (hasTexcoord)
  1130. {
  1131. float uv[2];
  1132. bx::memCopy(uv, &mesh.m_texcoords[index.m_texcoord == -1 ? 0 : index.m_texcoord], 2*sizeof(float) );
  1133. if (flipV)
  1134. {
  1135. uv[1] = -uv[1];
  1136. }
  1137. bgfx::vertexPack(uv, true, bgfx::Attrib::TexCoord0, layout, vertices);
  1138. }
  1139. if (hasNormal)
  1140. {
  1141. float normal[4];
  1142. bx::store(normal, bx::normalize(bx::load<bx::Vec3>(&mesh.m_normals[index.m_normal == -1 ? 0 : index.m_normal]) ) );
  1143. normal[3] = 0.0f;
  1144. bgfx::vertexPack(normal, true, bgfx::Attrib::Normal, layout, vertices);
  1145. }
  1146. uint32_t hash = bx::hash<bx::HashMurmur2A>(vertices, stride);
  1147. size_t bucket = hash & hashmod;
  1148. uint32_t vertexIndex = UINT32_MAX;
  1149. for (size_t probe = 0; probe <= hashmod; ++probe)
  1150. {
  1151. uint32_t& item = table[bucket];
  1152. if (item == ~0u)
  1153. {
  1154. vertices += stride;
  1155. item = numVertices++;
  1156. vertexIndex = item;
  1157. break;
  1158. }
  1159. if (0 == bx::memCmp(vertexData + item * stride, vertices, stride) )
  1160. {
  1161. vertexIndex = item;
  1162. break;
  1163. }
  1164. bucket = (bucket + probe + 1) & hashmod;
  1165. }
  1166. if ( vertexIndex == UINT32_MAX )
  1167. {
  1168. bx::printf("hash table insert failed");
  1169. exit(bx::kExitFailure);
  1170. }
  1171. *indices++ = (uint16_t)vertexIndex;
  1172. ++numIndices;
  1173. }
  1174. }
  1175. prim.m_numVertices = numVertices - prim.m_startVertex;
  1176. if (0 < prim.m_numVertices)
  1177. {
  1178. prim.m_numIndices = numIndices - prim.m_startIndex;
  1179. prim.m_name = groupIt->m_name;
  1180. primitives.push_back(prim);
  1181. prim.m_startVertex = numVertices;
  1182. prim.m_startIndex = numIndices;
  1183. }
  1184. BX_TRACE("%3d: s %5d, n %5d, %s\n"
  1185. , ii
  1186. , groupIt->m_startTriangle
  1187. , groupIt->m_numTriangles
  1188. , groupIt->m_material.c_str()
  1189. );
  1190. }
  1191. BX_ASSERT(0 == primitives.size(), "Not all primitives are written");
  1192. bx::printf("size: %d\n", uint32_t(bx::seek(&writer) ) );
  1193. bx::close(&writer);
  1194. delete [] table;
  1195. delete [] indexData;
  1196. delete [] vertexData;
  1197. now = bx::getHPCounter();
  1198. convertElapsed += now;
  1199. bx::printf("parse %f [s]\ntri reorder %f [s]\nconvert %f [s]\ng %d, p %d, v %d, i %d\n"
  1200. , double(parseElapsed)/bx::getHPFrequency()
  1201. , double(triReorderElapsed)/bx::getHPFrequency()
  1202. , double(convertElapsed)/bx::getHPFrequency()
  1203. , uint32_t(mesh.m_groups.size()-1)
  1204. , writtenPrimitives
  1205. , writtenVertices
  1206. , writtenIndices
  1207. );
  1208. return bx::kExitSuccess;
  1209. }