Commit: cdc1f42d451bb4a6a90915304604019723d84c5b
Author: Lukas Tönne
Date:   Fri Nov 28 17:58:04 2014 +0100
Branches: hair_immediate_fixes
https://developer.blender.org/rBcdc1f42d451bb4a6a90915304604019723d84c5b

Apply the hair matrices when converting from particle keys to bmesh,
so the edit data is consistently in object space.

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

M       source/blender/blenkernel/intern/edithair.c
M       source/blender/bmesh/intern/bmesh_strands_conv.c
M       source/blender/bmesh/intern/bmesh_strands_conv.h

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

diff --git a/source/blender/blenkernel/intern/edithair.c 
b/source/blender/blenkernel/intern/edithair.c
index 4bd5a8a..d9ced64 100644
--- a/source/blender/blenkernel/intern/edithair.c
+++ b/source/blender/blenkernel/intern/edithair.c
@@ -107,7 +107,7 @@ BMesh *BKE_particles_to_bmesh(Object *ob, ParticleSystem 
*psys)
        if (psmd && psmd->dm) {
                DM_ensure_tessface(psmd->dm);
                
-               BM_strands_bm_from_psys(bm, psys, psmd->dm, true, 
psys->shapenr);
+               BM_strands_bm_from_psys(bm, ob, psys, psmd->dm, true, 
psys->shapenr);
        }
 
        return bm;
@@ -126,7 +126,7 @@ void BKE_particles_from_bmesh(Object *ob, ParticleSystem 
*psys)
                        
                        bvhtree_from_mesh_faces(&bvhtree, psmd->dm, 0.0, 2, 6);
                        
-                       BM_strands_bm_to_psys(bm, psys, psmd->dm, &bvhtree);
+                       BM_strands_bm_to_psys(bm, ob, psys, psmd->dm, &bvhtree);
                        
                        free_bvhtree_from_mesh(&bvhtree);
                }
diff --git a/source/blender/bmesh/intern/bmesh_strands_conv.c 
b/source/blender/bmesh/intern/bmesh_strands_conv.c
index 95ee8cd..411d5ed 100644
--- a/source/blender/bmesh/intern/bmesh_strands_conv.c
+++ b/source/blender/bmesh/intern/bmesh_strands_conv.c
@@ -171,16 +171,19 @@ static KeyBlock *bm_set_shapekey_from_psys(BMesh *bm, 
ParticleSystem *psys, int
 }
 
 /* create vertex and edge data for BMesh based on particle hair keys */
-static void bm_make_particles(BMesh *bm, ParticleSystem *psys, struct 
DerivedMesh *emitter_dm, float (*keyco)[3], int cd_shape_keyindex_offset)
+static void bm_make_particles(BMesh *bm, Object *ob, ParticleSystem *psys, 
struct DerivedMesh *emitter_dm, float (*keyco)[3], int cd_shape_keyindex_offset)
 {
        KeyBlock *block;
        ParticleData *pa;
        HairKey *hkey;
        int p, k, j;
+       
        int vindex, eindex;
        BMVert *v = NULL, *v_prev;
        BMEdge *e;
        
+       float hairmat[4][4];
+       
        /* XXX currently all particles and keys have the same mass, this may 
change */
        float mass = psys->part->mass;
        
@@ -188,11 +191,18 @@ static void bm_make_particles(BMesh *bm, ParticleSystem 
*psys, struct DerivedMes
        eindex = 0;
        for (p = 0, pa = psys->particles; p < psys->totpart; ++p, ++pa) {
                
-               for (k = 0, hkey = pa->hair; k < pa->totkey; ++k, ++hkey) {
+               /* hair keys are in a local "hair space", but edit data should 
be in object space */
+               psys_mat_hair_to_object(ob, emitter_dm, psys->part->from, pa, 
hairmat);
                
+               for (k = 0, hkey = pa->hair; k < pa->totkey; ++k, ++hkey) {
+                       float co[3];
+                       
+                       copy_v3_v3(co, keyco ? keyco[vindex] : hkey->co);
+                       mul_m4_v3(hairmat, co);
+                       
                        v_prev = v;
-                       v = BM_vert_create(bm, keyco ? keyco[vindex] : 
hkey->co, NULL, BM_CREATE_SKIP_CD);
-                       BM_elem_index_set(v, vindex++); /* set_ok */
+                       v = BM_vert_create(bm, co, NULL, BM_CREATE_SKIP_CD);
+                       BM_elem_index_set(v, vindex); /* set_ok */
                        
                        /* transfer flag */
 //                     v->head.hflag = BM_vert_flag_from_mflag(mvert->flag & 
~SELECT);
@@ -233,9 +243,11 @@ static void bm_make_particles(BMesh *bm, ParticleSystem 
*psys, struct DerivedMes
                                }
                        }
                        
+                       vindex += 1;
+                       
                        if (k > 0) {
                                e = BM_edge_create(bm, v_prev, v, NULL, 
BM_CREATE_SKIP_CD);
-                               BM_elem_index_set(e, eindex++); /* set_ok; one 
less edge than vertices for each particle */
+                               BM_elem_index_set(e, eindex); /* set_ok; one 
less edge than vertices for each particle */
                                
                                /* transfer flags */
 //                             e->head.hflag = 
BM_edge_flag_from_mflag(medge->flag & ~SELECT);
@@ -246,10 +258,12 @@ static void bm_make_particles(BMesh *bm, ParticleSystem 
*psys, struct DerivedMes
 //                             }
                                
                                /* Copy Custom Data */
-//                             CustomData_to_bmesh_block(&me->edata, 
&bm->edata, vindex, &e->head.data, true);
+//                             CustomData_to_bmesh_block(&me->edata, 
&bm->edata, eindex, &e->head.data, true);
                                CustomData_bmesh_set_default(&bm->edata, 
&e->head.data);
+                               
+                               eindex += 1;
                        }
-               
+                       
                } /* hair keys */
        
        } /* particles */
