OpenGL中坐标体系

There are multiplecoordinate systems involved in 3D Graphics. In this article i will try toexplain how they interact with each other and what’s their purpose in afriendly and easy way.

在3D图形编程中有多个坐标系统被使用。在这篇文章中,我会尝试用一种友好简单的方式解释他们是怎样彼此相互作用,以及各自的目的是什么。

What always hashelped me in understanding 3D Math, Coordinate Systems and Transformations –Hands and Objects! Use them, play around with your (empty) Coffee Cup, drawaxis and objects … it’s hard to grasp the basics but believe me: At some pointit makes Bang and you understand it.

什么东西一直在帮助我理解3D数学,坐标系和变换—手掌和对象!使用他们,玩转你的咖啡杯,画出坐标轴和对象……这是很难掌握的基础知识,但是相信我:在某个时候你会理解他,他也会爆炸。

Part 1: Coordinate Systems in General and in OpenGLespecially

Part 1: 常规坐标系和OpenGL坐标系的特殊性

Most 3D Systemsuse the so called “Cartesian Coordinate System” – you probably know this infrom School where it’s heavily used in Geometry to draw different things. Oneproperty of a Cartesian coordinate System is that the cardinal axis areperpendicular. Take a look at the 2D Cartesian Coordinate System below:

大多数3D系统使用所谓的“笛卡尔坐标系”—你可能是在学校知道他的,在学校他大量地被用在几何中画不同的东西。笛卡尔坐标系的一个属性是其基本坐标轴是垂直的。下面是一个2D笛卡尔坐标系:

The X and Y axisare perpendicular and range from -infinity to +infinity on each axis. Let’sexpand it to the 3rd Dimension. We simply add ad new axis which goes throughthe origin and is perpendicular to X and Y – we call this new axis Z:

X/Y坐标轴互相垂直,每个坐标轴的取值区间从负无穷到正无穷。让我们扩展他到3维空间。我们简单的添加一个新轴,他贯穿原点,和X/Y互相垂直—我们叫这个新轴为Z轴:

One might ask whythe positive X is to the right, the positive Y goes up and the positive Z goesforward in our direction. That’s because of the Handiness of the coordinateSystem. There are two different Systems: Right-Handed and Left-Handedcoordinate systems. OpenGL uses a Right-Handed System (same as the one shownabove).  As a reminder, do the following:

有人可能为问为什么X正方向在右,Y正方向在上和Z的正方向面向我们呢?这是因为坐标系惯用手来比拟。有两个不同的坐标体系:右手坐标系和左手坐标系。OpenGL使用右手坐标系(也就是上图所示)。为了加深记忆,做做下列步骤:

  • Stretch your Right Arm and form a 90° angle with your Elbow
  • Point with your Thumb to the right side
  • Point with your Pointing Finger up
  • Point with your Middle Finger in your direction
  • 伸展右臂,在肘处形成一个直角
  • 用拇指指向右边
  • 食指指向上
  • 中指指向自己

Your Hand shouldnow form a right-handed coordinate System.

现在你的手就是一个右手坐标系。

Part 2: The different Coordinate Systems in OpenGL

Part 2: OpenGL中的不同坐标体系

There are multiplecoordinate Systems involved in 3D Graphics:

  • Object Space
  • World Space (aka Model Space)
  • Camera Space (aka Eye Space or View Space)
  • Screen Space (aka Clip Space)

So, what’s thedifference between them?

在3D图形中使用了多个坐标系统:

  • 对象空间
  • 世界空间 (模型空间)
  • 摄像机空间 (眼球空间或视图空间)
  • 屏幕空间 (裁剪空间)

那么,他们之间有什么不同呢?

Object Space

This is the localcoordinate System of your Geometrical Objects. Imagine a Cube which consists of8 vertices:

这是一个几何物体的局部坐标系统,想象一个拥有8个顶点的立方体。

When modellingthis cube you do this in Object Space – the Object stands alone for itself, noother things in here.

当在对象空间描述物体时,物体是单独描述的的,不会同时在一个坐标系中描述其他物体。(也就是说每个物体的中心都是在原点描述的)

World Space

This is the Spacewhere everything is positioned. Imagine the Cube created above – it has beenmodelled in it’s own absolute Object Space. But you probably want the Cube atsome other location in your World – so we need to transform the ObjectCoordinate System to the desired World coordinate System position. We do thisusing a Transformation Matrix – every vertex in the Cube (which is in ObjectSpace) is multiplied by the World Matrix which transforms the vertex to it’snew position and orientation.

世界空间将定位所有的物体。想象一下上面的立方体--他在自己的绝对对象空间中被描述。但是你可能想要将这个立方体定位在你的世界中的某个地方—,所以我们需要转换对象坐标系统到期望的世界坐标系中的位置。我们是使用一个转换矩阵来完成坐标转换—对象空间中的立方体的每个顶点被世界矩阵所乘,这样得到这个顶点的新位置和方向。

Let’s explain thiswith a example:

  1. We have the Cube shown above
  2. We want to position it in World coordinates at position X=5, Y=0, Z=0 (simply move it 5 units to the right)

用一个例子解释一下:

  1. 我们有一个立方体
  2. 我们要把他放在世界坐标系中的X=5, Y=0, Z=0 位置上。(simply move it 5 units to the right)

What we need todo, is to multiply each vertex with the corresponding Model Matrix for theactual positioning of this object (we have different Model Matrices fordifferently located objects). The resulting Vertex has the new position inWorld Space.

需要做的是,立方体的每个顶点乘以相应模型矩阵得到世界空间实际位置(对于不同位置的物体有不同的模型矩阵)。在世界空间的新位置上产生顶点。

Even if the mathfor doing the matrix multiplication is out of the scope i want to show you anexample:

The Vector V2 is[-1,-1,1]. We have to use a 4 Component Vector (x,y,z,w) so we set w simply at1 (-1,-1,1,1)
The Matrix M1 is:

[1,0,0,5,
0,1,0,0,
0,0,1,0,
0,0,0,1]

尽管矩阵乘法超出文章范围,这里我展示一个例子

有向量V2[-1,-1,1],但需要使用一个4元向量(x,y,z,w),所有我将w直接设为1,结果是V2[-1,-1,1,1]。

有矩阵M1

[1,0,0,5,
0,1,0,0,
0,0,1,0,
0,0,0,1]

When we nowmultiply those two (M1 . V2) we get a new vector: [4,-1,1,1] – which is V2moved 5 units to the left. This calculation is done for every vertex in thecube. If you don’t know how to actually do the matrix multiplication, just readone of the books mentioned at the bottom of this article (or the correspondingwikipedia article, your old math book, whatever, … :-))

两者相乘之后,我们得到一个新的向量[4,-1,1,1]—这是向量V2向右移动5个单位。对于立方体的每个顶点这次计算就完成了。【矩阵乘法可以死套公式,矩阵的构建是不是根据要变换的方向和距离构建的】如果你不知道矩阵乘法的实际过程,读一本文章最后提到书即可,或者相应的维基文章,或者老旧的数学课本无论什么。

Camera Space

Now that we havepositioned everything in our World we want to look at it. First, there is nosuch “real” thing as a Camera in OpenGL. Think about the following for a shorttime:

  • Moving a Video Camera Backward is the same as moving the filmed object forward

That’s exactlywhat we are going to do to get our view on the scene – multiply every vertex inthe World Space with our View Matrix. Each Vertex is then in Camera Space – andthe scene looks like we are looking at it through the camera.

Imagine the cameraas a abstract thing which is on the positive Z-Axis and looks down the negativeend of the Z-Axis. Imagine also a rotation and translation of the World aroundyou so that you can see what you want to see – this rotation and translation isthe View Matrix.

As for the WorldSpace, the Mathematics for doing this is out of Scope for this Overview.

现在我们已经在我们的世界中定位了所有想看到的物体。首先,在OpenGL中摄像机并不是一个真实的存在。快速思考一下下面所述

  • 向后移动摄像机如同向前移动拍摄的物体【Z轴正向读者,摄像机向后,沿着Z负向前进如同物体向前,沿着Z正向前进】

