BlendMode.hpp
1// Copyright 2019 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4
5#ifndef SMK_BLENDMODE_HPP
6#define SMK_BLENDMODE_HPP
7
8#include <smk/OpenGL.hpp>
9
10namespace smk {
11
12/// Blending modes for drawing.
13///
14/// When drawing something into a smk::RenderTarget, the smk::BlendMode
15/// represents how the colors are mixed with the colors already in the color
16/// buffer.
17///
18/// ## Commonly used BlendMode
19///
20/// SMK provides 6 predefined common BlendMode:
21/// ~~~cpp
22/// smk::BlendMode::Replace; // dst = src
23/// smk::BlendMode::Add; // dst += src
24/// smk::BlendMode::Subtract; // dst -= src
25/// smk::BlendMode::Multiply; // dst *= src
26/// smk::BlendMode::Alpha; // dst = src * a + dest * (1 - a)
27/// smk::BlendMode::Invert; // dst = 1 - dst
28/// ~~~
29///
30/// ### Example:
31///
32/// ~~~cpp
33/// sprite.SetBlendMode(smk::BlendMode::Add);
34/// ~~~
35///
36/// ## Custom BlendMode:
37///
38/// This struct wrap all the parameters used by:
39/// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glBlendEquation.xhtml
40///
41/// They define how the destination "dst" pixel is computed, knowing the source
42/// "src" pixel value.
43///
44/// ~~~cpp
45/// dst.rgb = equation_rgb(src_rgb * src.rgb, dst_rgb / src.rgb)
46/// dst.alpha = equation_alpha(src_alpha * src.alpha, dst_alpha / src.alpha)
47/// ~~~
48///
49/// ### Example
50///
51/// ~~~cpp
52/// smk::BlendMode blend_mode = {
53/// GL_FUNC_ADD, // equation_rgb
54/// GL_FUNC_ADD, // equation_alpha
55/// GL_ONE, // src_rgb factor
56/// GL_ZERO, // dst_rgb factor
57/// GL_ONE, // src_alpha factor
58/// GL_ZERO, // dst_alpha factor
59/// };
60/// sprite.SetBlendMode(blend_mode);
61/// ~~~
62struct BlendMode {
63 // Preset of BlendMode:
64 static const BlendMode Replace; // dst = src
65 static const BlendMode Add; // dst += src
66 static const BlendMode Subtract; // dst -= src
67 static const BlendMode Multiply; // dst *= src
68 static const BlendMode Alpha; // dst = src * a + dest * (1 - a)
69 static const BlendMode Invert; // dst = 1 - dst
70
71 // glBlendEquation
72 GLenum equation_rgb = GL_FUNC_ADD;
73 GLenum equation_alpha = GL_FUNC_ADD;
74
75 // See glBlendFuncSeparate
76 GLenum src_rgb = GL_SRC_ALPHA;
77 GLenum dst_rgb = GL_ONE_MINUS_SRC_ALPHA;
78 GLenum src_alpha = GL_ONE;
79 GLenum dst_alpha = GL_ONE;
80
81 bool operator==(const BlendMode&) const;
82 bool operator!=(const BlendMode&) const;
83};
84
85} // namespace smk
86
87#endif /* end of include guard: SMK_BLENDMODE_HPP */
static const BlendMode Add
destination += source.
Definition: BlendMode.hpp:65
static const BlendMode Subtract
destination -= source.
Definition: BlendMode.hpp:66
static const BlendMode Multiply
destination *= source
Definition: BlendMode.hpp:67
static const BlendMode Replace
destination = source.
Definition: BlendMode.hpp:64
static const BlendMode Alpha
destination = source * source.a + destination * (1 - souce.a)
Definition: BlendMode.hpp:68
static const BlendMode Invert
destination = 1 - destination
Definition: BlendMode.hpp:69