]> git.tdb.fi Git - libs/gl.git/blobdiff - source/blend.cpp
Add a utility class for switching renderables
[libs/gl.git] / source / blend.cpp
index 8b82772fded1e7fc6acc755c312964ac867a0b3b..3327885c74c1ef34b7a5c1d111f22935a8639de0 100644 (file)
@@ -1,13 +1,9 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2008, 2010  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <msp/gl/extensions/ext_blend_minmax.h>
+#include <msp/gl/extensions/ext_blend_subtract.h>
+#include <msp/strings/format.h>
 #include "blend.h"
-#include "extension.h"
-#include "version_1_2.h"
+
+using namespace std;
 
 namespace Msp {
 namespace GL {
@@ -29,8 +25,10 @@ Blend::Blend(BlendEquation e, BlendFactor sf, BlendFactor df):
        src_factor(sf),
        dst_factor(df)
 {
-       if(eq!=ADD)
-               static RequireVersion _ver(1, 2);
+       if(eq==MIN || eq==MAX)
+               static Require _req(EXT_blend_minmax);
+       else if(eq==SUBTRACT || eq==REVERSE_SUBTRACT)
+               static Require _req(EXT_blend_subtract);
 }
 
 void Blend::bind() const
@@ -38,34 +36,91 @@ void Blend::bind() const
        if(set_current(this))
        {
                glEnable(GL_BLEND);
-               // XXX Don't try to set equation if version < 1.2
-               glBlendEquation(eq);
+               if(EXT_blend_minmax)
+                       glBlendEquation(eq);
                glBlendFunc(src_factor, dst_factor);
        }
 }
 
+void Blend::unbind()
+{
+       if(set_current(0))
+               glDisable(GL_BLEND);
+}
+
 const Blend &Blend::alpha()
 {
        static Blend blend(SRC_ALPHA, ONE_MINUS_SRC_ALPHA);
        return blend;
 }
 
-void Blend::unbind()
+const Blend &Blend::additive()
 {
-       if(set_current(0))
-               glDisable(GL_BLEND);
+       static Blend blend(ONE, ONE);
+       return blend;
 }
 
+const Blend &Blend::additive_alpha()
+{
+       static Blend blend(SRC_ALPHA, ONE);
+       return blend;
+}
 
-void blend_equation(BlendEquation eq)
+void operator>>(const LexicalConverter &conv, BlendFactor &factor)
 {
-       static RequireVersion _ver(1, 2);
-       glBlendEquation(eq);
+       const string &str = conv.get();
+       if(str=="ZERO")
+               factor = ZERO;
+       else if(str=="ONE")
+               factor = ONE;
+       else if(str=="SRC_COLOR")
+               factor = SRC_COLOR;
+       else if(str=="ONE_MINUS_SRC_COLOR")
+               factor = ONE_MINUS_SRC_COLOR;
+       else if(str=="SRC_ALPHA")
+               factor = SRC_ALPHA;
+       else if(str=="ONE_MINUS_SRC_ALPHA")
+               factor = ONE_MINUS_SRC_ALPHA;
+       else if(str=="DST_COLOR")
+               factor = DST_COLOR;
+       else if(str=="ONE_MINUS_DST_COLOR")
+               factor = ONE_MINUS_DST_COLOR;
+       else if(str=="DST_ALPHA")
+               factor = DST_ALPHA;
+       else if(str=="ONE_MINUS_DST_ALPHA")
+               factor = ONE_MINUS_DST_ALPHA;
+       else if(str=="CONSTANT_COLOR")
+               factor = CONSTANT_COLOR;
+       else if(str=="ONE_MINUS_CONSTANT_COLOR")
+               factor = ONE_MINUS_CONSTANT_COLOR;
+       else if(str=="CONSTANT_ALPHA")
+               factor = CONSTANT_ALPHA;
+       else if(str=="ONE_MINUS_CONSTANT_ALPHA")
+               factor = ONE_MINUS_CONSTANT_ALPHA;
+       else
+               throw lexical_error(format("conversion of '%s' to BlendFactor", str));
 }
 
-void blend_func(BlendFactor src, BlendFactor dst)
+void operator<<(LexicalConverter &conv, BlendFactor factor)
 {
-       glBlendFunc(src, dst);
+       switch(factor)
+       {
+       case ZERO: conv.result("ZERO"); break;
+       case ONE: conv.result("ONE"); break;
+       case SRC_COLOR: conv.result("SRC_COLOR"); break;
+       case ONE_MINUS_SRC_COLOR: conv.result("ONE_MINUS_SRC_COLOR"); break;
+       case SRC_ALPHA: conv.result("SRC_ALPHA"); break;
+       case ONE_MINUS_SRC_ALPHA: conv.result("ONE_MINUS_SRC_ALPHA"); break;
+       case DST_COLOR: conv.result("DST_COLOR"); break;
+       case ONE_MINUS_DST_COLOR: conv.result("ONE_MINUS_DST_COLOR"); break;
+       case DST_ALPHA: conv.result("DST_ALPHA"); break;
+       case ONE_MINUS_DST_ALPHA: conv.result("ONE_MINUS_DST_ALPHA"); break;
+       case CONSTANT_COLOR: conv.result("CONSTANT_COLOR"); break;
+       case ONE_MINUS_CONSTANT_COLOR: conv.result("ONE_MINUS_CONSTANT_COLOR"); break;
+       case CONSTANT_ALPHA: conv.result("CONSTANT_ALPHA"); break;
+       case ONE_MINUS_CONSTANT_ALPHA: conv.result("ONE_MINUS_CONSTANT_ALPHA"); break;
+       default: conv.result(format("BlendFactor(%#x)", static_cast<int>(factor)));
+       }
 }
 
 } // namespace GL