surface_tool.h
1 /*************************************************************************/
2 /* surface_tool.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef SURFACE_TOOL_H
30 #define SURFACE_TOOL_H
31 
32 #include "scene/resources/mesh.h"
33 #include "mikktspace.h"
34 
35 
36 class SurfaceTool : public Reference {
37 
38  OBJ_TYPE(SurfaceTool, Reference );
39 public:
40  struct Vertex {
41 
42  Vector3 vertex;
43  Color color;
44  Vector3 normal; // normal, binormal, tangent
45  Vector3 binormal;
46  Vector3 tangent;
47  Vector2 uv;
48  Vector2 uv2;
49  Vector<int> bones;
50  Vector<float> weights;
51 
52  bool operator==(const Vertex& p_vertex) const;
53 
54  Vertex() { }
55  };
56 
57 
58 private:
59 
60  struct VertexHasher {
61  static _FORCE_INLINE_ uint32_t hash(const Vertex &p_vtx);
62  };
63 
64  bool begun;
65  bool first;
66  Mesh::PrimitiveType primitive;
67  int format;
68  Ref<Material> material;
69  //arrays
70  List< Vertex > vertex_array;
71  List< int > index_array;
72  Map<int,bool> smooth_groups;
73 
74  //memory
75  Color last_color;
76  Vector3 last_normal;
77  Vector2 last_uv;
78  Vector2 last_uv2;
79  Vector<int> last_bones;
80  Vector<float> last_weights;
81  Plane last_tangent;
82 
83  void _create_list(const Ref<Mesh>& p_existing, int p_surface, List<Vertex> *r_vertex, List<int> *r_index,int &lformat);
84 
85 
86  //mikktspace callbacks
87  static int mikktGetNumFaces(const SMikkTSpaceContext * pContext);
88  static int mikktGetNumVerticesOfFace(const SMikkTSpaceContext * pContext, const int iFace);
89  static void mikktGetPosition(const SMikkTSpaceContext * pContext, float fvPosOut[], const int iFace, const int iVert);
90  static void mikktGetNormal(const SMikkTSpaceContext * pContext, float fvNormOut[], const int iFace, const int iVert);
91  static void mikktGetTexCoord(const SMikkTSpaceContext * pContext, float fvTexcOut[], const int iFace, const int iVert);
92  static void mikktSetTSpaceBasic(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert);
93 protected:
94 
95  static void _bind_methods();
96 
97 public:
98 
99  void begin(Mesh::PrimitiveType p_primitive);
100 
101  void add_vertex( const Vector3& p_vertex);
102  void add_color( Color p_color );
103  void add_normal( const Vector3& p_normal);
104  void add_tangent( const Plane& p_tangent );
105  void add_uv( const Vector2& p_uv);
106  void add_uv2( const Vector2& p_uv);
107  void add_bones( const Vector<int>& p_indices);
108  void add_weights( const Vector<float>& p_weights);
109  void add_smooth_group(bool p_smooth);
110 
111  void add_index( int p_index);
112 
113  void index();
114  void deindex();
115  void generate_normals();
116  void generate_tangents();
117 
118  void add_to_format(int p_flags) { format|=p_flags; }
119 
120  void set_material(const Ref<Material>& p_material);
121 
122  void clear();
123 
124  List< Vertex > &get_vertex_array() { return vertex_array; }
125 
126  void create_from(const Ref<Mesh>& p_existing, int p_surface);
127  void append_from(const Ref<Mesh>& p_existing, int p_surface,const Transform& p_xform);
128  Ref<Mesh> commit(const Ref<Mesh>& p_existing=Ref<Mesh>());
129 
130  SurfaceTool();
131 };
132 
133 
134 #endif
Definition: surface_tool.h:36
Definition: color.h:37
Definition: reference.h:40
Definition: vector3.h:38
Definition: transform.h:38
Definition: list.h:44
Definition: mikktspace.h:106
Definition: math_2d.h:65
static void _bind_methods()
Definition: surface_tool.cpp:817
Definition: plane.h:35
Definition: surface_tool.h:40