@@ -260,7 +274,7 @@ static void bm_make_particles(BMesh *bm, ParticleSystem 
*psys, struct DerivedMes
 /**
  * \brief ParticleSystem -> BMesh
  */
-void BM_strands_bm_from_psys(BMesh *bm, ParticleSystem *psys, struct 
DerivedMesh *emitter_dm,
+void BM_strands_bm_from_psys(BMesh *bm, Object *ob, ParticleSystem *psys, 
struct DerivedMesh *emitter_dm,
                              const bool set_key, int act_key_nr)
 {
        KeyBlock *actkey;
@@ -303,7 +317,7 @@ void BM_strands_bm_from_psys(BMesh *bm, ParticleSystem 
*psys, struct DerivedMesh
 
        cd_shape_keyindex_offset = psys->key ? 
CustomData_get_offset(&bm->vdata, CD_SHAPE_KEYINDEX) : -1;
 
-       bm_make_particles(bm, psys, emitter_dm, set_key ? keyco : NULL, 
cd_shape_keyindex_offset);
+       bm_make_particles(bm, ob, psys, emitter_dm, set_key ? keyco : NULL, 
cd_shape_keyindex_offset);
 
 
 #if 0 /* TODO */
@@ -429,7 +443,7 @@ BLI_INLINE void bmesh_quick_edgedraw_flag(MEdge *med, 
BMEdge *e)
 }
 #endif
 
-static void make_particle_hair(BMesh *bm, BMVert *root, ParticleSystem *psys, 
struct DerivedMesh *emitter_dm, struct BVHTreeFromMesh *emitter_bvhtree, struct 
ParticleData *pa)
+static void make_particle_hair(BMesh *bm, BMVert *root, Object *ob, 
ParticleSystem *psys, struct DerivedMesh *emitter_dm, struct BVHTreeFromMesh 
*emitter_bvhtree, struct ParticleData *pa)
 {
        int totkey = BM_strands_keys_count(root);
        HairKey *hair;
@@ -439,6 +453,8 @@ static void make_particle_hair(BMesh *bm, BMVert *root, 
ParticleSystem *psys, st
        HairKey *hkey;
        int k;
        
+       float inv_hairmat[4][4];
+       
        pa->alive = PARS_ALIVE;
        pa->flag = 0;
        
@@ -460,10 +476,6 @@ static void make_particle_hair(BMesh *bm, BMVert *root, 
ParticleSystem *psys, st
        hkey = hair;
        k = 0;
        BM_ITER_STRANDS_ELEM(v, &iter, root, BM_VERTS_OF_STRAND) {
-               copy_v3_v3(hkey->co, v->co);
-               hkey->time = totkey > 0 ? (float)k / (float)(totkey - 1) : 0.0f;
-               hkey->weight = BM_elem_float_data_named_get(&bm->vdata, v, 
CD_PROP_FLT, CD_PSYS_WEIGHT);
-               
                /* root */
                if (k == 0) {
                        MSurfaceSample root_loc;
@@ -474,8 +486,17 @@ static void make_particle_hair(BMesh *bm, BMVert *root, 
ParticleSystem *psys, st
                                zero_v4(pa->fuv);
                                pa->foffset = 0.0f;
                        }
+                       
+                       /* edit data is in object space, hair keys must be 
converted back into "hair space" */
+                       psys_mat_hair_to_object(ob, emitter_dm, 
psys->part->from, pa, inv_hairmat);
+                       invert_m4(inv_hairmat);
                }
                
+               mul_v3_m4v3(hkey->co, inv_hairmat, v->co);
+               
+               hkey->time = totkey > 0 ? (float)k / (float)(totkey - 1) : 0.0f;
+               hkey->weight = BM_elem_float_data_named_get(&bm->vdata, v, 
CD_PROP_FLT, CD_PSYS_WEIGHT);
+               
                ++hkey;
                ++k;
                
@@ -489,7 +510,7 @@ static void make_particle_hair(BMesh *bm, BMVert *root, 
ParticleSystem *psys, st
        pa->totkey = totkey;
 }
 
-void BM_strands_bm_to_psys(BMesh *bm, ParticleSystem *psys, struct DerivedMesh 
*emitter_dm, struct BVHTreeFromMesh *emitter_bvhtree)
+void BM_strands_bm_to_psys(BMesh *bm, Object *ob, ParticleSystem *psys, struct 
DerivedMesh *emitter_dm, struct BVHTreeFromMesh *emitter_bvhtree)
 {
        ParticleData *particles, *oldparticles;
        int ototpart, ototkey, ntotpart;
@@ -520,7 +541,7 @@ void BM_strands_bm_to_psys(BMesh *bm, ParticleSystem *psys, 
struct DerivedMesh *
        p = 0;
        BM_ITER_STRANDS(root, &iter, bm, BM_STRANDS_OF_MESH) {
                
-               make_particle_hair(bm, root, psys, emitter_dm, emitter_bvhtree, 
pa);
+               make_particle_hair(bm, root, ob, psys, emitter_dm, 
emitter_bvhtree, pa);
                
                ++pa;
                ++p;
diff --git a/source/blender/bmesh/intern/bmesh_strands_conv.h 
b/source/blender/bmesh/intern/bmesh_strands_conv.h
index caa27ee..92a29ef 100644
--- a/source/blender/bmesh/intern/bmesh_strands_conv.h
+++ b/source/blender/bmesh/intern/bmesh_strands_conv.h
@@ -34,6 +34,7 @@
 
 struct BMesh;
 struct Mesh;
+struct Object;
 struct ParticleSystem;
 struct DerivedMesh;
 struct BVHTreeFromMesh;
@@ -48,9 +49,9 @@ void BM_strands_cd_flag_apply(struct BMesh *bm, const char 
cd_flag);
 char BM_strands_cd_flag_from_bmesh(struct BMesh *bm);
 
 int BM_strands_count_psys_keys(struct ParticleSystem *psys);
-void BM_strands_bm_from_psys(struct BMesh *bm, struct ParticleSystem *psys, 
struct DerivedMesh *emitter_dm,
+void BM_strands_bm_from_psys(struct BMesh *bm, struct Object *ob, struct 
ParticleSystem *psys, struct DerivedMesh *emitter_dm,
                              const bool set_key, int act_key_nr);
-void BM_strands_bm_to_psys(struct BMesh *bm, struct ParticleSystem *psys, 
struct DerivedMesh *emitter_dm, struct BVHTreeFromMesh *emitter_bvhtree);
+void BM_strands_bm_to_psys(struct BMesh *bm, struct Object *ob, struct 
ParticleSystem *psys, struct DerivedMesh *emitter_dm, struct BVHTreeFromMesh 
*emitter_bvhtree);
 
 #define BMALLOC_TEMPLATE_FROM_PSYS(psys) { (CHECK_TYPE_INLINE(psys, 
ParticleSystem *), \
        BM_strands_count_psys_keys(psys)), (BM_strands_count_psys_keys(psys) - 
(psys)->totpart), 0, 0 }

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to