source: trunk/src/easymesh/easymesh.h @ 2370

Last change on this file since 2370 was 2370, checked in by touky, 7 years ago

render : Added DebugRenderMode & corresponding shaders. naive (VERY) implementation in MeshViewer.

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