Blender  V3.3
bake.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
50 #include <limits.h>
51 
52 #include "MEM_guardedalloc.h"
53 
54 #include "BLI_math.h"
55 
56 #include "DNA_mesh_types.h"
57 #include "DNA_meshdata_types.h"
58 
59 #include "BKE_bvhutils.h"
60 #include "BKE_customdata.h"
61 #include "BKE_image.h"
62 #include "BKE_lib_id.h"
63 #include "BKE_mesh.h"
64 #include "BKE_mesh_runtime.h"
65 #include "BKE_mesh_tangent.h"
66 #include "BKE_node.h"
67 
68 #include "IMB_imbuf.h"
69 #include "IMB_imbuf_types.h"
70 
71 #include "RE_bake.h"
72 #include "RE_texture_margin.h"
73 
74 /* local include */
75 #include "render_types.h"
76 #include "zbuf.h"
77 
78 typedef struct BakeDataZSpan {
83  float du_dx, du_dy;
84  float dv_dx, dv_dy;
86 
90 typedef struct TSpace {
91  float tangent[3];
92  float sign;
94 
95 typedef struct TriTessFace {
96  const MVert *mverts[3];
97  const float *vert_normals[3];
98  const TSpace *tspace[3];
99  const float *loop_normal[3];
100  float normal[3]; /* for flat faces */
101  bool is_smooth;
103 
104 static void store_bake_pixel(void *handle, int x, int y, float u, float v)
105 {
106  BakeDataZSpan *bd = (BakeDataZSpan *)handle;
107  BakePixel *pixel;
108 
109  const int width = bd->bk_image->width;
110  const size_t offset = bd->bk_image->offset;
111  const int i = offset + y * width + x;
112 
113  pixel = &bd->pixel_array[i];
114  pixel->primitive_id = bd->primitive_id;
115 
116  /* At this point object_id is always 0, since this function runs for the
117  * low-poly mesh only. The object_id lookup indices are set afterwards. */
118 
119  copy_v2_fl2(pixel->uv, u, v);
120 
121  pixel->du_dx = bd->du_dx;
122  pixel->du_dy = bd->du_dy;
123  pixel->dv_dx = bd->dv_dx;
124  pixel->dv_dy = bd->dv_dy;
125  pixel->object_id = 0;
126  pixel->seed = i;
127 }
128 
129 void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t pixels_num, char *mask)
130 {
131  size_t i;
132  if (!mask) {
133  return;
134  }
135 
136  /* only extend to pixels outside the mask area */
137  for (i = 0; i < pixels_num; i++) {
138  if (pixel_array[i].primitive_id != -1) {
139  mask[i] = FILTER_MASK_USED;
140  }
141  }
142 }
143 
145  char *mask,
146  const int margin,
147  const char margin_type,
148  Mesh const *me,
149  char const *uv_layer,
150  const float uv_offset[2])
151 {
152  /* margin */
153  switch (margin_type) {
155  RE_generate_texturemargin_adjacentfaces(ibuf, mask, margin, me, uv_layer, uv_offset);
156  break;
157  default:
158  /* fall through */
159  case R_BAKE_EXTEND:
160  IMB_filter_extend(ibuf, mask, margin);
161  break;
162  }
163 
164  if (ibuf->planes != R_IMF_PLANES_RGBA) {
165  /* clear alpha added by filtering */
166  IMB_rectfill_alpha(ibuf, 1.0f);
167  }
168 }
169 
178  TriTessFace *triangles_cage,
179  const float mat_low[4][4],
180  const float mat_cage[4][4],
181  int primitive_id,
182  float u,
183  float v,
184  float r_co[3],
185  float r_dir[3])
186 {
187  float data[2][3][3];
188  float coord[2][3];
189  float dir[3];
190  int i;
191 
192  TriTessFace *triangle[2];
193 
194  triangle[0] = &triangles_low[primitive_id];
195  triangle[1] = &triangles_cage[primitive_id];
196 
197  for (i = 0; i < 2; i++) {
198  copy_v3_v3(data[i][0], triangle[i]->mverts[0]->co);
199  copy_v3_v3(data[i][1], triangle[i]->mverts[1]->co);
200  copy_v3_v3(data[i][2], triangle[i]->mverts[2]->co);
201  interp_barycentric_tri_v3(data[i], u, v, coord[i]);
202  }
203 
204  /* convert from local to world space */
205  mul_m4_v3(mat_low, coord[0]);
206  mul_m4_v3(mat_cage, coord[1]);
207 
208  sub_v3_v3v3(dir, coord[0], coord[1]);
209  normalize_v3(dir);
210 
211  copy_v3_v3(r_co, coord[1]);
212  copy_v3_v3(r_dir, dir);
213 }
214 
221  const float mat[4][4],
222  const float imat[4][4],
223  int primitive_id,
224  float u,
225  float v,
226  float cage_extrusion,
227  float r_co[3],
228  float r_dir[3],
229  const bool is_cage)
230 {
231  float data[3][3];
232  float coord[3];
233  float dir[3];
234  float cage[3];
235  bool is_smooth;
236 
237  TriTessFace *triangle = &triangles[primitive_id];
238  is_smooth = triangle->is_smooth || is_cage;
239 
240  copy_v3_v3(data[0], triangle->mverts[0]->co);
241  copy_v3_v3(data[1], triangle->mverts[1]->co);
242  copy_v3_v3(data[2], triangle->mverts[2]->co);
243 
244  interp_barycentric_tri_v3(data, u, v, coord);
245 
246  if (is_smooth) {
247  copy_v3_v3(data[0], triangle->vert_normals[0]);
248  copy_v3_v3(data[1], triangle->vert_normals[1]);
249  copy_v3_v3(data[2], triangle->vert_normals[2]);
250 
251  interp_barycentric_tri_v3(data, u, v, dir);
252  normalize_v3(dir);
253  }
254  else {
255  copy_v3_v3(dir, triangle->normal);
256  }
257 
258  mul_v3_v3fl(cage, dir, cage_extrusion);
259  add_v3_v3(coord, cage);
260 
261  normalize_v3(dir);
262  negate_v3(dir);
263 
264  /* convert from local to world space */
265  mul_m4_v3(mat, coord);
266  mul_transposed_mat3_m4_v3(imat, dir);
267  normalize_v3(dir);
268 
269  copy_v3_v3(r_co, coord);
270  copy_v3_v3(r_dir, dir);
271 }
272 
273 static void barycentric_differentials_from_position(const float co[3],
274  const float v1[3],
275  const float v2[3],
276  const float v3[3],
277  const float dxco[3],
278  const float dyco[3],
279  const float facenor[3],
280  const bool differentials,
281  float *u,
282  float *v,
283  float *dx_u,
284  float *dx_v,
285  float *dy_u,
286  float *dy_v)
287 {
288  /* find most stable axis to project */
289  int axis1, axis2;
290  axis_dominant_v3(&axis1, &axis2, facenor);
291 
292  /* compute u,v and derivatives */
293  float t00 = v3[axis1] - v1[axis1];
294  float t01 = v3[axis2] - v1[axis2];
295  float t10 = v3[axis1] - v2[axis1];
296  float t11 = v3[axis2] - v2[axis2];
297 
298  float detsh = (t00 * t11 - t10 * t01);
299  detsh = (detsh != 0.0f) ? 1.0f / detsh : 0.0f;
300  t00 *= detsh;
301  t01 *= detsh;
302  t10 *= detsh;
303  t11 *= detsh;
304 
305  *u = (v3[axis1] - co[axis1]) * t11 - (v3[axis2] - co[axis2]) * t10;
306  *v = (v3[axis2] - co[axis2]) * t00 - (v3[axis1] - co[axis1]) * t01;
307  if (differentials) {
308  *dx_u = dxco[axis1] * t11 - dxco[axis2] * t10;
309  *dx_v = dxco[axis2] * t00 - dxco[axis1] * t01;
310  *dy_u = dyco[axis1] * t11 - dyco[axis2] * t10;
311  *dy_v = dyco[axis2] * t00 - dyco[axis1] * t01;
312  }
313 }
314 
318 static bool cast_ray_highpoly(BVHTreeFromMesh *treeData,
319  TriTessFace *triangle_low,
320  TriTessFace *triangles[],
321  BakePixel *pixel_array_low,
322  BakePixel *pixel_array,
323  const float mat_low[4][4],
324  BakeHighPolyData *highpoly,
325  const float co[3],
326  const float dir[3],
327  const int pixel_id,
328  const int tot_highpoly,
329  const float max_ray_distance)
330 {
331  int i;
332  int hit_mesh = -1;
333  float hit_distance_squared = max_ray_distance * max_ray_distance;
334  if (hit_distance_squared == 0.0f) {
335  /* No ray distance set, use maximum. */
336  hit_distance_squared = FLT_MAX;
337  }
338 
339  BVHTreeRayHit *hits;
340  hits = MEM_mallocN(sizeof(BVHTreeRayHit) * tot_highpoly, "Bake Highpoly to Lowpoly: BVH Rays");
341 
342  for (i = 0; i < tot_highpoly; i++) {
343  float co_high[3], dir_high[3];
344 
345  hits[i].index = -1;
346  /* TODO: we should use FLT_MAX here, but sweep-sphere code isn't prepared for that. */
347  hits[i].dist = BVH_RAYCAST_DIST_MAX;
348 
349  /* Transform the ray from the world space to the `highpoly` space. */
350  mul_v3_m4v3(co_high, highpoly[i].imat, co);
351 
352  /* rotates */
353  mul_v3_mat3_m4v3(dir_high, highpoly[i].imat, dir);
354  normalize_v3(dir_high);
355 
356  /* cast ray */
357  if (treeData[i].tree) {
358  BLI_bvhtree_ray_cast(treeData[i].tree,
359  co_high,
360  dir_high,
361  0.0f,
362  &hits[i],
363  treeData[i].raycast_callback,
364  &treeData[i]);
365  }
366 
367  if (hits[i].index != -1) {
368  /* distance comparison in world space */
369  float hit_world[3];
370  mul_v3_m4v3(hit_world, highpoly[i].obmat, hits[i].co);
371  float distance_squared = len_squared_v3v3(hit_world, co);
372 
373  if (distance_squared < hit_distance_squared) {
374  hit_mesh = i;
375  hit_distance_squared = distance_squared;
376  }
377  }
378  }
379 
380  if (hit_mesh != -1) {
381  int primitive_id_high = hits[hit_mesh].index;
382  TriTessFace *triangle_high = &triangles[hit_mesh][primitive_id_high];
383  BakePixel *pixel_low = &pixel_array_low[pixel_id];
384  BakePixel *pixel_high = &pixel_array[pixel_id];
385 
386  pixel_high->primitive_id = primitive_id_high;
387  pixel_high->object_id = hit_mesh;
388  pixel_high->seed = pixel_id;
389 
390  /* ray direction in high poly object space */
391  float dir_high[3];
392  mul_v3_mat3_m4v3(dir_high, highpoly[hit_mesh].imat, dir);
393  normalize_v3(dir_high);
394 
395  /* compute position differentials on low poly object */
396  float duco_low[3], dvco_low[3], dxco[3], dyco[3];
397  sub_v3_v3v3(duco_low, triangle_low->mverts[0]->co, triangle_low->mverts[2]->co);
398  sub_v3_v3v3(dvco_low, triangle_low->mverts[1]->co, triangle_low->mverts[2]->co);
399 
400  mul_v3_v3fl(dxco, duco_low, pixel_low->du_dx);
401  madd_v3_v3fl(dxco, dvco_low, pixel_low->dv_dx);
402  mul_v3_v3fl(dyco, duco_low, pixel_low->du_dy);
403  madd_v3_v3fl(dyco, dvco_low, pixel_low->dv_dy);
404 
405  /* transform from low poly to high poly object space */
406  mul_mat3_m4_v3(mat_low, dxco);
407  mul_mat3_m4_v3(mat_low, dyco);
408  mul_mat3_m4_v3(highpoly[hit_mesh].imat, dxco);
409  mul_mat3_m4_v3(highpoly[hit_mesh].imat, dyco);
410 
411  /* transfer position differentials */
412  float tmp[3];
413  mul_v3_v3fl(tmp, dir_high, 1.0f / dot_v3v3(dir_high, triangle_high->normal));
414  madd_v3_v3fl(dxco, tmp, -dot_v3v3(dxco, triangle_high->normal));
415  madd_v3_v3fl(dyco, tmp, -dot_v3v3(dyco, triangle_high->normal));
416 
417  /* compute barycentric differentials from position differentials */
418  barycentric_differentials_from_position(hits[hit_mesh].co,
419  triangle_high->mverts[0]->co,
420  triangle_high->mverts[1]->co,
421  triangle_high->mverts[2]->co,
422  dxco,
423  dyco,
424  triangle_high->normal,
425  true,
426  &pixel_high->uv[0],
427  &pixel_high->uv[1],
428  &pixel_high->du_dx,
429  &pixel_high->dv_dx,
430  &pixel_high->du_dy,
431  &pixel_high->dv_dy);
432 
433  /* verify we have valid uvs */
434  BLI_assert(pixel_high->uv[0] >= -1e-3f && pixel_high->uv[1] >= -1e-3f &&
435  pixel_high->uv[0] + pixel_high->uv[1] <= 1.0f + 1e-3f);
436  }
437  else {
438  pixel_array[pixel_id].primitive_id = -1;
439  pixel_array[pixel_id].object_id = -1;
440  pixel_array[pixel_id].seed = 0;
441  }
442 
443  MEM_freeN(hits);
444  return hit_mesh != -1;
445 }
446 
451 static TriTessFace *mesh_calc_tri_tessface(Mesh *me, bool tangent, Mesh *me_eval)
452 {
453  int i;
454 
455  const int tottri = poly_to_tri_count(me->totpoly, me->totloop);
456  MLoopTri *looptri;
457  TriTessFace *triangles;
458 
459  /* calculate normal for each polygon only once */
460  unsigned int mpoly_prev = UINT_MAX;
461  float no[3];
462 
463  const MVert *mvert = CustomData_get_layer(&me->vdata, CD_MVERT);
464  looptri = MEM_mallocN(sizeof(*looptri) * tottri, __func__);
465  triangles = MEM_callocN(sizeof(TriTessFace) * tottri, __func__);
466 
467  const float(*precomputed_normals)[3] = BKE_mesh_poly_normals_are_dirty(me) ?
468  NULL :
470  const bool calculate_normal = precomputed_normals ? false : true;
471 
472  if (precomputed_normals != NULL) {
474  me->mloop, me->mpoly, me->mvert, me->totloop, me->totpoly, looptri, precomputed_normals);
475  }
476  else {
477  BKE_mesh_recalc_looptri(me->mloop, me->mpoly, me->mvert, me->totloop, me->totpoly, looptri);
478  }
479 
480  const TSpace *tspace = NULL;
481  const float(*loop_normals)[3] = NULL;
482  if (tangent) {
485  BKE_mesh_calc_loop_tangents(me_eval, true, NULL, 0);
486 
487  tspace = CustomData_get_layer(&me_eval->ldata, CD_TANGENT);
488  BLI_assert(tspace);
489 
490  loop_normals = CustomData_get_layer(&me_eval->ldata, CD_NORMAL);
491  }
492 
493  const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(me);
494  for (i = 0; i < tottri; i++) {
495  const MLoopTri *lt = &looptri[i];
496  const MPoly *mp = &me->mpoly[lt->poly];
497 
498  triangles[i].mverts[0] = &mvert[me->mloop[lt->tri[0]].v];
499  triangles[i].mverts[1] = &mvert[me->mloop[lt->tri[1]].v];
500  triangles[i].mverts[2] = &mvert[me->mloop[lt->tri[2]].v];
501  triangles[i].vert_normals[0] = vert_normals[me->mloop[lt->tri[0]].v];
502  triangles[i].vert_normals[1] = vert_normals[me->mloop[lt->tri[1]].v];
503  triangles[i].vert_normals[2] = vert_normals[me->mloop[lt->tri[2]].v];
504  triangles[i].is_smooth = (mp->flag & ME_SMOOTH) != 0;
505 
506  if (tangent) {
507  triangles[i].tspace[0] = &tspace[lt->tri[0]];
508  triangles[i].tspace[1] = &tspace[lt->tri[1]];
509  triangles[i].tspace[2] = &tspace[lt->tri[2]];
510  }
511 
512  if (loop_normals) {
513  triangles[i].loop_normal[0] = loop_normals[lt->tri[0]];
514  triangles[i].loop_normal[1] = loop_normals[lt->tri[1]];
515  triangles[i].loop_normal[2] = loop_normals[lt->tri[2]];
516  }
517 
518  if (calculate_normal) {
519  if (lt->poly != mpoly_prev) {
520  BKE_mesh_calc_poly_normal(mp, &me->mloop[mp->loopstart], me->mvert, no);
521  mpoly_prev = lt->poly;
522  }
523  copy_v3_v3(triangles[i].normal, no);
524  }
525  else {
526  copy_v3_v3(triangles[i].normal, precomputed_normals[lt->poly]);
527  }
528  }
529 
530  MEM_freeN(looptri);
531 
532  return triangles;
533 }
534 
536  BakePixel pixel_array_from[],
537  BakePixel pixel_array_to[],
538  BakeHighPolyData highpoly[],
539  const int tot_highpoly,
540  const size_t pixels_num,
541  const bool is_custom_cage,
542  const float cage_extrusion,
543  const float max_ray_distance,
544  float mat_low[4][4],
545  float mat_cage[4][4],
546  struct Mesh *me_cage)
547 {
548  size_t i;
549  int primitive_id;
550  float u, v;
551  float imat_low[4][4];
552  bool is_cage = me_cage != NULL;
553  bool result = true;
554 
555  Mesh *me_eval_low = NULL;
556  Mesh **me_highpoly;
557  BVHTreeFromMesh *treeData;
558 
559  /* NOTE: all coordinates are in local space. */
560  TriTessFace *tris_low = NULL;
561  TriTessFace *tris_cage = NULL;
562  TriTessFace **tris_high;
563 
564  /* Assume all low-poly tessfaces can be quads. */
565  tris_high = MEM_callocN(sizeof(TriTessFace *) * tot_highpoly, "MVerts Highpoly Mesh Array");
566 
567  /* Assume all high-poly tessfaces are triangles. */
568  me_highpoly = MEM_mallocN(sizeof(Mesh *) * tot_highpoly, "Highpoly Derived Meshes");
569  treeData = MEM_callocN(sizeof(BVHTreeFromMesh) * tot_highpoly, "Highpoly BVH Trees");
570 
571  if (!is_cage) {
572  me_eval_low = BKE_mesh_copy_for_eval(me_low, false);
573  tris_low = mesh_calc_tri_tessface(me_low, true, me_eval_low);
574  }
575  else if (is_custom_cage) {
576  tris_low = mesh_calc_tri_tessface(me_low, false, NULL);
577  tris_cage = mesh_calc_tri_tessface(me_cage, false, NULL);
578  }
579  else {
580  tris_cage = mesh_calc_tri_tessface(me_cage, false, NULL);
581  }
582 
583  invert_m4_m4(imat_low, mat_low);
584 
585  for (i = 0; i < tot_highpoly; i++) {
586  tris_high[i] = mesh_calc_tri_tessface(highpoly[i].me, false, NULL);
587 
588  me_highpoly[i] = highpoly[i].me;
589  BKE_mesh_runtime_looptri_ensure(me_highpoly[i]);
590 
591  if (me_highpoly[i]->runtime.looptris.len != 0) {
592  /* Create a BVH-tree for each `highpoly` object. */
593  BKE_bvhtree_from_mesh_get(&treeData[i], me_highpoly[i], BVHTREE_FROM_LOOPTRI, 2);
594 
595  if (treeData[i].tree == NULL) {
596  printf("Baking: out of memory while creating BHVTree for object \"%s\"\n",
597  highpoly[i].ob->id.name + 2);
598  result = false;
599  goto cleanup;
600  }
601  }
602  }
603 
604  for (i = 0; i < pixels_num; i++) {
605  float co[3];
606  float dir[3];
607  TriTessFace *tri_low;
608 
609  primitive_id = pixel_array_from[i].primitive_id;
610 
611  if (primitive_id == -1) {
612  pixel_array_to[i].primitive_id = -1;
613  continue;
614  }
615 
616  u = pixel_array_from[i].uv[0];
617  v = pixel_array_from[i].uv[1];
618 
619  /* calculate from low poly mesh cage */
620  if (is_custom_cage) {
622  tris_low, tris_cage, mat_low, mat_cage, primitive_id, u, v, co, dir);
623  tri_low = &tris_cage[primitive_id];
624  }
625  else if (is_cage) {
627  tris_cage, mat_low, imat_low, primitive_id, u, v, cage_extrusion, co, dir, true);
628  tri_low = &tris_cage[primitive_id];
629  }
630  else {
632  tris_low, mat_low, imat_low, primitive_id, u, v, cage_extrusion, co, dir, false);
633  tri_low = &tris_low[primitive_id];
634  }
635 
636  /* cast ray */
637  if (!cast_ray_highpoly(treeData,
638  tri_low,
639  tris_high,
640  pixel_array_from,
641  pixel_array_to,
642  mat_low,
643  highpoly,
644  co,
645  dir,
646  i,
647  tot_highpoly,
648  max_ray_distance)) {
649  /* if it fails mask out the original pixel array */
650  pixel_array_from[i].primitive_id = -1;
651  }
652  }
653 
654  /* garbage collection */
655 cleanup:
656  for (i = 0; i < tot_highpoly; i++) {
657  free_bvhtree_from_mesh(&treeData[i]);
658 
659  if (tris_high[i]) {
660  MEM_freeN(tris_high[i]);
661  }
662  }
663 
664  MEM_freeN(tris_high);
665  MEM_freeN(treeData);
666  MEM_freeN(me_highpoly);
667 
668  if (me_eval_low) {
669  BKE_id_free(NULL, me_eval_low);
670  }
671  if (tris_low) {
672  MEM_freeN(tris_low);
673  }
674  if (tris_cage) {
675  MEM_freeN(tris_cage);
676  }
677 
678  return result;
679 }
680 
682  const float *uv1,
683  const float *uv2,
684  const float *uv3)
685 {
686  float A;
687 
688  /* assumes dPdu = P1 - P3 and dPdv = P2 - P3 */
689  A = (uv2[0] - uv1[0]) * (uv3[1] - uv1[1]) - (uv3[0] - uv1[0]) * (uv2[1] - uv1[1]);
690 
691  if (fabsf(A) > FLT_EPSILON) {
692  A = 0.5f / A;
693 
694  bd->du_dx = (uv2[1] - uv3[1]) * A;
695  bd->dv_dx = (uv3[1] - uv1[1]) * A;
696 
697  bd->du_dy = (uv3[0] - uv2[0]) * A;
698  bd->dv_dy = (uv1[0] - uv3[0]) * A;
699  }
700  else {
701  bd->du_dx = bd->du_dy = 0.0f;
702  bd->dv_dx = bd->dv_dy = 0.0f;
703  }
704 }
705 
707  BakePixel pixel_array[],
708  const size_t pixels_num,
709  const BakeTargets *targets,
710  const char *uv_layer)
711 {
712  const MLoopUV *mloopuv;
713  if ((uv_layer == NULL) || (uv_layer[0] == '\0')) {
714  mloopuv = CustomData_get_layer(&me->ldata, CD_MLOOPUV);
715  }
716  else {
717  int uv_id = CustomData_get_named_layer(&me->ldata, CD_MLOOPUV, uv_layer);
718  mloopuv = CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, uv_id);
719  }
720 
721  if (mloopuv == NULL) {
722  return;
723  }
724 
725  BakeDataZSpan bd;
726  bd.pixel_array = pixel_array;
727  bd.zspan = MEM_callocN(sizeof(ZSpan) * targets->images_num, "bake zspan");
728 
729  /* initialize all pixel arrays so we know which ones are 'blank' */
730  for (int i = 0; i < pixels_num; i++) {
731  pixel_array[i].primitive_id = -1;
732  pixel_array[i].object_id = 0;
733  }
734 
735  for (int i = 0; i < targets->images_num; i++) {
736  zbuf_alloc_span(&bd.zspan[i], targets->images[i].width, targets->images[i].height);
737  }
738 
739  const int tottri = poly_to_tri_count(me->totpoly, me->totloop);
740  MLoopTri *looptri = MEM_mallocN(sizeof(*looptri) * tottri, __func__);
741 
742  BKE_mesh_recalc_looptri(me->mloop, me->mpoly, me->mvert, me->totloop, me->totpoly, looptri);
743  const int materials_num = targets->materials_num;
744 
745  for (int i = 0; i < tottri; i++) {
746  const MLoopTri *lt = &looptri[i];
747  const MPoly *mp = &me->mpoly[lt->poly];
748 
749  bd.primitive_id = i;
750 
751  /* Find images matching this material. */
752  const int material_index = (materials_num) ? clamp_i(mp->mat_nr, 0, materials_num - 1) : 0;
753  Image *image = targets->material_to_image[material_index];
754  for (int image_id = 0; image_id < targets->images_num; image_id++) {
755  BakeImage *bk_image = &targets->images[image_id];
756  if (bk_image->image != image) {
757  continue;
758  }
759 
760  /* Compute triangle vertex UV coordinates. */
761  float vec[3][2];
762  for (int a = 0; a < 3; a++) {
763  const float *uv = mloopuv[lt->tri[a]].uv;
764 
765  /* NOTE(campbell): workaround for pixel aligned UVs which are common and can screw up our
766  * intersection tests where a pixel gets in between 2 faces or the middle of a quad,
767  * camera aligned quads also have this problem but they are less common.
768  * Add a small offset to the UVs, fixes bug T18685. */
769  vec[a][0] = (uv[0] - bk_image->uv_offset[0]) * (float)bk_image->width - (0.5f + 0.001f);
770  vec[a][1] = (uv[1] - bk_image->uv_offset[1]) * (float)bk_image->height - (0.5f + 0.002f);
771  }
772 
773  /* Rasterize triangle. */
774  bd.bk_image = bk_image;
775  bake_differentials(&bd, vec[0], vec[1], vec[2]);
777  &bd.zspan[image_id], (void *)&bd, vec[0], vec[1], vec[2], store_bake_pixel);
778  }
779  }
780 
781  for (int i = 0; i < targets->images_num; i++) {
782  zbuf_free_span(&bd.zspan[i]);
783  }
784 
785  MEM_freeN(looptri);
786  MEM_freeN(bd.zspan);
787 }
788 
789 /* ******************** NORMALS ************************ */
790 
791 static void normal_compress(float out[3],
792  const float in[3],
793  const eBakeNormalSwizzle normal_swizzle[3])
794 {
795  const int swizzle_index[6] = {
796  0, /* R_BAKE_POSX */
797  1, /* R_BAKE_POSY */
798  2, /* R_BAKE_POSZ */
799  0, /* R_BAKE_NEGX */
800  1, /* R_BAKE_NEGY */
801  2, /* R_BAKE_NEGZ */
802  };
803  const float swizzle_sign[6] = {
804  +1.0f, /* R_BAKE_POSX */
805  +1.0f, /* R_BAKE_POSY */
806  +1.0f, /* R_BAKE_POSZ */
807  -1.0f, /* R_BAKE_NEGX */
808  -1.0f, /* R_BAKE_NEGY */
809  -1.0f, /* R_BAKE_NEGZ */
810  };
811 
812  int i;
813 
814  for (i = 0; i < 3; i++) {
815  int index;
816  float sign;
817 
818  sign = swizzle_sign[normal_swizzle[i]];
819  index = swizzle_index[normal_swizzle[i]];
820 
821  /*
822  * There is a small 1e-5f bias for precision issues. otherwise
823  * we randomly get 127 or 128 for neutral colors in tangent maps.
824  * we choose 128 because it is the convention flat color. *
825  */
826 
827  out[i] = sign * in[index] / 2.0f + 0.5f + 1e-5f;
828  }
829 }
830 
832  const size_t pixels_num,
833  const int depth,
834  float result[],
835  Mesh *me,
836  const eBakeNormalSwizzle normal_swizzle[3],
837  float mat[4][4])
838 {
839  size_t i;
840 
841  TriTessFace *triangles;
842 
843  Mesh *me_eval = BKE_mesh_copy_for_eval(me, false);
844 
845  triangles = mesh_calc_tri_tessface(me, true, me_eval);
846 
847  BLI_assert(pixels_num >= 3);
848 
849  for (i = 0; i < pixels_num; i++) {
850  TriTessFace *triangle;
851  float tangents[3][3];
852  float normals[3][3];
853  float signs[3];
854  int j;
855 
856  float tangent[3];
857  float normal[3];
858  float binormal[3];
859  float sign;
860  float u, v, w;
861 
862  float tsm[3][3]; /* tangent space matrix */
863  float itsm[3][3];
864 
865  size_t offset;
866  float nor[3]; /* texture normal */
867 
868  bool is_smooth;
869 
870  int primitive_id = pixel_array[i].primitive_id;
871 
872  offset = i * depth;
873 
874  if (primitive_id == -1) {
875  if (depth == 4) {
876  copy_v4_fl4(&result[offset], 0.5f, 0.5f, 1.0f, 1.0f);
877  }
878  else {
879  copy_v3_fl3(&result[offset], 0.5f, 0.5f, 1.0f);
880  }
881  continue;
882  }
883 
884  triangle = &triangles[primitive_id];
885  is_smooth = triangle->is_smooth;
886 
887  for (j = 0; j < 3; j++) {
888  const TSpace *ts;
889 
890  if (is_smooth) {
891  if (triangle->loop_normal[j]) {
892  copy_v3_v3(normals[j], triangle->loop_normal[j]);
893  }
894  else {
895  copy_v3_v3(normals[j], triangle->vert_normals[j]);
896  }
897  }
898 
899  ts = triangle->tspace[j];
900  copy_v3_v3(tangents[j], ts->tangent);
901  signs[j] = ts->sign;
902  }
903 
904  u = pixel_array[i].uv[0];
905  v = pixel_array[i].uv[1];
906  w = 1.0f - u - v;
907 
908  /* normal */
909  if (is_smooth) {
911  }
912  else {
913  copy_v3_v3(normal, triangle->normal);
914  }
915 
916  /* tangent */
917  interp_barycentric_tri_v3(tangents, u, v, tangent);
918 
919  /* sign */
920  /* The sign is the same at all face vertices for any non degenerate face.
921  * Just in case we clamp the interpolated value though. */
922  sign = (signs[0] * u + signs[1] * v + signs[2] * w) < 0 ? (-1.0f) : 1.0f;
923 
924  /* binormal */
925  /* `B = sign * cross(N, T)` */
926  cross_v3_v3v3(binormal, normal, tangent);
927  mul_v3_fl(binormal, sign);
928 
929  /* populate tangent space matrix */
930  copy_v3_v3(tsm[0], tangent);
931  copy_v3_v3(tsm[1], binormal);
932  copy_v3_v3(tsm[2], normal);
933 
934  /* texture values */
936 
937  /* converts from world space to local space */
939 
940  invert_m3_m3(itsm, tsm);
941  mul_m3_v3(itsm, nor);
942  normalize_v3(nor);
943 
944  /* save back the values */
945  normal_compress(&result[offset], nor, normal_swizzle);
946  }
947 
948  /* garbage collection */
949  MEM_freeN(triangles);
950 
951  if (me_eval) {
952  BKE_id_free(NULL, me_eval);
953  }
954 }
955 
956 void RE_bake_normal_world_to_object(const BakePixel pixel_array[],
957  const size_t pixels_num,
958  const int depth,
959  float result[],
960  struct Object *ob,
961  const eBakeNormalSwizzle normal_swizzle[3])
962 {
963  size_t i;
964  float iobmat[4][4];
965 
966  invert_m4_m4(iobmat, ob->obmat);
967 
968  for (i = 0; i < pixels_num; i++) {
969  size_t offset;
970  float nor[3];
971 
972  if (pixel_array[i].primitive_id == -1) {
973  continue;
974  }
975 
976  offset = i * depth;
978 
979  /* rotates only without translation */
980  mul_mat3_m4_v3(iobmat, nor);
981  normalize_v3(nor);
982 
983  /* save back the values */
984  normal_compress(&result[offset], nor, normal_swizzle);
985  }
986 }
987 
988 void RE_bake_normal_world_to_world(const BakePixel pixel_array[],
989  const size_t pixels_num,
990  const int depth,
991  float result[],
992  const eBakeNormalSwizzle normal_swizzle[3])
993 {
994  size_t i;
995 
996  for (i = 0; i < pixels_num; i++) {
997  size_t offset;
998  float nor[3];
999 
1000  if (pixel_array[i].primitive_id == -1) {
1001  continue;
1002  }
1003 
1004  offset = i * depth;
1006 
1007  /* save back the values */
1008  normal_compress(&result[offset], nor, normal_swizzle);
1009  }
1010 }
1011 
1012 void RE_bake_ibuf_clear(Image *image, const bool is_tangent)
1013 {
1014  ImBuf *ibuf;
1015  void *lock;
1016 
1017  const float vec_alpha[4] = {0.0f, 0.0f, 0.0f, 0.0f};
1018  const float vec_solid[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1019  const float nor_alpha[4] = {0.5f, 0.5f, 1.0f, 0.0f};
1020  const float nor_solid[4] = {0.5f, 0.5f, 1.0f, 1.0f};
1021 
1023  BLI_assert(ibuf);
1024 
1025  if (is_tangent) {
1026  IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? nor_alpha : nor_solid);
1027  }
1028  else {
1029  IMB_rectfill(ibuf, (ibuf->planes == R_IMF_PLANES_RGBA) ? vec_alpha : vec_solid);
1030  }
1031 
1033 }
1034 
1035 /* ************************************************************* */
1036 
1037 int RE_pass_depth(const eScenePassType pass_type)
1038 {
1039  /* IMB_buffer_byte_from_float assumes 4 channels
1040  * making it work for now - XXX */
1041  return 4;
1042 
1043  switch (pass_type) {
1044  case SCE_PASS_Z:
1045  case SCE_PASS_AO:
1046  case SCE_PASS_MIST: {
1047  return 1;
1048  }
1049  case SCE_PASS_UV: {
1050  return 2;
1051  }
1052  case SCE_PASS_COMBINED:
1053  case SCE_PASS_SHADOW:
1054  case SCE_PASS_POSITION:
1055  case SCE_PASS_NORMAL:
1056  case SCE_PASS_VECTOR:
1057  case SCE_PASS_INDEXOB: /* XXX double check */
1058  case SCE_PASS_EMIT:
1059  case SCE_PASS_ENVIRONMENT:
1060  case SCE_PASS_INDEXMA:
1066  case SCE_PASS_GLOSSY_COLOR:
1069  case SCE_PASS_TRANSM_COLOR:
1073  default: {
1074  return 3;
1075  }
1076  }
1077 }
typedef float(TangentPoint)[2]
void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data)
Definition: bvhutils.cc:1410
@ BVHTREE_FROM_LOOPTRI
Definition: BKE_bvhutils.h:73
BVHTree * BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, const struct Mesh *mesh, BVHCacheType bvh_cache_type, int tree_type)
Definition: bvhutils.cc:1213
CustomData interface, see also DNA_customdata_types.h.
void * CustomData_get_layer_n(const struct CustomData *data, int type, int n)
void * CustomData_get_layer(const struct CustomData *data, int type)
int CustomData_get_named_layer(const struct CustomData *data, int type, const char *name)
void BKE_image_release_ibuf(struct Image *ima, struct ImBuf *ibuf, void *lock)
struct ImBuf * BKE_image_acquire_ibuf(struct Image *ima, struct ImageUser *iuser, void **r_lock)
void BKE_id_free(struct Main *bmain, void *idv)
struct Mesh * BKE_mesh_copy_for_eval(const struct Mesh *source, bool reference)
const float(* BKE_mesh_poly_normals_ensure(const struct Mesh *mesh))[3]
const float(* BKE_mesh_vertex_normals_ensure(const struct Mesh *mesh))[3]
bool BKE_mesh_poly_normals_are_dirty(const struct Mesh *mesh)
void BKE_mesh_recalc_looptri_with_normals(const struct MLoop *mloop, const struct MPoly *mpoly, const struct MVert *mvert, int totloop, int totpoly, struct MLoopTri *mlooptri, const float(*poly_normals)[3])
void BKE_mesh_recalc_looptri(const struct MLoop *mloop, const struct MPoly *mpoly, const struct MVert *mvert, int totloop, int totpoly, struct MLoopTri *mlooptri)
void BKE_mesh_calc_poly_normal(const struct MPoly *mpoly, const struct MLoop *loopstart, const struct MVert *mvarray, float r_no[3])
void BKE_mesh_calc_normals_split(struct Mesh *mesh)
Definition: mesh.cc:1911
void BKE_mesh_ensure_normals_for_display(struct Mesh *mesh)
const struct MLoopTri * BKE_mesh_runtime_looptri_ensure(const struct Mesh *mesh)
void BKE_mesh_calc_loop_tangents(struct Mesh *me_eval, bool calc_active_tangent, const char(*tangent_names)[MAX_NAME], int tangent_names_len)
Definition: mesh_tangent.c:712
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BVH_RAYCAST_DIST_MAX
Definition: BLI_kdopbvh.h:89
int BLI_bvhtree_ray_cast(BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
Definition: BLI_kdopbvh.c:1942
MINLINE int clamp_i(int value, int min, int max)
void interp_barycentric_tri_v3(float data[3][3], float u, float v, float res[3])
Definition: math_geom.c:4501
MINLINE int poly_to_tri_count(int poly_count, int corner_count)
MINLINE void axis_dominant_v3(int *r_axis_a, int *r_axis_b, const float axis[3])
void mul_m3_v3(const float M[3][3], float r[3])
Definition: math_matrix.c:926
void mul_mat3_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:790
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1287
void mul_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:729
bool invert_m3_m3(float R[3][3], const float A[3][3])
Definition: math_matrix.c:1180
void mul_transposed_mat3_m4_v3(const float M[4][4], float r[3])
Definition: math_matrix.c:946
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:739
void mul_v3_mat3_m4v3(float r[3], const float M[4][4], const float v[3])
Definition: math_matrix.c:800
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void copy_v2_fl2(float v[2], float x, float y)
MINLINE void copy_v4_fl4(float v[4], float x, float y, float z, float w)
MINLINE float normalize_v3(float r[3])
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void copy_v3_fl3(float v[3], float x, float y, float z)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void negate_v3(float r[3])
MINLINE void mul_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void add_v3_v3(float r[3], const float a[3])
@ CD_MVERT
@ CD_MLOOPUV
@ CD_TANGENT
@ ME_SMOOTH
eBakeNormalSwizzle
@ R_BAKE_ADJACENT_FACES
@ R_BAKE_EXTEND
#define R_IMF_PLANES_RGBA
eScenePassType
@ SCE_PASS_NORMAL
@ SCE_PASS_GLOSSY_DIRECT
@ SCE_PASS_AO
@ SCE_PASS_DIFFUSE_COLOR
@ SCE_PASS_POSITION
@ SCE_PASS_UV
@ SCE_PASS_SUBSURFACE_INDIRECT
@ SCE_PASS_TRANSM_DIRECT
@ SCE_PASS_SUBSURFACE_COLOR
@ SCE_PASS_GLOSSY_COLOR
@ SCE_PASS_DIFFUSE_DIRECT
@ SCE_PASS_GLOSSY_INDIRECT
@ SCE_PASS_INDEXMA
@ SCE_PASS_INDEXOB
@ SCE_PASS_TRANSM_INDIRECT
@ SCE_PASS_COMBINED
@ SCE_PASS_Z
@ SCE_PASS_VECTOR
@ SCE_PASS_DIFFUSE_INDIRECT
@ SCE_PASS_SUBSURFACE_DIRECT
@ SCE_PASS_SHADOW
@ SCE_PASS_TRANSM_COLOR
@ SCE_PASS_MIST
@ SCE_PASS_EMIT
@ SCE_PASS_ENVIRONMENT
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei width
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
#define FILTER_MASK_USED
Definition: IMB_imbuf.h:459
void IMB_rectfill_alpha(struct ImBuf *ibuf, float value)
Definition: rectop.c:1262
void IMB_filter_extend(struct ImBuf *ibuf, char *mask, int filter)
Definition: filter.c:406
void IMB_rectfill(struct ImBuf *drect, const float col[4])
Definition: rectop.c:1050
Contains defines and structs used throughout the imbuf module.
Read Guarded memory(de)allocation.
volatile int lock
bool RE_bake_pixels_populate_from_objects(struct Mesh *me_low, BakePixel pixel_array_from[], BakePixel pixel_array_to[], BakeHighPolyData highpoly[], const int tot_highpoly, const size_t pixels_num, const bool is_custom_cage, const float cage_extrusion, const float max_ray_distance, float mat_low[4][4], float mat_cage[4][4], struct Mesh *me_cage)
Definition: bake.c:535
struct TriTessFace TriTessFace
int RE_pass_depth(const eScenePassType pass_type)
Definition: bake.c:1037
void RE_bake_pixels_populate(Mesh *me, BakePixel pixel_array[], const size_t pixels_num, const BakeTargets *targets, const char *uv_layer)
Definition: bake.c:706
static void barycentric_differentials_from_position(const float co[3], const float v1[3], const float v2[3], const float v3[3], const float dxco[3], const float dyco[3], const float facenor[3], const bool differentials, float *u, float *v, float *dx_u, float *dx_v, float *dy_u, float *dy_v)
Definition: bake.c:273
void RE_bake_margin(ImBuf *ibuf, char *mask, const int margin, const char margin_type, Mesh const *me, char const *uv_layer, const float uv_offset[2])
Definition: bake.c:144
static void calc_point_from_barycentric_cage(TriTessFace *triangles_low, TriTessFace *triangles_cage, const float mat_low[4][4], const float mat_cage[4][4], int primitive_id, float u, float v, float r_co[3], float r_dir[3])
Definition: bake.c:177
static void store_bake_pixel(void *handle, int x, int y, float u, float v)
Definition: bake.c:104
void RE_bake_ibuf_clear(Image *image, const bool is_tangent)
Definition: bake.c:1012
static void calc_point_from_barycentric_extrusion(TriTessFace *triangles, const float mat[4][4], const float imat[4][4], int primitive_id, float u, float v, float cage_extrusion, float r_co[3], float r_dir[3], const bool is_cage)
Definition: bake.c:220
static TriTessFace * mesh_calc_tri_tessface(Mesh *me, bool tangent, Mesh *me_eval)
Definition: bake.c:451
void RE_bake_normal_world_to_world(const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], const eBakeNormalSwizzle normal_swizzle[3])
Definition: bake.c:988
static void normal_compress(float out[3], const float in[3], const eBakeNormalSwizzle normal_swizzle[3])
Definition: bake.c:791
void RE_bake_normal_world_to_object(const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], struct Object *ob, const eBakeNormalSwizzle normal_swizzle[3])
Definition: bake.c:956
static bool cast_ray_highpoly(BVHTreeFromMesh *treeData, TriTessFace *triangle_low, TriTessFace *triangles[], BakePixel *pixel_array_low, BakePixel *pixel_array, const float mat_low[4][4], BakeHighPolyData *highpoly, const float co[3], const float dir[3], const int pixel_id, const int tot_highpoly, const float max_ray_distance)
Definition: bake.c:318
struct TSpace TSpace
struct BakeDataZSpan BakeDataZSpan
void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t pixels_num, char *mask)
Definition: bake.c:129
void RE_bake_normal_world_to_tangent(const BakePixel pixel_array[], const size_t pixels_num, const int depth, float result[], Mesh *me, const eBakeNormalSwizzle normal_swizzle[3], float mat[4][4])
Definition: bake.c:831
static void bake_differentials(BakeDataZSpan *bd, const float *uv1, const float *uv2, const float *uv3)
Definition: bake.c:681
static void raycast_callback(void *userdata, int index, const BVHTreeRay *ray, BVHTreeRayHit *UNUSED(hit))
ATTR_WARN_UNUSED_RESULT const BMVert * v2
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define A
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
void * tree
depth_tx normal_tx diffuse_light_tx specular_light_tx volume_light_tx environment_tx ambient_occlusion_tx aov_value_tx in_weight_img image(1, GPU_R32F, Qualifier::WRITE, ImageType::FLOAT_2D_ARRAY, "out_weight_img") .image(3
uint nor
#define UINT_MAX
Definition: hash_md5.c:43
IconTextureDrawCall normal
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:31
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
#define fabsf(x)
Definition: metal/compat.h:219
static unsigned a[3]
Definition: RandGen.cpp:78
double sign(double arg)
Definition: utility.h:250
void zbuf_alloc_span(ZSpan *zspan, int rectx, int recty, float clipcrop)
void zbuf_free_span(ZSpan *zspan)
T distance_squared(const vec_base< T, Size > &a, const vec_base< T, Size > &b)
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
MutableSpan< float3 > tangents
MutableSpan< float3 > normals
ZSpan * zspan
Definition: bake.c:82
BakeImage * bk_image
Definition: bake.c:81
float dv_dx
Definition: bake.c:84
float dv_dy
Definition: bake.c:84
float du_dy
Definition: bake.c:83
int primitive_id
Definition: bake.c:80
float du_dx
Definition: bake.c:83
BakePixel * pixel_array
Definition: bake.c:79
struct Mesh * me
Definition: RE_bake.h:63
int height
Definition: RE_bake.h:27
size_t offset
Definition: RE_bake.h:28
struct Image * image
Definition: RE_bake.h:23
float uv_offset[2]
Definition: RE_bake.h:25
int width
Definition: RE_bake.h:26
float dv_dx
Definition: RE_bake.h:57
float du_dx
Definition: RE_bake.h:56
int seed
Definition: RE_bake.h:54
float du_dy
Definition: RE_bake.h:56
float uv[2]
Definition: RE_bake.h:55
float dv_dy
Definition: RE_bake.h:57
int object_id
Definition: RE_bake.h:53
int primitive_id
Definition: RE_bake.h:53
struct Image ** material_to_image
Definition: RE_bake.h:40
int materials_num
Definition: RE_bake.h:41
int images_num
Definition: RE_bake.h:37
BakeImage * images
Definition: RE_bake.h:36
unsigned char planes
unsigned int poly
unsigned int tri[3]
unsigned int v
short mat_nr
float co[3]
CustomData vdata
struct MVert * mvert
struct MLoop * mloop
int totpoly
int totloop
struct MPoly * mpoly
CustomData ldata
float obmat[4][4]
Definition: bake.c:90
float sign
Definition: bake.c:92
float tangent[3]
Definition: bake.c:91
const TSpace * tspace[3]
Definition: bake.c:98
bool is_smooth
Definition: bake.c:101
const float * loop_normal[3]
Definition: bake.c:99
const float * vert_normals[3]
Definition: bake.c:97
float normal[3]
Definition: bake.c:100
const MVert * mverts[3]
Definition: bake.c:96
Definition: zbuf.h:14
void RE_generate_texturemargin_adjacentfaces(ImBuf *ibuf, char *mask, const int margin, const Mesh *me, char const *uv_layer, const float uv_offset[2])
void zspan_scanconvert(ZSpan *zspan, void *handle, float *v1, float *v2, float *v3, void(*func)(void *, int, int, float, float))
Definition: zbuf.c:159