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