You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

174 lines
5.7 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. #if !defined __EASYMESH_EASYMESH_H__
  17. #define __EASYMESH_EASYMESH_H__
  18. namespace lol
  19. {
  20. /* A safe enum for MeshCSG operations. */
  21. struct CSGUsage
  22. {
  23. enum Value
  24. {
  25. Union,
  26. Substract,
  27. SubstractLoss, //will remove B from A, but not add inverted B
  28. And,
  29. Xor,
  30. }
  31. m_value;
  32. inline CSGUsage(Value v) : m_value(v) {}
  33. inline operator Value() { return m_value; }
  34. };
  35. class EasyMesh
  36. {
  37. friend class EasyMeshParser;
  38. public:
  39. EasyMesh();
  40. bool Compile(char const *command);
  41. void MeshConvert(Shader* ProvidedShader = NULL);
  42. void Render(mat4 const &model, float damage = 0.f);
  43. private:
  44. void UpdateVertexDict(Array< int, int > &vertex_dict);
  45. //-------------------------------------------------------------------------
  46. //Mesh CSG operations
  47. //-------------------------------------------------------------------------
  48. private:
  49. void MeshCsg(CSGUsage csg_operation);
  50. public:
  51. void CsgUnion() { MeshCsg(CSGUsage::Union); }
  52. void CsgSubstract() { MeshCsg(CSGUsage::Substract); }
  53. void CsgSubstractLoss() { MeshCsg(CSGUsage::SubstractLoss); }
  54. void CsgAnd() { MeshCsg(CSGUsage::And); }
  55. void CsgXor() { MeshCsg(CSGUsage::Xor); }
  56. public:
  57. void OpenBrace();
  58. void CloseBrace();
  59. void ToggleScaleWinding();
  60. void SetCurColor(vec4 const &color);
  61. void SetCurColor2(vec4 const &color);
  62. private:
  63. void AddVertex(vec3 const &coord);
  64. void AddDuplicateVertex(int i);
  65. void AppendQuad(int i1, int i2, int i3, int i4, int base);
  66. void AppendQuadDuplicateVerts(int i1, int i2, int i3, int i4, int base);
  67. void AppendTriangle(int i1, int i2, int i3, int base);
  68. void AppendTriangleDuplicateVerts(int i1, int i2, int i3, int base);
  69. void ComputeNormals(int start, int vcount);
  70. void SetVertColor(vec4 const &color);
  71. void SetCurVertNormal(vec3 const &normal);
  72. void SetCurVertColor(vec4 const &color);
  73. public:
  74. //-------------------------------------------------------------------------
  75. //Mesh transform operations
  76. //-------------------------------------------------------------------------
  77. void Translate(vec3 const &v);
  78. void RotateX(float t);
  79. void RotateY(float t);
  80. void RotateZ(float t);
  81. void Rotate(float t, vec3 const &axis);
  82. void RadialJitter(float r);
  83. void TaperX(float y, float z, float xoff);
  84. void TaperY(float x, float z, float yoff);
  85. void TaperZ(float x, float y, float zoff);
  86. void Scale(vec3 const &s);
  87. void MirrorX();
  88. void MirrorY();
  89. void MirrorZ();
  90. void DupAndScale(vec3 const &s);
  91. void Chamfer(float f);
  92. //-------------------------------------------------------------------------
  93. //Mesh shape operations
  94. //-------------------------------------------------------------------------
  95. void AppendCylinder(int nsides, float h, float r1, float r2,
  96. int dualside, int smooth);
  97. void AppendCapsule(int ndivisions, float h, float r);
  98. void AppendSphere(int ndivisions, vec3 const &size);
  99. void AppendTorus(int ndivisions, float r1, float r2);
  100. void AppendBox(vec3 const &size, float chamf = 0.f);
  101. void AppendSmoothChamfBox(vec3 const &size, float chamf);
  102. void AppendFlatChamfBox(vec3 const &size, float chamf);
  103. void AppendBox(vec3 const &size, float chamf, bool smooth);
  104. void AppendStar(int nbranches, float r1, float r2,
  105. int fade = 0, int fade2 = 0);
  106. void AppendExpandedStar(int nbranches, float r1, float r2, float extrar);
  107. void AppendDisc(int nsides, float r, int fade = 0);
  108. void AppendSimpleTriangle(float size, int fade = 0);
  109. void AppendSimpleQuad(float size, int fade = 0);
  110. void AppendSimpleQuad(vec2 p1, vec2 p2, float z = 0.f, int fade = 0);
  111. void AppendCog(int nbsides, float h, float r10, float r20, float r1,
  112. float r2, float r12, float r22, float sidemul, int offset);
  113. //-------------------------------------------------------------------------
  114. //TODO : Mesh Bone operations
  115. //-------------------------------------------------------------------------
  116. //void AddBone(int parent_id) {}
  117. //Convenience functions
  118. public:
  119. int GetVertexCount() { return m_vert.Count(); }
  120. vec3 const &GetVertexLocation(int i) { return m_vert[i].m1; }
  121. private:
  122. vec4 m_color, m_color2;
  123. Array<uint16_t> m_indices;
  124. //TODO : <coord, norm, color, bone_id, bone_weight>
  125. //TODO : Array<vec3, vec3, vec4, ivec2, vec2> m_vert;
  126. //TODO : More bone blend support than 2 ?
  127. //<coord, norm, color>
  128. Array<vec3, vec3, vec4> m_vert;
  129. //<vert count, indices count>
  130. Array<int, int> m_cursors;
  131. //When this flag is up, negative scaling will not invert faces.
  132. bool m_ignore_winding_on_scale;
  133. /* FIXME: put this in a separate class so that we can copy meshes. */
  134. struct
  135. {
  136. /* FIXME: very naughty way of handling debug render modes */
  137. Array<Shader *>shader;
  138. Array<ShaderAttrib> coord, norm, color;
  139. Array<ShaderUniform> modelview, view, invview, proj, normalmat, damage, lights;
  140. VertexDeclaration *vdecl;
  141. VertexBuffer *vbo;
  142. IndexBuffer *ibo;
  143. int vertexcount, indexcount;
  144. }
  145. m_gpu;
  146. };
  147. } /* namespace lol */
  148. #endif /* __EASYMESH_EASYMESH_H__ */