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 | RenderState::RenderState():
|
---|
17 | mAlphaTestEnabled(false),
|
---|
18 | mTexturesEnabled(false),
|
---|
19 | mMode(RENDER),
|
---|
20 | mIsDepthPass(false)
|
---|
21 | {
|
---|
22 | Reset();
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | bool RenderState::SetState(Mode mode)
|
---|
27 | {
|
---|
28 | ///////////
|
---|
29 | //-- just change the modewhen necessary
|
---|
30 |
|
---|
31 | if (mode == mMode) return false;
|
---|
32 |
|
---|
33 | mMode = mode;
|
---|
34 |
|
---|
35 | if (mode == QUERY)
|
---|
36 | {
|
---|
37 | if (!mIsDepthPass)
|
---|
38 | {
|
---|
39 | glDisable(GL_LIGHTING);
|
---|
40 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
41 |
|
---|
42 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
43 | cgGLDisableProfile(sCgVertexProfile);
|
---|
44 | }
|
---|
45 |
|
---|
46 | glDepthMask(GL_FALSE);
|
---|
47 |
|
---|
48 | SetState(false, false);
|
---|
49 | }
|
---|
50 | else // mode returns to render
|
---|
51 | {
|
---|
52 | /////////////
|
---|
53 | //-- restore render state
|
---|
54 |
|
---|
55 | if (!mIsDepthPass)
|
---|
56 | {
|
---|
57 | glEnable(GL_LIGHTING);
|
---|
58 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
59 |
|
---|
60 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
61 | cgGLEnableProfile(sCgVertexProfile);
|
---|
62 | }
|
---|
63 |
|
---|
64 | glDepthMask(GL_TRUE);
|
---|
65 | }
|
---|
66 |
|
---|
67 | return true;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | void RenderState::SetState(bool texturing, bool alphaTest)
|
---|
72 | {
|
---|
73 | if (mAlphaTestEnabled && !alphaTest)
|
---|
74 | {
|
---|
75 | mAlphaTestEnabled = false;
|
---|
76 | glDisable(GL_ALPHA_TEST);
|
---|
77 | }
|
---|
78 | else if (!mAlphaTestEnabled && alphaTest)
|
---|
79 | {
|
---|
80 | mAlphaTestEnabled = true;
|
---|
81 | glEnable(GL_ALPHA_TEST);
|
---|
82 | }
|
---|
83 |
|
---|
84 | // allow only alpha textures in depth pass
|
---|
85 | if (mTexturesEnabled &&
|
---|
86 | (!texturing || mIsDepthPass && !mAlphaTestEnabled))
|
---|
87 | {
|
---|
88 | mTexturesEnabled = false;
|
---|
89 |
|
---|
90 | cgGLBindProgram(sCgMrtFragmentProgram);
|
---|
91 |
|
---|
92 | glDisable(GL_TEXTURE_2D);
|
---|
93 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
94 | }
|
---|
95 | else if (!mTexturesEnabled && texturing)
|
---|
96 | {
|
---|
97 | if (!mIsDepthPass || mAlphaTestEnabled)
|
---|
98 | {
|
---|
99 | mTexturesEnabled = true;
|
---|
100 |
|
---|
101 | cgGLBindProgram(sCgMrtFragmentTexProgram);
|
---|
102 |
|
---|
103 | glEnable(GL_TEXTURE_2D);
|
---|
104 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | void RenderState::Reset()
|
---|
111 | {
|
---|
112 | mCurrentVboId = -1;
|
---|
113 | SetState(false, false);
|
---|
114 | SetState(RENDER);
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | } |
---|