1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __MESH_H__
18 #define __MESH_H__
19 
20 #include "graph/node.h"
21 
22 #include "bvh/bvh_params.h"
23 #include "render/attribute.h"
24 #include "render/geometry.h"
25 #include "render/shader.h"
26 
27 #include "util/util_array.h"
28 #include "util/util_boundbox.h"
29 #include "util/util_list.h"
30 #include "util/util_map.h"
31 #include "util/util_param.h"
32 #include "util/util_set.h"
33 #include "util/util_types.h"
34 #include "util/util_vector.h"
35 
36 CCL_NAMESPACE_BEGIN
37 
38 class Attribute;
39 class BVH;
40 class Device;
41 class DeviceScene;
42 class Mesh;
43 class Progress;
44 class RenderStats;
45 class Scene;
46 class SceneParams;
47 class AttributeRequest;
48 struct SubdParams;
49 class DiagSplit;
50 struct PackedPatchTable;
51 
52 /* Mesh */
53 
54 class Mesh : public Geometry {
55  protected:
56   Mesh(const NodeType *node_type_, Type geom_type_);
57 
58  public:
59   NODE_DECLARE
60 
61   /* Mesh Triangle */
62   struct Triangle {
63     int v[3];
64 
65     void bounds_grow(const float3 *verts, BoundBox &bounds) const;
66 
67     void motion_verts(const float3 *verts,
68                       const float3 *vert_steps,
69                       size_t num_verts,
70                       size_t num_steps,
71                       float time,
72                       float3 r_verts[3]) const;
73 
74     void verts_for_step(const float3 *verts,
75                         const float3 *vert_steps,
76                         size_t num_verts,
77                         size_t num_steps,
78                         size_t step,
79                         float3 r_verts[3]) const;
80 
81     float3 compute_normal(const float3 *verts) const;
82 
83     bool valid(const float3 *verts) const;
84   };
85 
get_triangle(size_t i)86   Triangle get_triangle(size_t i) const
87   {
88     Triangle tri = {{triangles[i * 3 + 0], triangles[i * 3 + 1], triangles[i * 3 + 2]}};
89     return tri;
90   }
91 
num_triangles()92   size_t num_triangles() const
93   {
94     return triangles.size() / 3;
95   }
96 
97   /* Mesh SubdFace */
98   struct SubdFace {
99     int start_corner;
100     int num_corners;
101     int shader;
102     bool smooth;
103     int ptex_offset;
104 
is_quadSubdFace105     bool is_quad()
106     {
107       return num_corners == 4;
108     }
109     float3 normal(const Mesh *mesh) const;
num_ptex_facesSubdFace110     int num_ptex_faces() const
111     {
112       return num_corners == 4 ? 1 : num_corners;
113     }
114   };
115 
116   struct SubdEdgeCrease {
117     int v[2];
118     float crease;
119   };
120 
121   enum SubdivisionType {
122     SUBDIVISION_NONE,
123     SUBDIVISION_LINEAR,
124     SUBDIVISION_CATMULL_CLARK,
125   };
126 
127   SubdivisionType subdivision_type;
128 
129   /* Mesh Data */
130   array<int> triangles;
131   array<float3> verts;
132   array<int> shader;
133   array<bool> smooth;
134 
135   /* used for storing patch info for subd triangles, only allocated if there are patches */
136   array<int> triangle_patch; /* must be < 0 for non subd triangles */
137   array<float2> vert_patch_uv;
138 
139   array<SubdFace> subd_faces;
140   array<int> subd_face_corners;
141   int num_ngons;
142 
143   array<SubdEdgeCrease> subd_creases;
144 
145   SubdParams *subd_params;
146 
147   AttributeSet subd_attributes;
148 
149   PackedPatchTable *patch_table;
150 
151   /* BVH */
152   size_t vert_offset;
153 
154   size_t patch_offset;
155   size_t patch_table_offset;
156   size_t face_offset;
157   size_t corner_offset;
158 
159   size_t num_subd_verts;
160 
161  private:
162   unordered_map<int, int> vert_to_stitching_key_map; /* real vert index -> stitching index */
163   unordered_multimap<int, int>
164       vert_stitching_map; /* stitching index -> multiple real vert indices */
165   friend class DiagSplit;
166   friend class GeometryManager;
167 
168  public:
169   /* Functions */
170   Mesh();
171   ~Mesh();
172 
173   void resize_mesh(int numverts, int numfaces);
174   void reserve_mesh(int numverts, int numfaces);
175   void resize_subd_faces(int numfaces, int num_ngons, int numcorners);
176   void reserve_subd_faces(int numfaces, int num_ngons, int numcorners);
177   void clear(bool preserve_voxel_data);
178   void clear() override;
179   void add_vertex(float3 P);
180   void add_vertex_slow(float3 P);
181   void add_triangle(int v0, int v1, int v2, int shader, bool smooth);
182   void add_subd_face(int *corners, int num_corners, int shader_, bool smooth_);
183 
184   void copy_center_to_motion_step(const int motion_step);
185 
186   void compute_bounds() override;
187   void apply_transform(const Transform &tfm, const bool apply_to_motion) override;
188   void add_face_normals();
189   void add_vertex_normals();
190   void add_undisplaced();
191 
192   void get_uv_tiles(ustring map, unordered_set<int> &tiles) override;
193 
194   void pack_shaders(Scene *scene, uint *shader);
195   void pack_normals(float4 *vnormal);
196   void pack_verts(const vector<uint> &tri_prim_index,
197                   uint4 *tri_vindex,
198                   uint *tri_patch,
199                   float2 *tri_patch_uv,
200                   size_t vert_offset,
201                   size_t tri_offset);
202   void pack_patches(uint *patch_data, uint vert_offset, uint face_offset, uint corner_offset);
203 
204   void tessellate(DiagSplit *split);
205 };
206 
207 CCL_NAMESPACE_END
208 
209 #endif /* __MESH_H__ */
210