OpenGL ES 入門 (三) 投影—— 譯自《Beginning Android Games》

來源:互聯網
上載者:User

     投影

    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.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.