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

透视投影矩阵_透视投影矩阵推导知乎透视投影矩阵TheOpenGLPerspectiveProjectionMatrix关于透视投影矩阵的使用BuildingaBasicPerspectiveProjectionMatrixTheOpenGLPerspectiveProjectionMatrix首先,重要的是要记住OpenGL中的矩阵是使用列主顺序(而不是行主顺序)定义的。在所有的OpenGL书籍和参考文献中,OpenGL中使用的透视投影矩阵定义为:我们可以简单地转置矩阵,我们可以得到下面的以行向量为顺序的

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

首先,重要的是要记住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.net/185835.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 2020最新pycharm汉化安装(亲测有效)

    2020最新pycharm汉化安装(亲测有效)这里我推荐大家使用pycharm百度输入关键词:pycharm,点击如图所示网站进入pycharm官网选择电脑系统版本,这里我们选择Windows系统,点击Community版本下的download#Professional为限时免费试用的专业版#Community为免费的社区版本我们使用社区版基本够用了,等技能熟练再去使用专业版的。双击下载好的PyCharm安装包,出现如下图所示的界面,点击“next”选择安装目录,Pycharm需要的内存较多,建议安装在D盘或者

    2022年5月16日
    56
  • python2021激活码【2021.10最新】

    (python2021激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html1STL5S9V8F-eyJsa…

    2022年3月27日
    120
  • win10关闭445端口方法_服务器关闭445端口

    win10关闭445端口方法_服务器关闭445端口445端口是一种TCP端口,有了它我们可以在局域网中轻松访问各种共享文件夹或共享打印机,但也正是因为有了它,黑客们才有了可乘之机,他们能通过该端口偷偷共享你的硬盘,甚至会在悄无声息中将你的硬盘格式化掉。今天小编将为大家分享Win11关闭445端口的方法,一起来看看吧!更多重装系统教程尽在小白系统重装官网  1、首先,按Win+S组合键,或点击底部任务栏上的搜索图标,打开的Windows搜索窗口,顶部输入Windows防火墙,然后点击系统给出的最佳匹配WindowsDefender防

    2022年10月7日
    2
  • ES6学习04

    ES6学习04

    2021年6月7日
    108
  • MySQL常见面试题_web面试题

    MySQL常见面试题_web面试题一、存储引擎MySQL常见的两种存储引擎:MyISAM与InnoDB二、字符集及校对规则字符集指的是一种从二进制编码到某类字符符号的映射。校对规则则是指某种字符集下的排序规则。Mysql中每一种字符集都会对应一系列的校对规则。Mysql采用的是类似继承的方式指定字符集的默认值,每个数据库以及每张数据表都有自己的默认值,他们逐层继承。比如:某个库中所有表的默认字符集将是该数据库所指定…

    2022年8月26日
    11
  • 推荐一个非常实用的博客群发工具[通俗易懂]

    推荐一个非常实用的博客群发工具[通俗易懂]http://sns.juziyue.com/webinvite.php?u=30603这是注册地址,名字叫菊子曰,支持很多blog…

    2022年7月13日
    18

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号