From 5d2d36b0686d7253f9d61c00a63d273aba17677a Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Fri, 25 May 2018 22:24:24 +0530 Subject: Applied soc-2017-normal-tools --- source/blender/modifiers/CMakeLists.txt | 1 + source/blender/modifiers/MOD_modifiertypes.h | 1 + source/blender/modifiers/intern/MOD_util.c | 1 + .../blender/modifiers/intern/MOD_weighted_normal.c | 670 +++++++++++++++++++++ 4 files changed, 673 insertions(+) create mode 100644 source/blender/modifiers/intern/MOD_weighted_normal.c (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/CMakeLists.txt b/source/blender/modifiers/CMakeLists.txt index 397a3263e22..0a619ecf9c7 100644 --- a/source/blender/modifiers/CMakeLists.txt +++ b/source/blender/modifiers/CMakeLists.txt @@ -100,6 +100,7 @@ set(SRC intern/MOD_uvproject.c intern/MOD_warp.c intern/MOD_wave.c + intern/MOD_weighted_normal.c intern/MOD_weightvg_util.c intern/MOD_weightvgedit.c intern/MOD_weightvgmix.c diff --git a/source/blender/modifiers/MOD_modifiertypes.h b/source/blender/modifiers/MOD_modifiertypes.h index bf121af2bd1..3511b0edbec 100644 --- a/source/blender/modifiers/MOD_modifiertypes.h +++ b/source/blender/modifiers/MOD_modifiertypes.h @@ -86,6 +86,7 @@ extern ModifierTypeInfo modifierType_NormalEdit; extern ModifierTypeInfo modifierType_CorrectiveSmooth; extern ModifierTypeInfo modifierType_MeshSequenceCache; extern ModifierTypeInfo modifierType_SurfaceDeform; +extern ModifierTypeInfo modifierType_WeightedNormal; /* MOD_util.c */ void modifier_type_init(ModifierTypeInfo *types[]); diff --git a/source/blender/modifiers/intern/MOD_util.c b/source/blender/modifiers/intern/MOD_util.c index 4a62d17fa27..5d740ae9e20 100644 --- a/source/blender/modifiers/intern/MOD_util.c +++ b/source/blender/modifiers/intern/MOD_util.c @@ -444,5 +444,6 @@ void modifier_type_init(ModifierTypeInfo *types[]) INIT_TYPE(CorrectiveSmooth); INIT_TYPE(MeshSequenceCache); INIT_TYPE(SurfaceDeform); + INIT_TYPE(WeightedNormal); #undef INIT_TYPE } diff --git a/source/blender/modifiers/intern/MOD_weighted_normal.c b/source/blender/modifiers/intern/MOD_weighted_normal.c new file mode 100644 index 00000000000..b43ef698cc4 --- /dev/null +++ b/source/blender/modifiers/intern/MOD_weighted_normal.c @@ -0,0 +1,670 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * ***** END GPL LICENSE BLOCK ***** + * + */ + +/** \file blender/modifiers/intern/MOD_weighted_normal.c + * \ingroup modifiers + */ + +#include "MEM_guardedalloc.h" + +#include "DNA_mesh_types.h" +#include "DNA_object_types.h" +#include "DNA_scene_types.h" + +#include "BKE_cdderivedmesh.h" +#include "BKE_deform.h" +#include "BKE_library.h" +#include "BKE_library_query.h" +#include "BKE_mesh.h" + +#include "BLI_math.h" +#include "BLI_linklist.h" + +#include "MOD_modifiertypes.h" +#include "MOD_util.h" + +#define CLNORS_VALID_VEC_LEN (1e-6f) + +typedef struct ModePair { + float val; /* Contains mode based value (face area / corner angle). */ + int index; /* Index value per poly or per loop. */ +} ModePair; + +/* Sorting function used in modifier, sorts in decreasing order. */ +static int modepair_cmp_by_val_inverse(const void *p1, const void *p2) +{ + ModePair *r1 = (ModePair *)p1; + ModePair *r2 = (ModePair *)p2; + + return (r1->val < r2->val) ? 1 : ((r1->val > r2->val) ? -1 : 0); +} + +/* There will be one of those per vertex (simple case, computing one normal per vertex), or per smooth fan. */ +typedef struct WeightedNormalDataAggregateItem { + float normal[3]; + + int num_loops; /* Count number of loops using this item so far. */ + float curr_val; /* Current max val for this item. */ + int curr_strength; /* Current max strength encountered for this item. */ +} WeightedNormalDataAggregateItem; + +#define NUM_CACHED_INVERSE_POWERS_OF_WEIGHT 128 + +typedef struct WeightedNormalData { + const int numVerts; + const int numEdges; + const int numLoops; + const int numPolys; + + MVert *mvert; + MEdge *medge; + + MLoop *mloop; + short (*clnors)[2]; + const bool has_clnors; /* True if clnors already existed, false if we had to create them. */ + const float split_angle; + + MPoly *mpoly; + float (*polynors)[3]; + int *poly_strength; + + MDeformVert *dvert; + const int defgrp_index; + const bool use_invert_vgroup; + + const float weight; + const short mode; + + /* Lower-level, internal processing data. */ + float cached_inverse_powers_of_weight[NUM_CACHED_INVERSE_POWERS_OF_WEIGHT]; + + WeightedNormalDataAggregateItem *items_data; + + ModePair *mode_pair; + + int *loop_to_poly; +} WeightedNormalData; + +/* Check strength of given poly compared to those found so far for that given item (vertex or smooth fan), + * and reset matching item_data in case we get a stronger new strength. */ +static bool check_item_poly_strength( + WeightedNormalData *wn_data, WeightedNormalDataAggregateItem *item_data, const int mp_index) +{ + BLI_assert (wn_data->poly_strength != NULL); + + const int mp_strength = wn_data->poly_strength[mp_index]; + + if (mp_strength > item_data->curr_strength) { + item_data->curr_strength = mp_strength; + item_data->curr_val = 0.0f; + item_data->num_loops = 0; + zero_v3(item_data->normal); + } + + return mp_strength == item_data->curr_strength; +} + +static void aggregate_item_normal( + WeightedNormalModifierData *wnmd, WeightedNormalData *wn_data, + WeightedNormalDataAggregateItem *item_data, + const int mv_index, const int mp_index, + const float curr_val, const bool use_face_influence) +{ + float (*polynors)[3] = wn_data->polynors; + + MDeformVert *dvert = wn_data->dvert; + const int defgrp_index = wn_data->defgrp_index; + const bool use_invert_vgroup = wn_data->use_invert_vgroup; + + const float weight = wn_data->weight; + + float *cached_inverse_powers_of_weight = wn_data->cached_inverse_powers_of_weight; + + const bool has_vgroup = dvert != NULL; + const bool vert_of_group = has_vgroup && defvert_find_index(&dvert[mv_index], defgrp_index) != NULL; + + if (has_vgroup && ((vert_of_group && use_invert_vgroup) || (!vert_of_group && !use_invert_vgroup))) { + return; + } + + if (use_face_influence && !check_item_poly_strength(wn_data, item_data, mp_index)) { + return; + } + + /* If item's curr_val is 0 init it to present value. */ + if (item_data->curr_val == 0.0f) { + item_data->curr_val = curr_val; + } + if (!compare_ff(item_data->curr_val, curr_val, wnmd->thresh)) { + /* item's curr_val and present value differ more than threshold, update. */ + item_data->num_loops++; + item_data->curr_val = curr_val; + } + + /* Exponentially divided weight for each normal (since a few values will be used by most cases, we cache those). */ + const int num_loops = item_data->num_loops; + if (num_loops < NUM_CACHED_INVERSE_POWERS_OF_WEIGHT && cached_inverse_powers_of_weight[num_loops] == 0.0f) { + cached_inverse_powers_of_weight[num_loops] = 1.0f / powf(weight, num_loops); + } + const float inverted_n_weight = num_loops < NUM_CACHED_INVERSE_POWERS_OF_WEIGHT ? + cached_inverse_powers_of_weight[num_loops] : 1.0f / powf(weight, num_loops); + + madd_v3_v3fl(item_data->normal, polynors[mp_index], curr_val * inverted_n_weight); +} + +static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd, WeightedNormalData *wn_data) +{ + const int numVerts = wn_data->numVerts; + const int numEdges = wn_data->numEdges; + const int numLoops = wn_data->numLoops; + const int numPolys = wn_data->numPolys; + + MVert *mvert = wn_data->mvert; + MEdge *medge = wn_data->medge; + + MLoop *mloop = wn_data->mloop; + short (*clnors)[2] = wn_data->clnors; + int *loop_to_poly = wn_data->loop_to_poly; + + MPoly *mpoly = wn_data->mpoly; + float (*polynors)[3] = wn_data->polynors; + int *poly_strength = wn_data->poly_strength; + + MDeformVert *dvert = wn_data->dvert; + + const short mode = wn_data->mode; + ModePair *mode_pair = wn_data->mode_pair; + + const bool has_clnors = wn_data->has_clnors; + const float split_angle = wn_data->split_angle; + MLoopNorSpaceArray lnors_spacearr = {NULL}; + + const bool keep_sharp = (wnmd->flag & MOD_WEIGHTEDNORMAL_KEEP_SHARP) != 0; + const bool use_face_influence = (wnmd->flag & MOD_WEIGHTEDNORMAL_FACE_INFLUENCE) != 0 && poly_strength != NULL; + const bool has_vgroup = dvert != NULL; + + float (*loop_normals)[3] = NULL; + + WeightedNormalDataAggregateItem *items_data = NULL; + int num_items = 0; + if (keep_sharp) { + BLI_bitmap *done_loops = BLI_BITMAP_NEW(numLoops, __func__); + + /* This will give us loop normal spaces, we do not actually care about computed loop_normals for now... */ + loop_normals = MEM_calloc_arrayN((size_t)numLoops, sizeof(*loop_normals), __func__); + BKE_mesh_normals_loop_split(mvert, numVerts, medge, numEdges, + mloop, loop_normals, numLoops, mpoly, polynors, numPolys, + true, split_angle, &lnors_spacearr, has_clnors ? clnors : NULL, loop_to_poly); + + num_items = lnors_spacearr.num_spaces; + items_data = MEM_calloc_arrayN((size_t)num_items, sizeof(*items_data), __func__); + + /* In this first loop, we assign each WeightedNormalDataAggregateItem to its smooth fan of loops (aka lnor space). */ + MPoly *mp; + int mp_index; + int item_index; + for (mp = mpoly, mp_index = 0, item_index = 0; mp_index < numPolys; mp++, mp_index++) { + int ml_index = mp->loopstart; + const int ml_end_index = ml_index + mp->totloop; + + for (; ml_index < ml_end_index; ml_index++) { + if (BLI_BITMAP_TEST(done_loops, ml_index)) { + /* Smooth fan of this loop has already been processed, skip it. */ + continue; + } + BLI_assert(item_index < num_items); + + WeightedNormalDataAggregateItem *itdt = &items_data[item_index]; + itdt->curr_strength = FACE_STRENGTH_WEAK; + + MLoopNorSpace *lnor_space = lnors_spacearr.lspacearr[ml_index]; + lnor_space->user_data = itdt; + + if (!(lnor_space->flags & MLNOR_SPACE_IS_SINGLE)) { + for (LinkNode *lnode = lnor_space->loops; lnode; lnode = lnode->next) { + const int ml_fan_index = GET_INT_FROM_POINTER(lnode->link); + BLI_BITMAP_ENABLE(done_loops, ml_fan_index); + } + } + else { + BLI_BITMAP_ENABLE(done_loops, ml_index); + } + + item_index++; + } + } + + MEM_freeN(done_loops); + } + else { + num_items = numVerts; + items_data = MEM_calloc_arrayN((size_t)num_items, sizeof(*items_data), __func__); + if (use_face_influence) { + for (int item_index = 0; item_index < num_items; item_index++) { + items_data[item_index].curr_strength = FACE_STRENGTH_WEAK; + } + } + } + wn_data->items_data = items_data; + + switch (mode) { + case MOD_WEIGHTEDNORMAL_MODE_FACE: + for (int i = 0; i < numPolys; i++) { + const int mp_index = mode_pair[i].index; + const float mp_val = mode_pair[i].val; + + int ml_index = mpoly[mp_index].loopstart; + const int ml_index_end = ml_index + mpoly[mp_index].totloop; + for (; ml_index < ml_index_end; ml_index++) { + const int mv_index = mloop[ml_index].v; + WeightedNormalDataAggregateItem *item_data = keep_sharp ? + lnors_spacearr.lspacearr[ml_index]->user_data: + &items_data[mv_index]; + + aggregate_item_normal(wnmd, wn_data, item_data, mv_index, mp_index, mp_val, use_face_influence); + } + } + break; + case MOD_WEIGHTEDNORMAL_MODE_ANGLE: + case MOD_WEIGHTEDNORMAL_MODE_FACE_ANGLE: + BLI_assert(loop_to_poly != NULL); + + for (int i = 0; i < numLoops; i++) { + const int ml_index = mode_pair[i].index; + const float ml_val = mode_pair[i].val; + + const int mp_index = loop_to_poly[ml_index]; + const int mv_index = mloop[ml_index].v; + WeightedNormalDataAggregateItem *item_data = keep_sharp ? + lnors_spacearr.lspacearr[ml_index]->user_data: + &items_data[mv_index]; + + aggregate_item_normal(wnmd, wn_data, item_data, mv_index, mp_index, ml_val, use_face_influence); + } + break; + default: + BLI_assert(0); + } + + /* Validate computed weighted normals. */ + for (int item_index = 0; item_index < num_items; item_index++) { + if (normalize_v3(items_data[item_index].normal) < CLNORS_VALID_VEC_LEN) { + zero_v3(items_data[item_index].normal); + } + } + + if (keep_sharp) { + /* Set loop normals for normal computed for each lnor space (smooth fan). + * Note that loop_normals is already populated with clnors (before this modifier is applied, at start of + * this function), so no need to recompute them here. */ + for (int ml_index = 0; ml_index < numLoops; ml_index++) { + WeightedNormalDataAggregateItem *item_data = lnors_spacearr.lspacearr[ml_index]->user_data; + if (!is_zero_v3(item_data->normal)) { + copy_v3_v3(loop_normals[ml_index], item_data->normal); + } + } + + BKE_mesh_normals_loop_custom_set(mvert, numVerts, medge, numEdges, + mloop, loop_normals, numLoops, mpoly, polynors, numPolys, clnors); + } + else { + /* TODO: Ideally, we could add an option to BKE_mesh_normals_loop_custom_[from_vertices_]set() to keep current + * clnors instead of resetting them to default autocomputed ones, when given new custom normal is zero-vec. + * But this is not exactly trivial change, better to keep this optimization for later... + */ + if (!has_vgroup) { + /* Note: in theory, we could avoid this extra allocation & copying... But think we can live with it for now, + * and it makes code simpler & cleaner. */ + float (*vert_normals)[3] = MEM_calloc_arrayN((size_t)numVerts, sizeof(*loop_normals), __func__); + + for (int ml_index = 0; ml_index < numLoops; ml_index++) { + const int mv_index = mloop[ml_index].v; + copy_v3_v3(vert_normals[mv_index], items_data[mv_index].normal); + } + + BKE_mesh_normals_loop_custom_from_vertices_set(mvert, vert_normals, numVerts, medge, numEdges, + mloop, numLoops, mpoly, polynors, numPolys, clnors); + + MEM_freeN(vert_normals); + } + else { + loop_normals = MEM_calloc_arrayN((size_t)numLoops, sizeof(*loop_normals), __func__); + + BKE_mesh_normals_loop_split(mvert, numVerts, medge, numEdges, + mloop, loop_normals, numLoops, mpoly, polynors, numPolys, + true, split_angle, NULL, has_clnors ? clnors : NULL, loop_to_poly); + + for (int ml_index = 0; ml_index < numLoops; ml_index++) { + const int item_index = mloop[ml_index].v; + if (!is_zero_v3(items_data[item_index].normal)) { + copy_v3_v3(loop_normals[ml_index], items_data[item_index].normal); + } + } + + BKE_mesh_normals_loop_custom_set(mvert, numVerts, medge, numEdges, + mloop, loop_normals, numLoops, mpoly, polynors, numPolys, clnors); + } + } + + if (keep_sharp) { + BKE_lnor_spacearr_free(&lnors_spacearr); + } + MEM_SAFE_FREE(loop_normals); +} + +static void wn_face_area(WeightedNormalModifierData *wnmd, WeightedNormalData *wn_data) +{ + const int numPolys = wn_data->numPolys; + + MVert *mvert = wn_data->mvert; + MLoop *mloop = wn_data->mloop; + MPoly *mpoly = wn_data->mpoly; + + MPoly *mp; + int mp_index; + + ModePair *face_area = MEM_malloc_arrayN((size_t)numPolys, sizeof(*face_area), __func__); + + ModePair *f_area = face_area; + for (mp_index = 0, mp = mpoly; mp_index < numPolys; mp_index++, mp++, f_area++) { + f_area->val = BKE_mesh_calc_poly_area(mp, &mloop[mp->loopstart], mvert); + f_area->index = mp_index; + } + + qsort(face_area, numPolys, sizeof(*face_area), modepair_cmp_by_val_inverse); + + wn_data->mode_pair = face_area; + apply_weights_vertex_normal(wnmd, wn_data); +} + +static void wn_corner_angle(WeightedNormalModifierData *wnmd, WeightedNormalData *wn_data) +{ + const int numLoops = wn_data->numLoops; + const int numPolys = wn_data->numPolys; + + MVert *mvert = wn_data->mvert; + MLoop *mloop = wn_data->mloop; + MPoly *mpoly = wn_data->mpoly; + + MPoly *mp; + int mp_index; + + int *loop_to_poly = MEM_malloc_arrayN((size_t)numLoops, sizeof(*loop_to_poly), __func__); + + ModePair *corner_angle = MEM_malloc_arrayN((size_t)numLoops, sizeof(*corner_angle), __func__); + + for (mp_index = 0, mp = mpoly; mp_index < numPolys; mp_index++, mp++) { + MLoop *ml_start = &mloop[mp->loopstart]; + + float *index_angle = MEM_malloc_arrayN((size_t)mp->totloop, sizeof(*index_angle), __func__); + BKE_mesh_calc_poly_angles(mp, ml_start, mvert, index_angle); + + ModePair *c_angl = &corner_angle[mp->loopstart]; + float *angl = index_angle; + for (int ml_index = mp->loopstart; ml_index < mp->loopstart + mp->totloop; ml_index++, c_angl++, angl++) { + c_angl->val = (float)M_PI - *angl; + c_angl->index = ml_index; + + loop_to_poly[ml_index] = mp_index; + } + MEM_freeN(index_angle); + } + + qsort(corner_angle, numLoops, sizeof(*corner_angle), modepair_cmp_by_val_inverse); + + wn_data->loop_to_poly = loop_to_poly; + wn_data->mode_pair = corner_angle; + apply_weights_vertex_normal(wnmd, wn_data); +} + +static void wn_face_with_angle(WeightedNormalModifierData *wnmd, WeightedNormalData *wn_data) +{ + const int numLoops = wn_data->numLoops; + const int numPolys = wn_data->numPolys; + + MVert *mvert = wn_data->mvert; + MLoop *mloop = wn_data->mloop; + MPoly *mpoly = wn_data->mpoly; + + MPoly *mp; + int mp_index; + + int *loop_to_poly = MEM_malloc_arrayN((size_t)numLoops, sizeof(*loop_to_poly), __func__); + + ModePair *combined = MEM_malloc_arrayN((size_t)numLoops, sizeof(*combined), __func__); + + for (mp_index = 0, mp = mpoly; mp_index < numPolys; mp_index++, mp++) { + MLoop *ml_start = &mloop[mp->loopstart]; + + float face_area = BKE_mesh_calc_poly_area(mp, ml_start, mvert); + float *index_angle = MEM_malloc_arrayN((size_t)mp->totloop, sizeof(*index_angle), __func__); + BKE_mesh_calc_poly_angles(mp, ml_start, mvert, index_angle); + + ModePair *cmbnd = &combined[mp->loopstart]; + float *angl = index_angle; + for (int ml_index = mp->loopstart; ml_index < mp->loopstart + mp->totloop; ml_index++, cmbnd++, angl++) { + /* In this case val is product of corner angle and face area. */ + cmbnd->val = ((float)M_PI - *angl) * face_area; + cmbnd->index = ml_index; + + loop_to_poly[ml_index] = mp_index; + } + MEM_freeN(index_angle); + } + + qsort(combined, numLoops, sizeof(*combined), modepair_cmp_by_val_inverse); + + wn_data->loop_to_poly = loop_to_poly; + wn_data->mode_pair = combined; + apply_weights_vertex_normal(wnmd, wn_data); +} + +static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh) +{ + WeightedNormalModifierData *wnmd = (WeightedNormalModifierData *)md; + Object *ob = ctx->object; + + /* XXX TODO ARG GRRR XYQWNMPRXTYY + * Once we fully switch to Mesh evaluation of modifiers, we can expect to get that flag from the COW copy. + * But for now, it is lost in the DM intermediate step, so we need to directly check orig object's data. */ +#if 0 + if (!(mesh->flag & ME_AUTOSMOOTH)) { +#else + if (!(((Mesh *)ob->data)->flag & ME_AUTOSMOOTH)) { +#endif + modifier_setError((ModifierData *)wnmd, "Enable 'Auto Smooth' option in mesh settings"); + return mesh; + } + + Mesh *result; + BKE_id_copy_ex( + NULL, &mesh->id, (ID **)&result, + LIB_ID_CREATE_NO_MAIN | + LIB_ID_CREATE_NO_USER_REFCOUNT | + LIB_ID_CREATE_NO_DEG_TAG | + LIB_ID_COPY_NO_PREVIEW, + false); + + const int numVerts = result->totvert; + const int numEdges = result->totedge; + const int numLoops = result->totloop; + const int numPolys = result->totpoly; + + MEdge *medge = result->medge; + MPoly *mpoly = result->mpoly; + MVert *mvert = result->mvert; + MLoop *mloop = result->mloop; + + bool free_polynors = false; + + /* Right now: + * If weight = 50 then all faces are given equal weight. + * If weight > 50 then more weight given to faces with larger vals (face area / corner angle). + * If weight < 50 then more weight given to faces with lesser vals. However current calculation + * does not converge to min/max. + */ + float weight = ((float)wnmd->weight) / 50.0f; + if (wnmd->weight == 100) { + weight = (float)SHRT_MAX; + } + else if (wnmd->weight == 1) { + weight = 1 / (float)SHRT_MAX; + } + else if ((weight - 1) * 25 > 1) { + weight = (weight - 1) * 25; + } + + CustomData *pdata = &result->pdata; + float (*polynors)[3] = CustomData_get_layer(pdata, CD_NORMAL); + if (!polynors) { + polynors = CustomData_add_layer(pdata, CD_NORMAL, CD_CALLOC, NULL, numPolys); + } + BKE_mesh_calc_normals_poly(mvert, NULL, numVerts, mloop, mpoly, numLoops, numPolys, polynors, false); + + + const float split_angle = mesh->smoothresh; + short (*clnors)[2]; + CustomData *ldata = &result->ldata; + clnors = CustomData_get_layer(ldata, CD_CUSTOMLOOPNORMAL); + + /* Keep info whether we had clnors, it helps when generating clnor spaces and default normals. */ + const bool has_clnors = clnors != NULL; + if (!clnors) { + clnors = CustomData_add_layer(ldata, CD_CUSTOMLOOPNORMAL, CD_CALLOC, NULL, numLoops); + clnors = CustomData_get_layer(ldata, CD_CUSTOMLOOPNORMAL); + } + + MDeformVert *dvert; + int defgrp_index; + modifier_get_vgroup_mesh(ob, result, wnmd->defgrp_name, &dvert, &defgrp_index); + + WeightedNormalData wn_data = { + .numVerts = numVerts, + .numEdges = numEdges, + .numLoops = numLoops, + .numPolys = numPolys, + + .mvert = mvert, + .medge = medge, + + .mloop = mloop, + .clnors = clnors, + .has_clnors = has_clnors, + .split_angle = split_angle, + + .mpoly = mpoly, + .polynors = polynors, + .poly_strength = CustomData_get_layer_named(&result->pdata, CD_PROP_INT, MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID), + + .dvert = dvert, + .defgrp_index = defgrp_index, + .use_invert_vgroup = (wnmd->flag & MOD_WEIGHTEDNORMAL_INVERT_VGROUP) != 0, + + .weight = weight, + .mode = wnmd->mode, + }; + + switch (wnmd->mode) { + case MOD_WEIGHTEDNORMAL_MODE_FACE: + wn_face_area(wnmd, &wn_data); + break; + case MOD_WEIGHTEDNORMAL_MODE_ANGLE: + wn_corner_angle(wnmd, &wn_data); + break; + case MOD_WEIGHTEDNORMAL_MODE_FACE_ANGLE: + wn_face_with_angle(wnmd, &wn_data); + break; + } + + MEM_SAFE_FREE(wn_data.loop_to_poly); + MEM_SAFE_FREE(wn_data.mode_pair); + MEM_SAFE_FREE(wn_data.items_data); + + return result; +} + +static void initData(ModifierData *md) +{ + WeightedNormalModifierData *wnmd = (WeightedNormalModifierData *)md; + wnmd->mode = MOD_WEIGHTEDNORMAL_MODE_FACE; + wnmd->weight = 50; + wnmd->thresh = 1e-2f; + wnmd->flag = 0; +} + +static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md) +{ + WeightedNormalModifierData *wnmd = (WeightedNormalModifierData *)md; + CustomDataMask dataMask = CD_CUSTOMLOOPNORMAL; + + if (wnmd->defgrp_name[0]) { + dataMask |= CD_MASK_MDEFORMVERT; + } + + if (wnmd->flag & MOD_WEIGHTEDNORMAL_FACE_INFLUENCE) { + dataMask |= CD_MASK_PROP_INT; + } + + return dataMask; +} + +static bool dependsOnNormals(ModifierData *UNUSED(md)) +{ + return true; +} + +ModifierTypeInfo modifierType_WeightedNormal = { + /* name */ "Weighted Normal", + /* structName */ "WeightedNormalModifierData", + /* structSize */ sizeof(WeightedNormalModifierData), + /* type */ eModifierTypeType_Constructive, + /* flags */ eModifierTypeFlag_AcceptsMesh | + eModifierTypeFlag_SupportsMapping | + eModifierTypeFlag_SupportsEditmode | + eModifierTypeFlag_EnableInEditmode, + + /* copyData */ modifier_copyData_generic, + + /* deformVerts_DM */ NULL, + /* deformMatrices_DM */ NULL, + /* deformVertsEM_DM */ NULL, + /* deformMatricesEM_DM*/NULL, + /* applyModifier_DM */ NULL, + /* applyModifierEM_DM */NULL, + + /* deformVerts */ NULL, + /* deformMatrices */ NULL, + /* deformVertsEM */ NULL, + /* deformMatricesEM */ NULL, + /* applyModifier */ applyModifier, + /* applyModifierEM */ NULL, + + /* initData */ initData, + /* requiredDataMask */ requiredDataMask, + /* freeData */ NULL, + /* isDisabled */ NULL, + /* updateDepsgraph */ NULL, + /* dependsOnTime */ NULL, + /* dependsOnNormals */ dependsOnNormals, + /* foreachObjectLink */ NULL, + /* foreachIDLink */ NULL, + /* foreachTexLink */ NULL, +}; -- cgit v1.2.3 From 2903146826a3e88bb9f001c7ce6678057fb7b1f3 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Mon, 4 Jun 2018 15:13:54 +0530 Subject: Added UI support for seams and sharp edges and cleanup --- source/blender/modifiers/intern/MOD_bevel.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 81a4e94386f..61ecc5c734e 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -59,6 +59,7 @@ static void initData(ModifierData *md) bmd->val_flags = MOD_BEVEL_AMT_OFFSET; bmd->lim_flags = 0; bmd->e_flags = 0; + bmd->edge_flags = 0; bmd->mat = -1; bmd->profile = 0.5f; bmd->bevel_angle = DEG2RADF(30.0f); @@ -96,6 +97,8 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes const int offset_type = bmd->val_flags; const int mat = CLAMPIS(bmd->mat, -1, ctx->object->totcol - 1); const bool loop_slide = (bmd->flags & MOD_BEVEL_EVEN_WIDTHS) == 0; + const bool mark_seam = (bmd->edge_flags & MOD_BEVEL_MARK_SEAM); + const bool mark_sharp = (bmd->edge_flags & MOD_BEVEL_MARK_SHARP); bm = BKE_mesh_to_bmesh_ex( mesh, @@ -166,7 +169,7 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes BM_mesh_bevel(bm, bmd->value, offset_type, bmd->res, bmd->profile, vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp, - dvert, vgroup, mat, loop_slide); + dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp); result = BKE_bmesh_to_mesh_nomain(bm, &(struct BMeshToMeshParams){0}); -- cgit v1.2.3 From 0c6410ec0cffdcb0641fa85d8394f7c682c44143 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Mon, 11 Jun 2018 11:58:26 +0530 Subject: Added ability to harden normals. Uses 2 different params: mode and strength. There are still some hiccups with how 2.8 interacts with normals. Will resolve as support gets better --- source/blender/modifiers/intern/MOD_bevel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 61ecc5c734e..4df8f1d06ae 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -169,7 +169,7 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes BM_mesh_bevel(bm, bmd->value, offset_type, bmd->res, bmd->profile, vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp, - dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp); + dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, NULL); result = BKE_bmesh_to_mesh_nomain(bm, &(struct BMeshToMeshParams){0}); -- cgit v1.2.3 From dd752476b97aa3b35d1359422ca42e33d99ac851 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Tue, 19 Jun 2018 19:27:08 +0530 Subject: Added face strength in bevel modifier The selected face strength (Weak/Medium/High) can be used by the WN Modifier to determine influence of current face in --- source/blender/modifiers/intern/MOD_bevel.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 4df8f1d06ae..7a20eb005f3 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -34,6 +34,7 @@ #include "DNA_object_types.h" #include "DNA_mesh_types.h" +#include "DNA_scene_types.h" #include "BLI_utildefines.h" #include "BLI_math.h" @@ -77,6 +78,30 @@ static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md) return dataMask; } +static void bevel_set_weighted_normal_face_strength(BMesh *bm, Scene *scene) +{ + BMFace *f; + BMIter fiter; + const char *wn_layer_id = MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID; + int cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT, wn_layer_id); + + if (cd_prop_int_idx == -1) { + BM_data_layer_add_named(bm, &bm->pdata, CD_PROP_INT, wn_layer_id); + cd_prop_int_idx = CustomData_get_named_layer_index(&bm->pdata, CD_PROP_INT, wn_layer_id); + } + cd_prop_int_idx -= CustomData_get_layer_index(&bm->pdata, CD_PROP_INT); + const int cd_prop_int_offset = CustomData_get_n_offset(&bm->pdata, CD_PROP_INT, cd_prop_int_idx); + + const int face_strength = scene->toolsettings->face_strength; + + BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) { + if (BM_elem_flag_test(f, BM_ELEM_TAG)) { + int *strength = BM_ELEM_CD_GET_VOID_P(f, cd_prop_int_offset); + *strength = face_strength; + } + } +} + /* * This calls the new bevel code (added since 2.64) */ @@ -99,6 +124,7 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes const bool loop_slide = (bmd->flags & MOD_BEVEL_EVEN_WIDTHS) == 0; const bool mark_seam = (bmd->edge_flags & MOD_BEVEL_MARK_SEAM); const bool mark_sharp = (bmd->edge_flags & MOD_BEVEL_MARK_SHARP); + const bool set_wn_strength = (bmd->flags & MOD_BEVEL_SET_WN_STR); bm = BKE_mesh_to_bmesh_ex( mesh, @@ -171,6 +197,9 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp, dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, NULL); + if(set_wn_strength) + bevel_set_weighted_normal_face_strength(bm, md->scene); + result = BKE_bmesh_to_mesh_nomain(bm, &(struct BMeshToMeshParams){0}); BLI_assert(bm->vtoolflagpool == NULL && -- cgit v1.2.3 From 1757b381790cd490e14b7ff7b6c4c84e4f1132e9 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Sat, 23 Jun 2018 01:46:42 +0530 Subject: Added UI for harden normals and normal control in bevel modifier --- source/blender/modifiers/intern/MOD_bevel.c | 109 ++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 6902b811c26..77b306466c3 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -32,11 +32,14 @@ * \ingroup modifiers */ +#include "MEM_guardedalloc.h" + #include "DNA_object_types.h" #include "DNA_mesh_types.h" #include "DNA_scene_types.h" #include "BLI_utildefines.h" +#include "BLI_linklist_stack.h" #include "BLI_math.h" #include "BLI_string.h" @@ -102,6 +105,109 @@ static void bevel_set_weighted_normal_face_strength(BMesh *bm, Scene *scene) } } +static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn_strength, int hnmode, MDeformVert *dvert, int vgroup) +{ + if (bmd->res > 20) + return; + BM_mesh_normals_update(bm); + BM_lnorspace_update(bm); + BM_normals_loops_edges_tag(bm, true); + + int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL); + + BMFace *f; + BMLoop *l, *l_cur, *l_first; + BMIter fiter; + + BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) { + l_cur = l_first = BM_FACE_FIRST_LOOP(f); + do { + if ((!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG) || (!BM_elem_flag_test(l_cur, BM_ELEM_TAG) && + BM_loop_check_cyclic_smooth_fan(l_cur)))) { + + if (!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG) && !BM_elem_flag_test(l_cur->prev->e, BM_ELEM_TAG)) { + const int loop_index = BM_elem_index_get(l_cur); + short *clnors = BM_ELEM_CD_GET_VOID_P(l_cur, cd_clnors_offset); + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[loop_index], f->no, clnors); + } + else { + BMVert *v_pivot = l_cur->v; + BMEdge *e_next; + const BMEdge *e_org = l_cur->e; + BMLoop *lfan_pivot, *lfan_pivot_next; + + lfan_pivot = l_cur; + e_next = lfan_pivot->e; + BLI_SMALLSTACK_DECLARE(loops, BMLoop *); + float cn_wght[3] = { 0.0f, 0.0f, 0.0f }; + + while (true) { + lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next); + if (lfan_pivot_next) { + BLI_assert(lfan_pivot_next->v == v_pivot); + } + else { + e_next = (lfan_pivot->e == e_next) ? lfan_pivot->prev->e : lfan_pivot->e; + } + + BLI_SMALLSTACK_PUSH(loops, lfan_pivot); + + if (bmd->lim_flags & MOD_BEVEL_WEIGHT) { + int weight = BM_elem_float_data_get(&bm->edata, lfan_pivot->f, CD_BWEIGHT); + if (weight) { + if (bmd->hnmode == MOD_BEVEL_HN_FACE) { + float cur[3]; + mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f)); + add_v3_v3(cn_wght, cur); + } + else + add_v3_v3(cn_wght, lfan_pivot->f->no); + } + else + add_v3_v3(cn_wght, lfan_pivot->f->no); + + } + else if (bmd->lim_flags & MOD_BEVEL_VGROUP) { + const bool has_vgroup = dvert != NULL; + const bool vert_of_group = has_vgroup && defvert_find_index(&dvert[BM_elem_index_get(l->v)], vgroup) != NULL; + if (vert_of_group && bmd->hnmode == MOD_BEVEL_HN_FACE) { + float cur[3]; + mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f)); + add_v3_v3(cn_wght, cur); + } + else + add_v3_v3(cn_wght, lfan_pivot->f->no); + } + else { + float cur[3]; + mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f)); + add_v3_v3(cn_wght, cur); + } + if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) { + break; + } + lfan_pivot = lfan_pivot_next; + } + + normalize_v3(cn_wght); + mul_v3_fl(cn_wght, hn_strength); + float n_final[3]; + + while ((l = BLI_SMALLSTACK_POP(loops))) { + const int l_index = BM_elem_index_get(l); + short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset); + copy_v3_v3(n_final, l->f->no); + mul_v3_fl(n_final, 1.0f - hn_strength); + add_v3_v3(n_final, cn_wght); + normalize_v3(n_final); + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors); + } + } + } + } while ((l_cur = l_cur->next) != l_first); + } +} + /* * This calls the new bevel code (added since 2.64) */ @@ -197,6 +303,9 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp, dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, NULL); + if (bmd->hnmode != MOD_BEVEL_HN_NONE) + bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup); + if(set_wn_strength) bevel_set_weighted_normal_face_strength(bm, md->scene); -- cgit v1.2.3 From 3504b27c5074d49ed19f86c212c584f37a343d33 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Wed, 27 Jun 2018 20:19:15 +0530 Subject: Patch to fix shading continuity. Added it as extension to harden. Tried out different methods to fix normals, Though as with width and segments changes shape, orientation of new polys a non-smooth method of fix was not possible. Current method aggregates vertex normals into a smooth fan without affecting edge shading. Still need to fix the crease at new vertex edges --- source/blender/modifiers/intern/MOD_bevel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 77b306466c3..735436f3971 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -303,7 +303,7 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp, dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, NULL); - if (bmd->hnmode != MOD_BEVEL_HN_NONE) + if (bmd->hnmode != MOD_BEVEL_HN_NONE && bmd->hnmode != MOD_BEVEL_FIX_SHA) bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup); if(set_wn_strength) -- cgit v1.2.3 From 368a64fe041ee0950584f5b51e2f64036edb31d0 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Mon, 2 Jul 2018 22:55:33 +0530 Subject: Refactored bevel normal editing functionality. --- source/blender/modifiers/intern/MOD_bevel.c | 69 +++++++++++++++++++++++++++-- 1 file changed, 66 insertions(+), 3 deletions(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 735436f3971..b30d6c2e669 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -140,6 +140,7 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn e_next = lfan_pivot->e; BLI_SMALLSTACK_DECLARE(loops, BMLoop *); float cn_wght[3] = { 0.0f, 0.0f, 0.0f }; + bool normal_to_recon_face = false; while (true) { lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next); @@ -208,6 +209,62 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn } } +static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *bm) +{ + BM_mesh_normals_update(bm); + BM_lnorspace_update(bm); + + GHash *faceHash = bmd->clnordata.faceHash; + BMEdge *e; + BMLoop *l; + BMIter liter, eiter; + + int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL); + float ref = 10.0f; + + BM_ITER_MESH(e, &eiter, bm, BM_EDGES_OF_MESH) { + BMFace *f_a, *f_b; + BM_edge_face_pair(e, &f_a, &f_b); + + bool _f_a = false, _f_b = false; + if (f_a) + _f_a = BLI_ghash_haskey(faceHash, f_a); + if (f_b) + _f_b = BLI_ghash_haskey(faceHash, f_b); + if (_f_a ^ _f_b) { + + for (int i = 0; i < 2; i++) { + BMVert *v = (i == 0) ? e->v1 : e->v2; + BM_ITER_ELEM(l, &liter, v, BM_LOOPS_OF_VERT) { + + if (l->f == f_a || l->f == f_b) { + const int l_index = BM_elem_index_get(l); + short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset); + float n_final[3], pow_a[3], pow_b[3]; + + zero_v3(n_final); + copy_v3_v3(pow_a, f_a->no); + copy_v3_v3(pow_b, f_b->no); + if (_f_a) { + mul_v3_fl(pow_a, bmd->res / ref); + mul_v3_fl(pow_b, ref / bmd->res); + } + else { + mul_v3_fl(pow_b, bmd->res / ref); + mul_v3_fl(pow_a, ref / bmd->res); + } + add_v3_v3(n_final, pow_a); + add_v3_v3(n_final, pow_b); + normalize_v3(n_final); + + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors); + } + } + } + } + } +} + /* * This calls the new bevel code (added since 2.64) */ @@ -301,10 +358,14 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes BM_mesh_bevel(bm, bmd->value, offset_type, bmd->res, bmd->profile, vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp, - dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, NULL); + dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, &bmd->clnordata); - if (bmd->hnmode != MOD_BEVEL_HN_NONE && bmd->hnmode != MOD_BEVEL_FIX_SHA) - bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup); + if (bmd->hnmode != MOD_BEVEL_HN_NONE) { + if (bmd->hnmode != BEVEL_HN_FIX_SHA) + bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup); + else + bevel_fix_normal_shading_continuity(bmd, bm); + } if(set_wn_strength) bevel_set_weighted_normal_face_strength(bm, md->scene); @@ -316,6 +377,8 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes bm->ftoolflagpool == NULL); /* make sure we never alloc'd these */ BM_mesh_free(bm); + BLI_ghash_free(bmd->clnordata.faceHash, NULL, NULL); + result->runtime.cd_dirty_vert |= CD_MASK_NORMAL; return result; -- cgit v1.2.3 From 190d1b2f7ae04cbe244b581f78dfdeb3387c9ea0 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Tue, 3 Jul 2018 19:01:20 +0530 Subject: Fixed merge errors --- source/blender/modifiers/intern/MOD_bevel.c | 11 ++++++++--- source/blender/modifiers/intern/MOD_weighted_normal.c | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index aa12093b505..67634dba170 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -53,6 +53,8 @@ #include "bmesh.h" #include "bmesh_tools.h" +#include "DEG_depsgraph_query.h" + static void initData(ModifierData *md) { BevelModifierData *bmd = (BevelModifierData *) md; @@ -289,6 +291,8 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes const bool mark_sharp = (bmd->edge_flags & MOD_BEVEL_MARK_SHARP); const bool set_wn_strength = (bmd->flags & MOD_BEVEL_SET_WN_STR); + struct Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph); + bm = BKE_mesh_to_bmesh_ex( mesh, &(struct BMeshCreateParams){0}, @@ -363,12 +367,12 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes if (bmd->hnmode != MOD_BEVEL_HN_NONE) { if (bmd->hnmode != BEVEL_HN_FIX_SHA) bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup); - else + else if(bmd->clnordata.faceHash) bevel_fix_normal_shading_continuity(bmd, bm); } if(set_wn_strength) - bevel_set_weighted_normal_face_strength(bm, md->scene); + bevel_set_weighted_normal_face_strength(bm, scene); result = BKE_bmesh_to_mesh_nomain(bm, &(struct BMeshToMeshParams){0}); @@ -377,7 +381,8 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes bm->ftoolflagpool == NULL); /* make sure we never alloc'd these */ BM_mesh_free(bm); - BLI_ghash_free(bmd->clnordata.faceHash, NULL, NULL); + if(bmd->clnordata.faceHash) + BLI_ghash_free(bmd->clnordata.faceHash, NULL, NULL); result->runtime.cd_dirty_vert |= CD_MASK_NORMAL; diff --git a/source/blender/modifiers/intern/MOD_weighted_normal.c b/source/blender/modifiers/intern/MOD_weighted_normal.c index b43ef698cc4..a2ace1aadc4 100644 --- a/source/blender/modifiers/intern/MOD_weighted_normal.c +++ b/source/blender/modifiers/intern/MOD_weighted_normal.c @@ -554,7 +554,7 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes MDeformVert *dvert; int defgrp_index; - modifier_get_vgroup_mesh(ob, result, wnmd->defgrp_name, &dvert, &defgrp_index); + MOD_get_vgroup(ctx->object, mesh, wnmd->defgrp_name, &dvert, &defgrp_index); WeightedNormalData wn_data = { .numVerts = numVerts, -- cgit v1.2.3 From 1c699d75a03c76ac6922906e8b13e6dc53d8075a Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Tue, 3 Jul 2018 21:51:01 +0530 Subject: Fixed bugs in normal shading continuity and added support to have corner vertices of a vmesh to have same normal as reconstructed face in harden --- source/blender/modifiers/intern/MOD_bevel.c | 56 ++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 8 deletions(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 67634dba170..f89494f7c71 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -111,15 +111,18 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn { if (bmd->res > 20) return; + BM_mesh_normals_update(bm); BM_lnorspace_update(bm); BM_normals_loops_edges_tag(bm, true); + const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0; int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL); BMFace *f; BMLoop *l, *l_cur, *l_first; BMIter fiter; + GHash *faceHash = bmd->clnordata.faceHash; BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) { l_cur = l_first = BM_FACE_FIRST_LOOP(f); @@ -142,7 +145,8 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn e_next = lfan_pivot->e; BLI_SMALLSTACK_DECLARE(loops, BMLoop *); float cn_wght[3] = { 0.0f, 0.0f, 0.0f }; - bool normal_to_recon_face = false; + int recon_face_count = 0; /* Reconstructed face */ + BMFace *recon_face; while (true) { lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next); @@ -190,6 +194,10 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn break; } lfan_pivot = lfan_pivot_next; + if (!BLI_ghash_haskey(faceHash, f)) { + recon_face = f; + recon_face_count++; + } } normalize_v3(cn_wght); @@ -199,11 +207,19 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn while ((l = BLI_SMALLSTACK_POP(loops))) { const int l_index = BM_elem_index_get(l); short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset); - copy_v3_v3(n_final, l->f->no); - mul_v3_fl(n_final, 1.0f - hn_strength); - add_v3_v3(n_final, cn_wght); - normalize_v3(n_final); - BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors); + + if (!vertex_only || !recon_face_count) { + copy_v3_v3(n_final, l->f->no); + mul_v3_fl(n_final, 1.0f - hn_strength); + add_v3_v3(n_final, cn_wght); + normalize_v3(n_final); + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors); + } + else if (recon_face_count == 1) { + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], recon_face->no, clnors); + } + else if(BLI_ghash_haskey(faceHash, l->f)) + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], l->v->no, clnors); } } } @@ -222,6 +238,7 @@ static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *b BMIter liter, eiter; int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL); + const float hn_strength = bmd->hn_strength; float ref = 10.0f; BM_ITER_MESH(e, &eiter, bm, BM_EDGES_OF_MESH) { @@ -264,6 +281,29 @@ static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *b } } } + else if(_f_a == true && _f_b == true) { + for (int i = 0; i < 2; i++) { + BMVert *v = (i == 0) ? e->v1 : e->v2; + BM_ITER_ELEM(l, &liter, v, BM_LOOPS_OF_VERT) { + + if(l->f == f_a || l->f == f_b) { + const int l_index = BM_elem_index_get(l); + short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset); + float n_final[3], cn_wght[3]; + + copy_v3_v3(n_final, v->no); + mul_v3_fl(n_final, hn_strength); + + copy_v3_v3(cn_wght, l->f->no); + mul_v3_fl(cn_wght, 1.0f - hn_strength); + + add_v3_v3(n_final, cn_wght); + normalize_v3(n_final); + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors); + } + } + } + } } } @@ -364,10 +404,10 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp, dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, &bmd->clnordata); - if (bmd->hnmode != MOD_BEVEL_HN_NONE) { + if (bmd->value > 0 && bmd->hnmode != MOD_BEVEL_HN_NONE) { if (bmd->hnmode != BEVEL_HN_FIX_SHA) bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup); - else if(bmd->clnordata.faceHash) + else if(bmd->clnordata.faceHash && !vertex_only) bevel_fix_normal_shading_continuity(bmd, bm); } -- cgit v1.2.3 From b360c9d7f9d1fbb85b6cb71109da39731e3870c6 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Wed, 4 Jul 2018 00:04:03 +0530 Subject: cleanup of extend edge data and fixed minor errors --- source/blender/modifiers/intern/MOD_bevel.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index f89494f7c71..a61705646e6 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -190,14 +190,14 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f)); add_v3_v3(cn_wght, cur); } - if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) { - break; - } - lfan_pivot = lfan_pivot_next; if (!BLI_ghash_haskey(faceHash, f)) { recon_face = f; recon_face_count++; } + if (!BM_elem_flag_test(e_next, BM_ELEM_TAG) || (e_next == e_org)) { + break; + } + lfan_pivot = lfan_pivot_next; } normalize_v3(cn_wght); -- cgit v1.2.3 From 962f89d487da41107d7d1b1ccf7e720767359cd0 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Tue, 10 Jul 2018 22:21:28 +0530 Subject: Changed default strength in harden --- source/blender/modifiers/intern/MOD_bevel.c | 1 + 1 file changed, 1 insertion(+) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index a61705646e6..3418922e812 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -70,6 +70,7 @@ static void initData(ModifierData *md) bmd->profile = 0.5f; bmd->bevel_angle = DEG2RADF(30.0f); bmd->defgrp_name[0] = '\0'; + bmd->hn_strength = 0.5f; } static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md) -- cgit v1.2.3 From 75149b429f1567e3358750a62eadeb1c32fef032 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Wed, 11 Jul 2018 22:36:44 +0530 Subject: Added support for beveling curves --- source/blender/modifiers/intern/MOD_bevel.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 3418922e812..f19443f2d5c 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -442,8 +442,9 @@ ModifierTypeInfo modifierType_Bevel = { /* type */ eModifierTypeType_Constructive, /* flags */ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_SupportsEditmode | - eModifierTypeFlag_EnableInEditmode, - + eModifierTypeFlag_EnableInEditmode | + eModifierTypeFlag_AcceptsCVs, + /* copyData */ modifier_copyData_generic, /* deformVerts_DM */ NULL, -- cgit v1.2.3 From 10c1f3fbfeadf009bac8b7f145ae6d55be0f30be Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Thu, 12 Jul 2018 23:30:29 +0530 Subject: Fixed shading errors with normals and added proper weighting to harden normals to make it consistent with wn modifier --- source/blender/modifiers/intern/MOD_bevel.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index f19443f2d5c..06c4f8c57fd 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -119,6 +119,7 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0; int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL); + bool do_normal_to_recon = (hn_strength == 1.0f); BMFace *f; BMLoop *l, *l_cur, *l_first; @@ -126,6 +127,9 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn GHash *faceHash = bmd->clnordata.faceHash; BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) { + if (!BLI_ghash_haskey(faceHash, f)) { + BM_elem_flag_set(f, BM_ELEM_HIDDEN, true); + } l_cur = l_first = BM_FACE_FIRST_LOOP(f); do { if ((!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG) || (!BM_elem_flag_test(l_cur, BM_ELEM_TAG) && @@ -191,7 +195,7 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f)); add_v3_v3(cn_wght, cur); } - if (!BLI_ghash_haskey(faceHash, f)) { + if (!BLI_ghash_haskey(faceHash, lfan_pivot->f)) { recon_face = f; recon_face_count++; } @@ -209,16 +213,16 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn const int l_index = BM_elem_index_get(l); short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset); - if (!vertex_only || !recon_face_count) { + if (recon_face_count == 1 || do_normal_to_recon) { + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], recon_face->no, clnors); + } + else if (vertex_only == false || recon_face_count == 0) { copy_v3_v3(n_final, l->f->no); mul_v3_fl(n_final, 1.0f - hn_strength); add_v3_v3(n_final, cn_wght); normalize_v3(n_final); BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors); } - else if (recon_face_count == 1) { - BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], recon_face->no, clnors); - } else if(BLI_ghash_haskey(faceHash, l->f)) BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], l->v->no, clnors); } -- cgit v1.2.3 From b6b185691f018f6b175ffb58c65418991cda75f2 Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Thu, 19 Jul 2018 19:27:45 +0530 Subject: Code cleanup and fixes --- source/blender/modifiers/intern/MOD_bevel.c | 40 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 06c4f8c57fd..1ada8b215c5 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -108,9 +108,9 @@ static void bevel_set_weighted_normal_face_strength(BMesh *bm, Scene *scene) } } -static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn_strength, int hnmode, MDeformVert *dvert, int vgroup) +static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const float hn_strength, const int hnmode, MDeformVert *dvert, int vgroup) { - if (bmd->res > 20) + if (bmd->res > 20 || bmd->value == 0) return; BM_mesh_normals_update(bm); @@ -118,8 +118,8 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn BM_normals_loops_edges_tag(bm, true); const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0; - int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL); - bool do_normal_to_recon = (hn_strength == 1.0f); + const int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL); + const bool do_normal_to_recon = (hn_strength == 1.0f); BMFace *f; BMLoop *l, *l_cur, *l_first; @@ -127,9 +127,7 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn GHash *faceHash = bmd->clnordata.faceHash; BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) { - if (!BLI_ghash_haskey(faceHash, f)) { - BM_elem_flag_set(f, BM_ELEM_HIDDEN, true); - } + l_cur = l_first = BM_FACE_FIRST_LOOP(f); do { if ((!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG) || (!BM_elem_flag_test(l_cur, BM_ELEM_TAG) && @@ -234,6 +232,10 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, float hn static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *bm) { + const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0; + if (bmd->value == 0 || bmd->clnordata.faceHash == NULL && vertex_only) + return; + BM_mesh_normals_update(bm); BM_lnorspace_update(bm); @@ -242,7 +244,7 @@ static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *b BMLoop *l; BMIter liter, eiter; - int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL); + const int cd_clnors_offset = CustomData_get_offset(&bm->ldata, CD_CUSTOMLOOPNORMAL); const float hn_strength = bmd->hn_strength; float ref = 10.0f; @@ -250,12 +252,12 @@ static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *b BMFace *f_a, *f_b; BM_edge_face_pair(e, &f_a, &f_b); - bool _f_a = false, _f_b = false; + bool has_f_a = false, has_f_b = false; if (f_a) - _f_a = BLI_ghash_haskey(faceHash, f_a); + has_f_a = BLI_ghash_haskey(faceHash, f_a); if (f_b) - _f_b = BLI_ghash_haskey(faceHash, f_b); - if (_f_a ^ _f_b) { + has_f_b = BLI_ghash_haskey(faceHash, f_b); + if (has_f_a ^ has_f_b) { for (int i = 0; i < 2; i++) { BMVert *v = (i == 0) ? e->v1 : e->v2; @@ -269,7 +271,7 @@ static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *b zero_v3(n_final); copy_v3_v3(pow_a, f_a->no); copy_v3_v3(pow_b, f_b->no); - if (_f_a) { + if (has_f_a) { mul_v3_fl(pow_a, bmd->res / ref); mul_v3_fl(pow_b, ref / bmd->res); } @@ -286,7 +288,7 @@ static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *b } } } - else if(_f_a == true && _f_b == true) { + else if(has_f_a == true && has_f_b == true) { for (int i = 0; i < 2; i++) { BMVert *v = (i == 0) ? e->v1 : e->v2; BM_ITER_ELEM(l, &liter, v, BM_LOOPS_OF_VERT) { @@ -409,13 +411,11 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes vertex_only, bmd->lim_flags & MOD_BEVEL_WEIGHT, do_clamp, dvert, vgroup, mat, loop_slide, mark_seam, mark_sharp, bmd->hnmode, &bmd->clnordata); - if (bmd->value > 0 && bmd->hnmode != MOD_BEVEL_HN_NONE) { - if (bmd->hnmode != BEVEL_HN_FIX_SHA) - bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup); - else if(bmd->clnordata.faceHash && !vertex_only) - bevel_fix_normal_shading_continuity(bmd, bm); + if (bmd->hnmode != BEVEL_HN_FIX_SHA && bmd->hnmode != MOD_BEVEL_HN_NONE) { + bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup); } - + if(bmd->hnmode == BEVEL_HN_FIX_SHA) + bevel_fix_normal_shading_continuity(bmd, bm); if(set_wn_strength) bevel_set_weighted_normal_face_strength(bm, scene); -- cgit v1.2.3 From ef8ab3b4c2aa4b709acf6b4c8263ceb6d6f515de Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Wed, 1 Aug 2018 14:20:04 +0200 Subject: Fix issues after last 2.8 merge. --- source/blender/modifiers/intern/MOD_bevel.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 1ada8b215c5..cf0753334db 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -233,7 +233,7 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const fl static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *bm) { const bool vertex_only = (bmd->flags & MOD_BEVEL_VERT) != 0; - if (bmd->value == 0 || bmd->clnordata.faceHash == NULL && vertex_only) + if (bmd->value == 0 || (bmd->clnordata.faceHash == NULL && vertex_only)) return; BM_mesh_normals_update(bm); -- cgit v1.2.3 From 9a43ebdd5b5e7c8f4a4202d7eb2bcb3e33a2d76e Mon Sep 17 00:00:00 2001 From: Bastien Montagne Date: Wed, 1 Aug 2018 14:29:36 +0200 Subject: Fix more merge stupid leftover, and some build warnings. --- source/blender/modifiers/intern/MOD_bevel.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index cf0753334db..3db4089138b 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -143,13 +143,14 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const fl BMEdge *e_next; const BMEdge *e_org = l_cur->e; BMLoop *lfan_pivot, *lfan_pivot_next; + UNUSED_VARS_NDEBUG(v_pivot); lfan_pivot = l_cur; e_next = lfan_pivot->e; BLI_SMALLSTACK_DECLARE(loops, BMLoop *); float cn_wght[3] = { 0.0f, 0.0f, 0.0f }; int recon_face_count = 0; /* Reconstructed face */ - BMFace *recon_face; + BMFace *recon_face = NULL; while (true) { lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next); @@ -165,7 +166,7 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const fl if (bmd->lim_flags & MOD_BEVEL_WEIGHT) { int weight = BM_elem_float_data_get(&bm->edata, lfan_pivot->f, CD_BWEIGHT); if (weight) { - if (bmd->hnmode == MOD_BEVEL_HN_FACE) { + if (hnmode == MOD_BEVEL_HN_FACE) { float cur[3]; mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f)); add_v3_v3(cn_wght, cur); @@ -180,7 +181,7 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const fl else if (bmd->lim_flags & MOD_BEVEL_VGROUP) { const bool has_vgroup = dvert != NULL; const bool vert_of_group = has_vgroup && defvert_find_index(&dvert[BM_elem_index_get(l->v)], vgroup) != NULL; - if (vert_of_group && bmd->hnmode == MOD_BEVEL_HN_FACE) { + if (vert_of_group && hnmode == MOD_BEVEL_HN_FACE) { float cur[3]; mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f)); add_v3_v3(cn_wght, cur); -- cgit v1.2.3 From 710d2def6509d2c993cb96f42eb1a97ccc60bfcf Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Sat, 4 Aug 2018 23:38:12 +0530 Subject: Initialized normal data in BevMod --- source/blender/modifiers/intern/MOD_bevel.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 3db4089138b..152fee9eb77 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -70,7 +70,9 @@ static void initData(ModifierData *md) bmd->profile = 0.5f; bmd->bevel_angle = DEG2RADF(30.0f); bmd->defgrp_name[0] = '\0'; + bmd->hnmode = MOD_BEVEL_HN_NONE; bmd->hn_strength = 0.5f; + bmd->clnordata.faceHash = NULL; } static CustomDataMask requiredDataMask(Object *UNUSED(ob), ModifierData *md) -- cgit v1.2.3 From c41ce58fdeba46dd96dccd7d2b98b1d3f5de726e Mon Sep 17 00:00:00 2001 From: Rohan Rathi Date: Sun, 5 Aug 2018 08:39:20 +0530 Subject: Fix indentation, spacing and added comments --- source/blender/modifiers/intern/MOD_bevel.c | 51 ++++++++++++++-------- .../blender/modifiers/intern/MOD_weighted_normal.c | 6 ++- 2 files changed, 37 insertions(+), 20 deletions(-) (limited to 'source/blender/modifiers') diff --git a/source/blender/modifiers/intern/MOD_bevel.c b/source/blender/modifiers/intern/MOD_bevel.c index 152fee9eb77..f1dc73436ee 100644 --- a/source/blender/modifiers/intern/MOD_bevel.c +++ b/source/blender/modifiers/intern/MOD_bevel.c @@ -110,7 +110,9 @@ static void bevel_set_weighted_normal_face_strength(BMesh *bm, Scene *scene) } } -static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const float hn_strength, const int hnmode, MDeformVert *dvert, int vgroup) +static void bevel_mod_harden_normals( + BevelModifierData *bmd, BMesh *bm, const float hn_strength, + const int hnmode, MDeformVert *dvert, int vgroup) { if (bmd->res > 20 || bmd->value == 0) return; @@ -128,13 +130,16 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const fl BMIter fiter; GHash *faceHash = bmd->clnordata.faceHash; + /* Iterate throught all loops of a face */ BM_ITER_MESH(f, &fiter, bm, BM_FACES_OF_MESH) { l_cur = l_first = BM_FACE_FIRST_LOOP(f); do { - if ((!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG) || (!BM_elem_flag_test(l_cur, BM_ELEM_TAG) && - BM_loop_check_cyclic_smooth_fan(l_cur)))) { + if ((!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG)) || + (!BM_elem_flag_test(l_cur, BM_ELEM_TAG) && BM_loop_check_cyclic_smooth_fan(l_cur))) + { + /* previous and next edge is sharp, accumulate face normals into loop */ if (!BM_elem_flag_test(l_cur->e, BM_ELEM_TAG) && !BM_elem_flag_test(l_cur->prev->e, BM_ELEM_TAG)) { const int loop_index = BM_elem_index_get(l_cur); short *clnors = BM_ELEM_CD_GET_VOID_P(l_cur, cd_clnors_offset); @@ -151,8 +156,8 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const fl e_next = lfan_pivot->e; BLI_SMALLSTACK_DECLARE(loops, BMLoop *); float cn_wght[3] = { 0.0f, 0.0f, 0.0f }; - int recon_face_count = 0; /* Reconstructed face */ - BMFace *recon_face = NULL; + int recon_face_count = 0; /* Counts number of reconstructed faces current vert is connected to */ + BMFace *recon_face = NULL; /* Reconstructed face */ while (true) { lfan_pivot_next = BM_vert_step_fan_loop(lfan_pivot, &e_next); @@ -169,12 +174,12 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const fl int weight = BM_elem_float_data_get(&bm->edata, lfan_pivot->f, CD_BWEIGHT); if (weight) { if (hnmode == MOD_BEVEL_HN_FACE) { - float cur[3]; + float cur[3]; //Add area weighted face normals mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f)); add_v3_v3(cn_wght, cur); } else - add_v3_v3(cn_wght, lfan_pivot->f->no); + add_v3_v3(cn_wght, lfan_pivot->f->no); //Else simply add face normals } else add_v3_v3(cn_wght, lfan_pivot->f->no); @@ -182,7 +187,9 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const fl } else if (bmd->lim_flags & MOD_BEVEL_VGROUP) { const bool has_vgroup = dvert != NULL; - const bool vert_of_group = has_vgroup && defvert_find_index(&dvert[BM_elem_index_get(l->v)], vgroup) != NULL; + const bool vert_of_group = has_vgroup && + (defvert_find_index(&dvert[BM_elem_index_get(l->v)], vgroup) != NULL); + if (vert_of_group && hnmode == MOD_BEVEL_HN_FACE) { float cur[3]; mul_v3_v3fl(cur, lfan_pivot->f->no, BM_face_calc_area(lfan_pivot->f)); @@ -214,18 +221,22 @@ static void bevel_mod_harden_normals(BevelModifierData *bmd, BMesh *bm, const fl const int l_index = BM_elem_index_get(l); short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset); + /* If vertex is edge vert with 1 reconnected face */ if (recon_face_count == 1 || do_normal_to_recon) { - BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], recon_face->no, clnors); + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], recon_face->no, + clnors); } else if (vertex_only == false || recon_face_count == 0) { copy_v3_v3(n_final, l->f->no); mul_v3_fl(n_final, 1.0f - hn_strength); add_v3_v3(n_final, cn_wght); normalize_v3(n_final); - BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, clnors); + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], n_final, + clnors); } - else if(BLI_ghash_haskey(faceHash, l->f)) - BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], l->v->no, clnors); + else if (BLI_ghash_haskey(faceHash, l->f)) + BKE_lnor_space_custom_normal_to_data(bm->lnor_spacearr->lspacearr[l_index], l->v->no, + clnors); } } } @@ -261,6 +272,8 @@ static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *b if (f_b) has_f_b = BLI_ghash_haskey(faceHash, f_b); if (has_f_a ^ has_f_b) { + /* If one of both faces is present in faceHash then we are at a border + * between new vmesh created and reconstructed face */ for (int i = 0; i < 2; i++) { BMVert *v = (i == 0) ? e->v1 : e->v2; @@ -291,12 +304,14 @@ static void bevel_fix_normal_shading_continuity(BevelModifierData *bmd, BMesh *b } } } - else if(has_f_a == true && has_f_b == true) { + else if (has_f_a == true && has_f_b == true) { + /* Else if both faces are present we assign clnor corresponding + * to vert normal and face normal */ for (int i = 0; i < 2; i++) { BMVert *v = (i == 0) ? e->v1 : e->v2; BM_ITER_ELEM(l, &liter, v, BM_LOOPS_OF_VERT) { - if(l->f == f_a || l->f == f_b) { + if (l->f == f_a || l->f == f_b) { const int l_index = BM_elem_index_get(l); short *clnors = BM_ELEM_CD_GET_VOID_P(l, cd_clnors_offset); float n_final[3], cn_wght[3]; @@ -417,9 +432,9 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes if (bmd->hnmode != BEVEL_HN_FIX_SHA && bmd->hnmode != MOD_BEVEL_HN_NONE) { bevel_mod_harden_normals(bmd, bm, bmd->hn_strength, bmd->hnmode, dvert, vgroup); } - if(bmd->hnmode == BEVEL_HN_FIX_SHA) + if (bmd->hnmode == BEVEL_HN_FIX_SHA) bevel_fix_normal_shading_continuity(bmd, bm); - if(set_wn_strength) + if (set_wn_strength) bevel_set_weighted_normal_face_strength(bm, scene); result = BKE_bmesh_to_mesh_nomain(bm, &(struct BMeshToMeshParams){0}); @@ -429,7 +444,7 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes bm->ftoolflagpool == NULL); /* make sure we never alloc'd these */ BM_mesh_free(bm); - if(bmd->clnordata.faceHash) + if (bmd->clnordata.faceHash) BLI_ghash_free(bmd->clnordata.faceHash, NULL, NULL); result->runtime.cd_dirty_vert |= CD_MASK_NORMAL; @@ -450,7 +465,7 @@ ModifierTypeInfo modifierType_Bevel = { /* flags */ eModifierTypeFlag_AcceptsMesh | eModifierTypeFlag_SupportsEditmode | eModifierTypeFlag_EnableInEditmode | - eModifierTypeFlag_AcceptsCVs, + eModifierTypeFlag_AcceptsCVs, /* copyData */ modifier_copyData_generic, diff --git a/source/blender/modifiers/intern/MOD_weighted_normal.c b/source/blender/modifiers/intern/MOD_weighted_normal.c index a2ace1aadc4..3e788db9c00 100644 --- a/source/blender/modifiers/intern/MOD_weighted_normal.c +++ b/source/blender/modifiers/intern/MOD_weighted_normal.c @@ -217,7 +217,8 @@ static void apply_weights_vertex_normal(WeightedNormalModifierData *wnmd, Weight num_items = lnors_spacearr.num_spaces; items_data = MEM_calloc_arrayN((size_t)num_items, sizeof(*items_data), __func__); - /* In this first loop, we assign each WeightedNormalDataAggregateItem to its smooth fan of loops (aka lnor space). */ + /* In this first loop, we assign each WeightedNormalDataAggregateItem + * to its smooth fan of loops (aka lnor space). */ MPoly *mp; int mp_index; int item_index; @@ -572,7 +573,8 @@ static Mesh *applyModifier(ModifierData *md, const ModifierEvalContext *ctx, Mes .mpoly = mpoly, .polynors = polynors, - .poly_strength = CustomData_get_layer_named(&result->pdata, CD_PROP_INT, MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID), + .poly_strength = CustomData_get_layer_named(&result->pdata, CD_PROP_INT, + MOD_WEIGHTEDNORMALS_FACEWEIGHT_CDLAYER_ID), .dvert = dvert, .defgrp_index = defgrp_index, -- cgit v1.2.3