[Bf-blender-cvs] [f1df9e80938] hair_object: Add customdata for hair follicles.

Lukas Tönne noreply at git.blender.org
Sat Nov 10 15:11:57 CET 2018


Commit: f1df9e80938b8c11afc23a2c07c35d3df58ed18a
Author: Lukas Tönne
Date:   Sat Nov 10 14:11:40 2018 +0000
Branches: hair_object
https://developer.blender.org/rBf1df9e80938b8c11afc23a2c07c35d3df58ed18a

Add customdata for hair follicles.

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

M	source/blender/blenkernel/BKE_hair.h
M	source/blender/blenkernel/intern/hair.c
M	source/blender/blenloader/intern/readfile.c
M	source/blender/blenloader/intern/writefile.c
M	source/blender/editors/hair/edithair.c
M	source/blender/makesdna/DNA_hair_types.h

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

diff --git a/source/blender/blenkernel/BKE_hair.h b/source/blender/blenkernel/BKE_hair.h
index cf4f44e53bd..065a5f4d151 100644
--- a/source/blender/blenkernel/BKE_hair.h
+++ b/source/blender/blenkernel/BKE_hair.h
@@ -64,8 +64,9 @@ bool BKE_hair_minmax(struct HairSystem *hsys, float min[3], float max[3]);
 void BKE_hair_boundbox_calc(struct HairSystem *hsys);
 struct BoundBox *BKE_hair_boundbox_get(struct Object *ob);
 
+struct HairPattern *BKE_hair_pattern_new(void);
 void BKE_hair_pattern_free(struct HairPattern *pattern);
-struct HairPattern *BKE_hair_pattern_copy(const struct HairPattern *src_pattern);
+struct HairPattern *BKE_hair_pattern_copy(const struct HairPattern *src_pattern, int flag);
 
 /* Does not free the data pointer itself! */
 void BKE_hair_curve_data_free(struct HairCurveData *data);
diff --git a/source/blender/blenkernel/intern/hair.c b/source/blender/blenkernel/intern/hair.c
index 58e628cce7e..3cec48ca403 100644
--- a/source/blender/blenkernel/intern/hair.c
+++ b/source/blender/blenkernel/intern/hair.c
@@ -42,13 +42,13 @@
 #include "BLI_string_utf8.h"
 #include "BLI_string_utils.h"
 
-#include "DNA_hair_types.h"
 #include "DNA_mesh_types.h"
+#include "DNA_hair_types.h"
 #include "DNA_meshdata_types.h"
 #include "DNA_object_types.h"
 
 #include "BKE_animsys.h"
-#include "DEG_depsgraph_query.h"
+#include "BKE_customdata.h"
 #include "BKE_global.h"
 #include "BKE_hair.h"
 #include "BKE_hair_iterators.h"
@@ -58,6 +58,8 @@
 #include "BKE_mesh_sample.h"
 #include "BKE_object.h"
 
+#include "DEG_depsgraph_query.h"
+
 #include "BLT_translation.h"
 
 void BKE_hair_init(HairSystem *hsys)
@@ -66,7 +68,7 @@ void BKE_hair_init(HairSystem *hsys)
 
 	hsys->bb = BKE_boundbox_alloc_unit();
 
-	hsys->pattern = MEM_callocN(sizeof(HairPattern), "hair pattern");
+	hsys->pattern = BKE_hair_pattern_new();
 	hsys->draw_settings = BKE_hair_draw_settings_new();
 }
 
@@ -79,19 +81,31 @@ void *BKE_hair_add(Main *bmain, const char *name)
 	return hsys;
 }
 
+HairPattern *BKE_hair_pattern_new(void)
+{
+	HairPattern *pattern = MEM_callocN(sizeof(HairPattern), "hair pattern");
+	CustomData_reset(&pattern->fdata);
+	return pattern;
+}
+
 void BKE_hair_pattern_free(HairPattern *pattern)
 {
 	if (pattern) {
 		MEM_SAFE_FREE(pattern->follicles);
+		CustomData_free(&pattern->fdata, pattern->num_follicles);
 		MEM_freeN(pattern);
 	}
 }
 
