Revision: 17151
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=17151
Author:   theeth
Date:     2008-10-21 19:59:56 +0200 (Tue, 21 Oct 2008)

Log Message:
-----------
Auto Roll:

Bone roll to face the screen on painted strokes.

Modified Paths:
--------------
    branches/etch-a-ton/source/blender/include/BIF_editarmature.h
    branches/etch-a-ton/source/blender/src/editarmature.c
    branches/etch-a-ton/source/blender/src/editarmature_sketch.c

Modified: branches/etch-a-ton/source/blender/include/BIF_editarmature.h
===================================================================
--- branches/etch-a-ton/source/blender/include/BIF_editarmature.h       
2008-10-21 16:40:42 UTC (rev 17150)
+++ branches/etch-a-ton/source/blender/include/BIF_editarmature.h       
2008-10-21 17:59:56 UTC (rev 17151)
@@ -70,6 +70,8 @@
 
 EditBone *addEditBone(char *name, struct ListBase *ebones, struct bArmature 
*arm);
 
+float rollBoneToVector(EditBone *bone, float new_up_axis[3]);
+
 void   adduplicate_armature(void);
 void   addvert_armature(void);
 void   add_primitiveArmature(int type);

Modified: branches/etch-a-ton/source/blender/src/editarmature.c
===================================================================
--- branches/etch-a-ton/source/blender/src/editarmature.c       2008-10-21 
16:40:42 UTC (rev 17150)
+++ branches/etch-a-ton/source/blender/src/editarmature.c       2008-10-21 
17:59:56 UTC (rev 17151)
@@ -1817,6 +1817,31 @@
        }
 }
 
