[Bf-blender-cvs] [913b8396d97] blender2.8: Multires: Initial groundwork to hook up displacement to new Subdiv object

Sergey Sharybin noreply at git.blender.org
Wed Aug 15 15:40:15 CEST 2018


Commit: 913b8396d971f258df70827274bcdf86dd894185
Author: Sergey Sharybin
Date:   Tue Aug 14 17:05:54 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB913b8396d971f258df70827274bcdf86dd894185

Multires: Initial groundwork to hook up displacement to new Subdiv object

Adds a displacement support for OpenSubdiov based subsurf object implemented
as a callback which gives vector displacement in object space. Currently is
implemented to calculate displacement based on myltires displacement grids,
but we can support things in the future if needed.

Submitting to review to see if there is something obviously wrong in the
direction (old multires code was sharing same displacement code to both
calculate final displaced mesh and reshape an existing one, which is rather
confusing and probably can be done more cleanly?).

Reviewers: brecht

Reviewed By: brecht

Differential Revision: https://developer.blender.org/D3604

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

M	source/blender/blenkernel/BKE_subdiv.h
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/subdiv.c
A	source/blender/blenkernel/intern/subdiv_displacement.c
A	source/blender/blenkernel/intern/subdiv_displacement_multires.c
M	source/blender/blenkernel/intern/subdiv_eval.c
M	source/blender/blenkernel/intern/subdiv_mesh.c
M	source/blender/modifiers/intern/MOD_multires.c
M	source/blender/modifiers/intern/MOD_subsurf.c

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

diff --git a/source/blender/blenkernel/BKE_subdiv.h b/source/blender/blenkernel/BKE_subdiv.h
index c4628cb9da7..887eff6e7dc 100644
--- a/source/blender/blenkernel/BKE_subdiv.h
+++ b/source/blender/blenkernel/BKE_subdiv.h
@@ -29,9 +29,12 @@
 #include "BLI_sys_types.h"
 
 struct Mesh;
+struct MultiresModifierData;
+struct Object;
 struct OpenSubdiv_Converter;
 struct OpenSubdiv_Evaluator;
 struct OpenSubdiv_TopologyRefiner;
+struct Subdiv;
 
 /** \file BKE_subdiv.h
  *  \ingroup bke
@@ -92,22 +95,47 @@ typedef struct SubdivStats {
 	double begin_timestamp_[NUM_SUBDIV_STATS_VALUES];
 } SubdivStats;
 
+/* Functor which evaluates dispalcement at a given (u, v) of given ptex face. */
+typedef struct SubdivDisplacement {
+	/* Return displacement which is to be added to the original coordinate.
+	 *
+	 * NOTE: This function is supposed to return "continuous" displacement for
+	 * each pf PTex faces created for special (non-quad) polygon. This means,
+	 * if displacement is stored on per-corner manner (like MDisps for multires)
+	 * this is up the displacement implementation to average boundaries of the
+	 * displacement grids if needed.
+	 *
+	 * Averaging of displacement for vertices created for over coarse vertices
+	 * and edges is done by subdiv code.
+	 */
+	void (*eval_displacement)(struct SubdivDisplacement *displacement,
+	                          const int ptex_face_index,
+	                          const float u, const float v,
+	                          const float dPdu[3], const float dPdv[3],
+	                          float r_D[3]);
+
+	/* Free the data, not the evaluator itself. */
+	void (*free)(struct SubdivDisplacement *displacement);
+
+	void *user_data;
+} SubdivDisplacement;
+
 typedef struct Subdiv {
 	/* Settings this subdivision surface is created for.
 	 *
 	 * It is read-only after assignment in BKE_subdiv_new_from_FOO().
 	 */
 	SubdivSettings settings;
-
 	/* Topology refiner includes all the glue logic to feed Blender side
 	 * topology to OpenSubdiv. It can be shared by both evaluator and GL mesh
 	 * drawer.
 	 */
 	struct OpenSubdiv_TopologyRefiner *topology_refiner;
-
 	/* CPU side evaluator. */
 	struct OpenSubdiv_Evaluator *evaluator;
-
+	/* Optional displacement evaluator. */
+	struct SubdivDisplacement *displacement_evaluator;
+	/* Statistics for debugging. */
 	SubdivStats stats;
 } Subdiv;
 
