source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp @ 2861

Revision 2861, 2.9 KB checked in by mattausch, 16 years ago (diff)

cleaned up code

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