Changeset View
Changeset View
Standalone View
Standalone View
source/blender/render/intern/bake.cc
| Show First 20 Lines • Show All 92 Lines • ▼ Show 20 Lines | |||||
| TriTessFace *triangles; | TriTessFace *triangles; | ||||
| /* calculate normal for each polygon only once */ | /* calculate normal for each polygon only once */ | ||||
| uint mpoly_prev = UINT_MAX; | uint mpoly_prev = UINT_MAX; | ||||
| float no[3]; | float no[3]; | ||||
| const float(*positions)[3] = BKE_mesh_vert_positions(me); | const float(*positions)[3] = BKE_mesh_vert_positions(me); | ||||
| const MPoly *polys = BKE_mesh_polys(me); | const MPoly *polys = BKE_mesh_polys(me); | ||||
| const MLoop *loops = BKE_mesh_loops(me); | const int *corner_verts = BKE_mesh_corner_verts(me); | ||||
| looptri = static_cast<MLoopTri *>(MEM_mallocN(sizeof(*looptri) * tottri, __func__)); | looptri = static_cast<MLoopTri *>(MEM_mallocN(sizeof(*looptri) * tottri, __func__)); | ||||
| triangles = static_cast<TriTessFace *>(MEM_callocN(sizeof(TriTessFace) * tottri, __func__)); | triangles = static_cast<TriTessFace *>(MEM_callocN(sizeof(TriTessFace) * tottri, __func__)); | ||||
| const float(*precomputed_normals)[3] = BKE_mesh_poly_normals_are_dirty(me) ? | const float(*precomputed_normals)[3] = BKE_mesh_poly_normals_are_dirty(me) ? | ||||
| nullptr : | nullptr : | ||||
| BKE_mesh_poly_normals_ensure(me); | BKE_mesh_poly_normals_ensure(me); | ||||
| const bool calculate_normal = precomputed_normals ? false : true; | const bool calculate_normal = precomputed_normals ? false : true; | ||||
| if (precomputed_normals != nullptr) { | if (precomputed_normals != nullptr) { | ||||
| BKE_mesh_recalc_looptri_with_normals( | BKE_mesh_recalc_looptri_with_normals( | ||||
| loops, polys, positions, me->totloop, me->totpoly, looptri, precomputed_normals); | corner_verts, polys, positions, me->totloop, me->totpoly, looptri, precomputed_normals); | ||||
| } | } | ||||
| else { | else { | ||||
| BKE_mesh_recalc_looptri(loops, polys, positions, me->totloop, me->totpoly, looptri); | BKE_mesh_recalc_looptri(corner_verts, polys, positions, me->totloop, me->totpoly, looptri); | ||||
| } | } | ||||
| const TSpace *tspace = nullptr; | const TSpace *tspace = nullptr; | ||||
| const float(*loop_normals)[3] = nullptr; | const float(*loop_normals)[3] = nullptr; | ||||
| if (tangent) { | if (tangent) { | ||||
| BKE_mesh_ensure_normals_for_display(me_eval); | BKE_mesh_ensure_normals_for_display(me_eval); | ||||
| BKE_mesh_calc_normals_split(me_eval); | BKE_mesh_calc_normals_split(me_eval); | ||||
| BKE_mesh_calc_loop_tangents(me_eval, true, nullptr, 0); | BKE_mesh_calc_loop_tangents(me_eval, true, nullptr, 0); | ||||
| tspace = static_cast<const TSpace *>(CustomData_get_layer(&me_eval->ldata, CD_TANGENT)); | tspace = static_cast<const TSpace *>(CustomData_get_layer(&me_eval->ldata, CD_TANGENT)); | ||||
| BLI_assert(tspace); | BLI_assert(tspace); | ||||
| loop_normals = static_cast<const float(*)[3]>( | loop_normals = static_cast<const float(*)[3]>( | ||||
| CustomData_get_layer(&me_eval->ldata, CD_NORMAL)); | CustomData_get_layer(&me_eval->ldata, CD_NORMAL)); | ||||
| } | } | ||||
| const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(me); | const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(me); | ||||
| for (i = 0; i < tottri; i++) { | for (i = 0; i < tottri; i++) { | ||||
| const MLoopTri *lt = &looptri[i]; | const MLoopTri *lt = &looptri[i]; | ||||
| const MPoly *mp = &polys[lt->poly]; | const MPoly *mp = &polys[lt->poly]; | ||||
| triangles[i].positions[0] = positions[loops[lt->tri[0]].v]; | triangles[i].positions[0] = positions[corner_verts[lt->tri[0]]]; | ||||
| triangles[i].positions[1] = positions[loops[lt->tri[1]].v]; | triangles[i].positions[1] = positions[corner_verts[lt->tri[1]]]; | ||||
| triangles[i].positions[2] = positions[loops[lt->tri[2]].v]; | triangles[i].positions[2] = positions[corner_verts[lt->tri[2]]]; | ||||
| triangles[i].vert_normals[0] = vert_normals[loops[lt->tri[0]].v]; | triangles[i].vert_normals[0] = vert_normals[corner_verts[lt->tri[0]]]; | ||||
| triangles[i].vert_normals[1] = vert_normals[loops[lt->tri[1]].v]; | triangles[i].vert_normals[1] = vert_normals[corner_verts[lt->tri[1]]]; | ||||
| triangles[i].vert_normals[2] = vert_normals[loops[lt->tri[2]].v]; | triangles[i].vert_normals[2] = vert_normals[corner_verts[lt->tri[2]]]; | ||||
| triangles[i].is_smooth = (mp->flag & ME_SMOOTH) != 0; | triangles[i].is_smooth = (mp->flag & ME_SMOOTH) != 0; | ||||
| if (tangent) { | if (tangent) { | ||||
| triangles[i].tspace[0] = &tspace[lt->tri[0]]; | triangles[i].tspace[0] = &tspace[lt->tri[0]]; | ||||
| triangles[i].tspace[1] = &tspace[lt->tri[1]]; | triangles[i].tspace[1] = &tspace[lt->tri[1]]; | ||||
| triangles[i].tspace[2] = &tspace[lt->tri[2]]; | triangles[i].tspace[2] = &tspace[lt->tri[2]]; | ||||
| } | } | ||||
| if (loop_normals) { | if (loop_normals) { | ||||
| triangles[i].loop_normal[0] = loop_normals[lt->tri[0]]; | triangles[i].loop_normal[0] = loop_normals[lt->tri[0]]; | ||||
| triangles[i].loop_normal[1] = loop_normals[lt->tri[1]]; | triangles[i].loop_normal[1] = loop_normals[lt->tri[1]]; | ||||
| triangles[i].loop_normal[2] = loop_normals[lt->tri[2]]; | triangles[i].loop_normal[2] = loop_normals[lt->tri[2]]; | ||||
| } | } | ||||
| if (calculate_normal) { | if (calculate_normal) { | ||||
| if (lt->poly != mpoly_prev) { | if (lt->poly != mpoly_prev) { | ||||
| BKE_mesh_calc_poly_normal(mp, &loops[mp->loopstart], positions, no); | BKE_mesh_calc_poly_normal(mp, &corner_verts[mp->loopstart], positions, no); | ||||
| mpoly_prev = lt->poly; | mpoly_prev = lt->poly; | ||||
| } | } | ||||
| copy_v3_v3(triangles[i].normal, no); | copy_v3_v3(triangles[i].normal, no); | ||||
| } | } | ||||
| else { | else { | ||||
| copy_v3_v3(triangles[i].normal, precomputed_normals[lt->poly]); | copy_v3_v3(triangles[i].normal, precomputed_normals[lt->poly]); | ||||
| } | } | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 184 Lines • ▼ Show 20 Lines | |||||
| zbuf_alloc_span(&bd.zspan[i], targets->images[i].width, targets->images[i].height); | zbuf_alloc_span(&bd.zspan[i], targets->images[i].width, targets->images[i].height); | ||||
| } | } | ||||
| const int tottri = poly_to_tri_count(me->totpoly, me->totloop); | const int tottri = poly_to_tri_count(me->totpoly, me->totloop); | ||||
| MLoopTri *looptri = static_cast<MLoopTri *>(MEM_mallocN(sizeof(*looptri) * tottri, __func__)); | MLoopTri *looptri = static_cast<MLoopTri *>(MEM_mallocN(sizeof(*looptri) * tottri, __func__)); | ||||
| const float(*positions)[3] = BKE_mesh_vert_positions(me); | const float(*positions)[3] = BKE_mesh_vert_positions(me); | ||||
| const MPoly *polys = BKE_mesh_polys(me); | const MPoly *polys = BKE_mesh_polys(me); | ||||
| const MLoop *loops = BKE_mesh_loops(me); | const int *corner_verts = BKE_mesh_corner_verts(me); | ||||
| BKE_mesh_recalc_looptri(loops, polys, positions, me->totloop, me->totpoly, looptri); | BKE_mesh_recalc_looptri(corner_verts, polys, positions, me->totloop, me->totpoly, looptri); | ||||
| const int *material_indices = BKE_mesh_material_indices(me); | const int *material_indices = BKE_mesh_material_indices(me); | ||||
| const int materials_num = targets->materials_num; | const int materials_num = targets->materials_num; | ||||
| for (int i = 0; i < tottri; i++) { | for (int i = 0; i < tottri; i++) { | ||||
| const MLoopTri *lt = &looptri[i]; | const MLoopTri *lt = &looptri[i]; | ||||
| bd.primitive_id = i; | bd.primitive_id = i; | ||||
| ▲ Show 20 Lines • Show All 92 Lines • Show Last 20 Lines | |||||