Skip to content

Viewport

Chuck Walbourn edited this page Nov 3, 2015 · 17 revisions

Viewport class modeled after the XNA Game Studio graphics library.

Header

#include <SimpleMath.h>

Initialization

using namespace DirectX::SimpleMath;

Viewport vp;                        // Creates the viewport [0,0,0,0,0,1]
Viewport vp(0,0,640,480);           // Creates the viewport [0,0,640,480,0,1]
Viewport vp(0,0,640,480,0.1f,0.9f); // Creates the viewport [0,0,640.480,0.1,0.9]

RECT rct;
rct.top = 0;
rct.left = 0;
rct.right = 640;
rct.bottom = 480;
Viewport vp(rct); // Creates the viewport [0,0,640,480,0,1]

Fields

  • x: Upper left x position of the viewport
  • y: Upper left y position of the viewport
  • width: The width of the viewport
  • height: The height of the viewport
  • minDepth: The minimum depth of the viewport
  • maxDepth: The maximum depth of the viewport

Methods

  • Comparison operators: == and !=
  • Assignment operators: =
  • AspectRatio: Compute the aspect ratio. If the width or height is zero, it returns 0
  • Project: Projects a 3D point using given world, view, and projection matrices an the viewport, moving from world coordinates to screen space.
  • Unproject: Reverse projects a 3D point using given world, view, and projection matrices an the viewport moving from screen space to world coordinates.

Statics

  • ComputeDisplayArea: When using various scaling modes with the backbuffer via DXGI_SWAP_CHAIN_DESC1.Scaling, the backbuffer width and height is not necessarily the same size as the output window. Given the backbuffer width and height in pixels, the window output width and height in pixels, and the scaling setting, this function computes the pixel rectangle where the backbuffer content is visible within the window.

If using ICoreWindow, be sure to convert logicalWidth and logicalHeight from DIPs to pixels using the window's DPI property. int ConvertDipsToPixels(float dips, float dpi) { return int(dips * dpi / 96.f + 0.5f); }

  • ComputeTitleSafeArea: When displaying on televisions, edges of the screen can't always be seen by users. This function takes a backbuffer width and height in pixels and returns a 'safe' area rectangle for the content to be displayed.

Note that the XNA Game Studio equivalent of this function only returned the reduced safe area on Xbox, and returns the full rectangle otherwise on Windows phone and PC. Because the universal Windows app platform can share a single binary or set of binaries across many devices, it is up to the app to determine what device it is currently running under. Therefore, this function always returns the reduced safe area and it is up to the app to apply this in the correct circumstances.

Remarks

The Viewport can be converted to and from a D3D11_VIEWPORT structure. To use with Direct3D 11, you typically need an address of the viewport which is provided by Get11:

Viewport viewPort(0.0f, 0.0f, outputWidth, outputHeight);

context->RSSetViewports(1, viewPort.Get11());

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Windows 8.1
  • Xbox One

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v18
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectX Tool Kit for DirectX 12

DirectXMesh

DirectXTex

DirectXMath

Win2D

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

Clone this wiki locally