[Bf-blender-cvs] [b32832a7e8f] blender2.8: Subdiv: CCG, initialize grid mask from paint mask

Sergey Sharybin noreply at git.blender.org
Fri Sep 21 15:31:52 CEST 2018


Commit: b32832a7e8f6a12eee658ff1eb1be7e18d2e6eef
Author: Sergey Sharybin
Date:   Fri Sep 21 13:58:49 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBb32832a7e8f6a12eee658ff1eb1be7e18d2e6eef

Subdiv: CCG, initialize grid mask from paint mask

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

M	source/blender/blenkernel/BKE_subdiv_ccg.h
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/subdiv_ccg.c
A	source/blender/blenkernel/intern/subdiv_ccg_mask.c
M	source/blender/blenkernel/intern/subdiv_displacement_multires.c

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

diff --git a/source/blender/blenkernel/BKE_subdiv_ccg.h b/source/blender/blenkernel/BKE_subdiv_ccg.h
index cee7bce7f05..fa3782bbd0f 100644
--- a/source/blender/blenkernel/BKE_subdiv_ccg.h
+++ b/source/blender/blenkernel/BKE_subdiv_ccg.h
@@ -43,6 +43,31 @@ struct DMFlagMat;
 struct Mesh;
 struct Subdiv;
 
