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