【技术美术图形部分】坐标空间和MVP变换_mvp ndc_九九345的博客-CSDN博客坐标空间和MVP变换【技术美术图形部分】坐标空间和MVP变换_mvp ndc_九九345的博客-CSDN博客
每个模型的顶点坐标是围绕各自模型的中心的,我们要让它们在同一个屏幕中显示,就必须要用某些方法把它们的顶点坐标变换到屏幕中去,并且丢掉 z 轴后显示在屏幕上。
在三维空间中我们使用四维矩阵和四维向量进行齐次坐标变换。只需要用对应的变换矩阵乘以齐次顶点坐标,就可以得到变换后的顶点坐标。一般将把模型从模型空间变换到能正确在屏幕上显示的屏幕空间的过MVP变换,这个过程做了三件事,分别是 model 变换,view 变换 和 projection 变换。
屏幕空间是这样产生的:首先我们假设这样一个标准设备坐标 (NDC):所有我们需要显示的物体都出现在 {∀a∈{x,y,z}|a∈[−1,1]} 中,也就是一个以 (0,0,0) 为中心,边长为 2 的正方体中。然后,我们的摄像机看向 z 轴的负半轴,那么摄像机就是我们的 “屏幕”。
模型变换
这一步是将物体从模型空间变换到世界空间,意思是说,现在要将物体摆好。这一步在我们的软渲染器中暂时没什么可做的,主要就是将物体变换到你想要它在的地方。在模型变换的过程中,假设物体的初始位置是在 NDC 的 (0,0,0)。如果你希望将其放在某个位置,就创建相应的平移矩阵。除此以外你还可以将其旋转,缩放,反射。这其实就是做仿射变换。
视图/相机变换
因为我们最终是从相机观察物体,现在我们要把相机的位置作为原点,把其看向的方向作为 −z 轴,并把上向量的方向作为 y轴方向,创建一个新的坐标系,即相机空间,现在要把世界空间变换到相机空间。
注意矩阵的运算顺序,所有变换矩阵都是左乘顶点向量的,且变换顺序是从右到左,
OpenGl ES---纹理_opengl 纹理坐标_liyaoliy的博客-CSDN博客
第五章 opengl之摄像机_opengl设置相机位置_Re_view的博客-CSDN博客
void updateCameraVectors()
{
// calculate the new Front vector
glm::vec3 front;
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
front.y = sin(glm::radians(Pitch));
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Front = glm::normalize(front);
// also re-calculate the Right and Up vector
Right = glm::normalize(glm::cross(Front, WorldUp)); // normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
Up = glm::normalize(glm::cross(Right, Front));
}
template <typename T>
GLM_FUNC_QUALIFIER tquat<T>::tquat
(
tvec3<T> const & eulerAngle
)
{
tvec3<T> c = glm::cos(eulerAngle * value_type(0.5));
tvec3<T> s = glm::sin(eulerAngle * value_type(0.5));
this->w = c.x * c.y * c.z + s.x * s.y * s.z;
this->x = s.x * c.y * c.z - c.x * s.y * s.z;
this->y = c.x * s.y * c.z + s.x * c.y * s.z;
this->z = c.x * c.y * s.z - s.x * s.y * c.z;
}
template <typename T>
GLM_FUNC_QUALIFIER detail::tmat3x3<T> mat3_cast
(
detail::tquat<T> const & q
)
{
template <typename T>
GLM_FUNC_QUALIFIER detail::tquat<T> quat_cast
(
detail::tmat3x3<T> const & m
)
unity
glm很好的讲解gOpenGL相机自由移动旋转缩放,四元数,欧拉角,LookAt_opengl 欧拉角_梦鸢MoYuan的博客-CSDN博客
opengl 仿射变换
OpenGL with QtWidgets:屏幕坐标转相机坐标系
OpenGL with QtWidgets:屏幕坐标转世界坐标_opengl 屏幕坐标转世界坐标_龚建波的博客-CSDN博客
m_CamP[0]=1440.0f;
m_CamP[1]=1920.0f;
m_CamP[2]=10.0f;
m_CamP[3]=1436.286f;
m_CamP[4]=1436.286f;
m_CamP[5]=695.832f;
m_CamP[6]=958.52f;
memset(m_SlamData, 0, sizeof(float) * 16);
m_SlamData[0] = m_SlamData[5] = m_SlamData[10] = m_SlamData[15] = 1.0f;
、、、、、、、、、、、、、、、、、、、、、、、
m_program->setUniform4f("camP", camP_tmp[3], camP_tmp[4], camP_tmp[5],camP_tmp[6]);//fx,fy,cx,cy
m_program->setUniform3f("imgsize", camP_tmp[0], camP_tmp[1], camP_tmp[2]);//w,h,depth
///
attribute vec4 position;
attribute vec2 texCoord;
varying vec2 vTexCoord;
uniform mat4 proMatrix;
uniform mat4 vMatrix;
uniform vec4 camP;
uniform vec3 imgsize;
void main()
{
//gl_Position = position;
gl_Position = proMatrix*vMatrix*vec4((position.x*0.5*imgsize.x+0.5*imgsize.x-camP.z)*imgsize.z/camP.x, (position.y*0.5*imgsize.y+0.5*imgsize.y-camP.w)*imgsize.z/camP.y,-imgsize.z,1.0);
vTexCoord = texCoord.xy;
}
三维重建学习(2):相机标定基础_hongbin_xu的博客-CSDN博客
相机坐标