Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
787 chris 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
 
7
using BauzoidNET.math;
8
 
9
namespace BauzoidNET.graphics.renderstates
10
{
11
    public class RenderStates
12
    {
13
        public static int NUM_TEXTURE_STAGES = 2;
14
            public static int MAX_VERTEX_BUFFER_ID = 8;
15
 
16
            public BlendingStates blending = null;
17
        public CullingStates culling = null;
18
        public DepthTestStates depthTest = null;
19
        public ScissorStates scissor = null;
20
 
21
            private TextureStage[] mTextureStages = new TextureStage[NUM_TEXTURE_STAGES];
22
            private bool mLocked = false;
23
 
24
            public const int MATRIX_STACK_SIZE = 16;
25
 
26
            //public Matrix4 modelViewProjection; 
27
            public Matrix4 model = Matrix4.createIdentity();
28
            public Matrix4 view = Matrix4.createIdentity();
29
            public Matrix4 projection = Matrix4.createIdentity();
30
 
31
            private Matrix4[] mModelStack = new Matrix4[MATRIX_STACK_SIZE];
32
            private Matrix4[] mViewStack = new Matrix4[MATRIX_STACK_SIZE];
33
            private Matrix4[] mProjectionStack = new Matrix4[MATRIX_STACK_SIZE];
34
 
35
            private int mModelStackCurrentIndex = 0;
36
            private int mViewStackCurrentIndex = 0;
37
            private int mProjectionStackCurrentIndex = 0;
38
 
39
            /** Constructor. */
40
            public RenderStates()
41
            {
42
            blending = new BlendingStates(this);
43
            culling = new CullingStates(this);
44
                depthTest = new DepthTestStates(this);
45
                scissor = new ScissorStates(this);
46
 
47
                    for (int i = 0; i < NUM_TEXTURE_STAGES; i++)
48
                    {
49
                            mTextureStages[i] = new TextureStage(this, i);
50
                    }
51
 
52
                    for (int i = 0; i < MATRIX_STACK_SIZE; i++)
53
                    {
54
                            mModelStack[i] = Matrix4.createIdentity();
55
                            mViewStack[i] = Matrix4.createIdentity();
56
                            mProjectionStack[i] = Matrix4.createIdentity();
57
                    }
58
            }
59
 
60
            /** Initialize render states. */
61
            public void initialize() //throws BauzoidException
62
            {
63
                    blending.initialize();
64
                    culling.initialize();
65
                    depthTest.initialize();
66
                    scissor.initialize();
67
 
68
                    for (int i = 0; i < NUM_TEXTURE_STAGES; i++)
69
                    {
70
                            mTextureStages[i].initialize();
71
                    }
72
            }
73
 
74
            /** Activate renderstates - calls activate(false). */
75
            public void activate()
76
            {
77
                    activate(false);
78
            }
79
 
80
            /** Activate render states with force parameter. */
81
            public void activate(bool force)
82
            {
83
                    blending.activate(force);
84
                    culling.activate(force);
85
                    depthTest.activate(force);
86
                    scissor.activate(force);
87
            }
88
 
89
            /** Reset to default values. */
90
            public void reset()
91
            {
92
                    blending.reset();
93
                    culling.reset();
94
                    depthTest.reset();
95
                    scissor.reset();
96
            }
97
 
98
            /** Deactivate render states. */
99
            public void deactivate()
100
            {
101
                    reset();
102
                    activate(false);
103
            }
104
 
105
            /** Check if the renderstates are locked. */
106
            public bool isLocked()
107
            {
108
                    return mLocked;
109
            }
110
 
111
            /** Lock renderstates. */
112
            public void lockStates(bool doLock)
113
            {
114
                    mLocked = doLock;
115
            }
116
 
117
            /** Get a texture stage. */
118
            public TextureStage getTextureStage(int index)
119
            {
120
                    return mTextureStages[index];
121
            }
122
 
123
            /** Multiply matrices and return the full model-view-projection matrix. */
124
            public Matrix4 getModelViewProjection()
125
            {
126
                    Matrix4 mvpMatrix = Matrix4.multiply(model, view);
127
                    mvpMatrix = Matrix4.multiply(mvpMatrix, projection);
128
                    return mvpMatrix;
129
            }
130
 
131
            public void getModelViewProjection(Matrix4 target)
132
            {
133
                    Matrix4.multiply(target, model, view);
134
                    Matrix4.multiply(target, target, projection);
135
            }
136
 
137
            /** Push current model matrix to stack. */
138
            public void pushModelMatrix()
139
            {
140
                    if (mModelStackCurrentIndex >= MATRIX_STACK_SIZE)
141
                            return;                    
142
 
143
                    // Create copy so that calling code can work on a different matrix without affecting anything
144
                    mModelStack[mModelStackCurrentIndex].copyFrom(model);
145
                    mModelStackCurrentIndex++;
146
            }
147
 
148
            /** Pop current model matrix from stack. */
149
            public void popModelMatrix()
150
            {
151
                    if (mModelStackCurrentIndex <= 0)
152
                            return;
153
 
154
                    mModelStackCurrentIndex--;
155
                    model.copyFrom(mModelStack[mModelStackCurrentIndex]);              
156
            }
157
 
158
            /** Push current model matrix to stack. */
159
            public void pushViewMatrix()
160
            {
161
                    if (mViewStackCurrentIndex >= MATRIX_STACK_SIZE)
162
                            return;                    
163
 
164
                    mViewStack[mViewStackCurrentIndex].copyFrom(view);
165
                    mViewStackCurrentIndex++;
166
            }
167
 
168
            /** Pop current model matrix from stack. */
169
            public void popViewMatrix()
170
            {
171
                    if (mViewStackCurrentIndex <= 0)
172
                            return;
173
 
174
                    mViewStackCurrentIndex--;
175
                    view.copyFrom(mViewStack[mViewStackCurrentIndex]);         
176
            }
177
 
178
            /** Push current model matrix to stack. */
179
            public void pushProjectionMatrix()
180
            {
181
                    if (mProjectionStackCurrentIndex >= MATRIX_STACK_SIZE)
182
                            return;                    
183
 
184
                    mProjectionStack[mProjectionStackCurrentIndex].copyFrom(projection);
185
                    mProjectionStackCurrentIndex++;
186
            }
187
 
188
            /** Pop current model matrix from stack. */
189
            public void popProjectionMatrix()
190
            {
191
                    if (mProjectionStackCurrentIndex <= 0)
192
                            return;
193
 
194
                    mProjectionStackCurrentIndex--;
195
                    projection.copyFrom(mProjectionStack[mProjectionStackCurrentIndex]);               
196
            }
197
 
198
 
199
    }
200
}