前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >透视投影矩阵_透视投影矩阵推导知乎

透视投影矩阵_透视投影矩阵推导知乎

作者头像
全栈程序员站长
发布2022-11-09 14:31:56
1.1K0
发布2022-11-09 14:31:56
举报

大家好,又见面了,我是你们的朋友全栈君。

透视投影矩阵

首先,重要的是要记住OpenGL中的矩阵是使用列主顺序(而不是行主顺序)定义的。在所有的OpenGL书籍和参考文献中,OpenGL中使用的透视投影矩阵定义为:

透视投影矩阵
透视投影矩阵

我们可以简单地转置矩阵,就能得到下面的以行向量为顺序的矩阵:

在这里插入图片描述
在这里插入图片描述

下面对透视投影矩阵的参数做一些说明:

l, r:立方体的左,右在X轴上的投影 b, t:立方体的下,上在X轴上的投影 n:近平面在Z轴上的投影 f:远平面在Z轴上的投影

在这里插入图片描述
在这里插入图片描述

关于OpenGL透视投影矩阵的推导,可参考链接link.

在这里我们推荐另外一种大佬关于投影矩阵的推导方法,是基于计算机图形学投影矩阵的推导,求出来的结果会和OpenGL的透视投影矩阵有差别,但是在推导过程上更加简单,易于理解。可参照以下链接: link.

关于透视投影矩阵的使用

在旧的固定函数渲染管道中,使用两个函数来设置屏幕坐标和投影矩阵,这两个函数分别是gluPerspective(它是glu库的一部分)和glFrustum。

设置投影矩阵 glFrustum()

在OpenGL中设置透视投影矩阵是通过调用glFrustum来完成的。该函数有六个参数:

// 定义
glFrustum(float left, float right, float bottom, float top, float near, float far);

设置屏幕坐标 gluPerspective()

gluPerspective函数用于设置屏幕坐标。它将视角、图像长宽比(图像宽度除以图像高度)和剪切平面作为增广量。

fovy是垂直视角,也就是从近平面的上边的中心和下边的中心分别连一条线到摄像机所成的角度。 aspect是宽高比,aspect= width/height。

// 定义
void gluPerspective(float fovy, float aspect, float near, float far);

思考: 如何利用FOVY和Aspect确定近平面的 l,r,b,t参数? 注:n,f是固定值,不用确定!

在这里插入图片描述
在这里插入图片描述

write:

tan(FOVY/2)=opposite/adjacent=BC/AB=t/n

therefore:

t=tan(FOVY/2)∗n

由于相机的下半部分与上半部分是对称的,则:

b=−t

已知点C在Y轴的分量是 t,下方点D在Y轴的分量是 –t,由此我们可以知道平面高度是 2t。同理可知,平面宽度是 2r

所以宽高比:

Aspect = 2r/2t = r/t

则可进一步得知:

r = Aspect * t

l = -r

综上:只要定义视锥的宽高比和视角,即可得

l,r,b,t 参数用于定义正交投影的立方体。

附参考代码:

#include <cstdio> 
#include <cstdlib> 
#include <fstream> 
#include "geometry.h" 
#include "vertexdata.h" 
// compute screen coordinates first
void gluPerspective( 
const float &angleOfView, 
const float &imageAspectRatio, 
const float &n, const float &f, 
float &b, float &t, float &l, float &r) 
{ 
 
float scale = tan(angleOfView * 0.5 * M_PI / 180) * n; 
r = imageAspectRatio * scale, l = -r; 
t = scale, b = -t; 
} 
// set the OpenGL perspective projection matrix
void glFrustum( 
const float &b, const float &t, const float &l, const float &r, 
const float &n, const float &f, 
Matrix44f &M) 
{ 
 
// set OpenGL perspective projection matrix
M[0][0] = 2 * n / (r - l); 
M[0][1] = 0; 
M[0][2] = 0; 
M[0][3] = 0; 
M[1][0] = 0; 
M[1][1] = 2 * n / (t - b); 
M[1][2] = 0; 
M[1][3] = 0; 
M[2][0] = (r + l) / (r - l); 
M[2][1] = (t + b) / (t - b); 
M[2][2] = -(f + n) / (f - n); 
M[2][3] = -1; 
M[3][0] = 0; 
M[3][1] = 0; 
M[3][2] = -2 * f * n / (f - n); 
M[3][3] = 0; 
} 
void multPointMatrix(const Vec3f &in, Vec3f &out, const Matrix44f &M) 
{ 
 
//out = in * Mproj;
out.x   = in.x * M[0][0] + in.y * M[1][0] + in.z * M[2][0] + /* in.z = 1 */ M[3][0]; 
out.y   = in.x * M[0][1] + in.y * M[1][1] + in.z * M[2][1] + /* in.z = 1 */ M[3][1]; 
out.z   = in.x * M[0][2] + in.y * M[1][2] + in.z * M[2][2] + /* in.z = 1 */ M[3][2]; 
float w = in.x * M[0][3] + in.y * M[1][3] + in.z * M[2][3] + /* in.z = 1 */ M[3][3]; 
// normalize if w is different than 1 (convert from homogeneous to Cartesian coordinates)
if (w != 1) { 
 
out.x /= w; 
out.y /= w; 
out.z /= w; 
} 
} 
int main(int argc, char **argv) 
{ 
 
uint32_t imageWidth = 512, imageHeight = 512; 
Matrix44f Mproj; 
Matrix44f worldToCamera; 
worldToCamera[3][1] = -10; 
worldToCamera[3][2] = -20; 
float angleOfView = 90; 
float near = 0.1; 
float far = 100; 
float imageAspectRatio = imageWidth / (float)imageHeight; 
float b, t, l, r; 
gluPerspective(angleOfView, imageAspectRatio, near, far, b, t, l, r); 
glFrustum(b, t, l, r, near, far, Mproj); 
unsigned char *buffer = new unsigned char[imageWidth * imageHeight]; 
memset(buffer, 0x0, imageWidth * imageHeight); 
for (uint32_t i = 0; i < numVertices; ++i) { 
 
Vec3f vertCamera, projectedVert; 
multPointMatrix(vertices[i], vertCamera, worldToCamera); 
multPointMatrix(vertCamera, projectedVert, Mproj); 
if (projectedVert.x < -imageAspectRatio || projectedVert.x > imageAspectRatio || projectedVert.y < -1 || projectedVert.y > 1) continue; 
// convert to raster space and mark the position of the vertex in the image with a simple dot
uint32_t x = std::min(imageWidth - 1, (uint32_t)((projectedVert.x + 1) * 0.5 * imageWidth)); 
uint32_t y = std::min(imageHeight - 1, (uint32_t)((1 - (projectedVert.y + 1) * 0.5) * imageHeight)); 
buffer[y * imageWidth + x] = 255; 
//std::cerr << "here sometmes" << std::endl;
} 
// export to image
std::ofstream ofs; 
ofs.open("./out.ppm"); 
ofs << "P5\n" << imageWidth << " " << imageHeight << "\n255\n"; 
ofs.write((char*)buffer, imageWidth * imageHeight); 
ofs.close(); 
delete [] buffer; 
return 0; 
} 

参考链接:

  1. https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix
  2. https://zhuanlan.zhihu.com/p/144329075

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/185835.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022年10月5日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 透视投影矩阵
  • 关于透视投影矩阵的使用
    • 设置投影矩阵 glFrustum()
      • 设置屏幕坐标 gluPerspective()
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档