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

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

Added VertexDictionnary object to manage vertices with same coord && connected vertices.
Added UVs system with and UVs generation test (not very conclusive)

File size: 7.8 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#define VU_BONES   2
19#define VU_TEX_UV  1
20#define VU_VANILLA 0
21
22#define VERTEX_USEAGE VU_VANILLA
23
24#if !defined __EASYMESH_EASYMESH_H__
25#define __EASYMESH_EASYMESH_H__
26
27namespace lol
28{
29
30/* A safe enum for MeshCSG operations. */
31struct CSGUsage
32{
33    enum Value
34    {
35        Union,
36        Substract,
37        SubstractLoss, //will remove B from A, but not add inverted B
38        And,
39        Xor,
40    }
41    m_value;
42
43    inline CSGUsage(Value v) : m_value(v) {}
44    inline operator Value() { return m_value; }
45};
46
47/* A safe enum for VertexDictionnary operations. */
48struct VDictType
49{
50    enum Value
51    {
52        DoesNotExist=-3,
53        Alone=-2,
54        Master=-1
55    }
56    m_value;
57
58    inline VDictType(Value v) : m_value(v) {}
59    inline operator Value() { return m_value; }
60};
61
62//a class whose goal is to keep a list of the adjacent vertices for mesh operations purposes
63class VertexDictionnary
64{
65public:
66    int FindVertexMaster(const int search_idx);
67    bool FindMatchingVertices(const int search_idx, Array<int> &matching_ids);
68    bool FindConnectedVertices(const int search_idx, const Array<int> &tri_list, Array<int> &connected_vert, Array<int> const *ignored_tri = NULL);
69    bool FindConnectedTriangles(const int search_idx, const Array<int> &tri_list, Array<int> &connected_tri, Array<int> const *ignored_tri = NULL);
70    bool FindConnectedTriangles(const ivec2 &search_idx, const Array<int> &tri_list, Array<int> &connected_tri, Array<int> const *ignored_tri = NULL);
71    bool FindConnectedTriangles(const ivec3 &search_idx, const Array<int> &tri_list, Array<int> &connected_tri, Array<int> const *ignored_tri = NULL);
72    void AddVertex(int vert_id, vec3 vert_coord);
73    void Clear() { vertex_list.Empty(); }
74private:
75    //<VertexId, VertexLocation, VertexMasterId>
76    Array<int, vec3, int>   vertex_list;
77    //List of the master_ vertices
78    Array<int>              master_list;
79};
80
81class EasyMesh
82{
83    friend class EasyMeshParser;
84
85public:
86    EasyMesh();
87
88    bool Compile(char const *command);
89    void MeshConvert(Shader* ProvidedShader = NULL);
90    void Render(mat4 const &model, float damage = 0.f);
91
92private:
93    void UpdateVertexDict(Array< int, int > &vertex_dict);
94
95    //-------------------------------------------------------------------------
96    //Mesh CSG operations
97    //-------------------------------------------------------------------------
98private:
99    void MeshCsg(CSGUsage csg_operation);
100public:
101    void CsgUnion()         { MeshCsg(CSGUsage::Union); }
102    void CsgSubstract()     { MeshCsg(CSGUsage::Substract); }
103    void CsgSubstractLoss() { MeshCsg(CSGUsage::SubstractLoss); }
104    void CsgAnd()           { MeshCsg(CSGUsage::And); }
105    void CsgXor()           { MeshCsg(CSGUsage::Xor); }
106
107public:
108    void OpenBrace();
109    void CloseBrace();
110
111    void ToggleScaleWinding();
112    void SetCurColor(vec4 const &color);
113    void SetCurColor2(vec4 const &color);
114
115private:
116    void AddVertex(vec3 const &coord);
117    void AddDuplicateVertex(int i);
118    void AppendQuad(int i1, int i2, int i3, int i4, int base);
119    void AppendQuadDuplicateVerts(int i1, int i2, int i3, int i4, int base);
120    void AppendTriangle(int i1, int i2, int i3, int base);
121    void AppendTriangleDuplicateVerts(int i1, int i2, int i3, int base);
122    void ComputeNormals(int start, int vcount);
123public:
124    void ComputeTexCoord(float uv_scale, int uv_offset);
125    void SetVertColor(vec4 const &color);
126
127    void SetCurVertNormal(vec3 const &normal);
128    void SetCurVertColor(vec4 const &color);
129
130public:
131    //-------------------------------------------------------------------------
132    //Mesh transform operations
133    //-------------------------------------------------------------------------
134    void Translate(vec3 const &v);
135    void RotateX(float t);
136    void RotateY(float t);
137    void RotateZ(float t);
138    void Rotate(float t, vec3 const &axis);
139    void RadialJitter(float r);
140    void TaperX(float y, float z, float xoff);
141    void TaperY(float x, float z, float yoff);
142    void TaperZ(float x, float y, float zoff);
143    void Scale(vec3 const &s);
144    void MirrorX();
145    void MirrorY();
146    void MirrorZ();
147    void DupAndScale(vec3 const &s);
148    void Chamfer(float f);
149
150    //-------------------------------------------------------------------------
151    //Mesh shape operations
152    //-------------------------------------------------------------------------
153    void AppendCylinder(int nsides, float h, float r1, float r2,
154                        int dualside, int smooth);
155    void AppendCapsule(int ndivisions, float h, float r);
156    void AppendSphere(int ndivisions, vec3 const &size);
157    void AppendTorus(int ndivisions, float r1, float r2);
158    void AppendBox(vec3 const &size, float chamf = 0.f);
159    void AppendSmoothChamfBox(vec3 const &size, float chamf);
160    void AppendFlatChamfBox(vec3 const &size, float chamf);
161    void AppendBox(vec3 const &size, float chamf, bool smooth);
162    void AppendStar(int nbranches, float r1, float r2,
163                    int fade = 0, int fade2 = 0);
164    void AppendExpandedStar(int nbranches, float r1, float r2, float extrar);
165    void AppendDisc(int nsides, float r, int fade = 0);
166    void AppendSimpleTriangle(float size, int fade = 0);
167    void AppendSimpleQuad(float size, int fade = 0);
168    void AppendSimpleQuad(vec2 p1, vec2 p2, float z = 0.f, int fade = 0);
169    void AppendCog(int nbsides, float h, float r10, float r20, float r1,
170                   float r2, float r12, float r22, float sidemul, int offset);
171
172    //-------------------------------------------------------------------------
173    //TODO : Mesh Bone operations
174    //-------------------------------------------------------------------------
175    //void AddBone(int parent_id) {}
176
177    //Convenience functions
178public:
179    int GetVertexCount() { return m_vert.Count(); }
180    vec3 const &GetVertexLocation(int i) { return m_vert[i].m1; }
181
182private:
183    vec4 m_color, m_color2;
184    Array<uint16_t> m_indices;
185#if VERTEX_USEAGE == VU_BONES
186    //TODO : -- BONE SUPPORT --
187    //TODO : <COORD, NORM, COLOR, BONE_ID, BONE_WEIGHT>
188    Array<vec3, vec3, vec4, ivec2, vec2> m_vert;
189    //TODO : More bone blend support than 2 ?
190#elif VERTEX_USEAGE == VU_TEX_UV
191    //TODO : -- UV SUPPORT --
192    //TODO : <COORD, NORM, COLOR, UV>
193    Array<vec3, vec3, vec4, vec2> m_vert;
194#else
195    //-- VANILLA --
196    //<COORD, NORM, COLOR>
197    Array<vec3, vec3, vec4> m_vert;
198#endif
199
200    //<vert count, indices count>
201    Array<int, int> m_cursors;
202    //When this flag is up, negative scaling will not invert faces.
203    bool m_ignore_winding_on_scale;
204
205    /* FIXME: put this in a separate class so that we can copy meshes. */
206    struct
207    {
208        /* FIXME: very naughty way of handling debug render modes */
209        Array<Shader *>shader;
210#if VERTEX_USEAGE == VU_BONES
211        //TODO : -- BONE SUPPORT --
212        Array<ShaderAttrib> coord, norm, color, bone_id, bone_weight;
213#elif VERTEX_USEAGE == VU_TEX_UV
214        //-- UV SUPPORT --
215        Array<ShaderAttrib> coord, norm, color, tex_coord;
216#else
217        //-- VANILLA --
218        Array<ShaderAttrib> coord, norm, color;
219#endif
220        Array<ShaderUniform> modelview, invmodelview, view, invview, proj, normalmat, damage, lights;
221
222        VertexDeclaration *vdecl;
223        VertexBuffer *vbo;
224        IndexBuffer *ibo;
225        int vertexcount, indexcount;
226    }
227    m_gpu;
228};
229
230} /* namespace lol */
231
232#endif /* __EASYMESH_EASYMESH_H__ */
233
Note: See TracBrowser for help on using the repository browser.