OpenGL ES 入门 (三) 投影—— 译自《Beginning Android Games》

     投影

    OpenGL ES的2D绘图映射到屏幕上是通过“投影”来完成的。我们已经提到过OpenGL ES主要和三角形有关。一个三角形在3D空间中有3个顶点(记住OpenGL ES对图形的操作都是在3D空间中进行的)。想要在framebuffer中绘制这样一个三角形,OPenGL ES需要知道三角形在3D空间中的3个顶点的坐标,这个坐标是系统基于像素由framebuffer所提供。当OpenGL ES知道三角形3个顶点的坐标以后,它就可以很简单的绘制三角形中所包含的像素到framebuffer中。通过把在三维空间的点坐标映射到2维空间中,通过画布(Canvas)就可以实现在连接2个点,我们就可以完成对OpenGL ES的简单操作。

 

    通常在3D绘图中会有2中不同的投影模式:

    Parallel, or orthographic, projection(平行,正投影): 如果你曾经使用过CAD,那么你可能已经知道这个模式了。平行投影不会去关心对象里照相机(Camera)有多远。在最终的图片中,对象的大小始终和它实际的大小是一样的。这种投影模式通常被使用在Open GL2D绘图中。(2D其实就是用OpenGL把3D空间中的对象投影到屏幕上)。

    Perspective projections(透视投影):这种投影模式就如同我们的眼睛看东西一样。离我们视网膜越远的对象看起来会越小。这种投影模式我们通常使用在OpenGL ES的3D绘图中。

 

    不管使用哪一种投影模式都需要一个叫做投影平面的东西。投影平面和我们的视网膜非常相似(其实就是和投影布差不多)。照射到投影平面上的光线会最终把对象投影到平面上,形成最终的图片。数学上的平面的范围是无限大的,但是我们视网膜的范围是有限的。OpenGL 的“视网膜”就等于图7 - 1中视图平头截体(就是没有尖角的四棱锥 图见在上一节中)头部的矩形。OpenGL ES 把对象投影到视图平头截体的这个部分上。这就是所谓的裁剪平面,裁剪平面拥有自己的坐标系统。图7 - 2展示了从相机的角度来观察和它的坐标系叠加的统裁剪平面。

   

   

      Figure 7–2. 裁剪平面(也称作投影平面) 和它的坐标系统.  

 

    注意这个平面的坐标系统是不固定的。我们可以通过对它的操作,使我们的投影平面工作在任意的坐标系统中(e.g.,我们可以指定OpenGL ES的原点在左下角并且X轴有480个单位,Y轴有320个单位。PS.再次注意,是单位不是像素,一个单位可能有多个像素)。听起来很熟悉?是的,OpenGL ES 在投影上允许我们为其指定任意我们所希望的坐标系统。

    当我们指定了视图平头截体以后,OpenGL ES会投射一束光线,光线会穿过投影平面。平行投影和投射投影的不同之处在于这些光线投射的方向。图7 - 3展示了2者之间的不同。

 

    Figure 7–3. 透视投影(左边) 和平行投影(右边)

 

    透视投影投射出的光线从三角形出发,通过Camera(或者我们的眼睛。PS:投影平面在2者中间)。对象距离投影平面越远,这个对象在投影平面上就显得越小。当我们使用平行投影的时候,光线是垂直的射向我们的投影平面。在这种情况下,不管距离投影平面的距离远近,投影在投影平面上的对象将会保持原有的大小。

 

    在OpenGL ES中投影平面被奇怪的称作为裁剪平面。在视野平头截体中,所有的平面都有类似的叫法。距离Camera最远的平面被称之为远裁剪平面。其它的则被称之为左,又,顶部,和底部裁剪平面,任何不在这些平面,或者在这些平面后面的对象将不会被渲染。如果对象只有一部分在这些平面内,则这个对象将会通过平面被裁剪,意味着这个对象不在视图平头截体内的部分将被切除。这就是裁剪平面名称的由来。

 

    你可能在担心为什么图7 - 3中正投影的视图平头截体是矩形。它反映出我们是如何去定义投影的裁剪平面的。在投射投影的情况下,它上,下,左,右的裁剪平面不是垂直与近裁剪平面和远裁剪平面的(观察图7 - 3,它只显示了左,右裁剪平面)正投影在这种情况下,这些平面是垂直与远、近裁剪平面的,它们将告诉OpenGL ES忽略对象的距离照相机的远近,以对象实际的大小去渲染它们。

 

