geometryc.cpp 38 KB

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