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