前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >opengl投影矩阵变换_opengl 坐标

opengl投影矩阵变换_opengl 坐标

作者头像
全栈程序员站长
发布2022-11-04 15:30:18
1.6K0
发布2022-11-04 15:30:18
举报

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

英文原版地址

前言

先上一个运行效果图:

项目完整代码工程

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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.

翻译:

概述

计算机显示器是一个二维表面。由OpenGL渲染的3D场景必须作为2D图像投影到计算机屏幕上。GL_PROJECTION matrix投影 矩阵 用于此投影 转变。首先,它将所有顶点数据从眼睛坐标(相机坐标系)转换为裁剪坐标系。然后,裁剪坐标系转换到标准设备坐标系(NDC)通过除以裁剪坐标系的w分量。

A triangle clipped by frustum
A triangle clipped by frustum

A triangle clipped by frustum

Therefore, we have to keep in mind that both clipping (frustum culling) and NDC transformations are integrated into GL_PROJECTION matrix. The following sections describe how to build the projection matrix from 6 parameters; left, right, bottom, top, near and far boundary values.

Note that the frustum culling (clipping) is performed in the clip coordinates, just before dividing by wc. The clip coordinates, xc, yc and 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.

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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

翻译:

因此,我们必须记住:两次裁剪(视锥体裁剪剔除)和标准化设备坐标(NDC)转换是通过GL_PROJECTION matrix.投影矩阵完成。以下章节描述:如何从6个参数:左、右上、下远、近边缘值构建投影矩阵。

请注意:视锥体裁剪剔除是在裁剪坐标系下执行的,是在除以Wc之前。在裁剪坐系下:Xc、Yc和Zc通过和Wc进行比较,如果裁剪坐标小于-Wc或者大于Wc,那么这些顶点将会被丢弃。

-Wc < Xc,Yc,Zc

然后,openGL会建视锥体裁剪剔除多面体的边缘。

Perspective Projection

OpenGL Perspective Frustum and NDC
OpenGL Perspective Frustum and NDC

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.

翻译:

视锥体裁剪剔除和标准化设备坐标(NDC)

在透视投影中,一个3D点是在一个截去上半部分的金字塔形状内(视图坐标系)被映射到一个立方体(NDC);x坐标的范围从[l,r]到[-1,1],y坐标的范围从[b,t]到[-1,1],z坐标的范围从[-n,-f]到[-1,1]。

请注意:视图坐标系(相机坐标系或者眼睛坐标系)定义的是右手坐标系,但是NDC(标准设备坐标系)使用的是左手坐标系。也就是说:在视图坐标系下:相机在原点处向负Z轴看去,而在NDC(标准设备坐标系)下沿着正Z轴看去。由于glFrustum()函数只接受参数near和far参数的距离值为正,我们必须在投影矩阵创建期间把near和far取反。

在openGL中,在视图空间中的一个3D点,被投影到近平面(透视面)。下图中一个点(Xe,Ye,Ze)投影到视椎体的近平面(Xp,Yp,Zp)上。

Top View of Frustum
Top View of Frustum

Top View of Frustum

翻译:视椎体的顶视图

Side View of Frustum
Side 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;

翻译:

从视椎体顶视图看,视图空间的x坐标,利用相似三角形比例计算,Xe被映射到Xp

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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

翻译:从视椎体的另一方面,Yp坐标也用同样的方法计算。

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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.)

翻译:

请注意:Xp和Yp依赖Ze;他们和-Ze成反比,换句话说:他们两个除以-Ze,这时构造GL_PROJECTIO矩阵第一条线索,然后视图坐标系通过乘以透视矩阵转换。裁剪坐标依旧是齐次坐标。裁剪坐标除以W分量,最终成为标准设备坐标。

Clip Coordinates
Clip Coordinates

,

Normalized Device Coordinates
Normalized Device Coordinates

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).

翻译:因此,我们能够将裁剪坐标的w分量设置为-Ze,并且,把投影矩阵第四列变换成(0, 0, -1, 0)。

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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

翻译:接下来,我们通过线性关系把投影坐标Xp和Yp转换成NDC下的Xn和Yn,即:[l, r] ⇒ [-1, 1] and [b, t] ⇒ [-1, 1].

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

