Struct Viewport
Defines the viewport transformation from normalized device coordinates to screen space.
public readonly struct Viewport
- Inherited Members
Remarks
The viewport maps coordinates from NDC space [-1, 1] to screen space [x, x+width] and [y, y+height]. The depth range maps from NDC space [0, 1] to [minDepth, maxDepth].
Constructors
Viewport(float, float, float, float, float, float)
Creates a new viewport.
public Viewport(float x, float y, float width, float height, float minDepth = 0, float maxDepth = 1)
Parameters
xfloatX coordinate of upper-left corner.
yfloatY coordinate of upper-left corner.
widthfloatWidth in pixels. Must be greater than 0.
heightfloatHeight in pixels. Must be greater than 0.
minDepthfloatMinimum depth value (default 0.0).
maxDepthfloatMaximum depth value (default 1.0).
Exceptions
- ArgumentOutOfRangeException
Thrown when width or height is less than or equal to 0.
Fields
Height
Gets the height of the viewport in pixels.
public readonly float Height
Field Value
MaxDepth
Gets the maximum depth value (default 1.0).
public readonly float MaxDepth
Field Value
MinDepth
Gets the minimum depth value (default 0.0).
public readonly float MinDepth
Field Value
Width
Gets the width of the viewport in pixels.
public readonly float Width
Field Value
X
Gets the X coordinate of the viewport's upper-left corner in pixels.
public readonly float X
Field Value
Y
Gets the Y coordinate of the viewport's upper-left corner in pixels.
public readonly float Y
Field Value
Methods
FromFramebuffer(Framebuffer)
Creates a viewport that covers the entire framebuffer.
public static Viewport FromFramebuffer(Framebuffer framebuffer)
Parameters
framebufferFramebufferThe framebuffer to create a viewport for.
Returns
- Viewport
A viewport covering the entire framebuffer.
Transform(float, float, float, out float, out float, out float)
Transforms a position from normalized device coordinates to screen space.
public void Transform(float ndcX, float ndcY, float ndcZ, out float screenX, out float screenY, out float depth)
Parameters
ndcXfloatX coordinate in NDC space [-1, 1].
ndcYfloatY coordinate in NDC space [-1, 1].
ndcZfloatZ coordinate in NDC space [0, 1].
screenXfloatOutput X coordinate in screen space.
screenYfloatOutput Y coordinate in screen space.
depthfloatOutput depth value.
Remarks
NDC coordinates map as follows:
- X: -1 (left) to +1 (right)
- Y: -1 (top) to +1 (bottom) [Note: Y is inverted from standard GL convention]
- Z: 0 (near) to 1 (far)