Changeset View
Changeset View
Standalone View
Standalone View
source/blender/render/intern/source/bake_api.c
| Show First 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | |||||
| } TriTessFace; | } TriTessFace; | ||||
| static void store_bake_pixel(void *handle, int x, int y, float u, float v) | static void store_bake_pixel(void *handle, int x, int y, float u, float v) | ||||
| { | { | ||||
| BakeDataZSpan *bd = (BakeDataZSpan *)handle; | BakeDataZSpan *bd = (BakeDataZSpan *)handle; | ||||
| BakePixel *pixel; | BakePixel *pixel; | ||||
| const int width = bd->bk_image->width; | const int width = bd->bk_image->width; | ||||
| const int offset = bd->bk_image->offset; | const size_t offset = bd->bk_image->offset; | ||||
| const int i = offset + y * width + x; | const int i = offset + y * width + x; | ||||
| pixel = &bd->pixel_array[i]; | pixel = &bd->pixel_array[i]; | ||||
| 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; | ||||
| } | } | ||||
| void RE_bake_mask_fill(const BakePixel pixel_array[], const int num_pixels, char *mask) | void RE_bake_mask_fill(const BakePixel pixel_array[], const size_t num_pixels, char *mask) | ||||
| { | { | ||||
| int i; | size_t i; | ||||
| if (!mask) | if (!mask) | ||||
| return; | return; | ||||
| /* only extend to pixels outside the mask area */ | /* only extend to pixels outside the mask area */ | ||||
| for (i = 0; i < num_pixels; i++) { | for (i = 0; i < num_pixels; i++) { | ||||
| if (pixel_array[i].primitive_id != -1) { | if (pixel_array[i].primitive_id != -1) { | ||||
| mask[i] = FILTER_MASK_USED; | mask[i] = FILTER_MASK_USED; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 285 Lines • ▼ Show 20 Lines | for (i = 0; i < me->totface; i++) { | ||||
| } | } | ||||
| } | } | ||||
| 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[], | ||||
| BakeHighPolyData highpoly[], const int tot_highpoly, const int 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) | ||||
| { | { | ||||
| int 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; | ||||
| bool result = true; | bool result = true; | ||||
| DerivedMesh *dm_low = NULL; | DerivedMesh *dm_low = NULL; | ||||
| DerivedMesh **dm_highpoly; | DerivedMesh **dm_highpoly; | ||||
| ▲ Show 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | static void bake_differentials(BakeDataZSpan *bd, const float *uv1, const float *uv2, const float *uv3) | ||||
| else { | else { | ||||
| bd->du_dx = bd->du_dy = 0.0f; | bd->du_dx = bd->du_dy = 0.0f; | ||||
| bd->dv_dx = bd->dv_dy = 0.0f; | bd->dv_dx = bd->dv_dy = 0.0f; | ||||
| } | } | ||||
| } | } | ||||
| void RE_bake_pixels_populate( | void RE_bake_pixels_populate( | ||||
| Mesh *me, BakePixel pixel_array[], | Mesh *me, BakePixel pixel_array[], | ||||
| const int num_pixels, const BakeImages *bake_images, const char *uv_layer) | const size_t num_pixels, const BakeImages *bake_images, const char *uv_layer) | ||||
| { | { | ||||
| BakeDataZSpan bd; | BakeDataZSpan bd; | ||||
| int i, a; | size_t i; | ||||
| int p_id; | int a, p_id; | ||||
| MTFace *mtface; | MTFace *mtface; | ||||
| MFace *mface; | MFace *mface; | ||||
| /* we can't bake in edit mode */ | /* we can't bake in edit mode */ | ||||
| if (me->edit_btmesh) | if (me->edit_btmesh) | ||||
| return; | return; | ||||
| ▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Lines | for (i = 0; i < 3; i++) { | ||||
| out[i] = sign * in[index] / 2.0f + 0.5f + 1e-5f; | out[i] = sign * in[index] / 2.0f + 0.5f + 1e-5f; | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| * This function converts an object space normal map to a tangent space normal map for a given low poly mesh | * This function converts an object space normal map to a tangent space normal map for a given low poly mesh | ||||
| */ | */ | ||||
| void RE_bake_normal_world_to_tangent( | void RE_bake_normal_world_to_tangent( | ||||
| const BakePixel pixel_array[], const int num_pixels, const int depth, | const BakePixel pixel_array[], const size_t num_pixels, const int depth, | ||||
| float result[], Mesh *me, const BakeNormalSwizzle normal_swizzle[3], | float result[], Mesh *me, const BakeNormalSwizzle normal_swizzle[3], | ||||
| float mat[4][4]) | float mat[4][4]) | ||||
| { | { | ||||
| int i; | size_t i; | ||||
| TriTessFace *triangles; | TriTessFace *triangles; | ||||
| DerivedMesh *dm = CDDM_from_mesh(me); | DerivedMesh *dm = CDDM_from_mesh(me); | ||||
| triangles = MEM_mallocN(sizeof(TriTessFace) * (me->totface * 2), "MVerts Mesh"); | triangles = MEM_mallocN(sizeof(TriTessFace) * (me->totface * 2), "MVerts Mesh"); | ||||
| mesh_calc_tri_tessface(triangles, me, true, dm); | mesh_calc_tri_tessface(triangles, me, true, dm); | ||||
| Show All 10 Lines | for (i = 0; i < num_pixels; i++) { | ||||
| float normal[3]; | float normal[3]; | ||||
| float binormal[3]; | float binormal[3]; | ||||
| float sign; | float sign; | ||||
| float u, v, w; | float u, v, w; | ||||
| float tsm[3][3]; /* tangent space matrix */ | float tsm[3][3]; /* tangent space matrix */ | ||||
| float itsm[3][3]; | float itsm[3][3]; | ||||
| int offset; | size_t offset; | ||||
| float nor[3]; /* texture normal */ | float nor[3]; /* texture normal */ | ||||
| bool is_smooth; | bool is_smooth; | ||||
| int primitive_id = pixel_array[i].primitive_id; | int primitive_id = pixel_array[i].primitive_id; | ||||
| offset = i * depth; | offset = i * depth; | ||||
| ▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | void RE_bake_normal_world_to_tangent( | ||||
| /* garbage collection */ | /* garbage collection */ | ||||
| MEM_freeN(triangles); | MEM_freeN(triangles); | ||||
| if (dm) | if (dm) | ||||
| dm->release(dm); | dm->release(dm); | ||||
| } | } | ||||
| void RE_bake_normal_world_to_object( | void RE_bake_normal_world_to_object( | ||||
| const BakePixel pixel_array[], const int num_pixels, const int depth, | const BakePixel pixel_array[], const size_t num_pixels, const int depth, | ||||
| float result[], struct Object *ob, const BakeNormalSwizzle normal_swizzle[3]) | float result[], struct Object *ob, const BakeNormalSwizzle normal_swizzle[3]) | ||||
| { | { | ||||
| int i; | size_t i; | ||||
| float iobmat[4][4]; | float iobmat[4][4]; | ||||
| invert_m4_m4(iobmat, ob->obmat); | invert_m4_m4(iobmat, ob->obmat); | ||||
| for (i = 0; i < num_pixels; i++) { | for (i = 0; i < num_pixels; i++) { | ||||
| int offset; | size_t offset; | ||||
| float nor[3]; | float nor[3]; | ||||
| if (pixel_array[i].primitive_id == -1) | if (pixel_array[i].primitive_id == -1) | ||||
| continue; | continue; | ||||
| offset = i * depth; | offset = i * depth; | ||||
| normal_uncompress(nor, &result[offset]); | normal_uncompress(nor, &result[offset]); | ||||
| /* rotates only without translation */ | /* rotates only without translation */ | ||||
| mul_mat3_m4_v3(iobmat, nor); | mul_mat3_m4_v3(iobmat, nor); | ||||
| normalize_v3(nor); | normalize_v3(nor); | ||||
| /* save back the values */ | /* save back the values */ | ||||
| normal_compress(&result[offset], nor, normal_swizzle); | normal_compress(&result[offset], nor, normal_swizzle); | ||||
| } | } | ||||
| } | } | ||||
| void RE_bake_normal_world_to_world( | void RE_bake_normal_world_to_world( | ||||
| const BakePixel pixel_array[], const int num_pixels, const int depth, | const BakePixel pixel_array[], const size_t num_pixels, const int depth, | ||||
| float result[], const BakeNormalSwizzle normal_swizzle[3]) | float result[], const BakeNormalSwizzle normal_swizzle[3]) | ||||
| { | { | ||||
| int i; | size_t i; | ||||
| for (i = 0; i < num_pixels; i++) { | for (i = 0; i < num_pixels; i++) { | ||||
| int offset; | size_t offset; | ||||
| float nor[3]; | float nor[3]; | ||||
| if (pixel_array[i].primitive_id == -1) | if (pixel_array[i].primitive_id == -1) | ||||
| continue; | continue; | ||||
| offset = i * depth; | offset = i * depth; | ||||
| normal_uncompress(nor, &result[offset]); | normal_uncompress(nor, &result[offset]); | ||||
| Show All 23 Lines | void RE_bake_ibuf_clear(Image *image, const bool is_tangent) | ||||
| BKE_image_release_ibuf(image, ibuf, lock); | BKE_image_release_ibuf(image, ibuf, lock); | ||||
| } | } | ||||
| /* ************************************************************* */ | /* ************************************************************* */ | ||||
| /** | /** | ||||
| * not the real UV, but the internal per-face UV instead | * not the real UV, but the internal per-face UV instead | ||||
| * I'm using it to test if everything is correct */ | * I'm using it to test if everything is correct */ | ||||
| static bool bake_uv(const BakePixel pixel_array[], const int num_pixels, const int depth, float result[]) | static bool bake_uv(const BakePixel pixel_array[], const size_t num_pixels, const int depth, float result[]) | ||||
| { | { | ||||
| int i; | size_t i; | ||||
| for (i=0; i < num_pixels; i++) { | for (i=0; i < num_pixels; i++) { | ||||
| int offset = i * depth; | size_t offset = i * depth; | ||||
| copy_v2_v2(&result[offset], pixel_array[i].uv); | copy_v2_v2(&result[offset], pixel_array[i].uv); | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| bool RE_bake_internal( | bool RE_bake_internal( | ||||
| Render *UNUSED(re), Object *UNUSED(object), const BakePixel pixel_array[], | Render *UNUSED(re), Object *UNUSED(object), const BakePixel pixel_array[], | ||||
| const int num_pixels, const int depth, const ScenePassType pass_type, float result[]) | const size_t width, const size_t height, const int depth, const ScenePassType pass_type, float result[]) | ||||
| { | { | ||||
| const size_t num_pixels = width * height; | |||||
| switch (pass_type) { | switch (pass_type) { | ||||
| case SCE_PASS_UV: | case SCE_PASS_UV: | ||||
| { | { | ||||
| return bake_uv(pixel_array, num_pixels, depth, result); | return bake_uv(pixel_array, num_pixels, depth, result); | ||||
| break; | break; | ||||
| } | } | ||||
| default: | default: | ||||
| break; | break; | ||||
| ▲ Show 20 Lines • Show All 57 Lines • Show Last 20 Lines | |||||