Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/object/object_bake_api.c
| Show First 20 Lines • Show All 563 Lines • ▼ Show 20 Lines | static int bake( | ||||
| char restrict_flag_cage = 0; | char restrict_flag_cage = 0; | ||||
| Mesh *me_low = NULL; | Mesh *me_low = NULL; | ||||
| Mesh *me_cage = NULL; | Mesh *me_cage = NULL; | ||||
| float *result = NULL; | float *result = NULL; | ||||
| BakePixel *pixel_array_low = NULL; | BakePixel *pixel_array_low = NULL; | ||||
| BakePixel *pixel_array_high = NULL; | |||||
| const bool is_save_internal = (save_mode == R_BAKE_SAVE_INTERNAL); | const bool is_save_internal = (save_mode == R_BAKE_SAVE_INTERNAL); | ||||
| const bool is_noncolor = is_noncolor_pass(pass_type); | const bool is_noncolor = is_noncolor_pass(pass_type); | ||||
| const int depth = RE_pass_depth(pass_type); | const int depth = RE_pass_depth(pass_type); | ||||
| BakeImages bake_images = {NULL}; | BakeImages bake_images = {NULL}; | ||||
| size_t num_pixels; | size_t num_pixels; | ||||
| ▲ Show 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | if (is_cage && custom_cage[0] != '\0') { | ||||
| else { | else { | ||||
| restrict_flag_cage = ob_cage->restrictflag; | restrict_flag_cage = ob_cage->restrictflag; | ||||
| ob_cage->restrictflag |= OB_RESTRICT_RENDER; | ob_cage->restrictflag |= OB_RESTRICT_RENDER; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| pixel_array_low = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels low poly"); | pixel_array_low = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels low poly"); | ||||
| pixel_array_high = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels high poly"); | |||||
sergey: Is it correct that both hi and lo poly arrays are the same size? | |||||
| result = MEM_callocN(sizeof(float) * depth * num_pixels, "bake return pixels"); | result = MEM_callocN(sizeof(float) * depth * num_pixels, "bake return pixels"); | ||||
| /* get the mesh as it arrives in the renderer */ | /* get the mesh as it arrives in the renderer */ | ||||
| me_low = BKE_mesh_new_from_object(bmain, scene, ob_low, 1, 2, 1, 0); | me_low = BKE_mesh_new_from_object(bmain, scene, ob_low, 1, 2, 1, 0); | ||||
| /* populate the pixel array with the face data */ | /* populate the pixel array with the face data */ | ||||
| if ((is_selected_to_active && (ob_cage == NULL) && is_cage) == false) | if ((is_selected_to_active && (ob_cage == NULL) && is_cage) == false) | ||||
| RE_bake_pixels_populate(me_low, pixel_array_low, num_pixels, &bake_images, uv_layer); | RE_bake_pixels_populate(me_low, pixel_array_low, num_pixels, &bake_images, uv_layer); | ||||
| ▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | for (link = selected_objects->first; link; link = link->next) { | ||||
| Object *ob_iter = link->ptr.data; | Object *ob_iter = link->ptr.data; | ||||
| if (ob_iter == ob_low) | if (ob_iter == ob_low) | ||||
| continue; | continue; | ||||
| /* initialize highpoly_data */ | /* initialize highpoly_data */ | ||||
| highpoly[i].ob = ob_iter; | highpoly[i].ob = ob_iter; | ||||
| highpoly[i].restrict_flag = ob_iter->restrictflag; | highpoly[i].restrict_flag = ob_iter->restrictflag; | ||||
| highpoly[i].pixel_array = MEM_mallocN(sizeof(BakePixel) * num_pixels, "bake pixels high poly"); | |||||
| /* triangulating so BVH returns the primitive_id that will be used for rendering */ | /* triangulating so BVH returns the primitive_id that will be used for rendering */ | ||||
| highpoly[i].tri_mod = ED_object_modifier_add( | highpoly[i].tri_mod = ED_object_modifier_add( | ||||
| reports, bmain, scene, highpoly[i].ob, | reports, bmain, scene, highpoly[i].ob, | ||||
| "TmpTriangulate", eModifierType_Triangulate); | "TmpTriangulate", eModifierType_Triangulate); | ||||
| tmd = (TriangulateModifierData *)highpoly[i].tri_mod; | tmd = (TriangulateModifierData *)highpoly[i].tri_mod; | ||||
| tmd->quad_method = MOD_TRIANGULATE_QUAD_FIXED; | tmd->quad_method = MOD_TRIANGULATE_QUAD_FIXED; | ||||
| tmd->ngon_method = MOD_TRIANGULATE_NGON_EARCLIP; | tmd->ngon_method = MOD_TRIANGULATE_NGON_EARCLIP; | ||||
| Show All 15 Lines | if (is_selected_to_active) { | ||||
| } | } | ||||
| BLI_assert(i == tot_highpoly); | BLI_assert(i == tot_highpoly); | ||||
| ob_low->restrictflag |= OB_RESTRICT_RENDER; | ob_low->restrictflag |= OB_RESTRICT_RENDER; | ||||
| /* populate the pixel arrays with the corresponding face data for each high poly object */ | /* populate the pixel arrays with the corresponding face data for each high poly object */ | ||||
| if (!RE_bake_pixels_populate_from_objects( | if (!RE_bake_pixels_populate_from_objects( | ||||
| me_low, pixel_array_low, highpoly, tot_highpoly, num_pixels, ob_cage != NULL, | me_low, pixel_array_low, pixel_array_high, highpoly, tot_highpoly, num_pixels, ob_cage != NULL, | ||||
| cage_extrusion, ob_low->obmat, (ob_cage ? ob_cage->obmat : ob_low->obmat), me_cage)) | cage_extrusion, ob_low->obmat, (ob_cage ? ob_cage->obmat : ob_low->obmat), me_cage)) | ||||
| { | { | ||||
| BKE_report(reports, RPT_ERROR, "Error handling selected objects"); | BKE_report(reports, RPT_ERROR, "Error handling selected objects"); | ||||
| goto cage_cleanup; | goto cage_cleanup; | ||||
| } | } | ||||
| /* the baking itself */ | /* the baking itself */ | ||||
| for (i = 0; i < tot_highpoly; i++) { | for (i = 0; i < tot_highpoly; i++) { | ||||
| ok = RE_bake_engine(re, highpoly[i].ob, highpoly[i].pixel_array, num_pixels, | G.baking_object_id = i; | ||||
| ok = RE_bake_engine(re, highpoly[i].ob, pixel_array_high, num_pixels, | |||||
| depth, pass_type, result); | depth, pass_type, result); | ||||
| if (!ok) { | if (!ok) { | ||||
| BKE_reportf(reports, RPT_ERROR, "Error baking from object \"%s\"", highpoly[i].ob->id.name + 2); | BKE_reportf(reports, RPT_ERROR, "Error baking from object \"%s\"", highpoly[i].ob->id.name + 2); | ||||
| goto cage_cleanup; | goto cage_cleanup; | ||||
| } | } | ||||
| } | } | ||||
| cage_cleanup: | cage_cleanup: | ||||
| Show All 10 Lines | if (!ok) { | ||||
| goto cleanup; | goto cleanup; | ||||
| } | } | ||||
| } | } | ||||
| else { | else { | ||||
| /* make sure low poly renders */ | /* make sure low poly renders */ | ||||
| ob_low->restrictflag &= ~OB_RESTRICT_RENDER; | ob_low->restrictflag &= ~OB_RESTRICT_RENDER; | ||||
| if (RE_bake_has_engine(re)) { | if (RE_bake_has_engine(re)) { | ||||
| G.baking_object_id = 0; | |||||
| ok = RE_bake_engine(re, ob_low, pixel_array_low, num_pixels, depth, pass_type, result); | ok = RE_bake_engine(re, ob_low, pixel_array_low, num_pixels, depth, pass_type, result); | ||||
| } | } | ||||
| else { | else { | ||||
| BKE_report(reports, RPT_ERROR, "Current render engine does not support baking"); | BKE_report(reports, RPT_ERROR, "Current render engine does not support baking"); | ||||
| goto cleanup; | goto cleanup; | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | |||||
| cleanup: | cleanup: | ||||
| if (highpoly) { | if (highpoly) { | ||||
| int i; | int i; | ||||
| for (i = 0; i < tot_highpoly; i++) { | for (i = 0; i < tot_highpoly; i++) { | ||||
| highpoly[i].ob->restrictflag = highpoly[i].restrict_flag; | highpoly[i].ob->restrictflag = highpoly[i].restrict_flag; | ||||
| if (highpoly[i].pixel_array) | |||||
| MEM_freeN(highpoly[i].pixel_array); | |||||
| if (highpoly[i].tri_mod) | if (highpoly[i].tri_mod) | ||||
| ED_object_modifier_remove(reports, bmain, highpoly[i].ob, highpoly[i].tri_mod); | ED_object_modifier_remove(reports, bmain, highpoly[i].ob, highpoly[i].tri_mod); | ||||
| if (highpoly[i].me) | if (highpoly[i].me) | ||||
| BKE_libblock_free(bmain, highpoly[i].me); | BKE_libblock_free(bmain, highpoly[i].me); | ||||
| } | } | ||||
| MEM_freeN(highpoly); | MEM_freeN(highpoly); | ||||
| } | } | ||||
| ob_low->restrictflag = restrict_flag_low; | ob_low->restrictflag = restrict_flag_low; | ||||
| if (ob_cage) | if (ob_cage) | ||||
| ob_cage->restrictflag = restrict_flag_cage; | ob_cage->restrictflag = restrict_flag_cage; | ||||
| if (pixel_array_low) | if (pixel_array_low) | ||||
| MEM_freeN(pixel_array_low); | MEM_freeN(pixel_array_low); | ||||
| if (pixel_array_high) | |||||
| MEM_freeN(pixel_array_high); | |||||
| if (bake_images.data) | if (bake_images.data) | ||||
| MEM_freeN(bake_images.data); | MEM_freeN(bake_images.data); | ||||
| if (bake_images.lookup) | if (bake_images.lookup) | ||||
| MEM_freeN(bake_images.lookup); | MEM_freeN(bake_images.lookup); | ||||
| if (result) | if (result) | ||||
| MEM_freeN(result); | MEM_freeN(result); | ||||
| ▲ Show 20 Lines • Show All 352 Lines • Show Last 20 Lines | |||||
Is it correct that both hi and lo poly arrays are the same size?