1 | #include "RenderState.h"
|
---|
2 | #include "Geometry.h"
|
---|
3 | #include "Material.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | namespace CHCDemoEngine
|
---|
7 | {
|
---|
8 |
|
---|
9 | CGprofile RenderState::sCgFragmentProfile;
|
---|
10 | CGprofile RenderState::sCgVertexProfile;
|
---|
11 |
|
---|
12 | CGprogram RenderState::sCgMrtFragmentProgram = NULL;
|
---|
13 | CGprogram RenderState::sCgMrtFragmentTexProgram = NULL;
|
---|
14 |
|
---|
15 |
|
---|
16 | /////////////////
|
---|
17 |
|
---|
18 | RenderState::RenderState():
|
---|
19 | mAlphaTestEnabled(false),
|
---|
20 | mCullFaceEnabled(true),
|
---|
21 | mTexturesEnabled(false),
|
---|
22 | mMode(RENDER),
|
---|
23 | mRenderType(FIXED)
|
---|
24 | {
|
---|
25 | Reset();
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | bool RenderState::SetState(Mode mode)
|
---|
30 | {
|
---|
31 | ///////////
|
---|
32 | //-- just change the modewhen necessary
|
---|
33 |
|
---|
34 | if (mode == mMode) return false;
|
---|
35 |
|
---|
36 | mMode = mode;
|
---|
37 |
|
---|
38 | if (mode == QUERY)
|
---|
39 | {
|
---|
40 | if (mRenderType != DEPTH_PASS)
|
---|
41 | {
|
---|
42 | glDisable(GL_LIGHTING);
|
---|
43 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
44 |
|
---|
45 | if (mRenderType == DEFERRED)
|
---|
46 | {
|
---|
47 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
48 | cgGLDisableProfile(sCgVertexProfile);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | glDepthMask(GL_FALSE);
|
---|
53 |
|
---|
54 | SetState(false, false, false);
|
---|
55 | }
|
---|
56 | else // mode returns to render
|
---|
57 | {
|
---|
58 | /////////////
|
---|
59 | //-- restore render state
|
---|
60 |
|
---|
61 | if (mRenderType != DEPTH_PASS)
|
---|
62 | {
|
---|
63 | glEnable(GL_LIGHTING);
|
---|
64 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
65 |
|
---|
66 | if (mRenderType == DEFERRED)
|
---|
67 | {
|
---|
68 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
69 | cgGLEnableProfile(sCgVertexProfile);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | glDepthMask(GL_TRUE);
|
---|
74 | }
|
---|
75 |
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | void RenderState::SetState(bool texturing, bool alphaTest, bool cullFace)
|
---|
81 | {
|
---|
82 | if (mCullFaceEnabled && !cullFace)
|
---|
83 | {
|
---|
84 | mCullFaceEnabled = false;
|
---|
85 | glDisable(GL_CULL_FACE);
|
---|
86 | }
|
---|
87 | else if (!mCullFaceEnabled && cullFace)
|
---|
88 | {
|
---|
89 | mCullFaceEnabled = true;
|
---|
90 | glEnable(GL_CULL_FACE);
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (mAlphaTestEnabled && !alphaTest)
|
---|
94 | {
|
---|
95 | mAlphaTestEnabled = false;
|
---|
96 |
|
---|
97 | // not needed with GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
|
---|
98 | //glDisable(GL_ALPHA_TEST);
|
---|
99 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
100 | }
|
---|
101 | else if (!mAlphaTestEnabled && alphaTest)
|
---|
102 | {
|
---|
103 | mAlphaTestEnabled = true;
|
---|
104 |
|
---|
105 | // not needed with GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
|
---|
106 | //glEnable(GL_ALPHA_TEST);
|
---|
107 | glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (mTexturesEnabled &&
|
---|
111 | (!texturing || (mRenderType == DEPTH_PASS) && !mAlphaTestEnabled))
|
---|
112 | {
|
---|
113 | mTexturesEnabled = false;
|
---|
114 |
|
---|
115 | if (mRenderType == DEFERRED)
|
---|
116 | cgGLBindProgram(sCgMrtFragmentProgram);
|
---|
117 |
|
---|
118 | glDisable(GL_TEXTURE_2D);
|
---|
119 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
120 | }
|
---|
121 | else if (!mTexturesEnabled && texturing)
|
---|
122 | {
|
---|
123 | // support only alpha textures in depth pass
|
---|
124 | if ((mRenderType != DEPTH_PASS) || mAlphaTestEnabled)
|
---|
125 | {
|
---|
126 | mTexturesEnabled = true;
|
---|
127 |
|
---|
128 | if (mRenderType == DEFERRED)
|
---|
129 | cgGLBindProgram(sCgMrtFragmentTexProgram);
|
---|
130 |
|
---|
131 | glEnable(GL_TEXTURE_2D);
|
---|
132 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | void RenderState::Reset()
|
---|
139 | {
|
---|
140 | mCurrentVboId = -1;
|
---|
141 |
|
---|
142 | SetState(false, false, true);
|
---|
143 | SetState(RENDER);
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | } |
---|