Merge branch 'blendmodes'

Introduces the sf::BlendMode class that supports separate blend functions and equations.
Replaces the old sf::BlendMode enum.

Further changes:
* Fixed merge conflicts resulting from GLExtensions in mobile branch
* GLExtensions: Added GLEXT_blend_equation_separate
* Added file BlendMode.cpp
* Replaced occurrences of sf::BlendMode value parameters with const-references
* Adapted rendering code in sf::RenderTarget
This commit is contained in:
Jan Haller 2014-04-26 19:36:46 +02:00
commit cdf32a7881
8 changed files with 348 additions and 54 deletions

View file

@ -0,0 +1,103 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/BlendMode.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
// Commonly used blending modes
////////////////////////////////////////////////////////////
const BlendMode BlendAlpha(BlendMode::SrcAlpha, BlendMode::OneMinusSrcAlpha, BlendMode::Add,
BlendMode::One, BlendMode::OneMinusSrcAlpha, BlendMode::Add);
const BlendMode BlendAdd(BlendMode::SrcAlpha, BlendMode::One, BlendMode::Add,
BlendMode::One, BlendMode::One, BlendMode::Add);
const BlendMode BlendMultiply(BlendMode::DstColor, BlendMode::Zero);
const BlendMode BlendNone(BlendMode::One, BlendMode::Zero);
////////////////////////////////////////////////////////////
BlendMode::BlendMode() :
colorSrcFactor(BlendMode::SrcAlpha),
colorDstFactor(BlendMode::OneMinusSrcAlpha),
colorEquation (BlendMode::Add),
alphaSrcFactor(BlendMode::One),
alphaDstFactor(BlendMode::OneMinusSrcAlpha),
alphaEquation (BlendMode::Add)
{
}
////////////////////////////////////////////////////////////
BlendMode::BlendMode(Factor sourceFactor, Factor destinationFactor, Equation blendEquation) :
colorSrcFactor(sourceFactor),
colorDstFactor(destinationFactor),
colorEquation (blendEquation),
alphaSrcFactor(sourceFactor),
alphaDstFactor(destinationFactor),
alphaEquation (blendEquation)
{
}
////////////////////////////////////////////////////////////
BlendMode::BlendMode(Factor colorSourceFactor, Factor colorDestinationFactor,
Equation colorBlendEquation, Factor alphaSourceFactor,
Factor alphaDestinationFactor, Equation alphaBlendEquation) :
colorSrcFactor(colorSourceFactor),
colorDstFactor(colorDestinationFactor),
colorEquation (colorBlendEquation),
alphaSrcFactor(alphaSourceFactor),
alphaDstFactor(alphaDestinationFactor),
alphaEquation (alphaBlendEquation)
{
}
////////////////////////////////////////////////////////////
bool operator ==(const BlendMode& left, const BlendMode& right)
{
return (left.colorSrcFactor == right.colorSrcFactor) &&
(left.colorDstFactor == right.colorDstFactor) &&
(left.colorEquation == right.colorEquation) &&
(left.alphaSrcFactor == right.alphaSrcFactor) &&
(left.alphaDstFactor == right.alphaDstFactor) &&
(left.alphaEquation == right.alphaEquation);
}
////////////////////////////////////////////////////////////
bool operator !=(const BlendMode& left, const BlendMode& right)
{
return !(left == right);
}
} // namespace sf

View file

