Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Viewport.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Globalization;
5 using System.Runtime.InteropServices;
6 using SiliconStudio.Core.Mathematics;
7 
8 namespace SiliconStudio.Paradox.Graphics
9 {
10  /// <summary>
11  /// Defines the window dimensions of a render-target surface onto which a 3D volume projects.
12  /// </summary>
13  [StructLayout(LayoutKind.Sequential, Pack = 4)]
14  public struct Viewport : IEquatable<Viewport>
15  {
16  /// <summary>
17  /// Empty value for an undefined viewport.
18  /// </summary>
19  public static readonly Viewport Empty;
20 
21  /// <summary>
22  /// Gets or sets the pixel coordinate of the upper-left corner of the viewport on the render-target surface.
23  /// </summary>
24  public float X;
25 
26  /// <summary>Gets or sets the pixel coordinate of the upper-left corner of the viewport on the render-target surface.</summary>
27  public float Y;
28 
29  /// <summary>Gets or sets the width dimension of the viewport on the render-target surface, in pixels.</summary>
30  public float Width;
31 
32  /// <summary>Gets or sets the height dimension of the viewport on the render-target surface, in pixels.</summary>
33  public float Height;
34 
35  /// <summary>Gets or sets the minimum depth of the clip volume.</summary>
36  public float MinDepth;
37 
38  /// <summary>Gets or sets the maximum depth of the clip volume.</summary>
39  public float MaxDepth;
40 
41  /// <summary>Creates an instance of this object.</summary>
42  /// <param name="x">The x coordinate of the upper-left corner of the viewport in pixels.</param>
43  /// <param name="y">The y coordinate of the upper-left corner of the viewport in pixels.</param>
44  /// <param name="width">The width of the viewport in pixels.</param>
45  /// <param name="height">The height of the viewport in pixels.</param>
46  public Viewport(int x, int y, int width, int height)
47  {
48  X = x;
49  Y = y;
50  Width = width;
51  Height = height;
52  MinDepth = 0;
53  MaxDepth = 1;
54  }
55 
56  /// <summary>Creates an instance of this object.</summary>
57  /// <param name="bounds">A bounding box that defines the location and size of the viewport in a render target.</param>
58  public Viewport(Rectangle bounds)
59  {
60  X = bounds.X;
61  Y = bounds.Y;
62  Width = bounds.Width;
63  Height = bounds.Height;
64  MinDepth = 0;
65  MaxDepth = 1;
66  }
67 
68  /// <summary>Gets the size of this resource.</summary>
69  public Rectangle Bounds
70  {
71  get { return new Rectangle((int)X, (int)Y, (int)Width, (int)Width); }
72  set
73  {
74  X = value.X;
75  Y = value.Y;
76  Width = value.Width;
77  Height = value.Height;
78  }
79  }
80 
81  public bool Equals(Viewport other)
82  {
83  return other.X.Equals(X) && other.Y.Equals(Y) && other.Width.Equals(Width) && other.Height.Equals(Height) && other.MinDepth.Equals(MinDepth) && other.MaxDepth.Equals(MaxDepth);
84  }
85 
86  public override bool Equals(object obj)
87  {
88  if (ReferenceEquals(null, obj)) return false;
89  if (obj.GetType() != typeof (Viewport)) return false;
90  return Equals((Viewport) obj);
91  }
92 
93  public override int GetHashCode()
94  {
95  unchecked
96  {
97  int result = X.GetHashCode();
98  result = (result*397) ^ Y.GetHashCode();
99  result = (result*397) ^ Width.GetHashCode();
100  result = (result*397) ^ Height.GetHashCode();
101  result = (result*397) ^ MinDepth.GetHashCode();
102  result = (result*397) ^ MaxDepth.GetHashCode();
103  return result;
104  }
105  }
106 
107  public static bool operator ==(Viewport left, Viewport right)
108  {
109  return left.Equals(right);
110  }
111 
112  public static bool operator !=(Viewport left, Viewport right)
113  {
114  return !left.Equals(right);
115  }
116 
117  /// <summary>Retrieves a string representation of this object.</summary>
118  public override string ToString()
119  {
120  return string.Format(CultureInfo.CurrentCulture, "{{X:{0} Y:{1} Width:{2} Height:{3} MinDepth:{4} MaxDepth:{5}}}", new object[] { X, Y, Width, Height, MinDepth, MaxDepth });
121  }
122 
123  private static bool WithinEpsilon(float a, float b)
124  {
125  float num = a - b;
126  return ((-1.401298E-45f <= num) && (num <= float.Epsilon));
127  }
128 
129  /// <summary>Projects a 3D vector from object space into screen space.</summary>
130  /// <param name="source">The vector to project.</param>
131  /// <param name="projection">The projection matrix.</param>
132  /// <param name="view">The view matrix.</param>
133  /// <param name="world">The world matrix.</param>
134  public Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world)
135  {
136  Matrix matrix = Matrix.Multiply(Matrix.Multiply(world, view), projection);
137  Vector4 vector = Vector3.Transform(source, matrix);
138  float a = (((source.X * matrix.M14) + (source.Y * matrix.M24)) + (source.Z * matrix.M34)) + matrix.M44;
139  if (!WithinEpsilon(a, 1f))
140  {
141  vector = (vector / a);
142  }
143  vector.X = (((vector.X + 1f) * 0.5f) * Width) + X;
144  vector.Y = (((-vector.Y + 1f) * 0.5f) * Height) + Y;
145  vector.Z = (vector.Z * (MaxDepth - MinDepth)) + MinDepth;
146  return new Vector3(vector.X, vector.Y, vector.Z);
147  }
148 
149  /// <summary>Converts a screen space point into a corresponding point in world space.</summary>
150  /// <param name="source">The vector to project.</param>
151  /// <param name="projection">The projection matrix.</param>
152  /// <param name="view">The view matrix.</param>
153  /// <param name="world">The world matrix.</param>
154  public Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world)
155  {
156  Matrix matrix = Matrix.Invert(Matrix.Multiply(Matrix.Multiply(world, view), projection));
157  source.X = (((source.X - X) / Width) * 2f) - 1f;
158  source.Y = -((((source.Y - Y) / Height) * 2f) - 1f);
159  source.Z = (source.Z - MinDepth) / (MaxDepth - MinDepth);
160  Vector4 vector = Vector3.Transform(source, matrix);
161  float a = (((source.X * matrix.M14) + (source.Y * matrix.M24)) + (source.Z * matrix.M34)) + matrix.M44;
162  if (!WithinEpsilon(a, 1f))
163  {
164  vector = vector / a;
165  }
166  return new Vector3(vector.X, vector.Y, vector.Z);
167  }
168 
169  /// <summary>Gets the aspect ratio used by the viewport</summary>
170  public float AspectRatio
171  {
172  get
173  {
174  if ( Width != 0 && Height != 0)
175  {
176  return ((float) Width)/Height;
177  }
178  return 0f;
179  }
180  }
181  }
182 }
function b
override bool Equals(object obj)
Definition: Viewport.cs:86
function a
float Width
Gets or sets the width dimension of the viewport on the render-target surface, in pixels...
Definition: Viewport.cs:30
float Height
Gets or sets the height dimension of the viewport on the render-target surface, in pixels...
Definition: Viewport.cs:33
float X
The X component of the vector.
Definition: Vector4.cs:83
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
float MinDepth
Gets or sets the minimum depth of the clip volume.
Definition: Viewport.cs:36
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
float MaxDepth
Gets or sets the maximum depth of the clip volume.
Definition: Viewport.cs:39
Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world)
Projects a 3D vector from object space into screen space.
Definition: Viewport.cs:134
float X
Gets or sets the pixel coordinate of the upper-left corner of the viewport on the render-target surfa...
Definition: Viewport.cs:24
float M44
Value at row 4 column 4 of the matrix.
Definition: Matrix.cs:147
Viewport(Rectangle bounds)
Creates an instance of this object.
Definition: Viewport.cs:58
Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world)
Converts a screen space point into a corresponding point in world space.
Definition: Viewport.cs:154
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
Viewport(int x, int y, int width, int height)
Creates an instance of this object.
Definition: Viewport.cs:46
Defines the window dimensions of a render-target surface onto which a 3D volume projects.
Definition: Viewport.cs:14
float Y
Gets or sets the pixel coordinate of the upper-left corner of the viewport on the render-target surfa...
Definition: Viewport.cs:27
override string ToString()
Retrieves a string representation of this object.
Definition: Viewport.cs:118
float Y
The Y component of the vector.
Definition: Vector4.cs:89
System.Windows.Shapes.Rectangle Rectangle
Definition: ColorPicker.cs:16
static readonly Viewport Empty
Empty value for an undefined viewport.
Definition: Viewport.cs:19
float M34
Value at row 3 column 4 of the matrix.
Definition: Matrix.cs:142
SiliconStudio.Core.Mathematics.Vector3 Vector3
float Z
The Z component of the vector.
Definition: Vector4.cs:95
Structure using the same layout than System.Drawing.Rectangle
Definition: Rectangle.cs:35
float M24
Value at row 2 column 4 of the matrix.
Definition: Matrix.cs:137
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47