BabylonVertex.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #pragma once
  2. #include <vector>
  3. #include <set>
  4. #include <string>
  5. #include <memory>
  6. #include <fbxsdk.h>
  7. #include <cpprest\json.h>
  8. struct babylon_vector4{
  9. float x;
  10. float y;
  11. float z;
  12. float w;
  13. babylon_vector4() :x(0), y(0), z(0), w(0){}
  14. babylon_vector4(float x, float y, float z, float w) :x(x), y(y), z(z), w(w){}
  15. babylon_vector4(const babylon_vector4& v) :x(v.x), y(v.y), z(v.z), w(v.w) {}
  16. babylon_vector4(const FbxQuaternion& v) :x(static_cast<float>(v[0])), y(static_cast<float>(v[1])), z(static_cast<float>(v[2])), w(static_cast<float>(v[3])) {}
  17. };
  18. struct babylon_vector3{
  19. float x;
  20. float y;
  21. float z;
  22. babylon_vector3() :x(0), y(0), z(0){}
  23. babylon_vector3(float x, float y, float z) :x(x), y(y), z(z){}
  24. babylon_vector3(const babylon_vector3& v) :x(v.x), y(v.y), z(v.z){}
  25. babylon_vector3(const FbxDouble3& v) : x((float) v[0]), y((float) v[1]), z((float) v[2]){}
  26. babylon_vector3(const FbxDouble4& v) : x((float) v[0]), y((float) v[1]), z((float) v[2]){}
  27. };
  28. inline babylon_vector3 operator *(const babylon_vector3& v, float factor){
  29. return babylon_vector3(v.x*factor, v.y*factor, v.z*factor);
  30. }
  31. inline babylon_vector3 operator *(float factor, const babylon_vector3& v){
  32. return v*factor;
  33. }
  34. inline babylon_vector3 operator + (const babylon_vector3& lhs, const babylon_vector3& rhs){
  35. return babylon_vector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z);
  36. }
  37. inline babylon_vector3 operator - (const babylon_vector3& lhs, const babylon_vector3& rhs){
  38. return babylon_vector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z);
  39. }
  40. inline babylon_vector4 operator *(const babylon_vector4& v, float factor){
  41. return babylon_vector4(v.x*factor, v.y*factor, v.z*factor, v.w*factor);
  42. }
  43. inline babylon_vector4 operator *(float factor, const babylon_vector4& v){
  44. return v*factor;
  45. }
  46. inline babylon_vector4 operator + (const babylon_vector4& lhs, const babylon_vector4& rhs){
  47. return babylon_vector4(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z, lhs.w+rhs.w);
  48. }
  49. inline babylon_vector4 operator - (const babylon_vector4& lhs, const babylon_vector4& rhs){
  50. return babylon_vector4(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z, lhs.w - rhs.w);
  51. }
  52. inline bool operator <(const babylon_vector3& lhs, const babylon_vector3& rhs){
  53. if (lhs.x < rhs.x){
  54. return true;
  55. }
  56. else if (lhs.x > rhs.x){
  57. return false;
  58. }
  59. if (lhs.y < rhs.y){
  60. return true;
  61. }
  62. else if (lhs.y > rhs.y){
  63. return false;
  64. }
  65. return lhs.z < rhs.z;
  66. }
  67. struct babylon_vector2{
  68. float x;
  69. float y;
  70. babylon_vector2() :x(0), y(0){}
  71. babylon_vector2(float x, float y) :x(x), y(y){}
  72. babylon_vector2(const babylon_vector2& v) :x(v.x), y(v.y){}
  73. babylon_vector2(const FbxDouble2& v) :x(static_cast<float>(v[0])), y(static_cast<float>(v[1])){}
  74. };
  75. inline bool operator <(const babylon_vector2& lhs, const babylon_vector2& rhs){
  76. if (lhs.x < rhs.x){
  77. return true;
  78. }
  79. else if (lhs.x > rhs.x){
  80. return false;
  81. }
  82. return lhs.y < rhs.y;
  83. }
  84. struct babylon_vertex_normal_uv_color{
  85. float pos_x;
  86. float pos_y;
  87. float pos_z;
  88. float normal_x;
  89. float normal_y;
  90. float normal_z;
  91. float uv_x;
  92. float uv_y;
  93. float color_r;
  94. float color_g;
  95. float color_b;
  96. float color_a;
  97. babylon_vector3 getTransformedPosition(const FbxAMatrix& transform) const{
  98. auto result = transform.MultT(FbxVector4(pos_x, pos_y, pos_z));
  99. return babylon_vector3((float) result[0], (float) result[1], (float) result[2]);
  100. }
  101. };
  102. class babylon_boundingbox
  103. {
  104. private:
  105. float _minX;
  106. float _minY;
  107. float _minZ;
  108. float _maxX;
  109. float _maxY;
  110. float _maxZ;
  111. public:
  112. void addPosition(float x, float y, float z);
  113. void addPosition(const babylon_vector3& v){
  114. addPosition(v.x, v.y, v.z);
  115. }
  116. float getMinX() const{
  117. return _minX;
  118. }
  119. float getMinY() const{
  120. return _minY;
  121. }
  122. float getMinZ() const{
  123. return _minZ;
  124. }
  125. float getMaxX() const{
  126. return _maxX;
  127. }
  128. float getMaxY() const{
  129. return _maxY;
  130. }
  131. float getMaxZ() const{
  132. return _maxZ;
  133. }
  134. float getWidth() const{
  135. return _maxX - _minX;
  136. }
  137. float getHeight() const{
  138. return _maxY - _minY;
  139. }
  140. float getDepth() const{
  141. return _maxZ - _minZ;
  142. }
  143. babylon_vector3 getMin() const{
  144. return babylon_vector3(getMinX(), getMinY(), getMinZ());
  145. }
  146. babylon_vector3 getMax() const{
  147. return babylon_vector3(getMaxX(), getMaxY(), getMaxZ());
  148. }
  149. babylon_vector3 getCenter() const{
  150. return babylon_vector3(
  151. 0.5f*(_maxX + _minX),
  152. 0.5f*(_maxY + _minY),
  153. 0.5f*(_maxZ + _minZ));
  154. }
  155. babylon_boundingbox();
  156. babylon_boundingbox(FbxScene* scene);
  157. ~babylon_boundingbox();
  158. };
  159. babylon_boundingbox mergeBoundingBoxes(const std::vector<babylon_boundingbox>& boxes);
  160. struct babylon_submesh{
  161. std::string material_id;
  162. int index_start;
  163. int index_count;
  164. };
  165. template<typename TVertex>
  166. class babylon_mesh{
  167. private:
  168. std::string _name;
  169. std::vector<TVertex> _vertices;
  170. std::vector<int> _indices;
  171. std::vector<babylon_submesh> _submeshes;
  172. FbxAMatrix _transform;
  173. babylon_boundingbox _boundingBox;
  174. public:
  175. babylon_mesh(const char* name, std::vector<TVertex>&& vertices, std::vector<int>&& indices, std::vector<babylon_submesh>&& submeshes, FbxAMatrix transform) :
  176. _name(name),
  177. _vertices(std::move(vertices)),
  178. _indices(std::move(indices)),
  179. _submeshes(std::move(submeshes)),
  180. _transform(transform)
  181. {
  182. for (const auto& v : _vertices){
  183. babylon_vector3 pos = v.getTransformedPosition(_transform);
  184. _boundingBox.addPosition(pos.x, pos.y, pos.z);
  185. }
  186. }
  187. babylon_mesh() :_materialIndex(-1){
  188. }
  189. babylon_mesh(babylon_mesh&& mesh) :
  190. _name(std::move(mesh._name)),
  191. _vertices(std::move(mesh._vertices)),
  192. _indices(std::move(mesh._indices)),
  193. _transform(mesh._transform),
  194. _submeshes(std::move(mesh._submeshes)),
  195. _boundingBox(mesh._boundingBox)
  196. {
  197. }
  198. babylon_boundingbox getBoundingBox()const{
  199. return _boundingBox;
  200. }
  201. const FbxAMatrix& getTransform() const{ return _transform; }
  202. const std::string& getName() const{ return _name; }
  203. const std::vector<TVertex>& getVertices() const{ return _vertices; }
  204. const std::vector<int>& getIndices() const{ return _indices; }
  205. const std::vector<babylon_submesh>& getSubmeshes() const { return _submeshes; }
  206. int getUniqueIndexCount(const babylon_submesh& subMesh) const{
  207. std::set<int> knownIndices;
  208. for (int i = 0; i < subMesh.index_count; ++i){
  209. knownIndices.insert(_indices[i + subMesh.index_start]);
  210. }
  211. return knownIndices.size();
  212. }
  213. };
  214. struct babylon_color{
  215. float r;
  216. float g;
  217. float b;
  218. float a;
  219. babylon_color() :r(0), g(0), b(0), a(0){}
  220. babylon_color(const FbxColor& v) : r(static_cast<float>(v.mRed)), g(static_cast<float>(v.mGreen)), b(static_cast<float>(v.mBlue)), a(static_cast<float>(v.mAlpha)){}
  221. };
  222. inline bool operator <(const babylon_color& lhs, const babylon_color& rhs){
  223. if (lhs.r < rhs.r){
  224. return true;
  225. }
  226. else if (lhs.r > rhs.r){
  227. return false;
  228. }
  229. if (lhs.g < rhs.g){
  230. return true;
  231. }
  232. else if (lhs.g > rhs.g){
  233. return false;
  234. }
  235. if (lhs.b < rhs.b){
  236. return true;
  237. }
  238. else if (lhs.b > rhs.b){
  239. return false;
  240. }
  241. return lhs.a < rhs.a;
  242. }
  243. struct babylon_texture{
  244. std::string name;
  245. bool hasAlpha;
  246. };
  247. struct babylon_material{
  248. std::string id;
  249. std::string name;
  250. bool backFaceCulling;
  251. babylon_vector3 ambient;
  252. babylon_vector3 diffuse;
  253. babylon_vector3 specular;
  254. babylon_vector3 emissive;
  255. float specularPower;
  256. float alpha;
  257. std::shared_ptr<babylon_texture> diffuseTexture;
  258. std::shared_ptr<babylon_texture> ambientTexture;
  259. std::shared_ptr<babylon_texture> opacityTexture;
  260. std::shared_ptr<babylon_texture> reflectionTexture;
  261. std::shared_ptr<babylon_texture> emissiveTexture;
  262. std::shared_ptr<babylon_texture> specularTexture;
  263. std::shared_ptr<babylon_texture> bumpTexture;
  264. };
  265. struct babylon_global_settings{
  266. bool autoClear;
  267. babylon_vector3 clearColor;
  268. babylon_vector3 ambientColor;
  269. babylon_vector3 gravity;
  270. int fogMode;
  271. babylon_vector3 fogColor;
  272. float fogStart;
  273. float fogEnd;
  274. float fogDensity;
  275. babylon_global_settings(){
  276. autoClear = true;
  277. clearColor = babylon_vector3(0.2f, 0.2f, 0.3f);
  278. ambientColor = babylon_vector3(0.0f, 0.0f, 0.0f);
  279. gravity = babylon_vector3(0.0f, 0.0f, -0.9f);
  280. fogMode = 0;
  281. fogColor = babylon_vector3(0.0f, 0.0f, 0.0f);
  282. fogStart = 0;
  283. fogEnd = 0;
  284. fogDensity = 0;
  285. }
  286. };
  287. struct babylon_camera{
  288. std::string name;
  289. std::string id;
  290. babylon_vector3 position;
  291. babylon_vector3 target;
  292. babylon_vector3 rotation;
  293. float fov;
  294. float minZ;
  295. float maxZ;
  296. float speed;
  297. float inertia;
  298. bool checkCollisions;
  299. bool applyGravity;
  300. babylon_vector3 ellipsoid;
  301. };
  302. inline void writeVector3(web::json::value& obj, const wchar_t* name, const babylon_vector3& v) {
  303. obj[name] = web::json::value::array();
  304. obj[name][0] = v.x;
  305. obj[name][1] = v.y;
  306. obj[name][2] = v.z;
  307. }inline void writeVector4(web::json::value& obj, const wchar_t* name, const babylon_vector4& v) {
  308. obj[name] = web::json::value::array();
  309. obj[name][0] = v.x;
  310. obj[name][1] = v.y;
  311. obj[name][2] = v.z;
  312. obj[name][3] = v.w;
  313. }
  314. inline void writeVector2(web::json::value& obj, const wchar_t* name, const babylon_vector2& v) {
  315. obj[name] = web::json::value::array();
  316. obj[name][0] = v.x;
  317. obj[name][1] = v.y;
  318. }
  319. inline void writeFbxQuaterntion(web::json::value& obj, const wchar_t* name, const FbxQuaternion& v) {
  320. obj[name] = web::json::value::array();
  321. obj[name][0] = (float) v[0];
  322. obj[name][1] = (float) v[1];
  323. obj[name][2] = (float) v[2];
  324. obj[name][3] = (float) v[3];
  325. }
  326. inline void writeVector3IntoStream(web::json::value& array, const babylon_vector3& v) {
  327. auto size = array.size();
  328. array[size] = v.x;
  329. array[size + 1] = v.y;
  330. array[size + 2] = v.z;
  331. }
  332. inline void writeVector2IntoStream(web::json::value& array, float x, float y) {
  333. auto size = array.size();
  334. array[size] = x;
  335. array[size + 1] = y;
  336. }
  337. inline void writeMatrix(web::json::value& obj, const wchar_t* name, const FbxMatrix& v) {
  338. obj[name] = web::json::value::array();
  339. obj[name][0] = v.mData[0][0];
  340. obj[name][1] = v.mData[1][0];
  341. obj[name][2] = v.mData[2][0];
  342. obj[name][3] = v.mData[3][0];
  343. obj[name][4] = v.mData[0][1];
  344. obj[name][5] = v.mData[1][1];
  345. obj[name][6] = v.mData[2][1];
  346. obj[name][7] = v.mData[3][1];
  347. obj[name][8] = v.mData[0][2];
  348. obj[name][9] = v.mData[1][2];
  349. obj[name][10] = v.mData[2][2];
  350. obj[name][11] = v.mData[3][2];
  351. obj[name][12] = v.mData[0][3];
  352. obj[name][13] = v.mData[1][3];
  353. obj[name][14] = v.mData[2][3];
  354. obj[name][15] = v.mData[3][3];
  355. }