]> git.tdb.fi Git - libs/gl.git/blob - source/blend.cpp
Require the first keyframe of an animation to be at zero offset
[libs/gl.git] / source / blend.cpp
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 "blend.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 Blend::Blend():
12         eq(ADD),
13         src_factor(ONE),
14         dst_factor(ZERO)
15 { }
16
17 Blend::Blend(BlendFactor sf, BlendFactor df):
18         eq(ADD),
19         src_factor(sf),
20         dst_factor(df)
21 { }
22
23 Blend::Blend(BlendEquation e, BlendFactor sf, BlendFactor df):
24         eq(e),
25         src_factor(sf),
26         dst_factor(df)
27 {
28         if(eq==MIN || eq==MAX)
29                 static Require _req(EXT_blend_minmax);
30         else if(eq==SUBTRACT || eq==REVERSE_SUBTRACT)
31                 static Require _req(EXT_blend_subtract);
32 }
33
34 void Blend::bind() const
35 {
36         if(set_current(this))
37         {
38                 glEnable(GL_BLEND);
39                 if(EXT_blend_minmax)
40                         glBlendEquation(eq);
41                 glBlendFunc(src_factor, dst_factor);
42         }
43 }
44
45 void Blend::unbind()
46 {
47         if(set_current(0))
48                 glDisable(GL_BLEND);
49 }
50
51 const Blend &Blend::alpha()
52 {
53         static Blend blend(SRC_ALPHA, ONE_MINUS_SRC_ALPHA);
54         return blend;
55 }
56
57 const Blend &Blend::additive()
58 {
59         static Blend blend(ONE, ONE);
60         return blend;
61 }
62
63 const Blend &Blend::additive_alpha()
64 {
65         static Blend blend(SRC_ALPHA, ONE);
66         return blend;
67 }
68
69 void operator>>(const LexicalConverter &conv, BlendFactor &factor)
70 {
71         const string &str = conv.get();
72         if(str=="ZERO")
73                 factor = ZERO;
74         else if(str=="ONE")
75                 factor = ONE;
76         else if(str=="SRC_COLOR")
77                 factor = SRC_COLOR;
78         else if(str=="ONE_MINUS_SRC_COLOR")
79                 factor = ONE_MINUS_SRC_COLOR;
80         else if(str=="SRC_ALPHA")
81                 factor = SRC_ALPHA;
82         else if(str=="ONE_MINUS_SRC_ALPHA")
83                 factor = ONE_MINUS_SRC_ALPHA;
84         else if(str=="DST_COLOR")
85                 factor = DST_COLOR;
86         else if(str=="ONE_MINUS_DST_COLOR")
87                 factor = ONE_MINUS_DST_COLOR;
88         else if(str=="DST_ALPHA")
89                 factor = DST_ALPHA;
90         else if(str=="ONE_MINUS_DST_ALPHA")
91                 factor = ONE_MINUS_DST_ALPHA;
92         else if(str=="CONSTANT_COLOR")
93                 factor = CONSTANT_COLOR;
94         else if(str=="ONE_MINUS_CONSTANT_COLOR")
95                 factor = ONE_MINUS_CONSTANT_COLOR;
96         else if(str=="CONSTANT_ALPHA")
97                 factor = CONSTANT_ALPHA;
98         else if(str=="ONE_MINUS_CONSTANT_ALPHA")
99                 factor = ONE_MINUS_CONSTANT_ALPHA;
100         else
101                 throw lexical_error(format("conversion of '%s' to BlendFactor", str));
102 }
103
104 void operator<<(LexicalConverter &conv, BlendFactor factor)
105 {
106         switch(factor)
107         {
108         case ZERO: conv.result("ZERO"); break;
109         case ONE: conv.result("ONE"); break;
110         case SRC_COLOR: conv.result("SRC_COLOR"); break;
111         case ONE_MINUS_SRC_COLOR: conv.result("ONE_MINUS_SRC_COLOR"); break;
112         case SRC_ALPHA: conv.result("SRC_ALPHA"); break;
113         case ONE_MINUS_SRC_ALPHA: conv.result("ONE_MINUS_SRC_ALPHA"); break;
114         case DST_COLOR: conv.result("DST_COLOR"); break;
115         case ONE_MINUS_DST_COLOR: conv.result("ONE_MINUS_DST_COLOR"); break;
116         case DST_ALPHA: conv.result("DST_ALPHA"); break;
117         case ONE_MINUS_DST_ALPHA: conv.result("ONE_MINUS_DST_ALPHA"); break;
118         case CONSTANT_COLOR: conv.result("CONSTANT_COLOR"); break;
119         case ONE_MINUS_CONSTANT_COLOR: conv.result("ONE_MINUS_CONSTANT_COLOR"); break;
120         case CONSTANT_ALPHA: conv.result("CONSTANT_ALPHA"); break;
121         case ONE_MINUS_CONSTANT_ALPHA: conv.result("ONE_MINUS_CONSTANT_ALPHA"); break;
122         default: conv.result(format("BlendFactor(%#x)", static_cast<int>(factor)));
123         }
124 }
125
126 } // namespace GL
127 } // namespace Msp