@@ -148,29 +176,49 @@ void BKE_subdiv_eval_limit_point(
         Subdiv *subdiv,
         const int ptex_face_index,
         const float u, const float v,
-        float P[3]);
+        float r_P[3]);
 void BKE_subdiv_eval_limit_point_and_derivatives(
         Subdiv *subdiv,
         const int ptex_face_index,
         const float u, const float v,
-        float P[3], float dPdu[3], float dPdv[3]);
+        float r_P[3], float r_dPdu[3], float r_dPdv[3]);
 void BKE_subdiv_eval_limit_point_and_normal(
         Subdiv *subdiv,
         const int ptex_face_index,
         const float u, const float v,
-        float P[3], float N[3]);
+        float r_P[3], float r_N[3]);
 void BKE_subdiv_eval_limit_point_and_short_normal(
         Subdiv *subdiv,
         const int ptex_face_index,
         const float u, const float v,
-        float P[3], short N[3]);
+        float r_P[3], short r_N[3]);
 
 void BKE_subdiv_eval_face_varying(
         Subdiv *subdiv,
         const int face_varying_channel,
         const int ptex_face_index,
         const float u, const float v,
-        float varying[2]);
+        float r_varying[2]);
+
+/* NOTE: Expects derivatives to be correct.
+ *
+ * TODO(sergey): This is currently used together with
+ * BKE_subdiv_eval_final_point() which cas easily evaluate derivatives.
+ * Would be nice to have dispalcement evaluation function which does not require
+ * knowing derivatives ahead of a time.
+ */
+void BKE_subdiv_eval_displacement(
+        Subdiv *subdiv,
+        const int ptex_face_index,
+        const float u, const float v,
+		const float dPdu[3], const float dPdv[3],
+        float r_D[3]);
+
+void BKE_subdiv_eval_final_point(
+        Subdiv *subdiv,
+        const int ptex_face_index,
+        const float u, const float v,
+        float r_P[3]);
 
 /* Patch queries at given resolution.
  *
@@ -220,4 +268,13 @@ struct Mesh *BKE_subdiv_to_mesh(
         const SubdivToMeshSettings *settings,
         const struct Mesh *coarse_mesh);
 
+/* ============================ DISPLACEMENT API ============================ */
+
+void BKE_subdiv_displacement_attach_from_multires(
+        Subdiv *subdiv,
+        const struct Object *object,
+        const struct MultiresModifierData *mmd);
+
+void BKE_subdiv_displacement_detach(Subdiv *subdiv);
+
 #endif  /* __BKE_SUBDIV_H__ */
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 646665cdec2..aeb4830127d 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -194,6 +194,8 @@ set(SRC
 	intern/subdiv.c
 	intern/subdiv_converter.c
 	intern/subdiv_converter_mesh.c
+	intern/subdiv_displacement.c
+	intern/subdiv_displacement_multires.c
 	intern/subdiv_eval.c
 	intern/subdiv_mesh.c
 	intern/subdiv_stats.c
diff --git a/source/blender/blenkernel/intern/subdiv.c b/source/blender/blenkernel/intern/subdiv.c
index 4f630e8c38c..f847d62018a 100644
--- a/source/blender/blenkernel/intern/subdiv.c
+++ b/source/blender/blenkernel/intern/subdiv.c
@@ -90,6 +90,7 @@ Subdiv *BKE_subdiv_new_from_converter(const SubdivSettings *settings,
 	subdiv->settings = *settings;
 	subdiv->topology_refiner = osd_topology_refiner;
 	subdiv->evaluator = NULL;
+	subdiv->displacement_evaluator = NULL;
 	BKE_subdiv_stats_end(&stats, SUBDIV_STATS_TOPOLOGY_REFINER_CREATION_TIME);
 	subdiv->stats = stats;
 	return subdiv;
@@ -116,5 +117,6 @@ void BKE_subdiv_free(Subdiv *subdiv)
 	if (subdiv->topology_refiner != NULL) {
 		openSubdiv_deleteTopologyRefiner(subdiv->topology_refiner);
 	}
+	BKE_subdiv_displacement_detach(subdiv);
 	MEM_freeN(subdiv);
 }
diff --git a/source/blender/blenkernel/intern/subdiv_displacement.c b/source/blender/blenkernel/intern/subdiv_displacement.c
new file mode 100644
index 00000000000..a6af6f45e59
--- /dev/null
+++ b/source/blender/blenkernel/intern/subdiv_displacement.c
@@ -0,0 +1,46 @@
+/*
+ * ***** 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_displacement.c
+ *  \ingroup bke
+ */
+
+#include "BKE_subdiv.h"
+
+#include "BLI_utildefines.h"
+
+#include "MEM_guardedalloc.h"
+
+void BKE_subdiv_displacement_detach(Subdiv *subdiv)
+{
+	if (subdiv->displacement_evaluator == NULL) {
+		return;
+	}
+	if (subdiv->displacement_evaluator->free != NULL) {
+		subdiv->displacement_evaluator->free(subdiv->displacement_evaluator);
+	}
+	MEM_freeN(subdiv->displacement_evaluator);
+	subdiv->displacement_evaluator = NULL;
+}
diff --git a/source/blender/blenkernel/intern/subdiv_displacement_multires.c b/source/blender/blenkernel/intern/subdiv_displacement_multires.c
new file mode 100644
index 00000000000..ce42fa16438
--- /dev/null
+++ b/source/blender/blenkernel/intern/subdiv_displacement_multires.c
@@ -0,0 +1,440 @@
+/*
+ * ***** 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_displacement.c
+ *  \ingroup bke
+ */
+
+#include "BKE_subdiv.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 MultiresDisplacementData {
+	int grid_size;
+	const MPoly *mpoly;
+	const MDisps *mdisps;
+	/* 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;
+} MultiresDisplacementData;
+
+/* Denotes which grid to use to average value of the displacement read from the
+ * grid which corresponds to the ptex face.
+ */
+typedef enum eAverageWith {
+	AVERAGE_WITH_NONE,
+	AVERAGE_WITH_ALL,
+	AVERAGE_WITH_PREV,
+	AVERAGE_WITH_NEXT,
+} eAverageWith;
+
+/* Coordinates within grid has different convention from PTex coordinates.
+ * This function converts the latter ones to 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list