]> git.tdb.fi Git - libs/gl.git/blob - source/core/blend.cpp
Move all OpenGL-specific code to a separate directory
[libs/gl.git] / source / core / blend.cpp
1 #include <msp/strings/format.h>
2 #include <msp/strings/utils.h>
3 #include "blend.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Blend::Blend():
11         enabled(false),
12         equation(ADD),
13         src_factor(ONE),
14         dst_factor(ZERO),
15         constant(0.0f, 0.0f, 0.0f, 0.0f),
16         write_mask(WRITE_ALL)
17 { }
18
19 Blend::Blend(BlendFactor sf, BlendFactor df):
20         enabled(true),
21         equation(ADD),
22         src_factor(sf),
23         dst_factor(df),
24         constant(0.0f, 0.0f, 0.0f, 0.0f),
25         write_mask(WRITE_ALL)
26 { }
27
28 Blend::Blend(BlendEquation e, BlendFactor sf, BlendFactor df):
29         enabled(true),
30         equation(e),
31         src_factor(sf),
32         dst_factor(df),
33         constant(0.0f, 0.0f, 0.0f, 0.0f),
34         write_mask(WRITE_ALL)
35 { }
36
37
38 Blend::Loader::Loader(Blend &b):
39         ObjectLoader<Blend>(b)
40 {
41         add("equation", &Loader::equation);
42         add("factors", &Loader::factors);
43         add("constant", &Loader::constant);
44 }
45
46 void Blend::Loader::constant(float r, float g, float b, float a)
47 {
48         obj.constant = Color(r, g, b, a);
49 }
50
51 void Blend::Loader::equation(BlendEquation eq)
52 {
53         obj.enabled = true;
54         obj.equation = eq;
55 }
56
57 void Blend::Loader::factors(BlendFactor sf, BlendFactor df)
58 {
59         obj.enabled = true;
60         obj.src_factor = sf;
61         obj.dst_factor = df;
62 }
63
64
65 void operator>>(const LexicalConverter &conv, BlendEquation &eq)
66 {
67         const string &str = conv.get();
68         if(str=="ADD")
69                 eq = ADD;
70         else if(str=="SUBTRACT")
71                 eq = SUBTRACT;
72         else if(str=="REVERSE_SUBTRACT")
73                 eq = REVERSE_SUBTRACT;
74         else if(str=="MIN")
75                 eq = MIN;
76         else if(str=="MAX")
77                 eq = MAX;
78         else
79                 throw lexical_error(format("conversion of '%s' to BlendEquation", str));
80 }
81
82 void operator<<(LexicalConverter &conv, BlendEquation eq)
83 {
84         switch(eq)
85         {
86         case ADD: conv.result("ADD"); break;
87         case SUBTRACT: conv.result("SUBTRACT"); break;
88         case REVERSE_SUBTRACT: conv.result("REVERSE_SUBTRACT"); break;
89         case MIN: conv.result("MIN"); break;
90         case MAX: conv.result("MAX"); break;
91         default: conv.result(format("BlendEquation(%#x)", static_cast<int>(eq)));
92         }
93 }
94
95 void operator>>(const LexicalConverter &conv, BlendFactor &factor)
96 {
97         const string &str = conv.get();
98         if(str=="ZERO")
99                 factor = ZERO;
100         else if(str=="ONE")
101                 factor = ONE;
102         else if(str=="SRC_COLOR")
103                 factor = SRC_COLOR;
104         else if(str=="ONE_MINUS_SRC_COLOR")
105                 factor = ONE_MINUS_SRC_COLOR;
106         else if(str=="SRC_ALPHA")
107                 factor = SRC_ALPHA;
108         else if(str=="ONE_MINUS_SRC_ALPHA")
109                 factor = ONE_MINUS_SRC_ALPHA;
110         else if(str=="DST_COLOR")
111                 factor = DST_COLOR;
112         else if(str=="ONE_MINUS_DST_COLOR")
113                 factor = ONE_MINUS_DST_COLOR;
114         else if(str=="DST_ALPHA")
115                 factor = DST_ALPHA;
116         else if(str=="ONE_MINUS_DST_ALPHA")
117                 factor = ONE_MINUS_DST_ALPHA;
118         else if(str=="CONSTANT_COLOR")
119                 factor = CONSTANT_COLOR;
120         else if(str=="ONE_MINUS_CONSTANT_COLOR")
121                 factor = ONE_MINUS_CONSTANT_COLOR;
122         else if(str=="CONSTANT_ALPHA")
123                 factor = CONSTANT_ALPHA;
124         else if(str=="ONE_MINUS_CONSTANT_ALPHA")
125                 factor = ONE_MINUS_CONSTANT_ALPHA;
126         else
127                 throw lexical_error(format("conversion of '%s' to BlendFactor", str));
128 }
129
130 void operator<<(LexicalConverter &conv, BlendFactor factor)
131 {
132         switch(factor)
133         {
134         case ZERO: conv.result("ZERO"); break;
135         case ONE: conv.result("ONE"); break;
136         case SRC_COLOR: conv.result("SRC_COLOR"); break;
137         case ONE_MINUS_SRC_COLOR: conv.result("ONE_MINUS_SRC_COLOR"); break;
138         case SRC_ALPHA: conv.result("SRC_ALPHA"); break;
139         case ONE_MINUS_SRC_ALPHA: conv.result("ONE_MINUS_SRC_ALPHA"); break;
140         case DST_COLOR: conv.result("DST_COLOR"); break;
141         case ONE_MINUS_DST_COLOR: conv.result("ONE_MINUS_DST_COLOR"); break;
142         case DST_ALPHA: conv.result("DST_ALPHA"); break;
143         case ONE_MINUS_DST_ALPHA: conv.result("ONE_MINUS_DST_ALPHA"); break;
144         case CONSTANT_COLOR: conv.result("CONSTANT_COLOR"); break;
145         case ONE_MINUS_CONSTANT_COLOR: conv.result("ONE_MINUS_CONSTANT_COLOR"); break;
146         case CONSTANT_ALPHA: conv.result("CONSTANT_ALPHA"); break;
147         case ONE_MINUS_CONSTANT_ALPHA: conv.result("ONE_MINUS_CONSTANT_ALPHA"); break;
148         default: conv.result(format("BlendFactor(%#x)", static_cast<int>(factor)));
149         }
150 }
151
152 void operator>>(const LexicalConverter &conv, ColorWriteMask &mask)
153 {
154         ColorWriteMask result = WRITE_NONE;
155         for(const string &p: split(conv.get(), '_'))
156         {
157                 if(p=="ALL")
158                         result = result|WRITE_ALL;
159                 else if(p=="RED")
160                         result = result|WRITE_RED;
161                 else if(p=="GREEN")
162                         result = result|WRITE_GREEN;
163                 else if(p=="BLUE")
164                         result = result|WRITE_BLUE;
165                 else if(p=="ALPHA")
166                         result = result|WRITE_ALPHA;
167                 else
168                         throw lexical_error(format("conversion of '%s' to ColorWriteMask", conv.get()));
169         }
170         mask = result;
171 }
172
173 void operator<<(LexicalConverter &conv, ColorWriteMask mask)
174 {
175         if(mask==WRITE_ALL)
176                 conv.result("ALL");
177         else if(mask&~WRITE_ALL)
178                 conv.result(format("ColorWriteMask(%#x)", static_cast<int>(mask)));
179         else
180         {
181                 string result;
182                 if(mask&WRITE_RED)
183                         result = "RED";
184                 if(mask&WRITE_GREEN)
185                         append(result, "_", "GREEN");
186                 if(mask&WRITE_BLUE)
187                         append(result, "_", "BLUE");
188                 if(mask&WRITE_ALPHA)
189                         append(result, "_", "ALPHA");
190                 conv.result(result);
191         }
192 }
193
194 } // namespace GL
195 } // namespace Msp