-
-
Save oprypin/6cb6f3d0f1b1643937979fa785ab3549 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uniform sampler2D source; | |
uniform vec4 mask; | |
uniform vec2 direction; | |
void main() { | |
vec2 textureCoordinates = gl_TexCoord[0].xy; | |
vec4 color = vec4(0.0); | |
color += texture2D(source, textureCoordinates - 4.0 * direction) * 0.0162162162; | |
color += texture2D(source, textureCoordinates - 3.0 * direction) * 0.0540540541; | |
color += texture2D(source, textureCoordinates - 2.0 * direction) * 0.1216216216; | |
color += texture2D(source, textureCoordinates - direction) * 0.1945945946; | |
color += texture2D(source, textureCoordinates) * 0.2270270270; | |
color += texture2D(source, textureCoordinates + direction) * 0.1945945946; | |
color += texture2D(source, textureCoordinates + 2.0 * direction) * 0.1216216216; | |
color += texture2D(source, textureCoordinates + 3.0 * direction) * 0.0540540541; | |
color += texture2D(source, textureCoordinates + 4.0 * direction) * 0.0162162162; | |
color = mask * vec4(100.0, 100.0, 100.0, color[3]) / 100.0; | |
gl_FragColor = color; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2009--2016, The Cantarell Authors | |
This Font Software is licensed under the SIL Open Font License, Version 1.1. | |
This license is copied below, and is also available with a FAQ at: | |
http://scripts.sil.org/OFL | |
----------------------------------------------------------- | |
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 | |
----------------------------------------------------------- | |
PREAMBLE | |
The goals of the Open Font License (OFL) are to stimulate worldwide | |
development of collaborative font projects, to support the font creation | |
efforts of academic and linguistic communities, and to provide a free and | |
open framework in which fonts may be shared and improved in partnership | |
with others. | |
The OFL allows the licensed fonts to be used, studied, modified and | |
redistributed freely as long as they are not sold by themselves. The | |
fonts, including any derivative works, can be bundled, embedded, | |
redistributed and/or sold with any software provided that any reserved | |
names are not used by derivative works. The fonts and derivatives, | |
however, cannot be released under any other type of license. The | |
requirement for fonts to remain under this license does not apply | |
to any document created using the fonts or their derivatives. | |
DEFINITIONS | |
"Font Software" refers to the set of files released by the Copyright | |
Holder(s) under this license and clearly marked as such. This may | |
include source files, build scripts and documentation. | |
"Reserved Font Name" refers to any names specified as such after the | |
copyright statement(s). | |
"Original Version" refers to the collection of Font Software components as | |
distributed by the Copyright Holder(s). | |
"Modified Version" refers to any derivative made by adding to, deleting, | |
or substituting -- in part or in whole -- any of the components of the | |
Original Version, by changing formats or by porting the Font Software to a | |
new environment. | |
"Author" refers to any designer, engineer, programmer, technical | |
writer or other person who contributed to the Font Software. | |
PERMISSION & CONDITIONS | |
Permission is hereby granted, free of charge, to any person obtaining | |
a copy of the Font Software, to use, study, copy, merge, embed, modify, | |
redistribute, and sell modified and unmodified copies of the Font | |
Software, subject to the following conditions: | |
1) Neither the Font Software nor any of its individual components, | |
in Original or Modified Versions, may be sold by itself. | |
2) Original or Modified Versions of the Font Software may be bundled, | |
redistributed and/or sold with any software, provided that each copy | |
contains the above copyright notice and this license. These can be | |
included either as stand-alone text files, human-readable headers or | |
in the appropriate machine-readable metadata fields within text or | |
binary files as long as those fields can be easily viewed by the user. | |
3) No Modified Version of the Font Software may use the Reserved Font | |
Name(s) unless explicit written permission is granted by the corresponding | |
Copyright Holder. This restriction only applies to the primary font name as | |
presented to the users. | |
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font | |
Software shall not be used to promote, endorse or advertise any | |
Modified Version, except to acknowledge the contribution(s) of the | |
Copyright Holder(s) and the Author(s) or with their explicit written | |
permission. | |
5) The Font Software, modified or unmodified, in part or in whole, | |
must be distributed entirely under this license, and must not be | |
distributed under any other license. The requirement for fonts to | |
remain under this license does not apply to any document created | |
using the Font Software. | |
TERMINATION | |
This license becomes null and void if any of the above conditions are | |
not met. | |
DISCLAIMER | |
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT | |
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE | |
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL | |
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM | |
OTHER DEALINGS IN THE FONT SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "DropShadow.hpp" | |
#include <cassert> | |
#include <cmath> | |
#include <memory> | |
using namespace sfext; | |
namespace { | |
std::unique_ptr<sf::Shader> shadowShader; | |
sf::Glsl::Vec4 convert255to100(const sf::Color &color) { | |
return sf::Glsl::Vec4((float)color.r * 100.f / 255.f, | |
(float)color.g * 100.f / 255.f, (float)color.b * 100.f / 255.f, | |
(float)color.a * 100.f / 255.f); | |
} | |
} // namespace | |
bool DropShadow::load(const std::string &shadowPath) { | |
shadowShader = std::make_unique<sf::Shader>(); | |
return shadowShader->loadFromFile(shadowPath, sf::Shader::Fragment); | |
} | |
void DropShadow::draw( | |
sf::RenderTarget &target, sf::RenderStates baseStates) const { | |
auto transformable = dynamic_cast<const sf::Transformable *>(&drawable); | |
// Draw shadow | |
{ | |
sf::Vector2f pos = transformable->getPosition() - | |
transformable->getOrigin() + offset - | |
sf::Vector2f(10, 10); | |
pos = {std::round(pos.x), std::round(pos.y)}; | |
sf::RenderStates states = baseStates; | |
sf::Sprite sprite(texture.getTexture()); | |
shadowShader->setUniform("mask", convert255to100(color)); | |
shadowShader->setUniform("direction", sf::Vector2f(0.f, pixelSize.y)); | |
states.shader = shadowShader.get(); | |
states.transform.translate(pos); | |
target.draw(sprite, states); | |
} | |
// Draw drawable | |
{ | |
sf::Vector2f pos = | |
transformable->getPosition() - transformable->getOrigin(); | |
pos = {std::round(pos.x), std::round(pos.y)}; | |
target.draw(drawable, baseStates); | |
} | |
} | |
void DropShadow::regenerate() { | |
auto transformable = dynamic_cast<const sf::Transformable *>(&drawable); | |
sf::Vector2i textureSize = {size.x + 20, size.y + 20}; | |
pixelSize = {1.f / float(textureSize.x), 1.f / float(textureSize.y)}; | |
// Render font to a single texture | |
sf::RenderTexture tmpTexture; | |
{ | |
tmpTexture.create(textureSize.x, textureSize.y); | |
tmpTexture.clear(sf::Color::Transparent); | |
sf::RenderStates states = sf::RenderStates::Default; | |
states.transform = transformable->getInverseTransform(); | |
states.transform.translate({10, 10}); | |
tmpTexture.draw(drawable, states); | |
tmpTexture.display(); | |
} | |
// Render horizontal blur | |
{ | |
texture.create(textureSize.x, textureSize.y); | |
texture.clear(sf::Color::Transparent); | |
sf::Sprite sprite(tmpTexture.getTexture()); | |
sf::RenderStates states = sf::RenderStates::Default; | |
shadowShader->setUniform("mask", sf::Glsl::Vec4(100, 100, 100, 100)); | |
shadowShader->setUniform("direction", sf::Vector2f(pixelSize.x, 0.f)); | |
states.shader = shadowShader.get(); | |
texture.draw(sprite, states); | |
texture.display(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Graphics.hpp> | |
#include <string> | |
namespace sfext { | |
class DropShadow : public sf::Drawable, public sf::Transformable { | |
const sf::Drawable &drawable; | |
const sf::Vector2f size; | |
const sf::Vector2f offset; | |
const sf::Color color; | |
sf::RenderTexture texture; | |
sf::Vector2f pixelSize; | |
public: | |
DropShadow(const sf::Drawable &drawable, sf::Vector2f size, | |
sf::Vector2f offset, sf::Color color) | |
: drawable(drawable), size(size), offset(offset), color(color) { | |
regenerate(); | |
} | |
[[nodiscard]] static bool load(const std::string &shadowPath); | |
private: | |
virtual void draw( | |
sf::RenderTarget &target, sf::RenderStates states) const override; | |
void regenerate(); | |
}; | |
} // namespace sfext |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SFML/Graphics.hpp> | |
#include "DropShadow.hpp" | |
int main() { | |
sf::RenderWindow rwindow(sf::VideoMode(800, 600), "SFML window"); | |
sf::Font font; | |
printf("%d\n", font.loadFromFile("Cantarell-Regular.otf")); | |
sf::Text rawlabel = sf::Text("RVWP", font, 20); | |
rawlabel.setFillColor(sf::Color::White); | |
sf::Vector2f textsize(rawlabel.getLocalBounds().width, | |
rawlabel.getLocalBounds().height); | |
sfext::DropShadow::load("blur.frag"); | |
sfext::DropShadow label(static_cast<const sf::Drawable&>(rawlabel), | |
textsize, sf::Vector2f(1.f, 2.f), sf::Color(0x0000007f)); | |
while (rwindow.isOpen()) { | |
rwindow.clear(sf::Color::White); | |
const auto size = rwindow.getSize(); | |
label.setPosition(sf::Vector2f(size.x / 2, size.y * 0.1f)); | |
sf::FloatRect textRect = rawlabel.getLocalBounds(); | |
label.setOrigin(textRect.left + textRect.width / 2.0f, | |
textRect.top + textRect.height / 2.0f); | |
rwindow.draw(label); | |
rwindow.display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment