1 | #include "RenderState.h"
|
---|
2 | #include "Geometry.h"
|
---|
3 | #include "Material.h"
|
---|
4 | #include "ShaderProgram.h"
|
---|
5 | #include "Texture.h"
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 | namespace CHCDemoEngine
|
---|
10 | {
|
---|
11 |
|
---|
12 | CGprofile RenderState::sCgFragmentProfile;
|
---|
13 | CGprofile RenderState::sCgVertexProfile;
|
---|
14 |
|
---|
15 | ShaderProgram *RenderState::sCgMrtFragmentProgram = NULL;
|
---|
16 | ShaderProgram *RenderState::sCgMrtFragmentTexProgram = NULL;
|
---|
17 | ShaderProgram *RenderState::sCgMrtVertexProgram = NULL;
|
---|
18 |
|
---|
19 |
|
---|
20 | int RenderState::sTexParam;
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | /////////////////
|
---|
25 |
|
---|
26 | RenderState::RenderState():
|
---|
27 | mRenderType(FIXED),
|
---|
28 | mUseAlphaToCoverage(true),
|
---|
29 | mLockCullFaceEnabled(false)
|
---|
30 | {
|
---|
31 | Reset();
|
---|
32 | }
|
---|
33 |
|
---|
34 |
|
---|
35 | bool RenderState::SetMode(Mode mode)
|
---|
36 | {
|
---|
37 | ///////////
|
---|
38 | //-- just change the modewhen necessary
|
---|
39 |
|
---|
40 | if (mode == mMode) return false;
|
---|
41 |
|
---|
42 | mMode = mode;
|
---|
43 |
|
---|
44 | if (mode == QUERY)
|
---|
45 | {
|
---|
46 | if (mRenderType != DEPTH_PASS)
|
---|
47 | {
|
---|
48 | glDisable(GL_LIGHTING);
|
---|
49 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
50 |
|
---|
51 | if (mRenderType == DEFERRED)
|
---|
52 | {
|
---|
53 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
54 | cgGLDisableProfile(sCgVertexProfile);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | glDepthMask(GL_FALSE);
|
---|
59 |
|
---|
60 | static Material defaultMat;
|
---|
61 | SetState(&defaultMat);
|
---|
62 | }
|
---|
63 | else // mode returns to render
|
---|
64 | {
|
---|
65 | /////////////
|
---|
66 | //-- restore render state
|
---|
67 |
|
---|
68 | if (mRenderType != DEPTH_PASS)
|
---|
69 | {
|
---|
70 | glEnable(GL_LIGHTING);
|
---|
71 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
72 |
|
---|
73 | if (mRenderType == DEFERRED)
|
---|
74 | {
|
---|
75 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
76 | cgGLEnableProfile(sCgVertexProfile);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | glDepthMask(GL_TRUE);
|
---|
81 | }
|
---|
82 |
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | void RenderState::SetState(Material *mat)
|
---|
88 | {
|
---|
89 | Texture *tex = mat->GetTexture();
|
---|
90 |
|
---|
91 | const bool texturing = (tex != NULL);
|
---|
92 |
|
---|
93 | bool alphaTest = mat->IsAlphaTestEnabled();
|
---|
94 | bool cullFace = mat->IsCullFaceEnabled();
|
---|
95 |
|
---|
96 | if (!mLockCullFaceEnabled)
|
---|
97 | {
|
---|
98 | if (mCullFaceEnabled && !cullFace)
|
---|
99 | {
|
---|
100 | mCullFaceEnabled = false;
|
---|
101 | glDisable(GL_CULL_FACE);
|
---|
102 | }
|
---|
103 | else if (!mCullFaceEnabled && cullFace)
|
---|
104 | {
|
---|
105 | mCullFaceEnabled = true;
|
---|
106 | glEnable(GL_CULL_FACE);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | if (mAlphaTestEnabled && !alphaTest)
|
---|
110 | {
|
---|
111 | mAlphaTestEnabled = false;
|
---|
112 |
|
---|
113 | if (mUseAlphaToCoverage)
|
---|
114 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
115 | else
|
---|
116 | glDisable(GL_ALPHA_TEST);
|
---|
117 | }
|
---|
118 | else if (!mAlphaTestEnabled && alphaTest)
|
---|
119 | {
|
---|
120 | mAlphaTestEnabled = true;
|
---|
121 |
|
---|
122 | if (mUseAlphaToCoverage)
|
---|
123 | glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
124 | else
|
---|
125 | glEnable(GL_ALPHA_TEST);
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (mTexturesEnabled &&
|
---|
129 | (!texturing || (mRenderType == DEPTH_PASS) && !mAlphaTestEnabled))
|
---|
130 | {
|
---|
131 | mTexturesEnabled = false;
|
---|
132 |
|
---|
133 | if (mRenderType != DEFERRED)//!mat->mFragmentProgram)
|
---|
134 | glDisable(GL_TEXTURE_2D);
|
---|
135 |
|
---|
136 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
137 | }
|
---|
138 | else if (!mTexturesEnabled && texturing)
|
---|
139 | {
|
---|
140 | // support only alpha textures in depth pass
|
---|
141 | if ((mRenderType != DEPTH_PASS) || mAlphaTestEnabled)
|
---|
142 | {
|
---|
143 | mTexturesEnabled = true;
|
---|
144 |
|
---|
145 | if (mRenderType != DEFERRED)//!mat->mFragmentProgram)
|
---|
146 | glEnable(GL_TEXTURE_2D);
|
---|
147 |
|
---|
148 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (mRenderType == DEFERRED)
|
---|
153 | {
|
---|
154 | if (mat->GetFragmentProgram() && (mat->GetFragmentProgram() != mCurrentFragmentProgram))
|
---|
155 | {
|
---|
156 | mCurrentFragmentProgram = mat->GetFragmentProgram();
|
---|
157 | mCurrentFragmentProgram->Bind();
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (mat->GetVertexProgram() && (mat->GetVertexProgram() != mCurrentVertexProgram))
|
---|
161 | {
|
---|
162 | mCurrentVertexProgram = mat->GetVertexProgram();
|
---|
163 | mCurrentVertexProgram->Bind();
|
---|
164 | }
|
---|
165 | }
|
---|
166 | else
|
---|
167 | {
|
---|
168 | // set fixed texture
|
---|
169 | if (tex)
|
---|
170 | {
|
---|
171 | if (mCurrentTexId != tex->GetId())
|
---|
172 | {
|
---|
173 | tex->Bind();
|
---|
174 | mCurrentTexId = tex->GetId();
|
---|
175 | }
|
---|
176 | }
|
---|
177 | else
|
---|
178 | {
|
---|
179 | mCurrentTexId = 0;
|
---|
180 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | void RenderState::Reset()
|
---|
187 | {
|
---|
188 | SetMode(RENDER);
|
---|
189 |
|
---|
190 | mCurrentVboId = -1;
|
---|
191 | mCurrentTexId = 0;
|
---|
192 |
|
---|
193 | mCullFaceEnabled = true;
|
---|
194 | mAlphaTestEnabled = false;
|
---|
195 | mTexturesEnabled = false;
|
---|
196 |
|
---|
197 | mCurrentVertexProgram = NULL;
|
---|
198 | mCurrentFragmentProgram = NULL;
|
---|
199 |
|
---|
200 | glEnable(GL_CULL_FACE);
|
---|
201 |
|
---|
202 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
203 | glDisable(GL_ALPHA_TEST);
|
---|
204 |
|
---|
205 | glDisable(GL_TEXTURE_2D);
|
---|
206 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | void RenderState::SetUseAlphaToCoverage(bool useAlphaToCoverage)
|
---|
211 | {
|
---|
212 | mUseAlphaToCoverage = useAlphaToCoverage;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | void RenderState::SetRenderPassType(RenderPassType t)
|
---|
217 | {
|
---|
218 | mRenderType = t;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | RenderState::RenderPassType RenderState::GetRenderPassType() const
|
---|
223 | {
|
---|
224 | return mRenderType;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | void RenderState::LockCullFaceEnabled(bool lockCull)
|
---|
229 | {
|
---|
230 | mLockCullFaceEnabled = lockCull;
|
---|
231 | }
|
---|
232 |
|
---|
233 |
|
---|
234 | } |
---|