[Bf-blender-cvs] [9a5e178] temp_custom_loop_normals: Set clnors code: add option to use existing custom split normals as ref if possible

Bastien Montagne noreply at git.blender.org
Thu Aug 21 13:04:40 CEST 2014


Commit: 9a5e1782b9b443916deb6c4ab545ab19dd96294e
Author: Bastien Montagne
Date:   Thu Aug 21 13:01:35 2014 +0200
Branches: temp_custom_loop_normals
https://developer.blender.org/rB9a5e1782b9b443916deb6c4ab545ab19dd96294e

Set clnors code: add option to use existing custom split normals as ref if possible

In case of setting custom loop normals, add option to weight them against existing
custom loop normals if existing, instead of auto loop normals.

Allows to really edit existing clnors, instead of systematically overwritting them.

===================================================================

M	source/blender/blenkernel/BKE_mesh.h
M	source/blender/blenkernel/intern/mesh_evaluate.c
M	source/blender/makesrna/intern/rna_mesh_api.c

===================================================================

diff --git a/source/blender/blenkernel/BKE_mesh.h b/source/blender/blenkernel/BKE_mesh.h
index f98703a..d616021 100644
--- a/source/blender/blenkernel/BKE_mesh.h
+++ b/source/blender/blenkernel/BKE_mesh.h
@@ -222,7 +222,8 @@ void BKE_mesh_normals_loop_split(
 void BKE_mesh_normals_loop_custom_set(
         struct MVert *mverts, const int numVerts, struct MEdge *medges, const int numEdges,
         struct MLoop *mloops, float (*custom_loopnors)[3], const float *custom_loopnors_facs, const int numLoops,
-        struct MPoly *mpolys, const float (*polynors)[3], const int numPolys, short (*r_clnors_data)[2]);
+        struct MPoly *mpolys, const float (*polynors)[3], const int numPolys,
+        short (*r_clnors_data)[2], const bool use_clnors_data);
 void BKE_mesh_normals_loop_custom_from_vertices_set(
         struct MVert *mverts, float (*custom_vertnors)[3], const float *custom_vertnors_facs, const int numVerts,
         struct MEdge *medges, const int numEdges, struct MLoop *mloops, const int numLoops,
diff --git a/source/blender/blenkernel/intern/mesh_evaluate.c b/source/blender/blenkernel/intern/mesh_evaluate.c
index d33c890..9771a5b 100644
--- a/source/blender/blenkernel/intern/mesh_evaluate.c
+++ b/source/blender/blenkernel/intern/mesh_evaluate.c
@@ -1215,12 +1215,14 @@ void BKE_mesh_normals_loop_split(MVert *mverts, const int numVerts, MEdge *medge
  * same custom lnor for all loops sharing a same smooth fan.
  * If use_vertices if true, custom_loopnors and custom_loopnors_facs are assumed to be per-vertex, not per-loop
  * (this allows to set whole vert's normals at once, useful in some cases).
+ * If use_vertices is false and use_clnors_data is true, auto lnors will be computed using data available in given
+ * r_clnors_data. Useful to edit custom normals instead of simply overwriting them!
  */
 static void mesh_normals_loop_custom_set(MVert *mverts, const int numVerts, MEdge *medges, const int numEdges,
                                          MLoop *mloops, float (*custom_loopnors)[3],
                                          const float *custom_loopnors_facs, const int numLoops,
                                          MPoly *mpolys, const float (*polynors)[3], const int numPolys,
-                                         short (*r_clnors_data)[2], const bool use_vertices)
+                                         short (*r_clnors_data)[2], const bool use_clnors_data, const bool use_vertices)
 {
 	/* We *may* make that poor BKE_mesh_normals_loop_split() even more complex by making it handling that
 	 * feature too, would probably be more efficient in absolute.
@@ -1239,7 +1241,8 @@ static void mesh_normals_loop_custom_set(MVert *mverts, const int numVerts, MEdg
 
 	/* Compute current lnor spaces. */
 	BKE_mesh_normals_loop_split(mverts, numVerts, medges, numEdges, mloops, lnors, numLoops,
-	                            mpolys, polynors, numPolys, split_angle, &lnors_spaces, NULL, loop_to_poly);
+	                            mpolys, polynors, numPolys, split_angle,
+	                            &lnors_spaces, use_clnors_data ? r_clnors_data : NULL, loop_to_poly);
 
 	/* Now, check each current smooth fan (one lnor space per smooth fan!), and if all its matching custom lnors
 	 * are not (enough) equal, add sharp edges as needed.
@@ -1313,7 +1316,8 @@ static void mesh_normals_loop_custom_set(MVert *mverts, const int numVerts, MEdg
 		/* And now, recompute our new auto lnors and lnor spaces! */
 		BKE_free_loops_normal_spaces(&lnors_spaces);
 		BKE_mesh_normals_loop_split(mverts, numVerts, medges, numEdges, mloops, lnors, numLoops,
-		                            mpolys, polynors, numPolys, split_angle, &lnors_spaces, NULL, loop_to_poly);
+		                            mpolys, polynors, numPolys, split_angle,
+		                            &lnors_spaces, use_clnors_data ? r_clnors_data : NULL, loop_to_poly);
 	}
 	else {
 		BLI_BITMAP_ENABLE_ALL(done_loops, (size_t)numLoops);
@@ -1360,7 +1364,8 @@ static void mesh_normals_loop_custom_set(MVert *mverts, const int numVerts, MEdg
 				if (custom_loopnors_facs) {
 					avg_fac /= (float)nbr_nors;
 					if (avg_fac < 1.0f - 1e-6f) {
-						interp_v3_v3v3_slerp_safe(avg_nor, lnors_spaces.lspaces[i]->vec_lnor, avg_nor, avg_fac);
+						/* Slerp with final computed lnors, which may include custom lnors if required! */
+						interp_v3_v3v3_slerp_safe(avg_nor, lnors[i], avg_nor, avg_fac);
 					}
 				}
 				BKE_lnor_space_custom_normal_to_data(lnors_spaces.lspaces[i], avg_nor, clnor_data_tmp);
@@ -1379,7 +1384,8 @@ static void mesh_normals_loop_custom_set(MVert *mverts, const int numVerts, MEdg
 					const float fac = custom_loopnors_facs[nidx];
 
 					if (fac < 1.0f - 1e-6f) {
-						interp_v3_v3v3_slerp_safe(tnor, lnors_spaces.lspaces[i]->vec_lnor, nor, fac);
+						/* Slerp with final computed lnors, which may include custom lnors if required! */
+						interp_v3_v3v3_slerp_safe(tnor, lnors[i], nor, fac);
 						nor = tnor;
 					}
 				}
@@ -1400,11 +1406,11 @@ void BKE_mesh_normals_loop_custom_set(MVert *mverts, const int numVerts, MEdge *
                                       MLoop *mloops, float (*custom_loopnors)[3],
                                       const float *custom_loopnors_facs, const int numLoops,
                                       MPoly *mpolys, const float (*polynors)[3], const int numPolys,
-                                      short (*r_clnors_data)[2])
+                                      short (*r_clnors_data)[2], const bool use_clnors_data)
 {
 	mesh_normals_loop_custom_set(mverts, numVerts, medges, numEdges, mloops, custom_loopnors,
 	                             custom_loopnors_facs, numLoops, mpolys, polynors, numPolys,
-	                             r_clnors_data, false);
+	                             r_clnors_data, use_clnors_data, false);
 }
 
 void BKE_mesh_normals_loop_custom_from_vertices_set(MVert *mverts, float (*custom_vertnors)[3],
@@ -1416,7 +1422,7 @@ void BKE_mesh_normals_loop_custom_from_vertices_set(MVert *mverts, float (*custo
 {
 	mesh_normals_loop_custom_set(mverts, numVerts, medges, numEdges, mloops, custom_vertnors,
 	                             custom_vertnors_facs, numLoops, mpolys, polynors, numPolys,
-	                             r_clnors_data, true);
+	                             r_clnors_data, false, true);
 }
 
 /** \} */
diff --git a/source/blender/makesrna/intern/rna_mesh_api.c b/source/blender/makesrna/intern/rna_mesh_api.c
index cea845e..15504cc 100644
--- a/source/blender/makesrna/intern/rna_mesh_api.c
+++ b/source/blender/makesrna/intern/rna_mesh_api.c
@@ -154,7 +154,8 @@ static void rna_Mesh_calc_smooth_groups(Mesh *mesh, int use_bitflags, int *r_pol
 }
 
 static void rna_Mesh_define_normals_split_custom_do(Mesh *mesh, float (*custom_loopnors)[3],
-                                                    const float *custom_loopnors_factors, const bool use_vertices)
+                                                    const float *custom_loopnors_factors,
+                                                    bool use_current_clnors, const bool use_vertices)
 {
 	float (*polynors)[3];
 	short (*clnors)[2];
@@ -167,6 +168,7 @@ static void rna_Mesh_define_normals_split_custom_do(Mesh *mesh, float (*custom_l
 	}
 	else {
 		clnors = CustomData_add_layer(&mesh->ldata, CD_CUSTOMLOOPNORMAL, CD_DEFAULT, NULL, numloops);
+		use_current_clnors = false;
 	}
 
 	if (CustomData_has_layer(&mesh->pdata, CD_NORMAL)) {
@@ -188,7 +190,8 @@ static void rna_Mesh_define_normals_split_custom_do(Mesh *mesh, float (*custom_l
 	else {
 		BKE_mesh_normals_loop_custom_set(mesh->mvert, mesh->totvert, mesh->medge, mesh->totedge,
 		                                 mesh->mloop, custom_loopnors, custom_loopnors_factors, mesh->totloop,
-		                                 mesh->mpoly, (const float (*)[3])polynors, mesh->totpoly, clnors);
+		                                 mesh->mpoly, (const float (*)[3])polynors, mesh->totpoly,
+		                                 clnors, use_current_clnors);
 	}
 
 	if (free_polynors) {
@@ -197,7 +200,7 @@ static void rna_Mesh_define_normals_split_custom_do(Mesh *mesh, float (*custom_l
 }
 
 static void rna_Mesh_define_normals_split_custom(Mesh *mesh, ReportList *reports, int normals_len, float *normals,
-                                                 int factors_len, float *factors)
+                                                 int factors_len, float *factors, int use_current_custom_normals)
 {
 	float (*loopnors)[3] = (float (*)[3])normals;
 	const float *loopnors_factors = (const float *)factors;
@@ -216,7 +219,7 @@ static void rna_Mesh_define_normals_split_custom(Mesh *mesh, ReportList *reports
 		return;
 	}
 
-	rna_Mesh_define_normals_split_custom_do(mesh, loopnors, loopnors_factors, false);
+	rna_Mesh_define_normals_split_custom_do(mesh, loopnors, loopnors_factors, (bool)use_current_custom_normals, false);
 }
 
 static void rna_Mesh_define_normals_split_custom_from_vertices(Mesh *mesh, ReportList *reports,
@@ -242,7 +245,7 @@ static void rna_Mesh_define_normals_split_custom_from_vertices(Mesh *mesh, Repor
 		return;
 	}
 
-	rna_Mesh_define_normals_split_custom_do(mesh, vertnors, vertnors_factors, true);
+	rna_Mesh_define_normals_split_custom_do(mesh, vertnors, vertnors_factors, false, true);
 }
 
 static void rna_Mesh_transform(Mesh *mesh, float *mat)
@@ -315,6 +318,8 @@ void RNA_api_mesh(StructRNA *srna)
 	                           "Interpolation factors (0.0 to use full auto normal, 1.0 to use full custom given one)",
 	                           0.0f, 0.0f);
 	RNA_def_property_flag(parm, PROP_DYNAMIC);
+	RNA_def_boolean(func, "use_current_custom_normals", false, "",
+	                "Try to use current custom split normals data as basis (if available), instead of plain auto ones");
 
 	func = RNA_def_function(srna, "define_normals_split_custom_from_vertices",
 	                        "rna_Mesh_define_normals_split_custom_from_vertices");




More information about the Bf-blender-cvs mailing list