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

114 篇文章 1 订阅
25 篇文章 0 订阅


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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值