Viewports and Frustums

來源:互聯網
上載者:User
Viewports and Frustums

Provides overview of viewports and frustums as related to 3D graphics concepts.

Conceptually, a viewport is a two-dimensional (2D) rectangle onto which a 3D scene is projected. In the XNA Framework, the rectangle exists as as x- and y-coordinates within a 2D surface used by XNA as a rendering target.

iewporgt的概念。

 

Properly positioning graphical objects on this surface requires a mathematical transform (known as a projection transform). This transform converts 3D vertices into the coordinate system of the viewport. Additionally, a viewport can specify the range of depth values for the render-target surface. This range usually lies between 0.0 and 1.0.

  • The Viewing Frustum
  • Viewport Rectangle
  • Clearing a Viewport

The Viewing Frustum

A viewing frustum is a 3D volume positioned relative to the viewport's camera. The shape of the volume affects how models are projected from camera space onto the screen.

view frustum的定義。它的形狀決定了模型到螢幕的投影。

 

The most common type of projection, a perspective projection, is responsible for making objects near the camera appear bigger than objects in the distance. For perspective viewing, the viewing frustum can be visualized as a pyramid, with the camera positioned at the tip. This pyramid is intersected by a front and back clipping plane. The volume within the pyramid between the front and back clipping planes is referred to as the viewing frustum. Objects are visible only when they are in this volume.

注意,物體只有在front和back的clipping plane中間才會被看到。

Imagine that you are standing in a dark room and looking through a square window. This is a good representation of a viewing frustum. In this analogy, the near clipping plane is the window, and the back clipping plane is whatever finally interrupts your view - the skyscraper across the street, the mountains in the distance, or nothing at all. You can see everything inside the truncated pyramid that starts at the window and ends with whatever interrupts your view, and nothing else.

一個形象的比喻,就是人在黑屋裡,通過一個窗子向外看。

 

The viewing frustum is defined by a field of view (fov), and by the distances of the front and back clipping planes, which are specified in z-coordinates. You can create a perspective fov using the CreatePerspectiveFieldOfView method. For more information on setting up a frustum and perspective transformation, see How To: Make a Third-Person Camera.

如何建立view frustum:fov + distance of front and back clipping.

Viewport Rectangle

You define a viewport rectangle in XNA by using the Viewport structure. For more information about using this structure in your game application, see Displays, Client Bounds, Viewports, and Back Buffers.

The Viewport structure contains four members - X, Y, Width, Height - that define the area of the render-target surface in which a scene is rendered. These values correspond to the destination rectangle, or viewport rectangle.

在XNA如何定義viewport。

 

The specified values for the X, Y, Width, and Height members are screen coordinates relative to the upper-left corner of the render-target surface. The structure defines two additional members (MinDepth and MaxDepth), indicating the near and far view planes.

注意viewport中的定義的x,y是基於左上方的。

Tip

The depth buffer has a limited resolution which is distributed between the near and far clip planes. If these planes are too far apart, two objects could have the same depth value even though they lie on different depth planes. In cases like this, the depth buffer is unable to distinguish between them because of its limited resolution.

To avoid this situation, set your near clipping plane as high as possible without causing near-plane clipping issues. For instance, avoid settings such as 0.000001, which would certainly cause problems.

注意解析度的問題,我們不能隨意設定MinDepth或者MaxDepth。

The XNA Framework assumes that the viewport clipping volume ranges from −1.0 to 1.0 in X, and from 1.0 to −1.0 in Y. These were the settings used most often by applications in the past. You can specify the viewport aspect ratio, used for the projection transform, with a call to CreatePerspectiveFieldOfView.

 

The XNA Framework uses the viewport location and dimensions to scale the vertices to fit a rendered scene into the appropriate location on the target surface. An internal matrix is applied to each vertex to scale vertices according to the viewport dimensions and desired depth range and translate them to the appropriate location on the render-target surface. This matrix also reverses the y-coordinate to reflect a screen origin at the top-left corner with y increasing downward. After this matrix is applied, vertices are still homogeneous. That is, they still exist as [x,y,z,w] vertices—and they must be converted to non-homogeneous coordinates before being sent to the rasterizer.

Note
The viewport scaling matrix incorporates the MinDepth and MaxDepth members of the Viewport structure to scale vertices to fit the specified depth range.

Clearing a Viewport

Clearing the viewport resets the contents of the viewport rectangle on the render-target surface. It can also clear the depth and stencil buffer surfaces.

Use Clear to clear the various components of the viewport. Overloaded versions accept various parameters such as a set of rectangles, defining the areas on the surface to be cleared, or a ClearOptions enumeration that specifies which buffers or render target to clear.

Use the Clear method to clear stencil bits within a depth buffer. Set the options parameter to determine which render-target components are cleared. The color argument sets the color value of the render target after it has been cleared. The depth parameter clears the depth buffer to the specified depth: 0.0 is the closest distance, and 1.0 is the farthest. Finally, the stencil paramter resets the stencil bits to the specified value. Use integers ranging from 0 to 2n−1, where n is the stencil buffer bit depth.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.