-HairPattern *BKE_hair_pattern_copy(const HairPattern *src_pattern)
+HairPattern *BKE_hair_pattern_copy(const HairPattern *src_pattern, int flag)
 {
 	HairPattern *dst_pattern = MEM_dupallocN(src_pattern);
 	if (src_pattern) {
 		dst_pattern->follicles = MEM_dupallocN(src_pattern->follicles);
+
+		CustomDataMask mask = CD_MASK_EVERYTHING;
+		const eCDAllocType alloc_type = (flag & LIB_ID_COPY_CD_REFERENCE) ? CD_REFERENCE : CD_DUPLICATE;
+		CustomData_copy(&src_pattern->fdata, &dst_pattern->fdata, mask, alloc_type, dst_pattern->num_follicles);
 	}
 	return dst_pattern;
 }
@@ -151,13 +165,13 @@ void BKE_hair_free(HairSystem *hsys)
  *
  * \param flag  Copying options (see BKE_library.h's LIB_ID_COPY_... flags for more).
  */
-void BKE_hair_copy_data(Main *UNUSED(bmain), HairSystem *hsys_dst, const HairSystem *hsys_src, const int UNUSED(flag))
+void BKE_hair_copy_data(Main *UNUSED(bmain), HairSystem *hsys_dst, const HairSystem *hsys_src, const int flag)
 {
 	hsys_dst->bb = MEM_dupallocN(hsys_src->bb);
 
 	hsys_dst->edithair = NULL;
 
-	hsys_dst->pattern = BKE_hair_pattern_copy(hsys_src->pattern);
+	hsys_dst->pattern = BKE_hair_pattern_copy(hsys_src->pattern, flag);
 	BKE_hair_curve_data_copy(&hsys_dst->curve_data, &hsys_src->curve_data);
 
 	hsys_dst->mat = MEM_dupallocN(hsys_src->mat);
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index 736900d29da..3f397a6d10b 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -8418,6 +8418,7 @@ static void direct_link_hair(FileData *fd, HairSystem* hsys)
 	if ( hsys->pattern )
 	{
 		hsys->pattern->follicles = newdataadr(fd, hsys->pattern->follicles);
+		direct_link_customdata(fd, &hsys->pattern->fdata, hsys->pattern->num_follicles);
 	}
 
 	hsys->curve_data.curves = newdataadr(fd, hsys->curve_data.curves);
diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c
index f2f6a593761..cccdde99606 100644
--- a/source/blender/blenloader/intern/writefile.c
+++ b/source/blender/blenloader/intern/writefile.c
@@ -1618,31 +1618,6 @@ static void write_fmaps(WriteData *wd, ListBase *fbase)
 	}
 }
 
-static void write_hair(WriteData *wd, HairSystem *hsys)
-{
-	writestruct(wd, ID_HA, HairSystem, 1, hsys);
-	write_iddata(wd, &hsys->id);
-	if (hsys->adt) {
-		write_animdata(wd, hsys->adt);
-	}
-
-	if ( hsys->pattern )
-	{
-		writestruct(wd, DATA, HairPattern, 1, hsys->pattern);
-		writestruct(wd, DATA, HairFollicle, hsys->pattern->num_follicles, hsys->pattern->follicles);
-	}
-	
-	writestruct(wd, DATA, HairFiberCurve, hsys->curve_data.totcurves, hsys->curve_data.curves);
-	writestruct(wd, DATA, HairFiberVertex, hsys->curve_data.totverts, hsys->curve_data.verts);
-
-	if (hsys->draw_settings)
-	{
-		writestruct(wd, DATA, HairDrawSettings, 1, hsys->draw_settings);
-	}
-
-	writedata(wd, DATA, sizeof(void *) * hsys->totcol, hsys->mat);
-}
-
 static void write_modifiers(WriteData *wd, ListBase *modbase)
 {
 	ModifierData *md;
@@ -2241,6 +2216,48 @@ static void write_mesh(WriteData *wd, Mesh *mesh)
 	}
 }
 
