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