Index: source/blender/src/view.c =================================================================== --- source/blender/src/view.c (revision 15735) +++ source/blender/src/view.c (working copy) @@ -94,7 +94,10 @@ #define TRACKBALLSIZE (1.1) #define BL_NEAR_CLIP 0.001 +#define COS45 0.70710678118654746 +#define SIN45 COS45 + /* local prototypes ----------*/ void setcameratoview3d(void); /* windows.c & toets.c */ @@ -793,6 +796,32 @@ BIF_view3d_previewrender_signal(curarea, PR_DBASE|PR_DISPRECT); } +static int snap_view(float viewmat[][3], float *snapquat, float thres, int view, int oposite_dir){ + float snapmat[3][3]; + + QuatToMat3(snapquat, snapmat); + + if ((Inpf(snapmat[0], viewmat[0]) > thres) && + (Inpf(snapmat[1], viewmat[1]) > thres) && + (Inpf(snapmat[2], viewmat[2]) > thres)){ + + QUATCOPY(G.vd->viewquat, snapquat); + + if (view){ + G.vd->view = view; + if (oposite_dir){ + G.vd->flag2 |= V3D_OPP_DIRECTION_NAME; + }else{ + G.vd->flag2 &= ~V3D_OPP_DIRECTION_NAME; + } + } + + return 1; + } + + return 0; +} + void viewmove(int mode) { static float lastofs[3] = {0,0,0}; @@ -951,7 +980,6 @@ /* New turntable view code by John Aughey */ float m[3][3]; - float m_inv[3][3]; float xvec[3] = {1,0,0}; /* Sensitivity will control how fast the viewport rotates. 0.0035 was obtained experimentally by looking at viewport rotation sensitivities @@ -961,11 +989,10 @@ /* Get the 3x3 matrix and its inverse from the quaternion */ QuatToMat3(G.vd->viewquat, m); - Mat3Inv(m_inv,m); /* Determine the direction of the x vector (for rotating up and down) */ /* This can likely be compuated directly from the quaternion. */ - Mat3MulVecfl(m_inv,xvec); + Mat3TransMulVecfl(m, xvec); /* Perform the up/down rotation */ phi = sensitivity * -(mval[1] - mvalo[1]); @@ -997,6 +1024,73 @@ VecAddf(G.vd->ofs, G.vd->ofs, obofs); } } + + /* check for view snap */ + if (G.qual==LR_CTRLKEY){ + int i; + int snapped; + float m[3][3]; + + static const float thres = 0.93; //cos(20 deg); + + static float mainviewquats [6][6] = { + /*{q0, q1, q3, q4, view, oposite_direction}*/ + {COS45, -SIN45, 0.0, 0.0, 1, 0}, //front + {0.0, 0.0, -SIN45, -SIN45, 1, 1}, //back + {1.0, 0.0, 0.0, 0.0, 7, 0}, //top + {0.0, -1.0, 0.0, 0.0, 7, 1}, //bottom + {0.5, -0.5, -0.5, -0.5, 3, 0}, //left + {0.5, -0.5, 0.5, 0.5, 3, 1} //right + }; + + QuatToMat3(G.vd->viewquat, m); + + /* main named views snaps */ + for (i = 0 ; i < 6; i++){ + snapped = snap_view(m, mainviewquats[i], thres, (int)mainviewquats[i][4], (int)mainviewquats[i][5]); + } + + if (!snapped){ + /* some 45 deg snaps */ + double deg22_5 = M_PI / 8.0; + float cos22_5 = (float)cos(deg22_5); + float sin22_5 = (float)sin(deg22_5); + + float qTop[4]; + float qTop45[4]; + float qMid[4]; + float qBot45[4]; + float qBot[4]; + + float rotZ45[4] = {cos22_5, 0.0, 0.0, sin22_5}; + float rotX45up[4] = {cos22_5, -sin22_5, 0.0, 0.0}; + float rotX45down[4] = {cos22_5, sin22_5, 0.0, 0.0}; + + QUATCOPY(qTop, mainviewquats[2]); + QUATCOPY(qMid, mainviewquats[0]); + QUATCOPY(qBot, mainviewquats[3]); + QuatMul(qTop45, mainviewquats[0], rotX45up); + QuatMul(qBot45, mainviewquats[0], rotX45down); + for (i = 0; i < 8; i++){ + QuatMul(qMid, qMid, rotZ45); + if (snap_view(m, qMid, thres, 0, 0)) break; + + QuatMul(qTop, qTop, rotZ45); + if (snap_view(m, qTop, thres, 0, 0)) break; + + QuatMul(qBot, qBot, rotZ45); + if (snap_view(m, qBot, thres, 0, 0)) break; + + QuatMul(qTop45, qTop45, rotZ45); + if (snap_view(m, qTop45, thres, 0, 0)) break; + + QuatMul(qBot45, qBot45, rotZ45); + if (snap_view(m, qBot45, thres, 0, 0)) break; + } + } + + } + } else if(mode==1) { /* translate */ if(G.vd->persp==V3D_CAMOB) {