1 #include <msp/gl/extensions/ext_blend_minmax.h>
2 #include <msp/gl/extensions/ext_blend_subtract.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
17 constant(0.0f, 0.0f, 0.0f, 0.0f),
21 Blend::Blend(BlendFactor sf, BlendFactor df):
26 constant(0.0f, 0.0f, 0.0f, 0.0f),
30 Blend::Blend(BlendEquation e, BlendFactor sf, BlendFactor df):
35 constant(0.0f, 0.0f, 0.0f, 0.0f),
40 Blend::Loader::Loader(Blend &b):
41 ObjectLoader<Blend>(b)
43 add("equation", &Loader::equation);
44 add("factors", &Loader::factors);
45 add("constant", &Loader::constant);
48 void Blend::Loader::constant(float r, float g, float b, float a)
50 obj.constant = Color(r, g, b, a);
53 void Blend::Loader::equation(BlendEquation eq)
59 void Blend::Loader::factors(BlendFactor sf, BlendFactor df)
67 unsigned get_gl_blend_equation(BlendEquation eq)
71 case ADD: return GL_FUNC_ADD;
72 case SUBTRACT: return GL_FUNC_SUBTRACT;
73 case REVERSE_SUBTRACT: return GL_FUNC_REVERSE_SUBTRACT;
74 case MIN: return GL_MIN;
75 case MAX: return GL_MAX;
76 default: throw invalid_argument("get_gl_blend_equation");
80 unsigned get_gl_blend_factor(BlendFactor factor)
84 case ZERO: return GL_ZERO;
85 case ONE: return GL_ONE;
86 case SRC_COLOR: return GL_SRC_COLOR;
87 case ONE_MINUS_SRC_COLOR: return GL_ONE_MINUS_SRC_COLOR;
88 case SRC_ALPHA: return GL_SRC_ALPHA;
89 case ONE_MINUS_SRC_ALPHA: return GL_ONE_MINUS_SRC_ALPHA;
90 case DST_COLOR: return GL_DST_COLOR;
91 case ONE_MINUS_DST_COLOR: return GL_ONE_MINUS_DST_COLOR;
92 case DST_ALPHA: return GL_DST_ALPHA;
93 case ONE_MINUS_DST_ALPHA: return GL_ONE_MINUS_DST_ALPHA;
94 case CONSTANT_COLOR: return GL_CONSTANT_COLOR;
95 case ONE_MINUS_CONSTANT_COLOR: return GL_ONE_MINUS_CONSTANT_COLOR;
96 case CONSTANT_ALPHA: return GL_CONSTANT_ALPHA;
97 case ONE_MINUS_CONSTANT_ALPHA: return GL_ONE_MINUS_CONSTANT_ALPHA;
98 default: throw invalid_argument("get_gl_blend_factor");
102 void operator>>(const LexicalConverter &conv, BlendEquation &eq)
104 const string &str = conv.get();
107 else if(str=="SUBTRACT")
109 else if(str=="REVERSE_SUBTRACT")
110 eq = REVERSE_SUBTRACT;
116 throw lexical_error(format("conversion of '%s' to BlendEquation", str));
119 void operator<<(LexicalConverter &conv, BlendEquation eq)
123 case ADD: conv.result("ADD"); break;
124 case SUBTRACT: conv.result("SUBTRACT"); break;
125 case REVERSE_SUBTRACT: conv.result("REVERSE_SUBTRACT"); break;
126 case MIN: conv.result("MIN"); break;
127 case MAX: conv.result("MAX"); break;
128 default: conv.result(format("BlendEquation(%#x)", static_cast<int>(eq)));
132 void operator>>(const LexicalConverter &conv, BlendFactor &factor)
134 const string &str = conv.get();
139 else if(str=="SRC_COLOR")
141 else if(str=="ONE_MINUS_SRC_COLOR")
142 factor = ONE_MINUS_SRC_COLOR;
143 else if(str=="SRC_ALPHA")
145 else if(str=="ONE_MINUS_SRC_ALPHA")
146 factor = ONE_MINUS_SRC_ALPHA;
147 else if(str=="DST_COLOR")
149 else if(str=="ONE_MINUS_DST_COLOR")
150 factor = ONE_MINUS_DST_COLOR;
151 else if(str=="DST_ALPHA")
153 else if(str=="ONE_MINUS_DST_ALPHA")
154 factor = ONE_MINUS_DST_ALPHA;
155 else if(str=="CONSTANT_COLOR")
156 factor = CONSTANT_COLOR;
157 else if(str=="ONE_MINUS_CONSTANT_COLOR")
158 factor = ONE_MINUS_CONSTANT_COLOR;
159 else if(str=="CONSTANT_ALPHA")
160 factor = CONSTANT_ALPHA;
161 else if(str=="ONE_MINUS_CONSTANT_ALPHA")
162 factor = ONE_MINUS_CONSTANT_ALPHA;
164 throw lexical_error(format("conversion of '%s' to BlendFactor", str));
167 void operator<<(LexicalConverter &conv, BlendFactor factor)
171 case ZERO: conv.result("ZERO"); break;
172 case ONE: conv.result("ONE"); break;
173 case SRC_COLOR: conv.result("SRC_COLOR"); break;
174 case ONE_MINUS_SRC_COLOR: conv.result("ONE_MINUS_SRC_COLOR"); break;
175 case SRC_ALPHA: conv.result("SRC_ALPHA"); break;
176 case ONE_MINUS_SRC_ALPHA: conv.result("ONE_MINUS_SRC_ALPHA"); break;
177 case DST_COLOR: conv.result("DST_COLOR"); break;
178 case ONE_MINUS_DST_COLOR: conv.result("ONE_MINUS_DST_COLOR"); break;
179 case DST_ALPHA: conv.result("DST_ALPHA"); break;
180 case ONE_MINUS_DST_ALPHA: conv.result("ONE_MINUS_DST_ALPHA"); break;
181 case CONSTANT_COLOR: conv.result("CONSTANT_COLOR"); break;
182 case ONE_MINUS_CONSTANT_COLOR: conv.result("ONE_MINUS_CONSTANT_COLOR"); break;
183 case CONSTANT_ALPHA: conv.result("CONSTANT_ALPHA"); break;
184 case ONE_MINUS_CONSTANT_ALPHA: conv.result("ONE_MINUS_CONSTANT_ALPHA"); break;
185 default: conv.result(format("BlendFactor(%#x)", static_cast<int>(factor)));
189 void operator>>(const LexicalConverter &conv, ColorWriteMask &mask)
191 ColorWriteMask result = WRITE_NONE;
192 for(const string &p: split(conv.get(), '_'))
195 result = result|WRITE_ALL;
197 result = result|WRITE_RED;
199 result = result|WRITE_GREEN;
201 result = result|WRITE_BLUE;
203 result = result|WRITE_ALPHA;
205 throw lexical_error(format("conversion of '%s' to ColorWriteMask", conv.get()));
210 void operator<<(LexicalConverter &conv, ColorWriteMask mask)
214 else if(mask&~WRITE_ALL)
215 conv.result(format("ColorWriteMask(%#x)", static_cast<int>(mask)));
222 append(result, "_", "GREEN");
224 append(result, "_", "BLUE");
226 append(result, "_", "ALPHA");