@ -4,6 +4,7 @@ set(SRCROOT ${PROJECT_SOURCE_DIR}/src/SFML/Graphics)
# all source files
set(SRC
${SRCROOT}/BlendMode.cpp
${INCROOT}/BlendMode.hpp
${SRCROOT}/Color.cpp
${INCROOT}/Color.hpp

View file

@ -35,7 +35,9 @@
#include <SFML/OpenGL.hpp>
#define GLEXT_blend_func_separate GL_OES_blend_func_separate
#define GLEXT_blend_equation_separate GL_OES_blend_equation_separate
#define GLEXT_glBlendFuncSeparate glBlendFuncSeparateOES
#define GLEXT_glBlendEquationSeparate glBlendEquationSeparateOES
#define GLEXT_framebuffer_object GL_OES_framebuffer_object
#define GLEXT_glGenFramebuffers glGenFramebuffersOES
#define GLEXT_glGenRenderbuffers glGenRenderbuffersOES
@ -63,7 +65,9 @@
#include <SFML/OpenGL.hpp>
#define GLEXT_blend_func_separate GLEW_EXT_blend_func_separate
#define GLEXT_blend_equation_separate GLEW_EXT_blend_equation_separate
#define GLEXT_glBlendFuncSeparate glBlendFuncSeparateEXT
#define GLEXT_glBlendEquationSeparate glBlendEquationSeparateEXT
#define GLEXT_framebuffer_object GLEW_EXT_framebuffer_object
#define GLEXT_glGenFramebuffers glGenFramebuffersEXT
#define GLEXT_glGenRenderbuffers glGenRenderbuffersEXT

View file

@ -56,7 +56,7 @@ shader (NULL)
////////////////////////////////////////////////////////////
RenderStates::RenderStates(BlendMode theBlendMode) :
RenderStates::RenderStates(const BlendMode& theBlendMode) :
blendMode(theBlendMode),
transform(),
texture (NULL),
@ -86,7 +86,7 @@ shader (theShader)
////////////////////////////////////////////////////////////
RenderStates::RenderStates(BlendMode theBlendMode, const Transform& theTransform,
RenderStates::RenderStates(const BlendMode& theBlendMode, const Transform& theTransform,
const Texture* theTexture, const Shader* theShader) :
blendMode(theBlendMode),
transform(theTransform),

View file

@ -35,6 +35,41 @@
#include <iostream>
namespace
{
// Convert an sf::BlendMode::Factor constant to the corresponding OpenGL constant.
sf::Uint32 factorToGlConstant(sf::BlendMode::Factor blendFactor)
{
switch (blendFactor)
{
default:
case sf::BlendMode::Zero: return GL_ZERO;
case sf::BlendMode::One: return GL_ONE;
case sf::BlendMode::SrcColor: return GL_SRC_COLOR;
case sf::BlendMode::OneMinusSrcColor: return GL_ONE_MINUS_SRC_COLOR;
case sf::BlendMode::DstColor: return GL_DST_COLOR;
case sf::BlendMode::OneMinusDstColor: return GL_ONE_MINUS_DST_COLOR;
case sf::BlendMode::SrcAlpha: return GL_SRC_ALPHA;
case sf::BlendMode::OneMinusSrcAlpha: return GL_ONE_MINUS_SRC_ALPHA;
case sf::BlendMode::DstAlpha: return GL_DST_ALPHA;
case sf::BlendMode::OneMinusDstAlpha: return GL_ONE_MINUS_DST_ALPHA;
}
}
// Convert an sf::BlendMode::BlendEquation constant to the corresponding OpenGL constant.
sf::Uint32 equationToGlConstant(sf::BlendMode::Equation blendEquation)
{
switch (blendEquation)
{
default:
case sf::BlendMode::Add: return GL_FUNC_ADD;
case sf::BlendMode::Subtract: return GL_FUNC_SUBTRACT;
}
}
}
namespace sf
{
////////////////////////////////////////////////////////////
@ -369,49 +404,31 @@ void RenderTarget::applyCurrentView()
////////////////////////////////////////////////////////////
void RenderTarget::applyBlendMode(BlendMode mode)
void RenderTarget::applyBlendMode(const BlendMode& mode)
{
switch (mode)
// Apply the blend mode, falling back to the non-separate versions if necessary
if (GLEXT_blend_func_separate)
{
// glBlendFuncSeparate is used when available to avoid an incorrect alpha value when the target
// is a RenderTexture -- in this case the alpha value must be written directly to the target buffer
glCheck(GLEXT_glBlendFuncSeparate(
factorToGlConstant(mode.colorSrcFactor), factorToGlConstant(mode.colorDstFactor),
factorToGlConstant(mode.alphaSrcFactor), factorToGlConstant(mode.alphaDstFactor)));
}
else
{
glCheck(glBlendFunc(
factorToGlConstant(mode.colorSrcFactor),
factorToGlConstant(mode.colorDstFactor)));
}
// Alpha blending
default :
case BlendAlpha :
if (GLEXT_blend_func_separate)
{
glCheck(GLEXT_glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
}
else
{
glCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
}
break;
// Additive blending
case BlendAdd :
if (GLEXT_blend_func_separate)
{
glCheck(GLEXT_glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ONE, GL_ONE));
}
else
{
glCheck(glBlendFunc(GL_SRC_ALPHA, GL_ONE));
}
break;
// Multiplicative blending
case BlendMultiply :
glCheck(glBlendFunc(GL_DST_COLOR, GL_ZERO));
break;
// No blending
case BlendNone :
glCheck(glBlendFunc(GL_ONE, GL_ZERO));
break;
if (GLEXT_blend_equation_separate)
{
glCheck(GLEXT_glBlendEquationSeparate(
equationToGlConstant(mode.colorEquation),
equationToGlConstant(mode.alphaEquation)));
}
else
{
glCheck(glBlendEquation(equationToGlConstant(mode.colorEquation)));
}
m_cache.lastBlendMode = mode;
@ -462,8 +479,9 @@ void RenderTarget::applyShader(const Shader* shader)
// to render them.
//
// * Blending mode
// It's a simple integral value, so we can easily check
// whether the value to apply is the same as before or not.
// Since it overloads the == operator, we can easily check
// whether any of the 6 blending components changed and,
// thus, whether we need to update the blend mode.
//
// * Texture
// Storing the pointer or OpenGL ID of the last used texture