Changeset View
Changeset View
Standalone View
Standalone View
source/blender/render/intern/source/bake_api.c
| Show First 20 Lines • Show All 126 Lines • ▼ Show 20 Lines | static void store_bake_pixel(void *handle, int x, int y, float u, float v) | ||||
| pixel->primitive_id = bd->primitive_id; | pixel->primitive_id = bd->primitive_id; | ||||
| copy_v2_fl2(pixel->uv, u, v); | copy_v2_fl2(pixel->uv, u, v); | ||||
| pixel->du_dx = bd->du_dx; | pixel->du_dx = bd->du_dx; | ||||
| pixel->du_dy = bd->du_dy; | pixel->du_dy = bd->du_dy; | ||||
| pixel->dv_dx = bd->dv_dx; | pixel->dv_dx = bd->dv_dx; | ||||
| pixel->dv_dy = bd->dv_dy; | pixel->dv_dy = bd->dv_dy; | ||||
| pixel->object_id = 0; | |||||
| } | } | ||||
| void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t num_pixels, char *mask) | void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t num_pixels, char *mask) | ||||
| { | { | ||||
| size_t i; | size_t i; | ||||
| if (!mask) | if (!mask) | ||||
| return; | return; | ||||
| ▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | resolve_tri_uv_v3(r_uv, co, | ||||
| triangle->mverts[2]->co); | triangle->mverts[2]->co); | ||||
| *r_primitive_id = index; | *r_primitive_id = index; | ||||
| } | } | ||||
| /** | /** | ||||
| * This function populates pixel_array and returns TRUE if things are correct | * This function populates pixel_array and returns TRUE if things are correct | ||||
| */ | */ | ||||
| static bool cast_ray_highpoly( | static bool cast_ray_highpoly( | ||||
| BVHTreeFromMesh *treeData, TriTessFace *triangles[], BakeHighPolyData *highpoly, | BVHTreeFromMesh *treeData, TriTessFace *triangles[], BakePixel *pixel_array, BakeHighPolyData *highpoly, | ||||
| const float co[3], const float dir[3], const int pixel_id, const int tot_highpoly, | const float co[3], const float dir[3], const int pixel_id, const int tot_highpoly, | ||||
| const float du_dx, const float du_dy, const float dv_dx, const float dv_dy) | const float du_dx, const float du_dy, const float dv_dx, const float dv_dy) | ||||
| { | { | ||||
| int i; | int i; | ||||
| int primitive_id = -1; | int primitive_id = -1; | ||||
| float uv[2]; | float uv[2]; | ||||
| int hit_mesh = -1; | int hit_mesh = -1; | ||||
| float hit_distance = FLT_MAX; | float hit_distance = FLT_MAX; | ||||
| Show All 34 Lines | if (hits[i].index != -1) { | ||||
| if (distance < hit_distance) { | if (distance < hit_distance) { | ||||
| hit_mesh = i; | hit_mesh = i; | ||||
| hit_distance = distance; | hit_distance = distance; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| for (i = 0; i < tot_highpoly; i++) { | if (hit_mesh != -1) { | ||||
| if (hit_mesh == i) { | calc_barycentric_from_point(triangles[hit_mesh], hits[hit_mesh].index, hits[hit_mesh].co, &primitive_id, uv); | ||||
| calc_barycentric_from_point(triangles[i], hits[i].index, hits[i].co, &primitive_id, uv); | pixel_array[pixel_id].primitive_id = primitive_id; | ||||
| highpoly[i].pixel_array[pixel_id].primitive_id = primitive_id; | pixel_array[pixel_id].object_id = hit_mesh; | ||||
| copy_v2_v2(highpoly[i].pixel_array[pixel_id].uv, uv); | copy_v2_v2(pixel_array[pixel_id].uv, uv); | ||||
| /* the differentials are relative to the UV/image space, so the highpoly differentials | /* the differentials are relative to the UV/image space, so the highpoly differentials | ||||
| * are the same as the low poly differentials */ | * are the same as the low poly differentials */ | ||||
| highpoly[i].pixel_array[pixel_id].du_dx = du_dx; | pixel_array[pixel_id].du_dx = du_dx; | ||||
| highpoly[i].pixel_array[pixel_id].du_dy = du_dy; | pixel_array[pixel_id].du_dy = du_dy; | ||||
| highpoly[i].pixel_array[pixel_id].dv_dx = dv_dx; | pixel_array[pixel_id].dv_dx = dv_dx; | ||||
| highpoly[i].pixel_array[pixel_id].dv_dy = dv_dy; | pixel_array[pixel_id].dv_dy = dv_dy; | ||||
| } | } | ||||
| else { | else { | ||||
| highpoly[i].pixel_array[pixel_id].primitive_id = -1; | pixel_array[pixel_id].primitive_id = -1; | ||||
| } | pixel_array[pixel_id].object_id = -1; | ||||
| } | } | ||||
| MEM_freeN(hits); | MEM_freeN(hits); | ||||
| return hit_mesh != -1; | return hit_mesh != -1; | ||||
| } | } | ||||
| /** | /** | ||||
| * This function populates an array of verts for the triangles of a mesh | * This function populates an array of verts for the triangles of a mesh | ||||
| ▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | if (mf->v4 != 0) { | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| BLI_assert(p_id < me->totface * 2); | BLI_assert(p_id < me->totface * 2); | ||||
| } | } | ||||
| bool RE_bake_pixels_populate_from_objects( | bool RE_bake_pixels_populate_from_objects( | ||||
| struct Mesh *me_low, BakePixel pixel_array_from[], | 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, | BakeHighPolyData highpoly[], const int tot_highpoly, const size_t num_pixels, const bool is_custom_cage, | ||||
| const float cage_extrusion, float mat_low[4][4], float mat_cage[4][4], struct Mesh *me_cage) | const float cage_extrusion, float mat_low[4][4], float mat_cage[4][4], struct Mesh *me_cage) | ||||
| { | { | ||||
| size_t i; | size_t i; | ||||
| int primitive_id; | int primitive_id; | ||||
| float u, v; | float u, v; | ||||
| float imat_low[4][4]; | float imat_low[4][4]; | ||||
| bool is_cage = me_cage != NULL; | bool is_cage = me_cage != NULL; | ||||
| ▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | bool RE_bake_pixels_populate_from_objects( | ||||
| for (i = 0; i < num_pixels; i++) { | for (i = 0; i < num_pixels; i++) { | ||||
| float co[3]; | float co[3]; | ||||
| float dir[3]; | float dir[3]; | ||||
| primitive_id = pixel_array_from[i].primitive_id; | primitive_id = pixel_array_from[i].primitive_id; | ||||
| if (primitive_id == -1) { | if (primitive_id == -1) { | ||||
| int j; | pixel_array_to[i].primitive_id = -1; | ||||
| for (j = 0; j < tot_highpoly; j++) { | |||||
| highpoly[j].pixel_array[i].primitive_id = -1; | |||||
| } | |||||
| continue; | continue; | ||||
| } | } | ||||
| u = pixel_array_from[i].uv[0]; | u = pixel_array_from[i].uv[0]; | ||||
| v = pixel_array_from[i].uv[1]; | v = pixel_array_from[i].uv[1]; | ||||
| /* calculate from low poly mesh cage */ | /* calculate from low poly mesh cage */ | ||||
| if (is_custom_cage) { | if (is_custom_cage) { | ||||
| calc_point_from_barycentric_cage(tris_low, tris_cage, mat_low, mat_cage, primitive_id, u, v, co, dir); | calc_point_from_barycentric_cage(tris_low, tris_cage, mat_low, mat_cage, primitive_id, u, v, co, dir); | ||||
| } | } | ||||
| else if (is_cage) { | else if (is_cage) { | ||||
| calc_point_from_barycentric_extrusion(tris_cage, mat_low, imat_low, primitive_id, u, v, cage_extrusion, co, dir, true); | calc_point_from_barycentric_extrusion(tris_cage, mat_low, imat_low, primitive_id, u, v, cage_extrusion, co, dir, true); | ||||
| } | } | ||||
| else { | else { | ||||
| calc_point_from_barycentric_extrusion(tris_low, mat_low, imat_low, primitive_id, u, v, cage_extrusion, co, dir, false); | calc_point_from_barycentric_extrusion(tris_low, mat_low, imat_low, primitive_id, u, v, cage_extrusion, co, dir, false); | ||||
| } | } | ||||
| /* cast ray */ | /* cast ray */ | ||||
| if (!cast_ray_highpoly(treeData, tris_high, highpoly, co, dir, i, tot_highpoly, | if (!cast_ray_highpoly(treeData, tris_high, pixel_array_to, highpoly, co, dir, i, tot_highpoly, | ||||
| pixel_array_from[i].du_dx, pixel_array_from[i].du_dy, | pixel_array_from[i].du_dx, pixel_array_from[i].du_dy, | ||||
| pixel_array_from[i].dv_dx, pixel_array_from[i].dv_dy)) { | pixel_array_from[i].dv_dx, pixel_array_from[i].dv_dy)) { | ||||
| /* if it fails mask out the original pixel array */ | /* if it fails mask out the original pixel array */ | ||||
| pixel_array_from[i].primitive_id = -1; | pixel_array_from[i].primitive_id = -1; | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 449 Lines • Show Last 20 Lines | |||||