+/* =============================================================================
+ * Masks.
+ */
+
+/* Functor which evaluates mask value at a given (u, v) of given ptex face. */
+typedef struct SubdivCCGMask {
+	float (*eval_mask)(struct SubdivCCGMask *mask,
+	                   const int ptex_face_index,
+	                   const float u, const float v);
+
+	/* Free the data, not the evaluator itself. */
+	void (*free)(struct SubdivCCGMask *mask);
+
+	void *user_data;
+} SubdivCCGMask;
+
+/* Return true if mesh has mask and evaluator can be used. */
+bool BKE_subdiv_ccg_mask_init_from_paint(
+        SubdivCCGMask *mask_evaluator,
+        const struct Mesh *mesh);
+
+/* =============================================================================
+ * SubdivCCG.
+ */
+
 typedef struct SubdivToCCGSettings {
 	/* Resolution at which regular ptex (created for quad polygon) are being
 	 * evaluated. This defines how many vertices final mesh will have: every
@@ -190,7 +215,8 @@ typedef struct SubdivCCG {
  */
 struct SubdivCCG *BKE_subdiv_to_ccg(
         struct Subdiv *subdiv,
-        const SubdivToCCGSettings *settings);
+        const SubdivToCCGSettings *settings,
+        SubdivCCGMask *mask_evaluator);
 
 /* Destroy CCG representation of subdivision surface. */
 void BKE_subdiv_ccg_destroy(SubdivCCG *subdiv_ccg);
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 932e21b312e..452ca90ecf1 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -195,6 +195,7 @@ set(SRC
 	intern/studiolight.c
 	intern/subdiv.c
 	intern/subdiv_ccg.c
+	intern/subdiv_ccg_mask.c
 	intern/subdiv_converter.c
 	intern/subdiv_converter_mesh.c
 	intern/subdiv_displacement.c
diff --git a/source/blender/blenkernel/intern/subdiv_ccg.c b/source/blender/blenkernel/intern/subdiv_ccg.c
index 29327a465b3..5ec675494f5 100644
--- a/source/blender/blenkernel/intern/subdiv_ccg.c
+++ b/source/blender/blenkernel/intern/subdiv_ccg.c
@@ -183,6 +183,7 @@ typedef struct CCGEvalGridsData {
 	SubdivCCG *subdiv_ccg;
 	Subdiv *subdiv;
 	int *face_ptex_offset;
+	SubdivCCGMask *mask_evaluator;
 } CCGEvalGridsData;
 
 static void subdiv_ccg_eval_grid_element(
@@ -207,6 +208,16 @@ static void subdiv_ccg_eval_grid_element(
 		BKE_subdiv_eval_limit_point(
 		        subdiv, ptex_face_index, u, v, (float *)element);
 	}
+	if (subdiv_ccg->has_mask) {
+		float *mask_value_ptr = (float *)(element + subdiv_ccg->mask_offset);
+		if (data->mask_evaluator != NULL) {
+			*mask_value_ptr = data->mask_evaluator->eval_mask(
+			        data->mask_evaluator, ptex_face_index, u, v);
+		}
+		else {
+			*mask_value_ptr = 0;
+		}
+	}
 }
 
 BLI_INLINE void rotate_corner_to_quad(
@@ -320,7 +331,8 @@ static void subdiv_ccg_eval_grids_task(
 
 static bool subdiv_ccg_evaluate_grids(
         SubdivCCG *subdiv_ccg,
-        Subdiv *subdiv)
+        Subdiv *subdiv,
+        SubdivCCGMask *mask_evaluator)
 {
 	OpenSubdiv_TopologyRefiner *topology_refiner = subdiv->topology_refiner;
 	const int num_faces = topology_refiner->getNumFaces(topology_refiner);
@@ -329,6 +341,7 @@ static bool subdiv_ccg_evaluate_grids(
 	data.subdiv_ccg = subdiv_ccg;
 	data.subdiv = subdiv;
 	data.face_ptex_offset = BKE_subdiv_face_ptex_offset_get(subdiv);
+	data.mask_evaluator = mask_evaluator;
 	/* Threaded grids evaluation. */
 	ParallelRangeSettings parallel_range_settings;
 	BLI_parallel_range_settings_defaults(&parallel_range_settings);
@@ -626,7 +639,8 @@ static void subdiv_ccg_init_faces_neighborhood(SubdivCCG *subdiv_ccg)
 
 SubdivCCG *BKE_subdiv_to_ccg(
         Subdiv *subdiv,
-        const SubdivToCCGSettings *settings)
+        const SubdivToCCGSettings *settings,
+        SubdivCCGMask *mask_evaluator)
 {
 	BKE_subdiv_stats_begin(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_CCG);
 	SubdivCCG *subdiv_ccg = MEM_callocN(sizeof(SubdivCCG), "subdiv ccg");
@@ -638,7 +652,7 @@ SubdivCCG *BKE_subdiv_to_ccg(
 	subdiv_ccg_alloc_elements(subdiv_ccg, subdiv);
 	subdiv_ccg_init_faces(subdiv_ccg);
 	subdiv_ccg_init_faces_neighborhood(subdiv_ccg);
-	if (!subdiv_ccg_evaluate_grids(subdiv_ccg, subdiv)) {
+	if (!subdiv_ccg_evaluate_grids(subdiv_ccg, subdiv, mask_evaluator)) {
 		BKE_subdiv_ccg_destroy(subdiv_ccg);
 		BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_CCG);
 		return NULL;
@@ -660,7 +674,14 @@ Mesh *BKE_subdiv_to_ccg_mesh(
 		}
 	}
 	BKE_subdiv_stats_end(&subdiv->stats, SUBDIV_STATS_SUBDIV_TO_CCG);
-	SubdivCCG *subdiv_ccg = BKE_subdiv_to_ccg(subdiv, settings);
+	SubdivCCGMask mask_evaluator;
+	bool has_mask = BKE_subdiv_ccg_mask_init_from_paint(
+        &mask_evaluator, coarse_mesh);
+	SubdivCCG *subdiv_ccg = BKE_subdiv_to_ccg(
+	    subdiv, settings, has_mask ? &mask_evaluator : NULL);
+	if (has_mask) {
+		mask_evaluator.free(&mask_evaluator);
+	}
 	if (subdiv_ccg == NULL) {
 		return NULL;
 	}
diff --git a/source/blender/blenkernel/intern/subdiv_ccg_mask.c b/source/blender/blenkernel/intern/subdiv_ccg_mask.c
new file mode 100644
index 00000000000..3805e0b4bdc
--- /dev/null
+++ b/source/blender/blenkernel/intern/subdiv_ccg_mask.c
@@ -0,0 +1,241 @@
+/*
+ * ***** 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.
+ *
+ * The Original Code is Copyright (C) 2018 by Blender Foundation.
+ * All rights reserved.
+ *
+ * Contributor(s): Sergey Sharybin.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/blenkernel/intern/subdiv_ccg_mask.c
+ *  \ingroup bke
+ */
+
+#include "BKE_subdiv_ccg.h"
+
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+#include "DNA_modifier_types.h"
+#include "DNA_object_types.h"
+
+#include "BLI_utildefines.h"
+#include "BLI_math_vector.h"
+
+#include "BKE_customdata.h"
+
+#include "MEM_guardedalloc.h"
+
+typedef struct PolyCornerIndex {
+	int poly_index;
+	int corner;
+} PolyCornerIndex;
+
+typedef struct GridPaintMaskData {
+	// int grid_size;
+	const MPoly *mpoly;
+	const GridPaintMask *grid_paint_mask;
+	/* Indexed by ptex face index, contains polygon/corner which corresponds
+	 * to it.
+	 *
+	 * NOTE: For quad polygon this is an index of first corner only, since
+	 * there we only have one ptex.
+	 */
+	PolyCornerIndex *ptex_poly_corner;
+} GridPaintMaskData;
+
+/* Coordinates within grid has different convention from PTex coordinates.
+ * This function converts the latter ones to former.
+ */
+BLI_INLINE void ptex_uv_to_grid_uv(const float ptex_u, const float ptex_v,
+                                   float *r_grid_u, float *r_grid_v)
+{
+	*r_grid_u = 1.0f - ptex_v;
+	*r_grid_v = 1.0f - ptex_u;
+}
+
+/* Simplified version of mdisp_rot_face_to_crn, only handles quad and
+ * works in normalized coordinates.
+ *
+ * NOTE: Output coordinates are in ptex coordinates.
+ */
+BLI_INLINE int rotate_quad_to_corner(const float u, const float v,
+                                     float *r_u, float *r_v)
+{
+	int corner;
+	if (u <= 0.5f && v <= 0.5f) {
+		corner = 0;
+		*r_u = 2.0f * u;
+		*r_v = 2.0f * v;
+	}
+	else if (u > 0.5f  && v <= 0.5f) {
+		corner = 1;
+		*r_u = 2.0f * v;
+		*r_v = 2.0f * (1.0f - u);
+	}
+	else if (u > 0.5f  && v > 0.5f) {
+		corner = 2;
+		*r_u = 2.0f * (1.0f - u);
+		*r_v = 2.0f * (1.0f - v);
+	}
+	else if (u <= 0.5f && v >= 0.5f) {
+		corner = 3;
+		*r_u = 2.0f * (1.0f - v);
+		*r_v = 2.0f * u;
+	}
+	else {
+		BLI_assert(!"Unexpected corner configuration");
+	}
+	return corner;
+}
+
+static int mask_get_grid_and_coord(
+        SubdivCCGMask *mask_evaluator,
+        const int ptex_face_index, const float u, const float v,
+        const GridPaintMask **r_mask_grid,
+        float *grid_u, float *grid_v)
+{
+	GridPaintMaskData *data = mask_evaluator->user_data;
+	const PolyCornerIndex *poly_corner =
+	        &data->ptex_poly_corner[ptex_face_index];
+	const MPoly *poly = &data->mpoly[poly_corner->poly_index];
+	const int start_grid_index = poly->loopstart + poly_corner->corner;
+	int corner = 0;
+	if (poly->totloop == 4) {
+		float corner_u, corner_v;
+		corner = rotate_quad_to_corner(u, v, &corner_u, &corner_v);
+		*r_mask_grid =
+		        &data->grid_paint_mask[start_grid_index + corner];
+		ptex_uv_to_grid_uv(corner_u, corner_v, grid_u, grid_v);
+	}
+	else {
+		*r_mask_grid =
+		        &data->grid_paint_mask[start_grid_index];
+		ptex_uv_to_grid_uv(u, v, grid_u, grid_v);
+	}
+	return corner;
+}
+
+BLI_INLINE float read_mask_grid(const GridPaintMask *mask_grid,
+                                const float grid_u, const float grid_v)
+{
+	if (mask_grid->data == NULL) {
+		return 0;
+	}
+	const int grid_size = (1 << (mask_grid->level - 1)) + 1;
+	const int x = (grid_u * (grid_size - 1) + 0.5f);
+	const int y = (grid_v * (grid_size - 1) + 0.5f);
+	return mask_grid->data[y * grid_size + x];
+}
+
+static float eval_mask(SubdivCCGMask *mask_evaluator,
+                       const int ptex_face_index,
+                       const float u, const float v)
+{
+	const GridPaintMask *mask_grid;
+	float grid_u, grid_v;
+	mask_get_grid_and_coord(mask_evaluator,
+	                        ptex_face_index, u, v,
+	                        &mask_grid,
+	                        &grid_u, &grid_v);
+	return read_mask_grid(mask_grid, grid_u, grid_v);
+}
+
+static void f

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list