原文

OpenGL Projection Matrix

Related Topics: OpenGL Transformation

Updates: The MathML version is available here.

Overview

A computer monitor is a 2D surface. A 3D scene rendered by OpenGL must be projected onto the computer screen as a 2D image. GL_PROJECTION matrix is used for this projection transformation. First, it transforms all vertex data from the eye coordinates to the clip coordinates. Then, these clip coordinates are also transformed to the normalized device coordinates (NDC) by dividing with w component of the clip coordinates.

 
A triangle clipped by frustum

Therefore, we have to keep in mind that both clipping (frustum culling) and NDC transformations are integrated into GL_PROJECTIONmatrix. The following sections describe how to build the projection matrix from 6 parameters; leftrightbottomtopnear and farboundary values.

Note that the frustum culling (clipping) is performed in the clip coordinates, just before dividing by wc. The clip coordinates, xc, ycand zc are tested by comparing with wc. If any clip coordinate is less than -wc, or greater than wc, then the vertex will be discarded. 

Then, OpenGL will reconstruct the edges of the polygon where clipping occurs.

Perspective Projection

 
Perspective Frustum and Normalized Device Coordinates (NDC)

In perspective projection, a 3D point in a truncated pyramid frustum (eye coordinates) is mapped to a cube (NDC); the range of x-coordinate from [l, r] to [-1, 1], the y-coordinate from [b, t] to [-1, 1] and the z-coordinate from [n, f] to [-1, 1].

Note that the eye coordinates are defined in the right-handed coordinate system, but NDC uses the left-handed coordinate system. That is, the camera at the origin is looking along -Z axis in eye space, but it is looking along +Z axis in NDC. Since glFrustum() accepts only positive values of near and far distances, we need to negate them during the construction of GL_PROJECTION matrix.

In OpenGL, a 3D point in eye space is projected onto the near plane (projection plane). The following diagrams show how a point (xe, ye, ze) in eye space is projected to (xp, yp, zp) on the near plane.

 
Top View of Frustum
 
Side View of Frustum

From the top view of the frustum, the x-coordinate of eye space, xe is mapped to xp, which is calculated by using the ratio of similar triangles; 

From the side view of the frustum, yp is also calculated in a similar way; 

