fna-workbench

fna-workbench Git Source Tree


Root/src/Graphics/Viewport.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#region License
/* FNA - XNA4 Reimplementation for Desktop Platforms
 * Copyright 2009-2016 Ethan Lee and the MonoGame Team
 *
 * Released under the Microsoft Public License.
 * See LICENSE for details.
 */
#endregion
 
#region Using Statements
using System;
#endregion
 
namespace Microsoft.Xna.Framework.Graphics
{
    /// <summary>
    /// Describes the view bounds for render-target surface.
    /// </summary>
    [Serializable]
    public struct Viewport
    {
        #region Public Properties
 
        /// <summary>
        /// The height of the bounds in pixels.
        /// </summary>
        public int Height
        {
            get
            {
                return height;
            }
            set
            {
                height = value;
            }
        }
 
        /// <summary>
        /// The upper limit of depth of this viewport.
        /// </summary>
        public float MaxDepth
        {
            get
            {
                return maxDepth;
            }
            set
            {
                maxDepth = value;
            }
        }
 
        /// <summary>
        /// The lower limit of depth of this viewport.
        /// </summary>
        public float MinDepth
        {
            get
            {
                return minDepth;
            }
            set
            {
                minDepth = value;
            }
        }
 
        /// <summary>
        /// The width of the bounds in pixels.
        /// </summary>
        public int Width
        {
            get
            {
                return width;
            }
            set
            {
                width = value;
            }
        }
 
        /// <summary>
        /// The y coordinate of the beginning of this viewport.
        /// </summary>
        public int Y
        {
            get
            {
                return y;
 
            }
            set
            {
                y = value;
            }
        }
 
        /// <summary>
        /// The x coordinate of the beginning of this viewport.
        /// </summary>
        public int X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }
 
        /// <summary>
        /// Gets the aspect ratio of this <see cref="Viewport"/>, which is width / height.
        /// </summary>
        public float AspectRatio
        {
            get
            {
                if ((height != 0) && (width != 0))
                {
                    return (((float) width) / ((float) height));
                }
                return 0.0f;
            }
        }
 
        /// <summary>
        /// Gets or sets a boundary of this <see cref="Viewport"/>.
        /// </summary>
        public Rectangle Bounds
        {
            get
            {
                return new Rectangle(
                    x,
                    y,
                    width,
                    height
                );
            }
 
            set
            {
                x = value.X;
                y = value.Y;
                width = value.Width;
                height = value.Height;
            }
        }
 
        /// <summary>
        /// Returns the subset of the viewport that is guaranteed to be visible on a lower quality display.
        /// </summary>
        public Rectangle TitleSafeArea
        {
            get
            {
                return Bounds;
            }
        }
 
        #endregion
 
        #region Private Variables
 
        private int x;
        private int y;
        private int width;
        private int height;
        private float minDepth;
        private float maxDepth;
 
        #endregion
 
        #region Public Constructors
 
        /// <summary>
        /// Constructs a viewport from the given values. The <see cref="MinDepth"/> will be 0.0 and <see cref="MaxDepth"/> will be 1.0.
        /// </summary>
        /// <param name="x">The x coordinate of the upper-left corner of the view bounds in pixels.</param>
        /// <param name="y">The y coordinate of the upper-left corner of the view bounds in pixels.</param>
        /// <param name="width">The width of the view bounds in pixels.</param>
        /// <param name="height">The height of the view bounds in pixels.</param>
        public Viewport(int x, int y, int width, int height)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            minDepth = 0.0f;
            maxDepth = 1.0f;
        }
 
        /// <summary>
        /// Constructs a viewport from the given values.
        /// </summary>
        /// <param name="bounds">A <see cref="Rectangle"/> that defines the location and size of the <see cref="Viewport"/> in a render target.</param>
        public Viewport(Rectangle bounds)
        {
            x = bounds.X;
            y = bounds.Y;
            width = bounds.Width;
            height = bounds.Height;
            minDepth = 0.0f;
            maxDepth = 1.0f;
        }
 
        #endregion
 
        #region Public Methods
 
        /// <summary>
        /// Projects a <see cref="Vector3"/> from world space into screen space.
        /// </summary>
        /// <param name="source">The <see cref="Vector3"/> to project.</param>
        /// <param name="projection">The projection <see cref="Matrix"/>.</param>
        /// <param name="view">The view <see cref="Matrix"/>.</param>
        /// <param name="world">The world <see cref="Matrix"/>.</param>
        /// <returns></returns>
        public Vector3 Project(
            Vector3 source,
            Matrix projection,
            Matrix view,
            Matrix world
        ) {
            Matrix matrix = Matrix.Multiply(
                Matrix.Multiply(world, view),
                projection
            );
            Vector3 vector = Vector3.Transform(source, matrix);
 
            float a = (((source.X * matrix.M14) + (source.Y * matrix.M24)) + (source.Z * matrix.M34)) + matrix.M44;
            if (!MathHelper.WithinEpsilon(a, 1.0f))
            {
                vector.X = vector.X / a;
                vector.Y = vector.Y / a;
                vector.Z = vector.Z / a;
            }
 
            vector.X = (((vector.X + 1f) * 0.5f) * Width) + X;
            vector.Y = (((-vector.Y + 1f) * 0.5f) * Height) + Y;
            vector.Z = (vector.Z * (MaxDepth - MinDepth)) + MinDepth;
            return vector;
        }
 
        /// <summary>
        /// Unprojects a <see cref="Vector3"/> from screen space into world space.
        /// </summary>
        /// <param name="source">The <see cref="Vector3"/> to unproject.</param>
        /// <param name="projection">The projection <see cref="Matrix"/>.</param>
        /// <param name="view">The view <see cref="Matrix"/>.</param>
        /// <param name="world">The world <see cref="Matrix"/>.</param>
        /// <returns></returns>
        public Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world)
        {
            Matrix matrix = Matrix.Invert(
                Matrix.Multiply(
                    Matrix.Multiply(world, view),
                    projection
                )
            );
            source.X = (((source.X - X) / ((float) Width)) * 2f) - 1f;
            source.Y = -((((source.Y - Y) / ((float) Height)) * 2f) - 1f);
            source.Z = (source.Z - MinDepth) / (MaxDepth - MinDepth);
            Vector3 vector = Vector3.Transform(source, matrix);
 
            float a = (
                ((source.X * matrix.M14) + (source.Y * matrix.M24)) +
                (source.Z * matrix.M34)
            ) + matrix.M44;
            if (!MathHelper.WithinEpsilon(a, 1.0f))
            {
                vector.X = vector.X / a;
                vector.Y = vector.Y / a;
                vector.Z = vector.Z / a;
            }
 
            return vector;
        }
 
        /// <summary>
        /// Returns a <see cref="String"/> representation of this <see cref="Viewport"/> in the format:
        /// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Width:[<see cref="Width"/>] Height:[<see cref="Height"/>] MinDepth:[<see cref="MinDepth"/>] MaxDepth:[<see cref="MaxDepth"/>]}
        /// </summary>
        /// <returns>A <see cref="String"/> representation of this <see cref="Viewport"/>.</returns>
        public override string ToString()
        {
            return (
                "{" +
                "X:" + x.ToString() +
                " Y:" + y.ToString() +
                " Width:" + width.ToString() +
                " Height:" + height.ToString() +
                " MinDepth:" + minDepth.ToString() +
                " MaxDepth:" + maxDepth.ToString() +
                "}"
            );
        }
 
        #endregion
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.09160s using 11 queries.