+/* adjust bone roll to align Z axis with vector
+ * vec is in local space and is normalized
+ */
+float rollBoneToVector(EditBone *bone, float new_up_axis[3])
+{
+       float mat[3][3], nor[3], up_axis[3], vec[3];
+       float roll;
+
+       VecSubf(nor, bone->tail, bone->head);
+       
+       vec_roll_to_mat3(nor, 0, mat);
+       VECCOPY(up_axis, mat[2]);
+       
+       roll = NormalizedVecAngle2(new_up_axis, up_axis);
+       
+       Crossf(vec, up_axis, new_up_axis);
+       
+       if (Inpf(vec, nor) < 0)
+       {
+               roll = -roll;
+       }
+       
+       return roll;
+}
+
 /* Sets the roll value of selected bones, depending on the mode
  *     mode == 0: their z-axes point upwards 
  *     mode == 1: their z-axes point towards 3d-cursor

Modified: branches/etch-a-ton/source/blender/src/editarmature_sketch.c
===================================================================
--- branches/etch-a-ton/source/blender/src/editarmature_sketch.c        
2008-10-21 16:40:42 UTC (rev 17150)
+++ branches/etch-a-ton/source/blender/src/editarmature_sketch.c        
2008-10-21 17:59:56 UTC (rev 17151)
@@ -70,6 +70,7 @@
 typedef struct SK_Point
 {
        float p[3];
+       float no[3];
        SK_PType type;
        SK_PMode mode;
 } SK_Point;
@@ -373,6 +374,18 @@
        return sketch;
 }
 
+void sk_initPoint(SK_Point *pt)
+{
+       VECCOPY(pt->no, G.vd->viewinv[2]);
+       Normalize(pt->no);
+       /* more init code here */
+}
+
+void sk_copyPoint(SK_Point *dst, SK_Point *src)
+{
+       memcpy(dst, src, sizeof(SK_Point));
+}
+
 void sk_allocStrokeBuffer(SK_Stroke *stk)
 {
        stk->points = MEM_callocN(sizeof(SK_Point) * stk->buf_size, "SK_Point 
buffer");
@@ -519,7 +532,8 @@
                        SK_Point pt;
                        float vec[3];
                        
-                       pt.type = PT_CONTINUOUS;
+                       sk_copyPoint(&pt, &old_points[j+1]);
+
                        pt.p[0] = 0;
                        pt.p[1] = 0;
                        pt.p[2] = 0;
@@ -936,6 +950,8 @@
 {
        SK_Point pt;
        
+       sk_initPoint(&pt);
+       
        sk_getStrokeDrawPoint(&pt, sketch, stk, dd);
 
        sk_appendStrokePoint(stk, &pt);
@@ -983,9 +999,11 @@
 
 int sk_addStrokeSnapPoint(SK_Sketch *sketch, SK_Stroke *stk, SK_DrawData *dd)
 {
+       int point_added = 0;
        SK_Point pt;
-       int point_added = 0;
        
+       sk_initPoint(&pt);
+       
        point_added = sk_getStrokeSnapPoint(&pt, sketch, stk, dd);
 
        if (point_added)
@@ -1105,9 +1123,11 @@
 
 int sk_addStrokeEmbedPoint(SK_Sketch *sketch, SK_Stroke *stk, SK_DrawData *dd)
 {
+       int point_added;
        SK_Point pt;
-       int point_added;
        
+       sk_initPoint(&pt);
+
        point_added = sk_getStrokeEmbedPoint(&pt, sketch, stk, dd);
        
        if (point_added)
@@ -1224,6 +1244,22 @@
 }
 /********************************************/
 
+/* bone is assumed to be in GLOBAL space */
+void setBoneRollFromPoint(EditBone *bone, SK_Point *pt, float invmat[][4], 
float tmat[][3])
+{
+       float tangent[3], cotangent[3], normal[3];
+       
+       VecSubf(tangent, bone->tail, bone->head);
+       Crossf(cotangent, tangent, pt->no);
+       Crossf(normal, cotangent, tangent);
+       
+       Mat3MulVecfl(tmat, normal);
+       Normalize(normal);
+       
+       bone->roll = rollBoneToVector(bone, normal);
+
+}
+
 float calcStrokeCorrelation(SK_Stroke *stk, int start, int end, float v0[3], 
float n[3])
 {
        int len = 2 + abs(end - start);
@@ -1296,7 +1332,7 @@
        return -1;
 }
 
-EditBone * subdivideStrokeByCorrelation(SK_Stroke *stk, int start, int end, 
float invmat[][4])
+EditBone * subdivideStrokeByCorrelation(SK_Stroke *stk, int start, int end, 
float invmat[][4], float tmat[][3])
 {
        bArmature *arm= G.obedit->data;
        EditBone *lastBone = NULL;
@@ -1314,6 +1350,8 @@
                index = nextCorrelationSubdivision(stk, bone_start, end, 
parent->head, parent->tail);
                while (index != -1)
                {
+                       setBoneRollFromPoint(parent, &stk->points[end], invmat, 
tmat);
+                       
                        Mat4MulVecfl(invmat, parent->head); /* going to next 
bone, fix previous head */
 
                        child = addEditBone("Bone", &G.edbo, arm);
@@ -1327,9 +1365,11 @@
                        index = nextCorrelationSubdivision(stk, bone_start, 
end, parent->head, parent->tail);
                }
 
+               VECCOPY(parent->tail, stk->points[end].p);
+
+               setBoneRollFromPoint(parent, &stk->points[end], invmat, tmat);
+
                Mat4MulVecfl(invmat, parent->head);
-               
-               VECCOPY(parent->tail, stk->points[end].p);
                Mat4MulVecfl(invmat, parent->tail);
                lastBone = parent;
        }
@@ -1405,7 +1445,7 @@
        return -1;
 }
 
-EditBone * subdivideStrokeByLength(SK_Stroke *stk, int start, int end, float 
invmat[][4])
+EditBone * subdivideStrokeByLength(SK_Stroke *stk, int start, int end, float 
invmat[][4], float tmat[][3])
 {
        bArmature *arm= G.obedit->data;
        EditBone *lastBone = NULL;
@@ -1419,10 +1459,11 @@
                
                parent = addEditBone("Bone", &G.edbo, arm);
                VECCOPY(parent->head, stk->points[start].p);
-
+               
                index = nextLengthSubdivision(stk, bone_start, end, 
parent->head, parent->tail);
                while (index != -1)
                {
+                       setBoneRollFromPoint(parent, &stk->points[index], 
invmat, tmat);
                        Mat4MulVecfl(invmat, parent->head); /* going to next 
bone, fix previous head */
 
                        child = addEditBone("Bone", &G.edbo, arm);
@@ -1436,9 +1477,11 @@
                        index = nextLengthSubdivision(stk, bone_start, end, 
parent->head, parent->tail);
                }
 
+               VECCOPY(parent->tail, stk->points[end].p);
+
+               setBoneRollFromPoint(parent, &stk->points[end], invmat, tmat);
+
                Mat4MulVecfl(invmat, parent->head);
-               
-               VECCOPY(parent->tail, stk->points[end].p);
                Mat4MulVecfl(invmat, parent->tail);
                lastBone = parent;
        }
@@ -1452,6 +1495,7 @@
        SK_Point *head;
        EditBone *parent = NULL;
        float invmat[4][4]; /* move in caller function */
+       float tmat[3][3];
        int head_index = 0;
        int i;
        
@@ -1459,6 +1503,9 @@
        
        Mat4Invert(invmat, G.obedit->obmat);
        
+       Mat3CpyMat4(tmat, G.obedit->obmat);
+       Mat3Transp(tmat);
+       
        for (i = 0; i < stk->nb_points; i++)
        {
                SK_Point *pt = stk->points + i;
@@ -1477,11 +1524,11 @@
                                
                                if (i - head_index > 1)
                                {
-                                       bone = 
subdivideStrokeByCorrelation(stk, head_index, i, invmat);
+                                       bone = 
subdivideStrokeByCorrelation(stk, head_index, i, invmat, tmat);
                                        
                                        if (bone == NULL)
                                        {
-                                               bone = 
subdivideStrokeByLength(stk, head_index, i, invmat);
+                                               bone = 
subdivideStrokeByLength(stk, head_index, i, invmat, tmat);
                                        }
                                }
                                
@@ -1491,6 +1538,7 @@
                                        
                                        VECCOPY(bone->head, head->p);
                                        VECCOPY(bone->tail, pt->p);
+                                       setBoneRollFromPoint(bone, pt, invmat, 
tmat);
                                        
                                        Mat4MulVecfl(invmat, bone->head);
                                        Mat4MulVecfl(invmat, bone->tail);


_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to