No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

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