OpenGL随笔十二 opengl 世界坐标转相机坐标的方法(获取viewMatrix矩阵)

方式一、根据相机坐标、观察坐标、世界的上方向获取世界坐标转相机坐标的转换矩阵
在这里插入图片描述
方式二、根据相机坐标、相机仰角、相机的水平转角、世界的上方向获取世界坐标转相机坐标的转换矩阵
在这里插入图片描述
代码展示`

Camera::Camera(glm::vec3 postion,glm::vec3 target,glm::vec3 wordup)
{
	Position = postion;
	WorldUp = wordup;
	Forward = glm::normalize(target - postion);
	Right = glm::normalize(glm::cross(Forward, WorldUp));
	Up = glm::normalize(glm::cross(Forward, Right));
}

Camera::Camera(glm::vec3 postion, float pitch, float yaw, glm::vec3 wordup)
{
	Position = postion;
	WorldUp = wordup;
	Forward.x = glm::cos(pitch) * glm::sin(yaw);
	Forward.y = glm::sin(pitch);
	Forward.z = glm::cos(pitch) * glm::cos(yaw);
	Right = glm::normalize(glm::cross(Forward, WorldUp));
	Up = glm::normalize(glm::cross(Forward, Right));
}

glm::mat4 Camera::GetViewMatrix()
{
	//相机位置/观看位置/世界的上方向
	return glm::lookAt(Position,Position + Forward,WorldUp);
}

源代码地址[link](https://github.com/JackYanGit/OpenGLExercise11).


发布了32 篇原创文章 · 获赞 0 · 访问量 499
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览