Mapping from xp to xn

翻译:从Xp映射成Xn

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

Mapping from yp to yn

翻译:从Yp映射成Yn

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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

翻译:然后,我们替换Xp和Yp代入上面的方程。

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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.

翻译:

请注意:我们使两个方程都除以-Ze,(Xc/Wc,Yc/Wc),并且,我们更早设置Wc到-Ze,Xn和Yn方程括号内Xc和Yc是裁剪坐标。

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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.

翻译

现在,我们只需要处理投影矩阵的前三行。找到Zn和Xn、Yn有一点不同,因为在视图坐标中总是投影到-n的近平面。我们需要为唯一的z值做裁剪和深度测试,另外,我们应该能够对他取消投影(逆变换)。因此,我们知道Z值不依赖x和y值,所以,我们借用w分量去寻找Zn和Ze之间的关系。因此,我们能像下面指定投影矩阵的前三列。

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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

翻译:

在视图空间中,我们等于1,因此,方程变为:

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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.

翻译:

为了找到系数, A 和 B、 我们使用(ze,zn)关系(-n、 -1)和(-f,1),并将它们放入上述方程中。

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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

翻译:

解方程 A 和 B、 为B重写式(1);

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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

翻译:

将公式(1’)替换为 B 在式(2)中,求A;

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

Put A into eq.(1) to find B;

翻译:

放 A 进入式(1)中,找到 B;

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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

翻译:

我们发现 A 和 B.因此,ze 变成;

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

Finally, we found all entries of GL_PROJECTION matrix. The complete projection matrix is;

翻译:

最后,我们找到了投影矩阵的所有值。完成投影矩阵:

OpenGL Perspective Projection Matrix
OpenGL Perspective Projection Matrix

OpenGL Perspective Projection Matrix

翻译:

openGL透视投影矩阵

This projection matrix is for a general frustum. If the viewing volume is symmetric, which is

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

and

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

, then it can be simplified as;

翻译:

这个投影矩阵为一般的视锥体裁,如果我们观察到他的体积是对称的,可以简化为:

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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 the near 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.

翻译:

在我们继续讨论之前,请先再看看式(3)中,Ze和 Zn之间的关系,。你注意到它是一个有理函数,并且Ze和Zn是非线性关系。这意味着有非常高的精度近平面,但远平面精度很低 。如果范围[-n,-f]越来越大,则会导致深度精度问题(z-fighting);对远平面做一点小的改变对Zn的值没有影响。n和f之间的距离 n 和 f 应尽可能短,以尽量减少深度缓冲精度的问题。

Comparison of depth precision
Comparison of depth precision

Comparison of Depth Buffer Precisions

翻译:

深度缓冲精度比较

Orthographic Projection

翻译:

正交投影

OpenGL Orthographic Volume and NDC
OpenGL Orthographic Volume and NDC

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.

翻译

正交体积和标准化设备坐标(NDC)

为正交投影构造投影矩阵比透视模式简单得多。

Xe,Ye和Ze在视图空间中的成分被线性映射到NDC。我们只需要将一个矩形体积缩放成一个立方体,然后将它移到原点。让我们用线性关系找出投影的元素。

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

Mapping from xe to xn

翻译:

从Xe到Xn的映射

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

Mapping from ye to yn

翻译:

从Ye到Yn的映射

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

Mapping from ze to zn

从Ze到Zn的映射

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

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;

翻译:

由于正交投影不需要w分量,因此投影矩阵的第4行仍然是(0,0,0,1)。因此,完整的投影 矩阵对于正投影是;

OpenGL Orthographic Projection Matrix
OpenGL Orthographic Projection Matrix

OpenGL Orthographic Projection Matrix

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

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

and

opengl投影矩阵变换_opengl 坐标
opengl投影矩阵变换_opengl 坐标

.

翻译:

OpenGL正交投影矩阵

如果观察体积是对称的,可以进一步简化

OpenGL Symmetric Orthographic Projection Matrix
OpenGL Symmetric Orthographic Projection Matrix

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

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 英文原版地址
  • 前言
  • Overview
  • 概述
  • Perspective Projection
  • Orthographic Projection
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档