Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1051 chris 1
package com.gebauz.bauzoid.graphics.renderstates;
2
 
3
import com.gebauz.bauzoid.app.BauzoidException;
4
import com.gebauz.bauzoid.math.Matrix4;
5
 
6
/** RenderStates
7
 * Manage render states, tracking changes, supporting locking, and applying state switches only if necessary.
8
 * Usage example:
9
 *    renderStates.blending.setBlendingMode(BlendingStates.BlendingMode.MULTIPLY);
10
 *    // ... set other render states
11
 *    renderStates.activate();
12
 *    // ...render object...
13
 *    renderStates.deactivate();
14
 *
15
 * All Render State Objects have three kinds of internal values: target, current, and default.
16
 *
17
 * Target values are those set by the user - those are the states the user wishes to set.
18
 *
19
 * Current values are those that the system is tracking, the current state. Each render state
20
 * compares the target with the current value and only performs a state change when they differ
21
 * to prevent unnecessary state changes.
22
 *
23
 * Default values are the base values that are set initially or when calling reset.
24
 *
25
 */
26
public class RenderStates
27
{
28
        public static final int NUM_TEXTURE_STAGES = 2;
29
        public static final int MAX_VERTEX_BUFFER_ID = 8;
30
 
31
        public BlendingStates blending = new BlendingStates(this);
32
        public CullingStates culling = new CullingStates(this);
33
        public DepthTestStates depthTest = new DepthTestStates(this);
34
        public ScissorStates scissor = new ScissorStates(this);
35
 
36
        private TextureStage[] mTextureStages = new TextureStage[NUM_TEXTURE_STAGES];
37
        private boolean mLocked = false;
38
 
39
        public static final int MATRIX_STACK_SIZE = 16;
40
 
41
        //public Matrix4 modelViewProjection; 
42
        public Matrix4 model = Matrix4.createIdentity();
43
        public Matrix4 view = Matrix4.createIdentity();
44
        public Matrix4 projection = Matrix4.createIdentity();
45
 
46
        private Matrix4[] mModelStack = new Matrix4[MATRIX_STACK_SIZE];
47
        private Matrix4[] mViewStack = new Matrix4[MATRIX_STACK_SIZE];
48
        private Matrix4[] mProjectionStack = new Matrix4[MATRIX_STACK_SIZE];
49
 
50
        private int mModelStackCurrentIndex = 0;
51
        private int mViewStackCurrentIndex = 0;
52
        private int mProjectionStackCurrentIndex = 0;
53
 
54
        /** Constructor. */
55
        public RenderStates()
56
        {
57
                for (int i = 0; i < NUM_TEXTURE_STAGES; i++)
58
                {
59
                        mTextureStages[i] = new TextureStage(this, i);
60
                }
61
 
62
                for (int i = 0; i < MATRIX_STACK_SIZE; i++)
63
                {
64
                        mModelStack[i] = Matrix4.createIdentity();
65
                        mViewStack[i] = Matrix4.createIdentity();
66
                        mProjectionStack[i] = Matrix4.createIdentity();
67
                }
68
        }
69
 
70
        /** Initialize render states. */
71
        public void initialize() throws BauzoidException
72
        {
73
                blending.initialize();
74
                culling.initialize();
75
                depthTest.initialize();
76
                scissor.initialize();
77
 
78
                for (int i = 0; i < NUM_TEXTURE_STAGES; i++)
79
                {
80
                        mTextureStages[i].initialize();
81
                }
82
        }
83
 
84
        /** Activate renderstates - calls activate(false). */
85
        public void activate()
86
        {
87
                activate(false);
88
        }
89
 
90
        /** Activate render states with force parameter. */
91
        public void activate(boolean force)
92
        {
93
                blending.activate(force);
94
                culling.activate(force);
95
                depthTest.activate(force);
96
                scissor.activate(force);
97
        }
98
 
99
        /** Reset to default values. */
100
        public void reset()
101
        {
102
                blending.reset();
103
                culling.reset();
104
                depthTest.reset();
105
                scissor.reset();
106
        }
107
 
108
        /** Deactivate render states. */
109
        public void deactivate()
110
        {
111
                reset();
112
                activate(false);
113
        }
114
 
115
        /** Check if the renderstates are locked. */
116
        public boolean isLocked()
117
        {
118
                return mLocked;
119
        }
120
 
121
        /** Lock renderstates. */
122
        public void lock(boolean lock)
123
        {
124
                mLocked = lock;
125
        }
126
 
127
        /** Get a texture stage. */
128
        public final TextureStage getTextureStage(int index)
129
        {
130
                return mTextureStages[index];
131
        }
132
 
133
        /** Multiply matrices and return the full model-view-projection matrix. */
134
        public final Matrix4 getModelViewProjection()
135
        {
136
                Matrix4 mvpMatrix = Matrix4.multiply(model, view);
137
                mvpMatrix = Matrix4.multiply(mvpMatrix, projection);
138
                return mvpMatrix;
139
        }
140
 
141
        public final void getModelViewProjection(Matrix4 target)
142
        {
143
                Matrix4.multiply(target, model, view);
144
                Matrix4.multiply(target, target, projection);
145
        }
146
 
147
        /** Push current model matrix to stack. */
148
        public final void pushModelMatrix()
149
        {
150
                if (mModelStackCurrentIndex >= MATRIX_STACK_SIZE)
151
                        return;                
152
 
153
                // Create copy so that calling code can work on a different matrix without affecting anything
154
                mModelStack[mModelStackCurrentIndex].copyFrom(model);
155
                mModelStackCurrentIndex++;
156
        }
157
 
158
        /** Pop current model matrix from stack. */
159
        public final void popModelMatrix()
160
        {
161
                if (mModelStackCurrentIndex <= 0)
162
                        return;
163
 
164
                mModelStackCurrentIndex--;
165
                model.copyFrom(mModelStack[mModelStackCurrentIndex]);          
166
        }
167
 
168
        /** Push current model matrix to stack. */
169
        public final void pushViewMatrix()
170
        {
171
                if (mViewStackCurrentIndex >= MATRIX_STACK_SIZE)
172
                        return;                
173
 
174
                mViewStack[mViewStackCurrentIndex].copyFrom(view);
175
                mViewStackCurrentIndex++;
176
        }
177
 
178
        /** Pop current model matrix from stack. */
179
        public final void popViewMatrix()
180
        {
181
                if (mViewStackCurrentIndex <= 0)
182
                        return;
183
 
184
                mViewStackCurrentIndex--;
185
                view.copyFrom(mViewStack[mViewStackCurrentIndex]);             
186
        }
187
 
188
        /** Push current model matrix to stack. */
189
        public final void pushProjectionMatrix()
190
        {
191
                if (mProjectionStackCurrentIndex >= MATRIX_STACK_SIZE)
192
                        return;                
193
 
194
                mProjectionStack[mProjectionStackCurrentIndex].copyFrom(projection);
195
                mProjectionStackCurrentIndex++;
196
        }
197
 
198
        /** Pop current model matrix from stack. */
199
        public final void popProjectionMatrix()
200
        {
201
                if (mProjectionStackCurrentIndex <= 0)
202
                        return;
203
 
204
                mProjectionStackCurrentIndex--;
205
                projection.copyFrom(mProjectionStack[mProjectionStackCurrentIndex]);           
206
        }
207
 
208
 
209
}
210
 
211
 
212