+static void write_hair(WriteData *wd, HairSystem *hsys)
+{
+	writestruct(wd, ID_HA, HairSystem, 1, hsys);
+	write_iddata(wd, &hsys->id);
+	if (hsys->adt) {
+		write_animdata(wd, hsys->adt);
+	}
+
+	if ( hsys->pattern )
+	{
+		CustomDataLayer *flayers = NULL, flayers_buff[CD_TEMP_CHUNK_SIZE];
+
+		/* write a copy of the hair pattern, don't modify in place because it is
+		 * not thread safe for threaded renders that are reading this */
+		HairPattern cpattern = *hsys->pattern;
+
+		/**
+		 * Those calls:
+		 *   - Reduce pattern->xdata.totlayer to number of layers to write.
+		 *   - Fill xlayers with those layers to be written.
+		 * Note that pattern->xdata is from now on invalid for Blender, but this is why the whole mesh is
+		 * a temp local copy!
+		 */
+		CustomData_file_write_prepare(&cpattern.fdata, &flayers, flayers_buff, ARRAY_SIZE(flayers_buff));
+
+		writestruct_at_address(wd, DATA, HairPattern, 1, hsys->pattern, &cpattern);
+		writestruct(wd, DATA, HairFollicle, cpattern.num_follicles, cpattern.follicles);
+
+		write_customdata(wd, &hsys->id, cpattern.num_follicles, &cpattern.fdata, flayers, -1, 0);
+	}
+
+	writestruct(wd, DATA, HairFiberCurve, hsys->curve_data.totcurves, hsys->curve_data.curves);
+	writestruct(wd, DATA, HairFiberVertex, hsys->curve_data.totverts, hsys->curve_data.verts);
+
+	if (hsys->draw_settings)
+	{
+		writestruct(wd, DATA, HairDrawSettings, 1, hsys->draw_settings);
+	}
+
+	writedata(wd, DATA, sizeof(void *) * hsys->totcol, hsys->mat);
+}
+
 static void write_lattice(WriteData *wd, Lattice *lt)
 {
 	if (lt->id.us > 0 || wd->use_memfile) {
diff --git a/source/blender/editors/hair/edithair.c b/source/blender/editors/hair/edithair.c
index aedb9b871fc..69482f6bbd2 100644
--- a/source/blender/editors/hair/edithair.c
+++ b/source/blender/editors/hair/edithair.c
@@ -76,7 +76,7 @@ void ED_hair_edithair_make(Object *obedit)
 
 	hsys->edithair = MEM_callocN(sizeof(EditHair), "edithair");
 
-	hsys->edithair->pattern = BKE_hair_pattern_copy(hsys->pattern);
+	hsys->edithair->pattern = BKE_hair_pattern_copy(hsys->pattern, 0);
 	BKE_hair_curve_data_copy(&hsys->edithair->curve_data, &hsys->curve_data);
 }
 
@@ -86,7 +86,7 @@ void ED_hair_edithair_load(Object *obedit)
 	
 	BKE_hair_pattern_free(hsys->pattern);
 	BKE_hair_curve_data_free(&hsys->curve_data);
-	hsys->pattern = BKE_hair_pattern_copy(hsys->edithair->pattern);
+	hsys->pattern = BKE_hair_pattern_copy(hsys->edithair->pattern, 0);
 	BKE_hair_curve_data_copy(&hsys->curve_data, &hsys->edithair->curve_data);
 }
 
diff --git a/source/blender/makesdna/DNA_hair_types.h b/source/blender/makesdna/DNA_hair_types.h
index 1cc17f6b843..d5472f019f3 100644
--- a/source/blender/makesdna/DNA_hair_types.h
+++ b/source/blender/makesdna/DNA_hair_types.h
@@ -32,6 +32,7 @@
 #include "DNA_listBase.h"
 #include "DNA_ID.h"
 #include "DNA_meshdata_types.h"
+#include "DNA_customdata_types.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -54,6 +55,9 @@ typedef struct HairPattern {
 	struct HairFollicle *follicles;
 	int num_follicles;
 	int pad;
+
+	/* Per follicle custom data */
+	struct CustomData fdata;
 } HairPattern;
 
 typedef struct HairFiberCurve {



More information about the Bf-blender-cvs mailing list