summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Graphics/Effects/MatrixChainEffect.cs
blob: 046479f32d1bd3cd1bdddf9142c35ac33145e1f3 (plain)
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
using System.Collections.Specialized;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MonoGame.Extended.Graphics.Effects
{
    /// <summary>
    ///     An <see cref="Effect" /> that uses the standard chain of matrix transformations to represent a 3D object on a 2D
    ///     monitor.
    /// </summary>
    /// <seealso cref="Effect" />
    /// <seealso cref="IEffectMatrices" />
    public abstract class MatrixChainEffect : Effect, IMatrixChainEffect
    {
        /// <summary>
        ///     The bitmask for use with <see cref="Flags"/> indicating wether <see cref="World"/>, <see cref="View"/>, or <see cref="Projection"/> has changed in the last frame.
        /// </summary>
        protected static int DirtyWorldViewProjectionBitMask = BitVector32.CreateMask();

        /// <summary>
        ///     The bitmask for use with <see cref="Flags"/> indicating wether to use a default projection matrix or a custom projection matrix.
        /// </summary>
        protected static int UseDefaultProjectionBitMask = BitVector32.CreateMask(DirtyWorldViewProjectionBitMask);

        /// <summary>
        ///     The dirty flags associated with this <see cref="MatrixChainEffect"/>.
        /// </summary>
        protected BitVector32 Flags;

        private Matrix _projection = Matrix.Identity;
        private Matrix _view = Matrix.Identity;
        private Matrix _world = Matrix.Identity;
        private EffectParameter _matrixParameter;

        /// <summary>
        ///     Gets or sets the model-to-world <see cref="Matrix" />.
        /// </summary>
        /// <value>
        ///     The model-to-world <see cref="Matrix" />.
        /// </value>
        public Matrix World
        {
            get { return _world; }
            set { SetWorld(ref value); }
        }

        /// <summary>
        ///     Gets or sets the world-to-view <see cref="Matrix" />.
        /// </summary>
        /// <value>
        ///     The world-to-view <see cref="Matrix" />.
        /// </value>
        public Matrix View
        {
            get { return _view; }
            set { SetView(ref value); }
        }

        /// <summary>
        ///     Gets or sets the view-to-projection <see cref="Matrix" />.
        /// </summary>
        /// <value>
        ///     The view-to-projection <see cref="Matrix" />.
        /// </value>
        public Matrix Projection
        {
            get { return _projection; }
            set { SetProjection(ref value); }
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="MatrixChainEffect" /> class.
        /// </summary>
        /// <param name="graphicsDevice">The graphics device.</param>
        /// <param name="byteCode">The effect code.</param>
        protected MatrixChainEffect(GraphicsDevice graphicsDevice, byte[] byteCode)
            : base(graphicsDevice, byteCode)
        {
            Initialize();
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="MatrixChainEffect" /> class.
        /// </summary>
        /// <param name="cloneSource">The clone source.</param>
        protected MatrixChainEffect(Effect cloneSource)
            : base(cloneSource)
        {
            Initialize();
        }

        private void Initialize()
        {
            Flags[UseDefaultProjectionBitMask] = true;

            _matrixParameter = Parameters["WorldViewProjection"];
        }

        /// <summary>
        ///     Sets the model-to-world <see cref="Matrix" />.
        /// </summary>
        /// <param name="world">The model-to-world <see cref="Matrix" />.</param>
        public void SetWorld(ref Matrix world)
        {
            _world = world;
            Flags[DirtyWorldViewProjectionBitMask] = true;
        }

        /// <summary>
        ///     Sets the world-to-view <see cref="Matrix" />.
        /// </summary>
        /// <param name="view">The world-to-view <see cref="Matrix" />.</param>
        public void SetView(ref Matrix view)
        {
            _view = view;
            Flags[DirtyWorldViewProjectionBitMask] = true;
        }

        /// <summary>
        ///     Sets the view-to-projection <see cref="Matrix" />.
        /// </summary>
        /// <param name="projection">The view-to-projection <see cref="Matrix" />.</param>
        public void SetProjection(ref Matrix projection)
        {
            _projection = projection;
            Flags[DirtyWorldViewProjectionBitMask] = true;
            Flags[UseDefaultProjectionBitMask] = false;
        }

        /// <summary>
        ///     Computes derived parameter values immediately before applying the effect.
        /// </summary>
        protected override void OnApply()
        {
            base.OnApply();

            // ReSharper disable once InvertIf
            if (Flags[DirtyWorldViewProjectionBitMask] || Flags[UseDefaultProjectionBitMask])
            {
                if (Flags[UseDefaultProjectionBitMask])
                {
                    var viewport = GraphicsDevice.Viewport;
                    _projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, -1);
                }

                Matrix worldViewProjection;
                Matrix.Multiply(ref _world, ref _view, out worldViewProjection);
                Matrix.Multiply(ref worldViewProjection, ref _projection, out worldViewProjection);
                _matrixParameter.SetValue(worldViewProjection);

                Flags[DirtyWorldViewProjectionBitMask] = false;
            }
        }
    }
}