/
Doom3ShaderLayer.cpp
277 lines (238 loc) · 6.02 KB
/
Doom3ShaderLayer.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "Doom3ShaderLayer.h"
#include "Doom3ShaderSystem.h"
namespace shaders
{
// Map string blend functions to their GLenum equivalents
GLenum glBlendFromString(const std::string& value)
{
if (value == "gl_zero") {
return GL_ZERO;
}
if (value == "gl_one") {
return GL_ONE;
}
if (value == "gl_src_color") {
return GL_SRC_COLOR;
}
if (value == "gl_one_minus_src_color") {
return GL_ONE_MINUS_SRC_COLOR;
}
if (value == "gl_src_alpha") {
return GL_SRC_ALPHA;
}
if (value == "gl_one_minus_src_alpha") {
return GL_ONE_MINUS_SRC_ALPHA;
}
if (value == "gl_dst_color") {
return GL_DST_COLOR;
}
if (value == "gl_one_minus_dst_color") {
return GL_ONE_MINUS_DST_COLOR;
}
if (value == "gl_dst_alpha") {
return GL_DST_ALPHA;
}
if (value == "gl_one_minus_dst_alpha") {
return GL_ONE_MINUS_DST_ALPHA;
}
if (value == "gl_src_alpha_saturate") {
return GL_SRC_ALPHA_SATURATE;
}
return GL_ZERO;
}
// Convert a string pair describing a blend function into a BlendFunc object
BlendFunc blendFuncFromStrings(const StringPair& blendFunc)
{
// Handle predefined blend modes first: add, modulate, filter
if (blendFunc.first == "add")
{
return BlendFunc(GL_ONE, GL_ONE);
}
else if (blendFunc.first == "modulate" || blendFunc.first == "filter")
{
return BlendFunc(GL_DST_COLOR, GL_ZERO);
}
else if (blendFunc.first == "blend")
{
return BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
else if (blendFunc.first == "none")
{
return BlendFunc(GL_ZERO, GL_ONE);
}
else
{
// Not predefined, just use the specified blend function directly
return BlendFunc(
glBlendFromString(blendFunc.first),
glBlendFromString(blendFunc.second)
);
}
}
// ShaderLayer implementation
const IShaderExpressionPtr Doom3ShaderLayer::NULL_EXPRESSION;
Doom3ShaderLayer::Doom3ShaderLayer(ShaderTemplate& material, ShaderLayer::Type type, const NamedBindablePtr& btex)
: _material(material),
_registers(NUM_RESERVED_REGISTERS),
_condition(REG_ONE),
_bindableTex(btex),
_type(type),
_mapType(MapType::Map),
_blendFuncStrings("gl_one", "gl_zero"), // needs to be lowercase
_vertexColourMode(VERTEX_COLOUR_NONE),
_cubeMapMode(CUBE_MAP_NONE),
_stageFlags(0),
_clampType(CLAMP_REPEAT),
_alphaTest(REG_ZERO),
_texGenType(TEXGEN_NORMAL),
_privatePolygonOffset(0)
{
_registers[REG_ZERO] = 0;
_registers[REG_ONE] = 1;
// Init the colour to 1,1,1,1
_colIdx[0] = REG_ONE;
_colIdx[1] = REG_ONE;
_colIdx[2] = REG_ONE;
_colIdx[3] = REG_ONE;
// Scale is set to 1,1 by default
_scale[0] = REG_ONE;
_scale[1] = REG_ONE;
// Translation is set to 0,0 by default
_translation[0] = REG_ZERO;
_translation[1] = REG_ZERO;
// Rotation is set to 0 by default
_rotation = REG_ZERO;
// No shearing so far
_shear[0] = REG_ZERO;
_shear[1] = REG_ZERO;
_texGenParams[0] = _texGenParams[1] = _texGenParams[2] = 0;
}
TexturePtr Doom3ShaderLayer::getTexture() const
{
// Bind texture to GL if needed
if (!_texture && _bindableTex)
{
_texture = GetTextureManager().getBinding(_bindableTex);
}
return _texture;
}
BlendFunc Doom3ShaderLayer::getBlendFunc() const
{
return blendFuncFromStrings(_blendFuncStrings);
}
Colour4 Doom3ShaderLayer::getColour() const
{
// Resolve the register values
Colour4 colour(getRegisterValue(_colIdx[0]), getRegisterValue(_colIdx[1]),
getRegisterValue(_colIdx[2]), getRegisterValue(_colIdx[3]));
if (!colour.isValid())
{
return Colour4::WHITE();
}
return colour;
}
void Doom3ShaderLayer::setColourExpression(ColourComponentSelector comp, const IShaderExpressionPtr& expr)
{
// Store the expression and link it to our registers
_expressions.push_back(expr);
std::size_t index = expr->linkToRegister(_registers);
// Now assign the index to our colour components
switch (comp)
{
case COMP_RED:
_colIdx[0] = index;
break;
case COMP_GREEN:
_colIdx[1] = index;
break;
case COMP_BLUE:
_colIdx[2] = index;
break;
case COMP_ALPHA:
_colIdx[3] = index;
break;
case COMP_RGB:
_colIdx[0] = index;
_colIdx[1] = index;
_colIdx[2] = index;
break;
case COMP_RGBA:
_colIdx[0] = index;
_colIdx[1] = index;
_colIdx[2] = index;
_colIdx[3] = index;
break;
};
}
void Doom3ShaderLayer::setColour(const Vector4& col)
{
// Assign all 3 components of the colour, allocating new registers on the fly where needed
for (std::size_t i = 0; i < 4; ++i)
{
// Does this colour component refer to a reserved constant index?
if (_colIdx[i] < NUM_RESERVED_REGISTERS)
{
// Yes, break this up by allocating a new register for this value
_colIdx[i] = getNewRegister(static_cast<float>(col[i]));
}
else
{
// Already using a custom register
setRegister(_colIdx[i], static_cast<float>(col[i]));
}
}
}
ShaderLayer::VertexColourMode Doom3ShaderLayer::getVertexColourMode() const
{
return _vertexColourMode;
}
ShaderLayer::CubeMapMode Doom3ShaderLayer::getCubeMapMode() const
{
return _cubeMapMode;
}
ShaderLayer::MapType Doom3ShaderLayer::getMapType() const
{
return _mapType;
}
void Doom3ShaderLayer::setMapType(MapType type)
{
_mapType = type;
}
const Vector2& Doom3ShaderLayer::getRenderMapSize()
{
return _renderMapSize;
}
void Doom3ShaderLayer::setRenderMapSize(const Vector2& size)
{
_renderMapSize = size;
}
bool Doom3ShaderLayer::hasAlphaTest() const
{
return _alphaTest != REG_ZERO;
}
float Doom3ShaderLayer::getAlphaTest() const
{
return _registers[_alphaTest];
}
TexturePtr Doom3ShaderLayer::getFragmentMap(int index)
{
if (index < 0 || index >= static_cast<int>(_fragmentMaps.size()))
{
return TexturePtr();
}
return GetTextureManager().getBinding(_fragmentMaps[index]);
}
std::string Doom3ShaderLayer::getMapImageFilename()
{
auto image = std::dynamic_pointer_cast<ImageExpression>(_bindableTex);
if (image)
{
return image->getIdentifier();
}
return std::string();
}
IMapExpression::Ptr Doom3ShaderLayer::getMapExpression()
{
return std::dynamic_pointer_cast<IMapExpression>(_bindableTex);
}
}