<转载> OpenGL Projection Matrix

原文

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 

时间: 2024-10-18 07:10:27

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

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

(翻译,图片也来自原文) 一.概述 绝大部分计算机的显示器是二维的(a 2D surface).在OpenGL中一个3D场景需要被投影到屏幕上成为一个2D图像(image).这称为投影变换(参见这或这),需要用到投影矩阵(projection matrix). 首先,投影矩阵会把所有顶点坐标从eye coordinates(观察空间,eye space或view space)变换到裁剪坐标(clip coordinated,属于裁剪空间,clip space).然后,这些裁剪坐标被变换到标准化设

[转载]OpenGL矩阵变换 一篇很好的译文

转自  http://blog.csdn.net/lyx2007825/article/details/8792475,太精华了收藏下  英文版本:http://www.songho.ca/opengl/gl_transform.html Overview 几何数据——顶点位置,和标准向量(normal vectors),在OpenGL 管道raterization 处理过程之前可通过顶点操作(Vertex Operation)和基本组合操作改变这些数据. Object Coordinates

【转载】The Matrix Cookbook

The Matrix Cookbook Kaare Brandt Petersen, Michael Syskind Pedersen Abstract Matrix identities, relations and approximations. A desktop reference for quick overview of mathematics of matrices. Keywords Matrix identity, matrix relations, inverse, matr

[转载]OpenGL ES着色器语言之内建函数(官方文档第八章)

OpenGL ES着色语言为标量和向量操作定义了一套内建便利函数.有些内建函数可以用在多个类型的着色器中,有些是针对固定硬件的,所以这部分只能用在某个特定的着色器上. 内建函数基本上可以分为一下三类: (1)它们使用一些简便的方式提供必要的硬件功能,如材质贴图.这些函数单独通过着色器是无法模拟出来的. (2)它们展示了一些可以常简单的写入的繁琐操作(clamp, mix等),但是这些操作非常普遍,并且提供直接对硬件的支持.对于编译器来说,将表达式映射到复杂的装配线指令上是非常困难的. (3)它们

OpenGL一些函数详解(二)

OpenGL ES顶点数据绘制技巧 在OpenGL中,绘制一个长方体,需要将每个顶点的坐标放在一个数组中.保存坐标时有一些技巧(由于字母下标不好表示,因此将下标表示为单引号,如A1将在后文中表示为A' ): (1)将对立面坐标保存在相邻的位置,如坐标的保存顺序为:前面(A'ABB'),后面(D'DCC'),上面(D'A'B'C'),下面(DABC),左面(D'A'AD),右面(C'B'BC).因为对立面的坐标除了其垂直的那根轴的坐标相反以外,其他坐标值一样:如前面和后面(垂直于z轴),x和y的坐

OpenGl从零开始之坐标变换

http://www.tuicool.com/articles/uiayYrI OpenGL学习脚印: 坐标变换过程(vertex transformation) http://blog.csdn.net/wangdingqiaoit/article/details/51594408 写在前面 前面几节分别介绍了模型变换,视变换,以及给出了投影矩阵和视口变换矩阵的推导,本节从全局把握一遍OpenGL坐标转换的过程,从整体上认识坐标变换过程.相关矩阵的数学推导过程请参考前面几节对应的内容. 通过本

OpenGL学习脚印: 坐标变换过程(vertex transformation)

写在前面 前面几节分别介绍了模型变换,视变换,以及给出了投影矩阵和视口变换矩阵的推导,本节从全局把握一遍OpenGL坐标转换的过程,从整体上认识坐标变换过程.相关矩阵的数学推导过程请参考前面几节对应的内容. 通过本节可以了解到 坐标变换的各个阶段 利用GLM数学库实现坐标变换 坐标变换的全局图 OpenGL中的坐标处理过程包括模型变换.视变换.投影变换.视口变换等过程,如下图所示: 在上面的图中,注意,OpenGL只定义了裁剪坐标系.规范化设备坐标系和屏幕坐标系,而局部坐标系(模型坐标系).世界

OpenGL原理学习笔记

说明:简单了解一下OpenGL的工作原理,初步认识计算机对于图形渲染的底层设计与实现,第一次接触,也没学过C艹,欢迎各位批评指正. 一  什么是OpenGL? OpenGL是一个开放标准(specification),是一种接口规范,并没有固定实现.每个硬件厂商负责对自己的硬件提供OpenGL接口标准的具体实现.三者关系如下链表:OpenGL API---硬件厂商[各自完成具体实现接口]--使用者[调用OpenGL提供的接口]. [厂商的第三方库并不开源,但目前已有开源GL实现的DEMO,如Me

openGl超级宝典学习笔记 (2) 7个基本的几何图元

点(GL_POINTS): 点总是正方形的像素,默认情况下,点的大小不受透视除法影响.即不管与视点的距离如何,它的大小都不改变.为了获得圆点,必须在抗锯齿模式下绘制点. 可以用glPointSize改变点的大小. //点 //建立批次 GLBatch pointBatch; GLfloat vCoast[24][3] = {{2.80, 1.20, 0.0 }, {2.0, 1.20, 0.0 }, {2.0, 1.08, 0.0 }, {2.0, 1.08, 0.0 }, {0.0, 0.80