1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
#if 0
# include "MEM_guardedalloc.h"
# include "BLI_alloca.h"
# include "BLI_array.h"
# include "BLI_compiler_attrs.h"
# include "BLI_compiler_compat.h"
# include "BLI_ghash.h"
# include "BLI_linklist.h"
# include "BLI_math.h"
# include "BLI_memarena.h"
# include "BLI_memblock.h"
# include "BLI_mempool.h"
# include "BLI_utildefines.h"
# include "BLI_hash.h"
# include "BKE_context.h"
# include "BKE_global.h"
# include "BKE_image.h"
# include "BKE_mesh.h"
# include "BKE_multires.h"
# include "BKE_object.h"
# include "BKE_pbvh.h"
# include "BKE_scene.h"
# include "BLI_bitmap.h"
# include "DNA_customdata_types.h"
# include "DNA_image_types.h"
# include "DNA_material_types.h"
# include "DNA_mesh_types.h"
# include "DNA_meshdata_types.h"
# include "DNA_object_types.h"
# include "DNA_scene_types.h"
# include "pbvh_intern.h"
# include "bmesh.h"
void *BKE_pbvh_get_tex_settings(PBVH *pbvh, PBVHNode *node, TexLayerRef vdm)
{
return NULL; // implement me!
}
void *BKE_pbvh_get_tex_data(PBVH *pbvh, PBVHNode *node, TexPointRef vdm)
{
return NULL; // implement me!
}
typedef union TexelKey {
struct {
int idx; // index in image
int co_key; // used to differentiate same texel used in different 3d points in space
} key;
intptr_t i;
} TexelKey;
BLI_INLINE int calc_co_key(const float *co)
{
const int mul = 65535;
const int mask = 65535;
int x = (int)co[0] + (((int)co[0] * mul) & mask);
int y = (int)co[0] + (((int)co[0] * mul) & mask);
int z = (int)co[0] + (((int)co[0] * mul) & mask);
return BLI_hash_int_3d(x, y, z);
}
typedef struct TextureVDMSettings {
ImageUser ima_user;
ID *image;
bool tangent_space;
char uv_layer[64];
// used by texture_vdm_get_points
// BLI_bitmap *texel_used_map;
GSet *texel_used_map;
int width, height;
bool invalid;
} TextureVDMSettings;
typedef struct TextureNodeData {
TexPointRef *point_ids;
float (*point_cos)[3];
float (*point_uvs)[2];
float **point_cos_ptrs;
int totpoint;
} TextureNodeData;
void texture_vdm_begin(PBVH *pbvh, PBVHNode *node, TexLayerRef vdm)
{
TextureVDMSettings *settings = BKE_pbvh_get_tex_settings(pbvh, node, vdm);
if (!settings->image) {
return;
}
Image *image = (Image *)settings->image;
int w = 0, h = 0;
BKE_image_get_size(image, &settings->ima_user, &w, &h);
// Image *image = settings->image.
settings->width = w;
settings->height = h;
// settings->texel_used_map = BLI_BITMAP_NEW(w * h, "texel_used_map");
settings->texel_used_map = BLI_gset_ptr_new("texel_used_map");
}
void texture_vdm_build_points(PBVH *pbvh, PBVHNode *node, TexLayerRef vdm)
{
TextureVDMSettings *settings = BKE_pbvh_get_tex_settings(pbvh, node, vdm);
TextureNodeData *data = BKE_pbvh_get_tex_data(pbvh, node, vdm);
int idx;
if (!settings->uv_layer[0]) {
idx = CustomData_get_layer_index(&pbvh->bm->ldata, CD_MLOOPUV);
}
else {
idx = CustomData_get_named_layer_index(&pbvh->bm->ldata, CD_MLOOPUV, settings->uv_layer);
}
if (idx < 0) {
settings->invalid = true;
return;
}
const int cd_uv = pbvh->bm->ldata.layers[idx].offset;
const int w = settings->width, h = settings->height;
float **point_cos_ptrs = NULL;
float *uvs = NULL;
float *cos = NULL;
TexPointRef *ids = NULL;
BLI_array_declare(point_cos_ptrs);
BLI_array_declare(uvs);
BLI_array_declare(cos);
BLI_array_declare(ids);
for (int i = 0; i < node->tribuf->tottri; i++) {
PBVHTri *tri = node->tribuf->tris + i;
BMLoop *ls[3] = {(BMLoop *)tri->l[0], (BMLoop *)tri->l[1], (BMLoop *)tri->l[2]};
float min[2] = {FLT_MAX, FLT_MAX}, max[2] = {FLT_MIN, FLT_MIN};
float tricos[3][3];
copy_v3_v3(tricos[0], ls[0]->v->co);
copy_v3_v3(tricos[1], ls[1]->v->co);
copy_v3_v3(tricos[2], ls[2]->v->co);
for (int j = 0; j < 3; j++) {
MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(ls[j], cd_uv);
minmax_v2v2_v2(min, max, luv->uv);
}
int dw = (int)((max[0] - min[0]) * (float)w + 0.000001f);
int dh = (int)((max[1] - min[1]) * (float)h + 0.000001f);
dw = MAX2(dw, 1);
dh = MAX2(dh, 1);
float du = (max[0] - min[0]) / dw;
float dv = (max[1] - min[1]) / dh;
float u = min[0], v = min[1];
for (int y = 0; y < dh; y++, v += dv) {
u = min[0];
for (int x = 0; x < dw; x++, u += du) {
int idx = y * w + x;
float co[3];
interp_barycentric_tri_v3(tricos, u, v, co);
TexelKey key;
key.key.idx = idx;
key.key.co_key = calc_co_key(co);
if (BLI_gset_haskey(settings->texel_used_map, (void *)key.i)) {
continue;
}
BLI_gset_insert(settings->texel_used_map, (void *)key.i);
BLI_array_append(uvs, u);
BLI_array_append(uvs, v);
BLI_array_append(cos, co[0]);
BLI_array_append(cos, co[1]);
BLI_array_append(cos, co[2]);
BLI_array_append(ids, (TexPointRef)key.i);
}
}
}
settings->invalid = false;
MEM_SAFE_FREE(data->point_cos);
MEM_SAFE_FREE(data->point_ids);
MEM_SAFE_FREE(data->point_uvs);
MEM_SAFE_FREE(data->point_cos_ptrs);
int totpoint = BLI_array_len(ids);
data->totpoint = totpoint;
data->point_cos_ptrs = MEM_malloc_arrayN(totpoint, sizeof(void *), "point_cos_ptrs");
// dumb casting trick
union {
float *cos;
float (*cos3)[3];
} castcos;
union {
float *uvs;
float (*uvs2)[2];
} castuvs;
castcos.cos = cos;
castuvs.uvs = uvs;
data->point_cos = castcos.cos3;
data->point_ids = ids;
data->point_uvs = castuvs.uvs2;
for (int i = 0; i < totpoint; i++) {
data->point_cos_ptrs[i] = cos + i * 3;
}
}
void texture_vdm_get_points(PBVH *pbvh,
PBVHNode *node,
TexLayerRef vdm,
TexPointRef **r_ids,
float ***r_cos,
float ***r_nos,
int *r_totpoint)
{
TextureVDMSettings *settings = BKE_pbvh_get_tex_settings(pbvh, node, vdm);
TextureNodeData *data = BKE_pbvh_get_tex_data(pbvh, node, vdm);
if (r_totpoint) {
*r_totpoint = data->totpoint;
}
if (r_cos) {
*r_cos = data->point_cos_ptrs;
}
if (r_ids) {
*r_ids = data->point_ids;
}
}
static SculptDisplacementDef texture_vdm = {
.type = SCULPT_TEXTURE_UV,
.settings_size = sizeof(TextureNodeData),
.getPointsFromNode = texture_vdm_get_points,
};
#endif
|