我们要做的是将场景可视化—世界空间中每个顶点都和我们的视图矩阵相乘。然后摄像机空间中的每个顶点和场景就像通过摄像机看到的一样。

把摄像机想象成一个在Z轴正向上的抽象物体,朝向Z轴负向拍摄。再想象一下你旋转和变换一下你周围的世界,南洋就能看到想看到的—这个旋转和变换就是视图矩阵。

世界坐标体系到摄像机坐标体系转换的数学计算就超过这篇略说的范围了。

Projection Space

So, we have everyvertex in a position that we see the scene the way we want it to see. The lastCoordinate System transformation is from Camera to Screen Space – which isessentially nothing more than going from the 3D Coordinate System to the 2DScreen in front of us. This transformation is necessary as long as we don’thave real 3D Holographic Screens. In the process of transforming from Camera toScreen Space you can either choose a Orthographic or Perspective Projection.

The difference between those two is that the Orthographic Projection does not apply aperspective distortion and the perspective one does. Perspective Projection isthe natural one as we Humans see in a perspective Way – things farther awayfrom us appear smaller.

现在,按照我们想要的观察方式,场景中所有的顶点都在自己的位置上。最后一次坐标系变换是从摄像机坐标体系到屏幕坐标体系—对我们来说这不比3D坐标体系到2D体系的转换多什么。因为我们没有真正的3D全息屏幕,所以这种转换是必要的。从摄像机空间到屏幕空间转换的过程中,你可以选在正交投影或者透视投影。

两者不同之处在于,正交投影不会应用透视失真,而透视投影会应用这一特性。投射投影是自然的,人眼是在透视投影下工作的—近大远小。

Graphical overviewof the Transformation

If you neverworked with 3D graphics before it’s probably more intuitive to take a look atthe graphic below to get an idea on how the transformations actually modify thecoordinates of the vertices:

如果之前从未在3D图形下工作过,下面图形可能看起来更直观理解顶点坐标实际怎样被修改的

From top left tobottom right, the following actions occur:从坐上到右下发生了如下的事情

  1. Vertices of the Object to draw are in Object space (as modelled in your 3D Modeller)
  2. 物体顶点被画在物体空间
  3. … get transformed into World space by multiplying it with the Model Matrix
  4. …乘以模型矩阵转换到世界空间
  5. Vertices are now in World space (used to position the all the objects in your scene)
  6. 此刻顶点正在世界空间中
  7. … get transformed into Camera space by multiplying it with the View Matrix
  8. …乘以视图矩阵转换到摄像机空间
  9. Vertices are now in View Space – think of it as if you were looking at the scene throught “the camera”
  10. 此刻顶点正在视图空间中
  11. … get transformed into Screen space by multiplying it with the Projection Matrix
  12. …乘以投影矩阵转换到屏幕空间
  13. Vertex is now in Screen Space – This is actually what you see on your Display.
  14. 此刻顶点正在屏幕空间中

Part 3: Further Reading

Part 3: 延伸阅读

There are manygood books out which explain the Mathematics behind those Transformations in agood and easy to understanding way – i can absolutely recommend you thefollowing two books:

OpenGL Superbible5th Edition:

It’s all aboutOpenGL Programming – and completly relies on the Programmable Pipeline presentin OpenGL 3.x and higher. Definitively worth a read if you want to learn OpenGL

3D Math Primer forGraphics and Game Development

It’s a completeBook on only one subject: 3D (and also 2D) Computer Graphics. Read this one ifyou really want to understand what’s going on under the hood.

If you can readGerman, i also recommend you the Book “XNA Spieleprogrammierung” published byMITP. Even if it’s for XNA the Math Chapter is excellent and is freelyavailable as a sample chapter here

Please keep inmind that you should have at least a good and solid understanding of BasicMathematics before reading those two books – doing multiplications, divisions,solving parenthesis and solving simple equations should make you not runningaway. Dig out your old School Books ! 

One last thing ineed to mention is that most books talk about a MODELVIEW Matrix. Becausemoving an Object around and “positioning the camera” is actually the same theyuse just one matrix. For the sake of simplicity i like to keep them seperate –should be no problem for actual hardware to make a few more matrixmultiplications … (first from object to world and then from world to view)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值