Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ShaderTechnique.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "ShaderTechnique.h"
21 
23  m_HasAlpha(false), m_HasBlend(false), m_HasColorMask(false), m_HasDepthMask(false), m_HasDepthFunc(false)
24 {
25 }
26 
28 {
29  m_Shader->Bind();
30 
31 #if !CONFIG2_GLES
32  if (m_HasAlpha)
33  {
34  glEnable(GL_ALPHA_TEST);
35  glAlphaFunc(m_AlphaFunc, m_AlphaRef);
36  }
37 #endif
38  // TODO: maybe emit some warning if GLSL shaders try to use alpha test;
39  // the test should be done inside the shader itself
40 
41  if (m_HasBlend)
42  {
43  glEnable(GL_BLEND);
44  glBlendFunc(m_BlendSrc, m_BlendDst);
45  }
46 
47  if (m_HasColorMask)
49 
50  if (m_HasDepthMask)
51  glDepthMask(m_DepthMask);
52 
53  if (m_HasDepthFunc)
54  glDepthFunc(m_DepthFunc);
55 }
56 
58 {
59  m_Shader->Unbind();
60 
61 #if !CONFIG2_GLES
62  if (m_HasAlpha)
63  glDisable(GL_ALPHA_TEST);
64 #endif
65 
66  if (m_HasBlend)
67  glDisable(GL_BLEND);
68 
69  if (m_HasColorMask)
70  glColorMask(1, 1, 1, 1);
71 
72  if (m_HasDepthMask)
73  glDepthMask(1);
74 
75  if (m_HasDepthFunc)
76  glDepthFunc(GL_LEQUAL);
77 }
78 
79 void CShaderPass::AlphaFunc(GLenum func, GLclampf ref)
80 {
81  m_HasAlpha = true;
82  m_AlphaFunc = func;
83  m_AlphaRef = ref;
84 }
85 
86 void CShaderPass::BlendFunc(GLenum src, GLenum dst)
87 {
88  m_HasBlend = true;
89  m_BlendSrc = src;
90  m_BlendDst = dst;
91 }
92 
93 void CShaderPass::ColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a)
94 {
95  m_HasColorMask = true;
96  m_ColorMaskR = r;
97  m_ColorMaskG = g;
98  m_ColorMaskB = b;
99  m_ColorMaskA = a;
100 }
101 
102 void CShaderPass::DepthMask(GLboolean mask)
103 {
104  m_HasDepthMask = true;
105  m_DepthMask = mask;
106 }
107 
108 void CShaderPass::DepthFunc(GLenum func)
109 {
110  m_HasDepthFunc = true;
111  m_DepthFunc = func;
112 }
113 
114 
116  : m_SortByDistance(false)
117 {
118 }
119 
121 {
122  m_Passes.push_back(pass);
123 }
124 
126 {
127  return m_Passes.size();
128 }
129 
131 {
132  ENSURE(0 <= pass && pass < (int)m_Passes.size());
133  m_Passes[pass].Bind();
134 }
135 
137 {
138  ENSURE(0 <= pass && pass < (int)m_Passes.size());
139  m_Passes[pass].Unbind();
140 }
141 
143 {
144  ENSURE(0 <= pass && pass < (int)m_Passes.size());
145  return m_Passes[pass].GetShader();
146 }
147 
149 {
150  return m_SortByDistance;
151 }
152 
154 {
155  m_SortByDistance = enable;
156 }
157 
159 {
160  m_SortByDistance = false;
161  m_Passes.clear();
162 }
void Bind()
Set up all the GL state that was previously specified on this pass.
void AlphaFunc(GLenum func, GLclampf ref)
void BeginPass(int pass=0)
GLclampf m_AlphaRef
const CShaderProgramPtr & GetShader(int pass=0) const
void SetSortByDistance(bool enable)
GLboolean m_ColorMaskA
GLenum m_BlendSrc
void ColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a)
GLboolean m_DepthMask
CShaderProgramPtr m_Shader
Implements a render pass consisting of various GL state changes and a shader, used by CShaderTechniqu...
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
bool GetSortByDistance() const
Whether this technique uses alpha blending that requires objects to be drawn from furthest to nearest...
void BlendFunc(GLenum src, GLenum dst)
GLboolean m_ColorMaskG
void EndPass(int pass=0)
GLenum m_DepthFunc
GLenum m_AlphaFunc
GLenum m_BlendDst
int GetNumPasses() const
void DepthFunc(GLenum func)
void DepthMask(GLboolean mask)
GLboolean m_ColorMaskB
void AddPass(const CShaderPass &pass)
GLboolean m_ColorMaskR
shared_ptr< CShaderProgram > CShaderProgramPtr
std::vector< CShaderPass > m_Passes
void Unbind()
Reset the GL state to the default.