Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

404 рядки
16 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2009-2013 Cédric Lecacheur <jordx@free.fr>
  6. // (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the Do What The Fuck You Want To
  9. // Public License, Version 2, as published by Sam Hocevar. See
  10. // http://www.wtfpl.net/ for more details.
  11. //
  12. //
  13. // The EasyMesh class
  14. // ------------------
  15. //
  16. #include "commandstack.h"
  17. #include "easymeshrender.h"
  18. #include "easymeshbuild.h"
  19. #if !defined __EASYMESH_EASYMESH_H__
  20. #define __EASYMESH_EASYMESH_H__
  21. namespace lol
  22. {
  23. /* A safe enum for MeshCSG operations. */
  24. LOL_SAFE_ENUM(CSGUsage,
  25. Union,
  26. Substract,
  27. SubstractLoss, // will remove B from A, but not add inverted B
  28. And,
  29. Xor
  30. );
  31. LOL_SAFE_ENUM(Axis,
  32. X,
  33. Y,
  34. Z
  35. );
  36. LOL_SAFE_ENUM(MeshTransform,
  37. Taper,
  38. Twist,
  39. Bend,
  40. Stretch,
  41. Shear
  42. );
  43. class EasyMesh : public Mesh
  44. {
  45. friend class EasyMeshParser;
  46. friend class GpuEasyMeshData;
  47. public:
  48. EasyMesh();
  49. EasyMesh(const EasyMesh& em);
  50. bool Compile(char const *command, bool Execute = true);
  51. void ExecuteCmdStack(bool ExecAllStack = true);
  52. void MeshConvert();
  53. MeshRender GetMeshState() { return m_state; }
  54. bool SetRender(bool should_render);
  55. private:
  56. void UpdateVertexDict(array< int, int > &vertex_dict);
  57. //-------------------------------------------------------------------------
  58. //Mesh CSG operations
  59. //-------------------------------------------------------------------------
  60. private:
  61. void MeshCsg(CSGUsage csg_operation);
  62. public:
  63. /* [cmd:csgu] Performs a Union operation as (mesh0_Outside + mesh1_Outside) */
  64. void CsgUnion() { MeshCsg(CSGUsage::Union); }
  65. /* [cmd:csgs] Performs a Substract operation as (mesh0_Outside + mesh1_Inside-inverted) */
  66. void CsgSub() { MeshCsg(CSGUsage::Substract); }
  67. /* [cmd:csgsl] Performs a Substract operation without keeping the mesh1 part */
  68. void CsgSubL() { MeshCsg(CSGUsage::SubstractLoss); }
  69. /* [cmd:csga] Performs an And operation as (mesh0_Inside + mesh1_Inside) */
  70. void CsgAnd() { MeshCsg(CSGUsage::And); }
  71. /* [cmd:csgx] Performs a Xor operation as (m0_Outside/m0_Inside-inverted + m1_Outside/m1_Inside-inverted) */
  72. void CsgXor() { MeshCsg(CSGUsage::Xor); }
  73. //-------------------------------------------------------------------------
  74. //Mesh Base operations
  75. //-------------------------------------------------------------------------
  76. public:
  77. /* [cmd:lp[ ]] will perform a loop of loopnb */
  78. void LoopStart(int loopnb);
  79. /* No cmd, implicit ] */
  80. void LoopEnd();
  81. /* [cmd:[] from this point onward, any operation will not be performed on previous vertices */
  82. void OpenBrace();
  83. /* [cmd:]] Merge current vertices with previous context */
  84. void CloseBrace();
  85. /* [cmd:tsw] When active, on negative-scaling, normal-vector correction will not occur */
  86. void ToggleScaleWinding();
  87. /* [cmd:tqw] When active, quad will have a fifth center vertex */
  88. void ToggleQuadWeighting();
  89. /* [cmd:tpbn] When active, normal will be computed after the build */
  90. void TogglePostBuildNormal();
  91. /* [cmd:tpbn] When active, normal will be computed after the build */
  92. void ToggleVerticeNoCleanup();
  93. /* [cmd:sc] Set both color */
  94. void SetCurColor(vec4 const &color);
  95. /* [cmd:sca] Set base color A */
  96. void SetCurColorA(vec4 const &color);
  97. /* [cmd:scb] Set base color B */
  98. void SetCurColorB(vec4 const &color);
  99. /* [cmd:scv] Sets all vertices in this scope color. */
  100. void SetVertColor(vec4 const &color);
  101. //-------------------------------------------------------------------------
  102. //Internal : Basic triangle/vertex operations
  103. //-------------------------------------------------------------------------
  104. private:
  105. void AddVertex(vec3 const &coord);
  106. void AddDuplicateVertex(int i);
  107. void AddLerpVertex(int i, int j, float alpha);
  108. void AddLerpVertex(VertexData const &vi, VertexData const &vj, float alpha);
  109. VertexData GetLerpVertex(int i, int j, float alpha);
  110. VertexData GetLerpVertex(VertexData const &vi, VertexData const &vj, float alpha);
  111. void AppendQuad(int i1, int i2, int i3, int i4, int base);
  112. void AppendQuadDuplicateVerts(int i1, int i2, int i3, int i4, int base);
  113. void AppendTriangle(int i1, int i2, int i3, int base);
  114. void AppendTriangleDuplicateVerts(int i1, int i2, int i3, int base);
  115. void ComputeNormals(int start, int vcount);
  116. public:
  117. /* Remove all unused */
  118. void VerticesCleanup();
  119. /* Merge vertices AKA: smooth */
  120. void VerticesMerge();
  121. /* Merge vertices AKA: Flat */
  122. void VerticesSeparate();
  123. public: //DEBUG
  124. void ComputeTexCoord(float uv_scale, int uv_offset);
  125. //-------------------------------------------------------------------------
  126. //Internal : Vertices operations
  127. //-------------------------------------------------------------------------
  128. private:
  129. void SetTexCoordData(vec2 const &new_offset, vec2 const &new_scale);
  130. void SetTexCoordData2(vec2 const &new_offset, vec2 const &new_scale);
  131. void SetCurVertNormal(vec3 const &normal);
  132. void SetCurVertColor(vec4 const &color);
  133. void SetCurVertTexCoord(vec2 const &texcoord);
  134. void SetCurVertTexCoord2(vec2 const &texcoord);
  135. public:
  136. //-------------------------------------------------------------------------
  137. //Mesh transform operations
  138. //-------------------------------------------------------------------------
  139. /* [cmd:t/tx/ty/tz] Translate vertices
  140. - v : Translation quantity.
  141. */
  142. void Translate(vec3 const &v);
  143. /* See Rotate */
  144. void RotateX(float angle);
  145. /* See Rotate */
  146. void RotateY(float angle);
  147. /* See Rotate */
  148. void RotateZ(float angle);
  149. /* [cmd:r/rx/ry/rz] Rotate vertices
  150. - angle : rotation quantity.
  151. - axis : rotation axis.
  152. */
  153. void Rotate(float angle, vec3 const &axis);
  154. /* [cmd:rj] Randomly move vertices along Origin-to-vertex as f(vtx) = vtx + o2v * (1.0 + rand(r))
  155. - r : jitter maximum value.
  156. */
  157. void RadialJitter(float r);
  158. /* [cmd:tax] multiply axis y&z by x as f(y) = y * (1.0 + (ny * x + xoff))
  159. - ny : value of n for y.
  160. - nz : value of n for z.
  161. - xoff : value of xoff.
  162. - absolute (def:true) : if (true) Multiply will use an absolute x.
  163. */
  164. void TaperX(float ny, float nz, float xoff=0.f, bool absolute=true);
  165. /* [cmd:tay] Same as TaperX, with Y */
  166. void TaperY(float nx, float nz, float yoff=0.f, bool absolute=true);
  167. /* [cmd:taz] Same as TaperX, with Z */
  168. void TaperZ(float nx, float ny, float zoff=0.f, bool absolute=true);
  169. /* [cmd:twx] Twist vertices around x axis with x as rotation value as f(p) = (RotateX(x * t + toff) * p)
  170. - t : Angle multiplier.
  171. - toff : Applied offset.
  172. */
  173. void TwistX(float t, float toff=0.f);
  174. /* [cmd:twy] Same as TwistX, with Y */
  175. void TwistY(float t, float toff=0.f);
  176. /* [cmd:twz] Same as TwistX, with Z */
  177. void TwistZ(float t, float toff=0.f);
  178. /* [cmd:shx] Shear vertices using x value as shear quantity as f(y) = y + (ny * x + xoff)
  179. - ny : Value of n for y.
  180. - nz : Value of n for z.
  181. - xoff : Value of xoff.
  182. - absolute (def:true) : if (true) Multiply will use an absolute x.
  183. */
  184. void ShearX(float ny, float nz, float xoff=0.f, bool absolute=true);
  185. /* [cmd:shy] Same as ShearX, with Y */
  186. void ShearY(float nx, float nz, float yoff=0.f, bool absolute=true);
  187. /* [cmd:shz] Same as ShearX, with Z */
  188. void ShearZ(float nx, float ny, float zoff=0.f, bool absolute=true);
  189. /* [cmd:stx] Stretch vertices using x value as stretch quantity as f(y) = y + (pow(x, ny) + xoff)
  190. - ny : Value of n for y.
  191. - nz : Value of n for z.
  192. - xoff : Value of xoff.
  193. */
  194. void StretchX(float ny, float nz, float xoff=0.f);
  195. /* [cmd:sty] Same as StretchX, with Y */
  196. void StretchY(float nx, float nz, float yoff=0.f);
  197. /* [cmd:stz] Same as StretchX, with Z */
  198. void StretchZ(float nx, float ny, float zoff=0.f);
  199. /* [cmd:bdxy] Bend vertices using x as bend quantity along y axis using f(p) = (RotateY(x * t + toff) * p)
  200. - t : Angle multiplier.
  201. - xoff : Applied offset.
  202. */
  203. void BendXY(float t, float toff=0.f);
  204. /* [cmd:bdxz] Same as BendXY, with X & Z */
  205. void BendXZ(float t, float toff=0.f);
  206. /* [cmd:bdyx] Same as BendXY, with Y & X */
  207. void BendYX(float t, float toff=0.f);
  208. /* [cmd:bdyz] Same as BendXY, with Y & Z */
  209. void BendYZ(float t, float toff=0.f);
  210. /* [cmd:bdzx] Same as BendXY, with Z & X */
  211. void BendZX(float t, float toff=0.f);
  212. /* [cmd:bdzy] Same as BendXY, with Z & Y */
  213. void BendZY(float t, float toff=0.f);
  214. private:
  215. void DoMeshTransform(MeshTransform ct, Axis axis0, Axis axis1, float n0, float n1, float noff, bool absolute=false);
  216. public:
  217. /* [cmd:s/sx/sy/sz] Scale vertices
  218. - s : scale quantity.
  219. */
  220. void Scale(vec3 const &s);
  221. void Scale(float s) { Scale(vec3(s)); }
  222. /* [cmd:mx] Mirror vertices through X-plane
  223. Acts as an OpenBrace
  224. */
  225. void MirrorX();
  226. /* [cmd:my] Mirror vertices through Y-plane
  227. Acts as an OpenBrace
  228. */
  229. void MirrorY();
  230. /* [cmd:mz] Mirror vertices through Z-plane
  231. Acts as an OpenBrace
  232. */
  233. void MirrorZ();
  234. /* [no-cmd] Duplicates vertices and scale duplicate
  235. Acts as an OpenBrace
  236. */
  237. void DupAndScale(vec3 const &s, bool open_brace=false);
  238. /* [cmd:ch] Performs a chamfer operation //TODO : Make it work
  239. - f : Chamfer quantity.
  240. */
  241. void Chamfer(float f);
  242. /* [cmd:splt] split triangles in 4 smaller ones
  243. - pass : Number of pass applied.
  244. */
  245. void SplitTriangles(int pass);
  246. private:
  247. void SplitTriangles(int pass, VertexDictionnary *vert_dict);
  248. public:
  249. /* [cmd:smth] Smooth the mesh by subdivising it.
  250. - pass : a pass is made of (n0 split then n1 smooth) repeat.
  251. - split_per_pass : n0 value in above explanation.
  252. - smooth_per_pass : n1 value in above explanation.
  253. */
  254. void SmoothMesh(int pass, int split_per_pass, int smooth_per_pass);
  255. //-------------------------------------------------------------------------
  256. //Mesh shape operations
  257. //-------------------------------------------------------------------------
  258. /* [cmd:ac] Cylinder centered on (0,0,0) with BBox [-.5*max(d1, d2), -.5*h, -.5*max(d1, d2)]
  259. - nbsides : Number of sides. [+.5*max(d1, d2), +.5*h, +.5*max(d1, d2)]
  260. - h : Height of the cylinder.
  261. - d1 : Lower diameter.
  262. - d2 : Upper diameter.
  263. - dualside : if (true) will also create inner sides : TOOD:TOREMOVE?? : needed ?
  264. - smooth : if (true) will smooth normals : TOOD:TOREMOVE : smooth should be handled elsewhere
  265. - close : if (true) will add discs to close the cylinder
  266. */
  267. void AppendCylinder(int nsides, float h, float d1, float d2,
  268. bool dualside=false, bool smooth=false, bool close=false);
  269. /* [cmd:asph] Sphere centered on (0,0,0) with BBox [-.5*d][.5*d]
  270. - ndivisions : number of subdivisions each Sphere triangle will sustain.
  271. - d : Diameter.
  272. */
  273. void AppendSphere(int ndivisions, float d);
  274. /* [cmd:acap] Capsule centered on (0,0,0) with BBox [-.5*d, -(.5*d+h), -.5*d][.5*d, (.5*d+h), .5*d]
  275. - ndivisions : number of subdivisions each Sphere triangle will sustain.
  276. - h : Inner height.
  277. - d : Diameter.
  278. */
  279. void AppendCapsule(int ndivisions, float h, float d);
  280. /* [cmd:ato] Torus centered on (0,0,0) with BBox [-.5*d2][.5*d2]
  281. - ndivisions : number of subdivisions of the torus.
  282. - d1 : Inner diameter.
  283. - d2 : Outer diameter.
  284. */
  285. void AppendTorus(int ndivisions, float d1, float d2);
  286. /* [cmd:ab] Box centered on (0,0,0) with BBox [-.5 * size][.5 * size]
  287. - size : size of the box.
  288. - chamf : size of the chamfer.
  289. */
  290. void AppendBox(vec3 const &size, float chamf=0.f);
  291. //Same as AppendBox
  292. void AppendSmoothChamfBox(vec3 const &size, float chamf);
  293. //Same as AppendBox
  294. void AppendFlatChamfBox(vec3 const &size, float chamf);
  295. //Same as AppendBox
  296. void AppendBox(vec3 const &size, float chamf, bool smooth);
  297. /* [cmd:as]
  298. Append a Star centered on (0,0,0) contained within a disc of "max(d1, d2)" diameter.
  299. - nbranches : Number of branches.
  300. - d1 : double Length of the branches.
  301. - d2 : double Length of the "branch" located between d1-branches.
  302. - fade : if (true) in-between branches use ColorB.
  303. - fade2 : if (true) Star branches use ColorB.
  304. */
  305. void AppendStar(int nbranches, float d1, float d2,
  306. bool fade=false, bool fade2=false);
  307. /* [cmd:aes] Star centered on (0,0,0) contained within a disc of "max(max(d1, d2), max(d1 + extrad, d2 + extrad))" diameter.
  308. Expanded star branches use ColorB.
  309. - nbranches : Number of branches.
  310. - d1 : Double Length of the branches.
  311. - d2 : Double Length of the "branch" located between r1-branches.
  312. - extrad : Extra length added to expand all branches.
  313. */
  314. void AppendExpandedStar(int nbranches, float d1, float d2, float extrad=0.f);
  315. /* [cmd:ad] Disc centered on (0,0,0) with d diameter.
  316. - nbsides : Number of sides.
  317. - d : Diameter.
  318. - fade : if (true) Outer vertices will use ColorB
  319. */
  320. void AppendDisc(int nsides, float d, bool fade=false);
  321. /* [cmd:at] Triangle centered on (0,0,0) contained within a disc of "d" diameter.
  322. - d : diameter of the containing disc..
  323. - fade : if (true) 2nd & 3rd Vertices will use ColorB
  324. */
  325. void AppendSimpleTriangle(float d, bool fade=false);
  326. /* [cmd:aq] Quad centered on (0,0,0) contained within BBox [-size*.5f, 0, -size*.5f][size*.5f, 0, size*.5f]
  327. - size : Size of quad.
  328. - fade : if (true) 3rd & 4th Vertices will use ColorB
  329. */
  330. void AppendSimpleQuad(float size, bool fade=false);
  331. private:
  332. //complex version of above one
  333. void AppendSimpleQuad(vec2 p1, vec2 p2, float z=0.f, bool fade=false);
  334. public:
  335. /* [cmd:acg] Gear centered on (0,0,0) contained within BBox [-.5*max(d1,d2), -.5*h, -.5*max(d1, d2)]
  336. - h : Height of the Gear. [+.5*max(d1,d2), +.5*h, +.5*max(d1, d2)]
  337. - d10 : Upper Inner diameter.
  338. - d20 : Lower Inner diameter.
  339. - d1 : Upper Outer diameter.
  340. - d2 : Lower Outer diameter.
  341. - d12 : Upper Cog diameter.
  342. - d22 : Lower Cog diameter.
  343. - sidemul : multiplier for the size of the cogs.
  344. - offset : useless
  345. */
  346. void AppendCog(int nbsides, float h, float d10, float d20, float d11,
  347. float d21, float d12, float d22, float sidemul=0.f, bool offset=false);
  348. //-------------------------------------------------------------------------
  349. //TODO : Mesh Bone operations
  350. //-------------------------------------------------------------------------
  351. //void AddBone(int parent_id) {}
  352. //Convenience functions
  353. public:
  354. int GetVertexCount() { return m_vert.Count(); }
  355. vec3 const &GetVertexLocation(int i) { return m_vert[i].m_coord; }
  356. //private:
  357. array<uint16_t> m_indices;
  358. array<VertexData> m_vert;
  359. //<vert count, indices count>
  360. array<int, int> m_cursors;
  361. MeshRender m_state;
  362. public:
  363. inline EasyMeshBuildData* BD()
  364. {
  365. if (!m_build_data)
  366. m_build_data = new EasyMeshBuildData();
  367. return m_build_data;
  368. };
  369. private:
  370. class EasyMeshBuildData* m_build_data;
  371. };
  372. } /* namespace lol */
  373. #endif /* __EASYMESH_EASYMESH_H__ */