1
Graphics / Re: How to draw a Shape with Shader to a Rendertexture ?
« on: November 30, 2019, 08:43:35 pm »
I can draw the shader to a RenderTexture now, but even if the base circle shape is set to transparent
i dont get transparent edges but black ones.
if i set
Why is this color picked up when its 100% transparent??
Example:
i dont get transparent edges but black ones.
circle.setFillColor(sf::Color::Transparent);
if i set
sf::Color
to {1,1,1,0}
i get a white border.Why is this color picked up when its 100% transparent??
Example:
#include <SFML/Graphics.hpp>
#include <iostream>
#pragma region Shader
const char VertexShader[] =
"void main()"
"{"
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
"gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;"
"gl_FrontColor = gl_Color;"
"}";
const char RadialGradient[] =
"uniform vec4 color;"
"uniform vec2 center;"
"uniform float radius;"
"uniform float expand;"
"uniform float windowHeight;"
"void main(void)"
"{"
"vec2 centerFromSfml = vec2(center.x, windowHeight - center.y);"
"vec2 p = (gl_FragCoord.xy - centerFromSfml) / radius;"
"float r = sqrt(dot(p, p));"
"if (r < 1.0)"
"{"
"gl_FragColor = mix(color, gl_Color, (r - expand) / (1 - expand));"
"}"
"else"
"{"
"gl_FragColor = gl_Color;"
"}"
"}";
#pragma endregion
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Radial Gradient", sf::Style::Default);
sf::Event event;
sf::CircleShape circle;
sf::Shader shader;
circle.setRadius(50.f);
circle.setOrigin(circle.getRadius(), circle.getRadius());
circle.setPosition(sf::Vector2f(window.getSize()) / 2.f);
circle.setFillColor(sf::Color::Transparent);
shader.loadFromMemory(VertexShader, RadialGradient);
shader.setUniform("windowHeight", static_cast<float>(window.getSize().y)); // needs to be set whenever windowsize changes
sf::RenderTexture RT;
RT.create(800, 600);
RT.clear(sf::Color::Transparent);
sf::Sprite SP;
SP.setTexture(RT.getTexture(), true);
//--------------------------------------------------------------------------------------------------------------------
sf::Texture TX_BG;
TX_BG.loadFromFile("img/bg.png");
sf::Sprite SP_Backgr;
SP_Backgr.setTexture(TX_BG);
SP_Backgr.setPosition(sf::Vector2f(0, 0));
SP_Backgr.setOrigin(sf::Vector2f(0, 0));
//--------------------------------------------------------------------------------------------------------------------
sf::RenderStates states;
states.shader = &shader;
states.blendMode = sf::BlendAlpha;
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) { window.close(); }
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Num1) { states.blendMode = sf::BlendAlpha; std::cout << "BlendAlpha" << std::endl; }
if (event.key.code == sf::Keyboard::Num2) { states.blendMode = sf::BlendAdd; std::cout << "BlendAdd" << std::endl; }
if (event.key.code == sf::Keyboard::Num3) { states.blendMode = sf::BlendMultiply; std::cout << "BlendMultiply" << std::endl; }
if (event.key.code == sf::Keyboard::Num4) { states.blendMode = sf::BlendNone; std::cout << "BlendNone" << std::endl; }
}
}
sf::Vector2i MG = sf::Mouse::getPosition(window); // Mouse Global
sf::Vector2f ML = window.mapPixelToCoords(MG); // Mouse Local
circle.setPosition(ML);
shader.setUniform("color", sf::Glsl::Vec4(1, 0, 0, 1.f));
shader.setUniform("center", circle.getPosition());
shader.setUniform("radius", circle.getRadius());
shader.setUniform("expand", 0.f);
//RT.clear(sf::Color::Transparent);
RT.draw(circle, states);
RT.display();
window.clear();
window.draw(SP_Backgr);
window.draw(SP);
window.display();
//window.draw(circle, &shader);
//window.display();
}
return EXIT_SUCCESS;
}
#include <iostream>
#pragma region Shader
const char VertexShader[] =
"void main()"
"{"
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;"
"gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;"
"gl_FrontColor = gl_Color;"
"}";
const char RadialGradient[] =
"uniform vec4 color;"
"uniform vec2 center;"
"uniform float radius;"
"uniform float expand;"
"uniform float windowHeight;"
"void main(void)"
"{"
"vec2 centerFromSfml = vec2(center.x, windowHeight - center.y);"
"vec2 p = (gl_FragCoord.xy - centerFromSfml) / radius;"
"float r = sqrt(dot(p, p));"
"if (r < 1.0)"
"{"
"gl_FragColor = mix(color, gl_Color, (r - expand) / (1 - expand));"
"}"
"else"
"{"
"gl_FragColor = gl_Color;"
"}"
"}";
#pragma endregion
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Radial Gradient", sf::Style::Default);
sf::Event event;
sf::CircleShape circle;
sf::Shader shader;
circle.setRadius(50.f);
circle.setOrigin(circle.getRadius(), circle.getRadius());
circle.setPosition(sf::Vector2f(window.getSize()) / 2.f);
circle.setFillColor(sf::Color::Transparent);
shader.loadFromMemory(VertexShader, RadialGradient);
shader.setUniform("windowHeight", static_cast<float>(window.getSize().y)); // needs to be set whenever windowsize changes
sf::RenderTexture RT;
RT.create(800, 600);
RT.clear(sf::Color::Transparent);
sf::Sprite SP;
SP.setTexture(RT.getTexture(), true);
//--------------------------------------------------------------------------------------------------------------------
sf::Texture TX_BG;
TX_BG.loadFromFile("img/bg.png");
sf::Sprite SP_Backgr;
SP_Backgr.setTexture(TX_BG);
SP_Backgr.setPosition(sf::Vector2f(0, 0));
SP_Backgr.setOrigin(sf::Vector2f(0, 0));
//--------------------------------------------------------------------------------------------------------------------
sf::RenderStates states;
states.shader = &shader;
states.blendMode = sf::BlendAlpha;
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) { window.close(); }
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Num1) { states.blendMode = sf::BlendAlpha; std::cout << "BlendAlpha" << std::endl; }
if (event.key.code == sf::Keyboard::Num2) { states.blendMode = sf::BlendAdd; std::cout << "BlendAdd" << std::endl; }
if (event.key.code == sf::Keyboard::Num3) { states.blendMode = sf::BlendMultiply; std::cout << "BlendMultiply" << std::endl; }
if (event.key.code == sf::Keyboard::Num4) { states.blendMode = sf::BlendNone; std::cout << "BlendNone" << std::endl; }
}
}
sf::Vector2i MG = sf::Mouse::getPosition(window); // Mouse Global
sf::Vector2f ML = window.mapPixelToCoords(MG); // Mouse Local
circle.setPosition(ML);
shader.setUniform("color", sf::Glsl::Vec4(1, 0, 0, 1.f));
shader.setUniform("center", circle.getPosition());
shader.setUniform("radius", circle.getRadius());
shader.setUniform("expand", 0.f);
//RT.clear(sf::Color::Transparent);
RT.draw(circle, states);
RT.display();
window.clear();
window.draw(SP_Backgr);
window.draw(SP);
window.display();
//window.draw(circle, &shader);
//window.display();
}
return EXIT_SUCCESS;
}