137 for (i = 0; i < pixels_num; i++) {
138 if (pixel_array[i].primitive_id != -1) {
147 const char margin_type,
149 char const *uv_layer,
150 const float uv_offset[2])
153 switch (margin_type) {
179 const float mat_low[4][4],
180 const float mat_cage[4][4],
194 triangle[0] = &triangles_low[primitive_id];
195 triangle[1] = &triangles_cage[primitive_id];
197 for (i = 0; i < 2; i++) {
221 const float mat[4][4],
222 const float imat[4][4],
226 float cage_extrusion,
238 is_smooth = triangle->
is_smooth || is_cage;
279 const float facenor[3],
280 const bool differentials,
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];
298 float detsh = (t00 * t11 - t10 * t01);
299 detsh = (detsh != 0.0f) ? 1.0f / detsh : 0.0f;
305 *u = (v3[axis1] - co[axis1]) * t11 - (v3[axis2] - co[axis2]) * t10;
306 *
v = (v3[axis2] - co[axis2]) * t00 - (v3[axis1] - co[axis1]) * t01;
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;
323 const float mat_low[4][4],
328 const int tot_highpoly,
329 const float max_ray_distance)
333 float hit_distance_squared = max_ray_distance * max_ray_distance;
334 if (hit_distance_squared == 0.0f) {
336 hit_distance_squared = FLT_MAX;
342 for (i = 0; i < tot_highpoly; i++) {
343 float co_high[3], dir_high[3];
357 if (treeData[i].
tree) {
367 if (hits[i].index != -1) {
370 mul_v3_m4v3(hit_world, highpoly[i].obmat, hits[i].co);
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];
388 pixel_high->
seed = pixel_id;
396 float duco_low[3], dvco_low[3], dxco[3], dyco[3];
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);
440 pixel_array[pixel_id].
seed = 0;
444 return hit_mesh != -1;
464 looptri =
MEM_mallocN(
sizeof(*looptri) * tottri, __func__);
470 const bool calculate_normal = precomputed_normals ? false :
true;
472 if (precomputed_normals !=
NULL) {
494 for (i = 0; i < tottri; i++) {
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]];
518 if (calculate_normal) {
519 if (lt->
poly != mpoly_prev) {
521 mpoly_prev = lt->
poly;
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,
545 float mat_cage[4][4],
546 struct Mesh *me_cage)
551 float imat_low[4][4];
552 bool is_cage = me_cage !=
NULL;
568 me_highpoly =
MEM_mallocN(
sizeof(
Mesh *) * tot_highpoly,
"Highpoly Derived Meshes");
575 else if (is_custom_cage) {
585 for (i = 0; i < tot_highpoly; i++) {
588 me_highpoly[i] = highpoly[i].
me;
591 if (me_highpoly[i]->runtime.looptris.len != 0) {
596 printf(
"Baking: out of memory while creating BHVTree for object \"%s\"\n",
597 highpoly[i].ob->id.name + 2);
604 for (i = 0; i < pixels_num; i++) {
611 if (primitive_id == -1) {
616 u = pixel_array_from[i].
uv[0];
617 v = pixel_array_from[i].
uv[1];
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];
627 tris_cage, mat_low, imat_low, primitive_id, u,
v, cage_extrusion, co, dir,
true);
628 tri_low = &tris_cage[primitive_id];
632 tris_low, mat_low, imat_low, primitive_id, u,
v, cage_extrusion, co, dir,
false);
633 tri_low = &tris_low[primitive_id];
656 for (i = 0; i < tot_highpoly; i++) {
689 A = (uv2[0] - uv1[0]) * (uv3[1] - uv1[1]) - (uv3[0] - uv1[0]) * (uv2[1] - uv1[1]);
691 if (
fabsf(
A) > FLT_EPSILON) {
694 bd->
du_dx = (uv2[1] - uv3[1]) *
A;
695 bd->
dv_dx = (uv3[1] - uv1[1]) *
A;
697 bd->
du_dy = (uv3[0] - uv2[0]) *
A;
698 bd->
dv_dy = (uv1[0] - uv3[0]) *
A;
708 const size_t pixels_num,
710 const char *uv_layer)
713 if ((uv_layer ==
NULL) || (uv_layer[0] ==
'\0')) {
721 if (mloopuv ==
NULL) {
730 for (
int i = 0; i < pixels_num; i++) {
735 for (
int i = 0; i < targets->
images_num; i++) {
745 for (
int i = 0; i < tottri; i++) {
752 const int material_index = (materials_num) ?
clamp_i(mp->
mat_nr, 0, materials_num - 1) : 0;
754 for (
int image_id = 0; image_id < targets->
images_num; image_id++) {
762 for (
int a = 0;
a < 3;
a++) {
763 const float *uv = mloopuv[lt->
tri[
a]].
uv;
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);
781 for (
int i = 0; i < targets->
images_num; i++) {
795 const int swizzle_index[6] = {
803 const float swizzle_sign[6] = {
814 for (i = 0; i < 3; i++) {
818 sign = swizzle_sign[normal_swizzle[i]];
819 index = swizzle_index[normal_swizzle[i]];
827 out[i] =
sign * in[index] / 2.0f + 0.5f + 1e-5f;
832 const size_t pixels_num,
849 for (i = 0; i < pixels_num; i++) {
874 if (primitive_id == -1) {
884 triangle = &triangles[primitive_id];
887 for (j = 0; j < 3; j++) {
904 u = pixel_array[i].
uv[0];
905 v = pixel_array[i].
uv[1];
922 sign = (signs[0] * u + signs[1] *
v + signs[2] *
w) < 0 ? (-1.0f) : 1.0f;
957 const size_t pixels_num,
968 for (i = 0; i < pixels_num; i++) {
972 if (pixel_array[i].primitive_id == -1) {
989 const size_t pixels_num,
996 for (i = 0; i < pixels_num; i++) {
1000 if (pixel_array[i].primitive_id == -1) {
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};
1043 switch (pass_type) {
typedef float(TangentPoint)[2]
void free_bvhtree_from_mesh(struct BVHTreeFromMesh *data)
BVHTree * BKE_bvhtree_from_mesh_get(struct BVHTreeFromMesh *data, const struct Mesh *mesh, BVHCacheType bvh_cache_type, int tree_type)
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)
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)
#define BVH_RAYCAST_DIST_MAX
int BLI_bvhtree_ray_cast(BVHTree *tree, const float co[3], const float dir[3], float radius, BVHTreeRayHit *hit, BVHTree_RayCastCallback callback, void *userdata)
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])
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])
void mul_mat3_m4_v3(const float M[4][4], float r[3])
bool invert_m4_m4(float R[4][4], const float A[4][4])
void mul_m4_v3(const float M[4][4], float r[3])
bool invert_m3_m3(float R[3][3], const float A[3][3])
void mul_transposed_mat3_m4_v3(const float M[4][4], float r[3])
void mul_v3_m4v3(float r[3], const float M[4][4], const float v[3])
void mul_v3_mat3_m4v3(float r[3], const float M[4][4], const float v[3])
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])
#define R_IMF_PLANES_RGBA
@ SCE_PASS_SUBSURFACE_INDIRECT
@ SCE_PASS_SUBSURFACE_COLOR
@ SCE_PASS_DIFFUSE_DIRECT
@ SCE_PASS_GLOSSY_INDIRECT
@ SCE_PASS_TRANSM_INDIRECT
@ SCE_PASS_DIFFUSE_INDIRECT
@ SCE_PASS_SUBSURFACE_DIRECT
_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
void IMB_rectfill_alpha(struct ImBuf *ibuf, float value)
void IMB_filter_extend(struct ImBuf *ibuf, char *mask, int filter)
void IMB_rectfill(struct ImBuf *drect, const float col[4])
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 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)
struct TriTessFace TriTessFace
int RE_pass_depth(const eScenePassType pass_type)
void RE_bake_pixels_populate(Mesh *me, BakePixel pixel_array[], const size_t pixels_num, const BakeTargets *targets, const char *uv_layer)
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)
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])
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])
static void store_bake_pixel(void *handle, int x, int y, float u, float v)
void RE_bake_ibuf_clear(Image *image, const bool is_tangent)
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)
static TriTessFace * mesh_calc_tri_tessface(Mesh *me, bool tangent, Mesh *me_eval)
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])
static void normal_compress(float out[3], const float in[3], const eBakeNormalSwizzle normal_swizzle[3])
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])
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)
struct BakeDataZSpan BakeDataZSpan
void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t pixels_num, char *mask)
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])
static void bake_differentials(BakeDataZSpan *bd, const float *uv1, const float *uv2, const float *uv3)
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
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
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
IconTextureDrawCall normal
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
void(* MEM_freeN)(void *vmemh)
void *(* MEM_callocN)(size_t len, const char *str)
void *(* MEM_mallocN)(size_t len, const char *str)
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
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
struct Image ** material_to_image
const float * loop_normal[3]
const float * vert_normals[3]
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))