// Copyright (c) Wojciech Figat. All rights reserved.
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
#pragma once
#include "Vector2.h"
struct Matrix;
struct Rectangle;
///
/// Describes the viewport dimensions.
///
API_STRUCT(InBuild) struct FLAXENGINE_API Viewport
{
public:
union
{
struct
{
// Position of the pixel coordinate of the upper-left corner of the viewport.
float X;
// Position of the pixel coordinate of the upper-left corner of the viewport.
float Y;
};
// Upper left corner location.
Float2 Location;
};
union
{
struct
{
// Width dimension of the viewport.
float Width;
// Height dimension of the viewport.
float Height;
};
// Size
Float2 Size;
};
// Minimum depth of the clip volume.
float MinDepth;
// Maximum depth of the clip volume.
float MaxDepth;
public:
///
/// Empty constructor.
///
Viewport() = default;
///
/// Initializes a new instance of the struct.
///
/// The x coordinate of the upper-left corner of the viewport in pixels.
/// The y coordinate of the upper-left corner of the viewport in pixels.
/// The width of the viewport in pixels.
/// The height of the viewport in pixels.
/// The minimum depth of the clip volume.
/// The maximum depth of the clip volumes.
Viewport(float x, float y, float width, float height, float minDepth = 0.0f, float maxDepth = 1.0f)
: X(x)
, Y(y)
, Width(width)
, Height(height)
, MinDepth(minDepth)
, MaxDepth(maxDepth)
{
}
///
/// Initializes a new instance of the struct.
///
/// The viewport size.
explicit Viewport(const Float2& size)
: X(0)
, Y(0)
, Width(size.X)
, Height(size.Y)
, MinDepth(0.0f)
, MaxDepth(1.0f)
{
}
///
/// Initializes a new instance of the struct.
///
/// A bounding rectangle that defines the location and size of the viewport in a render target.
Viewport(const Rectangle& bounds);
public:
String ToString() const;
public:
// Gets the aspect ratio used by the viewport.
float GetAspectRatio() const
{
return Height != 0.0f ? Width / Height : 0.0f;
}
// Gets the size of the viewport.
Rectangle GetBounds() const;
// Sets the size of the viewport.
void SetBounds(const Rectangle& bounds);
public:
bool operator==(const Viewport& other) const
{
return X == other.X && Y == other.Y && Width == other.Width && Height == other.Height && MinDepth == other.MinDepth && MaxDepth == other.MaxDepth;
}
bool operator!=(const Viewport& other) const
{
return X != other.X || Y != other.Y || Width != other.Width || Height != other.Height || MinDepth != other.MinDepth || MaxDepth != other.MaxDepth;
}
public:
///
/// Projects a 3D vector from object space into screen space.
///
/// The vector to project.
/// A combined World*View*Projection matrix.
/// The projected vector.
void Project(const Vector3& source, const Matrix& vp, Vector3& result) const;
///
/// Converts a screen space point into a corresponding point in world space.
///
/// The vector to un-project.
/// An inverted combined World*View*Projection matrix.
/// The un-projected vector
void Unproject(const Vector3& source, const Matrix& ivp, Vector3& result) const;
};
template<>
struct TIsPODType
{
enum { Value = true };
};
DEFINE_DEFAULT_FORMATTING(Viewport, "X:{0} Y:{1} Width:{2} Height:{3} MinDepth:{4} MaxDeth:{5}", v.X, v.Y, v.Width, v.Height, v.MinDepth, v.MaxDepth);