Blender V5.3
mesh_subdivision.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2025 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/attribute.h"
6#include "scene/mesh.h"
7
9#include "subd/osd.h"
10#include "subd/patch.h"
11#include "subd/split.h"
12
13#include "util/algorithm.h"
14#include "util/vector.h"
15
17
19{
20 const int num_faces = get_num_subd_faces();
21
22 /* If there are no faces there is nothing to subdivide, and the required
23 * subd position attribute may not exist. */
24 if (num_faces == 0) {
25 return;
26 }
27
28#ifdef WITH_OPENSUBDIV
29 OsdMesh osd_mesh(*this);
30 OsdData osd_data;
31
32 if (subdivision_type == SUBDIVISION_CATMULL_CLARK) {
33 if (get_num_subd_faces()) {
34 osd_data.build(osd_mesh);
35 }
36 }
37 else
38#endif
39 {
40 /* force linear subdivision if OpenSubdiv is unavailable to avoid
41 * falling into catmull-clark code paths by accident
42 */
43 subdivision_type = SUBDIVISION_LINEAR;
44 }
45
46 /* count patches */
47 int num_patches = 0;
48 for (int f = 0; f < num_faces; f++) {
49 SubdFace face = get_subd_face(f);
50
51 if (face.is_quad()) {
52 num_patches++;
53 }
54 else {
55 num_patches += face.num_corners;
56 }
57 }
58
59 /* build patches from faces */
60#ifdef WITH_OPENSUBDIV
61 if (subdivision_type == SUBDIVISION_CATMULL_CLARK) {
62 vector<OsdPatch> osd_patches(num_patches, OsdPatch(osd_data));
63 OsdPatch *patch = osd_patches.data();
64
65 for (int f = 0; f < num_faces; f++) {
66 SubdFace face = get_subd_face(f);
67
68 if (face.is_quad()) {
69 patch->patch_index = face.ptex_offset;
70 patch->from_ngon = false;
71 patch->shader = face.shader;
72 patch->smooth = face.smooth;
73 patch++;
74 }
75 else {
76 for (int corner = 0; corner < face.num_corners; corner++) {
77 patch->patch_index = face.ptex_offset + corner;
78 patch->from_ngon = true;
79 patch->shader = face.shader;
80 patch->smooth = face.smooth;
81 patch++;
82 }
83 }
84 }
85
86 /* Split patches. */
87 DiagSplit split(params);
88 split.split_patches(osd_patches.data(), sizeof(OsdPatch));
89
90 /* Setup interpolation. */
91 SubdAttributeInterpolation interpolation(*this, osd_mesh, osd_data);
92
93 /* Dice patches. */
94 EdgeDice dice(params, split.get_num_verts(), split.get_num_triangles(), interpolation);
95 dice.dice(split);
96 }
97 else
98#endif
99 {
100 vector<LinearQuadPatch> linear_patches(num_patches);
101 LinearQuadPatch *patch = linear_patches.data();
102 const Attribute *subd_attr_P = subd_attributes.find(ATTR_STD_POSITION);
103 const packed_float3 *verts = subd_attr_P->data<packed_float3>();
104
105 for (int f = 0; f < num_faces; f++) {
106 SubdFace face = get_subd_face(f);
107
108 if (face.is_quad()) {
109 /* Simple quad case. */
110 float3 *hull = patch->hull;
111
112 patch->patch_index = face.ptex_offset;
113 patch->from_ngon = false;
114
115 hull[0] = verts[subd_face_corners[face.start_corner + 0]];
116 hull[1] = verts[subd_face_corners[face.start_corner + 1]];
117 hull[2] = verts[subd_face_corners[face.start_corner + 3]];
118 hull[3] = verts[subd_face_corners[face.start_corner + 2]];
119
120 patch->shader = face.shader;
121 patch->smooth = face.smooth;
122 patch++;
123 }
124 else {
125 /* N-gon split into N quads. */
126 float3 center_vert = zero_float3();
127
128 const float inv_num_corners = 1.0f / float(face.num_corners);
129 for (int corner = 0; corner < face.num_corners; corner++) {
130 center_vert += verts[subd_face_corners[face.start_corner + corner]] * inv_num_corners;
131 }
132
133 for (int corner = 0; corner < face.num_corners; corner++) {
134 float3 *hull = patch->hull;
135
136 patch->patch_index = face.ptex_offset + corner;
137 patch->from_ngon = true;
138
139 patch->shader = face.shader;
140 patch->smooth = face.smooth;
141
142 const int v0 = subd_face_corners[face.start_corner + mod(corner + 0, face.num_corners)];
143 const int v1 = subd_face_corners[face.start_corner + mod(corner + 1, face.num_corners)];
144 const int v3 = subd_face_corners[face.start_corner +
145 mod(corner + face.num_corners - 1, face.num_corners)];
146
147 hull[0] = verts[v0];
148 hull[1] = 0.5f * (verts[v0] + verts[v1]);
149 hull[2] = 0.5f * (verts[v3] + verts[v0]);
150 hull[3] = center_vert;
151
152 patch++;
153 }
154 }
155 }
156
157 /* Split patches. */
158 DiagSplit split(params);
159 split.split_patches(linear_patches.data(), sizeof(LinearQuadPatch));
160
161 /* Setup interpolation. */
162#ifdef WITH_OPENSUBDIV
163 SubdAttributeInterpolation interpolation(*this, osd_mesh, osd_data);
164#else
165 SubdAttributeInterpolation interpolation(*this);
166#endif
167
168 /* Dice patches. */
169 EdgeDice dice(params, split.get_num_verts(), split.get_num_triangles(), interpolation);
170 dice.dice(split);
171 }
172
173 // TODO: Free subd base data? Or will this break interactive updates?
174}
175
Attribute * find(ustring name) const
const T * data(const int step=0) const
void dice(const DiagSplit &split)
Definition dice.cpp:667
float3 hull[4]
Definition patch.h:30
friend class EdgeDice
Definition scene/mesh.h:183
void tessellate(SubdParams &params)
size_t get_num_subd_faces() const
Definition scene/mesh.h:221
AttributeSet subd_attributes
Definition scene/mesh.h:169
SubdFace get_subd_face(const size_t index) const
@ SUBDIVISION_LINEAR
Definition scene/mesh.h:115
@ SUBDIVISION_CATMULL_CLARK
Definition scene/mesh.h:116
friend class DiagSplit
Definition scene/mesh.h:182
int shader
Definition patch.h:21
bool smooth
Definition patch.h:22
bool from_ngon
Definition patch.h:23
int patch_index
Definition patch.h:20
#define CCL_NAMESPACE_END
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
@ ATTR_STD_POSITION
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float