Matrix4x4 OrbitCamera::getFilmToWorld(int width, int height) { //compute the rotation transform V3 pos = getPos(); V3 dir = center - pos; Vector3 look = Vector3(dir.x, dir.y, dir.z); Vector3 w = (look / look.getMagnitude()); Vector3 tmp_up = Vector3(up.x, up.y, up.z); Vector3 u = (tmp_up.cross(w))/((tmp_up.cross(w))).getMagnitude(); Vector3 v = w.cross(u); Matrix4x4 rotate = Matrix4x4::identity(); rotate.a = u.x; rotate.b = u.y; rotate.c = u.z; rotate.e = v.x; rotate.f = v.y; rotate.g = v.z; rotate.i = w.x; rotate.j = w.y; rotate.k = w.z; //compute translation transform Matrix4x4 translate = getInvTransMat(Vector4(pos.x, pos.y, pos.z, 1)); return (rotate * translate).getInverse(); }
// @returns the rotation matrix around the vector and point by the specified angle //the angle is in degrees Matrix4x4 getRotMat (const Vector4 &p, const Vector4 &v, const REAL a) { /* cout<<"point in: "<<p.x<<" "<<p.y<<" "<<p.z<<" "<<p.w<<endl; cout<<"vec in:"<<v.x<<" "<<v.y<<" "<<v.z<<" "<<v.w<<endl; cout<<"angle in:"<<a<<endl;*/ Matrix4x4 trans = getTransMat(p); Matrix4x4 invtrans = getInvTransMat(p); REAL theta = atan2(v.z,v.x); REAL phi = -1 * atan2(v.y , sqrt(v.x*v.x + v.z*v.z)); Matrix4x4 M1 = getRotYMat(theta); Matrix4x4 M2 = getRotZMat(phi); Matrix4x4 M2Inv = getInvRotZMat(phi); Matrix4x4 M1Inv = getInvRotYMat(theta); //Matrix4x4 M3 = getRotXMat(M_PI*a/180); Matrix4x4 M3 = getRotXMat(a); Matrix4x4 toReturn = trans * M1Inv * M2Inv * M3 * M2 * M1 * invtrans; return toReturn; //return trans * M1 * M2 * M3 * M2Inv * M1Inv * invtrans; }
// @returns the rotation matrix around the vector and point by the specified angle Matrix4x4 getRotMat (const Vector4 &p, const Vector4 &v, const REAL a) { // [CAMTRANS] Fill this in... REAL theta = atan2(v.z,v.x); REAL lambda = a; REAL phi = -atan2(v.y, sqrt(v.x*v.x+v.z*v.z)); Matrix4x4 To = getInvTransMat(p);// translate to origin Matrix4x4 M1 = getRotYMat(theta); Matrix4x4 M2 = getRotZMat(phi); Matrix4x4 M3 = getRotXMat(lambda); Matrix4x4 M1inv = getInvRotYMat(theta); Matrix4x4 M2inv = getInvRotZMat(phi); Matrix4x4 M = M1inv*M2inv*M3*M2*M1;// full rotation matrix Matrix4x4 ToInv = getTransMat(p);//translate back to point Matrix4x4 finalmat = ToInv*M*To;//translate to origin, rotate, and then translate back to point return finalmat; }
// @returns the rotation matrix around the vector and point by the specified angle Matrix4x4 getRotMat (const Vector4 &p, const Vector4 &v, const REAL a) { // @TODO: [CAMTRANS] Fill this in... const REAL y = atan2(v.z,v.x), z = -atan2(v.y,std::sqrt(SQ(v.x)+SQ(v.z))); const Matrix4x4 M1 = getRotYMat(y), M1_inv = getInvRotYMat(y), M2 = getRotZMat(z), M2_inv = getInvRotZMat(z), M3 = getRotXMat(a); return getInvTransMat(-p)*M1_inv*M2_inv*M3*M2*M1*getTransMat(-p); }
// @returns the rotation matrix around the vector and point by the specified angle Matrix4x4 getRotMat (const Vector4 &p, const Vector4 &v, const REAL a) { // angles to rotate v to x-axis REAL th = atan2(v.z,v.x), ph = -atan2(v.y,sqrt(pow(v.z,2)+pow(v.x,2))); // all transformations required // Note: Inverse of Rot mat is just transpose Matrix4x4 T = getInvTransMat(p), Tinv = getTransMat(p), M1 = getRotYMat(th), M2 = getRotZMat(ph), M3 = getRotXMat(a); return Tinv * M1.getTranspose() * M2.getTranspose() * M3 * M2 * M1 * T; }
// @returns the inverse rotation matrix around the vector and point by the specified angle Matrix4x4 getInvRotMat (const Point3 &p, const Vector3 &v, const real_t a) { const real_t vZ = v[2]; const real_t vX = v[0]; const real_t theta = atan2(vZ, vX); const real_t phi = -atan2(v[1], sqrt(vX * vX + vZ * vZ)); const Matrix4x4 &transToOrigin = getInvTransMat(Vector3(p[0], p[1], p[2])); const Matrix4x4 &A = getRotYMat(theta); const Matrix4x4 &B = getRotZMat(phi); const Matrix4x4 &C = getRotXMat(a); const Matrix4x4 &invA = getInvRotYMat(theta); const Matrix4x4 &invB = getInvRotZMat(phi); const Matrix4x4 &transBack = getTransMat(Vector3(p[0], p[1], p[2])); return transBack * (invA * invB * C * B * A).getTranspose() * transToOrigin; }
// @returns the rotation matrix around the vector and point by the specified angle Matrix4x4 getRotMat (const Vector4 &p, const Vector4 &v, const REAL a) { // [PASS] REAL theta = atan2(v.z, v.x); REAL phi = -atan2(v.y, sqrt(v.x*v.x + v.z*v.z)); // translate to the origin and back Matrix4x4 Mt = getTransMat(-p); Matrix4x4 Mt_1 = getInvTransMat(-p); Matrix4x4 M1 = getRotYMat(theta); Matrix4x4 M2 = getRotZMat(phi); Matrix4x4 M3 = getRotXMat(a); Matrix4x4 M1_1 = getInvRotYMat(theta); Matrix4x4 M2_1 = getInvRotZMat(phi); return Mt_1*M1_1*M2_1*M3*M2*M1*Mt; }
void CamtransCamera::updateModelViewMatrix() { Matrix4x4 translate = Matrix4x4::identity(); translate = getInvTransMat(m_eyePosition); Matrix4x4 rotate = Matrix4x4::identity(); rotate.data[0] = m_u.x; rotate.data[1] = m_u.y; rotate.data[2] = m_u.z; rotate.data[4] = m_v.x; rotate.data[5] = m_v.y; rotate.data[6] = m_v.z; rotate.data[8] = m_n.x; rotate.data[9] = m_n.y; rotate.data[10] = m_n.z; m_modelView = rotate*translate; }
// @returns the inverse rotation matrix around the vector and // point by the specified angle Matrix4x4 getInvRotMat (const Vector4 &h, const Vector4 &a, const REAL lambda) { // @DONE: [CAMTRANS] Filled in. double theta = atan2(a.z, a.x); double phi = -atan2(a.y, sqrt(a.x*a.x + a.z*a.z)); //want to translate by -h to move from h to the origin Matrix4x4 Th = getTransMat(-h); Matrix4x4 M1Inv = getInvRotYMat(theta); Matrix4x4 M2Inv = getInvRotZMat(phi); Matrix4x4 M3Inv = getInvRotXMat(lambda); Matrix4x4 M2 = getRotZMat(phi); Matrix4x4 M1 = getRotYMat(theta); Matrix4x4 ThInv = getInvTransMat(-h); Matrix4x4 result = ThInv * M1Inv * M2Inv * M3Inv * M2 * M1 * Th; return result; }