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 "Material.h"
21 
22 static CColor BrokenColor(0.3f, 0.3f, 0.3f, 1.0f);
23 
CMaterial()24 CMaterial::CMaterial() :
25 	m_AlphaBlending(false)
26 {
27 }
28 
SetShaderEffect(const CStr & effect)29 void CMaterial::SetShaderEffect(const CStr& effect)
30 {
31 	m_ShaderEffect = CStrIntern(effect);
32 }
33 
AddShaderDefine(CStrIntern key,CStrIntern value)34 void CMaterial::AddShaderDefine(CStrIntern key, CStrIntern value)
35 {
36 	m_ShaderDefines.Add(key, value);
37 	m_CombinedShaderDefines.clear();
38 }
39 
AddConditionalDefine(const char * defname,const char * defvalue,int type,std::vector<float> & args)40 void CMaterial::AddConditionalDefine(const char* defname, const char* defvalue, int type, std::vector<float> &args)
41 {
42 	m_ConditionalDefines.Add(defname, defvalue, type, args);
43 	m_CombinedShaderDefines.clear();
44 }
45 
AddStaticUniform(const char * key,const CVector4D & value)46 void CMaterial::AddStaticUniform(const char* key, const CVector4D& value)
47 {
48 	m_StaticUniforms.Add(key, value);
49 }
50 
AddSampler(const TextureSampler & texture)51 void CMaterial::AddSampler(const TextureSampler& texture)
52 {
53 	m_Samplers.push_back(texture);
54 	if (texture.Name == str_baseTex)
55 		m_DiffuseTexture = texture.Sampler;
56 }
57 
AddRenderQuery(const char * key)58 void CMaterial::AddRenderQuery(const char* key)
59 {
60 	m_RenderQueries.Add(key);
61 }
62 
AddRequiredSampler(const CStr & samplerName)63 void CMaterial::AddRequiredSampler(const CStr& samplerName)
64 {
65 	CStrIntern string(samplerName);
66 	m_RequiredSamplers.push_back(string);
67 }
68 
69 
70 // Set up m_CombinedShaderDefines so that index i contains m_ShaderDefines, plus
71 // the extra defines from m_ConditionalDefines[j] for all j where bit j is set in i.
72 // This lets GetShaderDefines() cheaply return the defines for any combination of conditions.
73 //
74 // (This might scale badly if we had a large number of conditional defines per material,
75 // but currently we don't expect to have many.)
RecomputeCombinedShaderDefines()76 void CMaterial::RecomputeCombinedShaderDefines()
77 {
78 	m_CombinedShaderDefines.clear();
79 	int size = m_ConditionalDefines.GetSize();
80 
81 	// Loop over all 2^n combinations of flags
82 	for (int i = 0; i < (1 << size); i++)
83 	{
84 		CShaderDefines defs = m_ShaderDefines;
85 		for (int j = 0; j < size; j++)
86 		{
87 			if (i & (1 << j))
88 			{
89 				const CShaderConditionalDefines::CondDefine& def = m_ConditionalDefines.GetItem(j);
90 				defs.Add(def.m_DefName, def.m_DefValue);
91 			}
92 		}
93 		m_CombinedShaderDefines.push_back(defs);
94 	}
95 }
96