PS ~: 觉得这个对你们有帮助的就留个脚印在这呗~给我点动力嘛,同时有什么建议也可以提出来^.^

 

    附上原文:

 

    Projections


    This 2D mapping is done via something called projection. We already mentioned that
OpenGL ES is mainly concerned with triangles. A single triangle has three points defined
in 3D space. To render such a triangle to the framebuffer, OpenGL ES needs to know
the coordinates of these 3D points within the pixel-based coordinate system of the
framebuffer. Once it knows those three corner-point coordinates, it can simply draw the
pixels in the framebuffer that are inside the triangle. We could even write our own little
OpenGL ES implementation by projecting 3D points to 2D, and simply draw lines
between them via the Canvas.


    There are two kinds of projections that are commonly used in 3D graphics. :
   

    Parallel, or orthographic, projection: If you have ever played with a
CAD application you might already know about these. A parallel
projection doesn’t care how far an object is away from the camera; the
object will always have the same size in the final image. This type of
projection is typically used for rendering 2D graphics in OpenGL ES.

    Perspective projections: These are what we are used to when using
our eyes. Objects further away from us will appear smaller on our
retina. This type of projection is typically used when we do 3D
graphics with OpenGL ES.


    In both cases we need something called a projection plane. This is nearly exactly the
same as the retina of our eyes. It’s where the light is actually registered to form the final
image. While a mathematical plane is infinite, our retina is limited in area. Our OpenGLES “retina” is equal to the rectangle at the top of the view frustum in Figure 7–1. This part of the view frustum is where OpenGL ES will project the points to. It is called the near clipping plane and has its own little 2D coordinate system. Figure 7–2 shows that
near clipping plane again, from the point of view of the camera, with the coordinate
system superimposed. 

 

    Note that the coordinate system is by no means fixed. We can manipulate it so that we
can work in any projected coordinate system we like (e.g., we could instruct OpenGL
ESto let the origin be in the bottom-left corner, and let the visible area of the “retina” be
480 units on the x-axis and 320 units on the y-axis). Sounds familiar? Yes, OpenGL ES
allows us to specify any coordinate system we want for the projected points.
Once we specify our view frustum, OpenGL ES then takes each point of a triangle and
shoots a ray from it through the projection plane. The difference between a parallel and
a perspective projection is how the direction of those rays is constructed. Figure 7–3
shows the difference between the two, viewed from above.

 

    A perspective projection shoots the rays from the triangle points through the camera (or
eye, in this case). Objects further away will thus appear smaller on the projection plane.
When we use a parallel projection, the rays are shot perpendicular to the projection
plane. In this case an object will keep its size on the projection plane no matter how far
away it is.


    Our projection plane is called a near clipping plane in OpenGL ES lingo, as pointed out
earlier. All of the sides of the view frustum have similar names. The one furthest away
from the camera is called the far clipping plane. The others are called the left, right, top,
and bottom clipping planes. Anything outside or behind those planes will not be
rendered. Objects that are partially within the view frustum will be clipped from these
planes, meaning that the parts outside the view frustum get cut away. That’s where the
name clipping plane comes from. 


    You might be wondering why the view frustum of the parallel projection case in Figure 7–3
is rectangular. It turns out that the projection is actually governed by how we define our
clipping planes. In the case of a perspective projection, the left, right, top, and bottom
clipping planes are not perpendicular to the near and far planes (see Figure 7–3, which
only shows the left and right clipping planes. In the case of the parallel projection, these planes are perpendicular, which tells OpenGL ES to render everything at the same size
no matter how far away it is from the camera.

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值