Note that both xp and yp depend on ze; they are inversely propotional to -ze. In other words, they are both divided by -ze. It is a very first clue to construct GL_PROJECTION matrix. After the eye coordinates are transformed by multiplying GL_PROJECTION matrix, the clip coordinates are still a homogeneous coordinates. It finally becomes the normalized device coordinates (NDC) by divided by the w-component of the clip coordinates. (See more details on OpenGL Transformation.
 ,  

Therefore, we can set the w-component of the clip coordinates as -ze. And, the 4th of GL_PROJECTION matrix becomes (0, 0, -1, 0). 

Next, we map xp and yp to xn and yn of NDC with linear relationship; [l, r] ⇒ [-1, 1] and [b, t] ⇒ [-1, 1].

 
Mapping from xp to xn

 
Mapping from yp to yn

Then, we substitute xp and yp into the above equations.

 

Note that we make both terms of each equation divisible by -ze for perspective division (xc/wc, yc/wc). And we set wc to -ze earlier, and the terms inside parentheses become xc and yc of the clip coordiantes.

From these equations, we can find the 1st and 2nd rows of GL_PROJECTION matrix. 

Now, we only have the 3rd row of GL_PROJECTION matrix to solve. Finding zn is a little different from others because ze in eye space is always projected to -n on the near plane. But we need unique z value for the clipping and depth test. Plus, we should be able to unproject (inverse transform) it. Since we know z does not depend on x or y value, we borrow w-component to find the relationship between zn and ze. Therefore, we can specify the 3rd row of GL_PROJECTION matrix like this. 

In eye space, we equals to 1. Therefore, the equation becomes; 

To find the coefficients, A and B, we use the (ze, zn) relation; (-n, -1) and (-f, 1), and put them into the above equation. 

To solve the equations for A and B, rewrite eq.(1) for B; 

Substitute eq.(1') to B in eq.(2), then solve for A; 

Put A into eq.(1) to find B

We found A and B. Therefore, the relation between ze and zn becomes; 

Finally, we found all entries of GL_PROJECTION matrix. The complete projection matrix is; 
 
OpenGL Perspective Projection Matrix

This projection matrix is for a general frustum. If the viewing volume is symmetric, which is  and , then it can be simplified as; 

Before we move on, please take a look at the relation between ze and zn, eq.(3) once again. You notice it is a rational function and is non-linear relationship between ze and zn. It means there is very high precision at thenear plane, but very little precision at the far plane. If the range [-n, -f] is getting larger, it causes a depth precision problem (z-fighting); a small change of ze around the far plane does not affect on zn value. The distance between n and f should be short as possible to minimize the depth buffer precision problem.

 
Comparison of Depth Buffer Precisions

Orthographic Projection

 
Orthographic Volume and Normalized Device Coordinates (NDC)

Constructing GL_PROJECTION matrix for orthographic projection is much simpler than perspective mode.

All xe, ye and ze components in eye space are linearly mapped to NDC. We just need to scale a rectangular volume to a cube, then move it to the origin. Let's find out the elements of GL_PROJECTION using linear relationship.

 
Mapping from xe to xn

 
Mapping from ye to yn

 
Mapping from ze to zn

Since w-component is not necessary for orthographic projection, the 4th row of GL_PROJECTION matrix remains as (0, 0, 0, 1). Therefore, the complete GL_PROJECTION matrix for orthographic projection is; 
 
OpenGL Orthographic Projection Matrix

It can be further simplified if the viewing volume is symmetrical,  and 

 
 

<转载> OpenGL Projection Matrix的更多相关文章

  1. OpenGL投影矩阵(Projection Matrix)构造方法

    (翻译,图片也来自原文) 一.概述 绝大部分计算机的显示器是二维的(a 2D surface).在OpenGL中一个3D场景需要被投影到屏幕上成为一个2D图像(image).这称为投影变换(参见这或这 ...

  2. 论文阅读笔记(三)【AAAI2017】:Learning Heterogeneous Dictionary Pair with Feature Projection Matrix for Pedestrian Video Retrieval via Single Query Image

    Introduction (1)IVPR问题: 根据一张图片从视频中识别出行人的方法称为 image to video person re-id(IVPR) 应用: ① 通过嫌犯照片,从视频中识别出嫌 ...

  3. OpenGL Column-Major Matrix 使用注意事项

    这column major的矩阵是彻底把我搞晕了,以后右乘规则下的矩阵应该这么用 假设我想创建一个2x2的矩阵,数学上我这么写: 1 2 3 4 用代码创建的话这么写 // 按照 row major ...

  4. Android平台下OpenGL初步

    Android OpenGL ES 开发教程 从入门到精通 http://blog.csdn.net/zhoudailiang/article/details/50176143 http://blog ...

  5. OpenGl从零开始之坐标变换

    http://www.tuicool.com/articles/uiayYrI OpenGL学习脚印: 坐标变换过程(vertex transformation) http://blog.csdn.n ...

  6. OpenGL一些函数详解(二)

    OpenGL ES顶点数据绘制技巧 在OpenGL中,绘制一个长方体,需要将每个顶点的坐标放在一个数组中.保存坐标时有一些技巧(由于字母下标不好表示,因此将下标表示为单引号,如A1将在后文中表示为A' ...

  7. 【GISER && Painter】Chapter00:OpenGL原理学习笔记

    说明:简单了解一下OpenGL的工作原理,初步认识计算机对于图形渲染的底层设计与实现,第一次接触,也没学过C艹,欢迎各位批评指正. 一  什么是OpenGL? OpenGL是一个开放标准(specif ...

  8. OpenGL(6)——坐标系

    在掌握基本变换后,学习如何变换coordinate space. 对coordinate space进行变换的目的是将local space中各顶点坐标转换成normalized device coo ...

  9. GPU相关资料汇总

    qemu, quick emulator systemc xilinx qemu nvdla, nvidia deep learning accelerator gpgpu-sim ffgpu ope ...

随机推荐

  1. python中的装饰器

    一.什么是装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能 ...

  2. Fragment一些问题

    1.使用fragment静态加载,当需要替换的时候使用replace方式是无效的....... 2.replace的容器如果是线性布局,那么将会出现之前的页面残留的情况,正确做法是使用FrameLay ...

  3. android Drawable的问题

    1.资源解析成Drawable getDrawable(int id); 挺简单一方法,可是 require api 21......如何向下兼容呢???? 幸亏有ContextCompat类...( ...

  4. 主机+虚拟机ubuntu+mini2440开发板互相ping通

    折腾这么久,终于将主机,虚拟机和开发板三者之间能够相互ping通,虽然还没有实现我要的功能,不管怎么说先将步骤简单的概括下,用交叉网线将开发板与主机相连,开发板与主机的ip要设置在同一网段内,在配置u ...

  5. mybatis关联查询,一对一,一对多

    注:这篇文章的代码有部分删减,不能直接使用,不过关键代码都存在  应用场景: 想用mybatis做关联查询,并且把查询出的数据自动组装成对象可以使用关联查询. 1.一对一实现 例如:一部小说,属于一个 ...

  6. xtrabackup备份与恢复实践

    说明   xtrabackup  --percona 特点: 开源,在线备份innodb表 支持限速备份,避免对业务造成影响 支持流备 支持增量备份 支持备份文件压缩与加密 支持并行备份与恢复,速度快 ...

  7. smarty模板原理

    smarty模板原理   模板主要是用来让前端和后端分离的,前台页面只是一个前台页面,后台页面用php代码写逻辑,写完逻辑拿到前台显示. 一.写法 一般需要以下:写3个页面: 1.显示页面aa.htm ...

  8. java代码性能优化总结(转载)

    原文链接:http://developer.51cto.com/art/201511/496263.htm 前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改 ...

  9. SVN服务器安装

    CentOS 6.5 SVN搭建 (YUM安装)   参考文献:http://www.linuxidc.com/Linux/2013-10/91903.htm 安装说明 安装了一下SVN服务器,过程如 ...

  10. Scrum会议10.20

    Scrum会议   组名称:好好学习 项目名称:记账本 参会成员:林莉(Master)胡丽娜 汪东涵 宫丽君 时间:2016.10.20 已完成内容: 1.理解项目和代码. 2.讨论新功能. 计划完成 ...