Vector3<float> Camera::screenToWorldPoint(const Vector3<float>& p) { double x = p.x; double y = p.y; //if (type == TYPE_ORTHO || type == TYPE_PERSPECTIVE) { x = 2.0 * p.getX() / viewportWidth - 1; y = -2.0 * p.getY() / viewportHeight + 1; //} else if (type == TYPE_ORTHO_2D) { // x = p.getX() / viewportWidth; // y = -p.getY() / viewportHeight; //} //Matrix44<float> viewProjection = *projection.current() * *view.current(); Matrix44 viewProjection = projection.m_stack[projection.m_stackIndex] * view.m_stack[view.m_stackIndex]; viewProjection.inverse(); Vector4 point4d(x, y, 0, 0); point4d = viewProjection * point4d; Vector3<float> ret(point4d.x, point4d.y, 0.0f); if (type == TYPE_ORTHO_2D) { ret.x += viewportWidth * 0.5f; ret.y += viewportHeight * 0.5f; } return ret; //return Vector3<float>(0,0,0); }
Vector3 Camera::getLocalVector(const Vector3& v) { Matrix44 iV = view_matrix; if (iV.inverse() == false) std::cout << "Matrix Inverse error" << std::endl; Vector3 result = iV.rotateVector(v); return result; }
Matrix44 Matrix44::getRotationOnly() { Matrix44 trans = *this; trans.transpose(); Matrix44 inv = *this; inv.inverse(); return trans * inv; }