您的位置:首页 > 其它

To get the View matrix from the rotation,translation,scale

2017-06-21 21:24 603 查看
glm::mat4 Camera::GetViewMatrix() const {
return glm::inverse(GetTransformationMatrix());
}


glm::vec3 position_; //translation vector (tx,ty,tz)
glm::quat rotation_; //quat (w,x,y,z)
glm::vec3 scale_;    //scale(scale_x,scale_y,scale_z)


glm::mat4 Transform::GetTransformationMatrix() const {
glm::mat4 trans_mat = glm::scale(glm::mat4_cast(rotation_), scale_);
trans_mat[3][0] = position_.x;
trans_mat[3][1] = position_.y;
trans_mat[3][2] = position_.z;
glm::mat4 parent_mat = glm::mat4(1.0f);
if (parent_ != NULL) {
parent_mat = parent_->GetTransformationMatrix();
trans_mat = parent_mat * trans_mat;
}
return trans_mat;
}


template <typename T, precision P>
GLM_FUNC_QUALIFIER detail::tmat4x4<T, P> scale
(
detail::tmat4x4<T, P> const & m,
detail::tvec3<T, P> const & v
)
{
detail::tmat4x4<T, P> Result(detail::tmat4x4<T, P>::_null);
Result[0] = m[0] * v[0];
Result[1] = m[1] * v[1];
Result[2] = m[2] * v[2];
Result[3] = m[3];
return Result;
}


GLM_FUNC_QUALIFIER detail::tmat4x4<T, P> mat4_cast
(
detail::tquat<T, P> const & q
)
{
return detail::tmat4x4<T, P>(mat3_cast(q));
}


template <typename T, precision P>
GLM_FUNC_QUALIFIER detail::tmat3x3<T, P> mat3_cast
(
detail::tquat<T, P> const & q
)
{
detail::tmat3x3<T, P> Result(T(1));
T qxx(q.x * q.x);
T qyy(q.y * q.y);
T qzz(q.z * q.z);
T qxz(q.x * q.z);
T qxy(q.x * q.y);
T qyz(q.y * q.z);
T qwx(q.w * q.x);
T qwy(q.w * q.y);
T qwz(q.w * q.z);

Result[0][0] = 1 - 2 * (qyy +  qzz);
Result[0][1] = 2 * (qxy + qwz);
Result[0][2] = 2 * (qxz - qwy);

Result[1][0] = 2 * (qxy - qwz);
Result[1][1] = 1 - 2 * (qxx +  qzz);
Result[1][2] = 2 * (qyz + qwx);

Result[2][0] = 2 * (qxz + qwy);
Result[2][1] = 2 * (qyz - qwx);
Result[2][2] = 1 - 2 * (qxx +  qyy);
return Result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