Hello
Basically, I'm doing the following:
(SFML 2.1 statically linked)
void TestFunc ()
{
sf::RenderWindow Window;
Window.create(sf::VideoMode(800, 600), "TestFunc");
//rect
sf::RectangleShape Rect;
Rect.setPosition(300.0f, 200.0f);
Rect.setSize(sf::Vector2f(200.0f, 100.0f));
Rect.setOutlineThickness(4);
Rect.setFillColor(sf::Color(0, 0, 0, 0));
Rect.setOutlineColor(sf::Color(0x00, 0x33, 0xff));//light blue
//texture to render into
sf::RenderTexture RenderTexture;
bool bRes = RenderTexture.create(512, 512);
if (bRes == false) return;
//pixelate shader from examples
sf::Shader ShaderPixelateF;
bRes = ShaderPixelateF.loadFromFile("dat/shaders/pixelate_f.glsl", sf::Shader::Type::Fragment);
if (bRes == false) return;
ShaderPixelateF.setParameter("texture", sf::Shader::CurrentTexture);
ShaderPixelateF.setParameter("pixel_threshold", 10.0f);
//sprite to render shader with
sf::Sprite Sprite;
//main cycle
while (Window.isOpen())
{
sf::Event Event;
while (Window.pollEvent(Event))
{
if (Event.type == sf::Event::Closed || (Event.type == sf::Event::KeyPressed && Event.key.code == sf::Keyboard::Key::Escape))
{
Window.close();
}
}
RenderTexture.clear(sf::Color(0, 0, 0, 0));
RenderTexture.draw(Rect);
RenderTexture.display();
Sprite.setTexture(RenderTexture.getTexture());
Window.clear(sf::Color(0, 128, 0));//green
Window.draw(Sprite, &ShaderPixelateF);
// Window.draw(Sprite);
Window.display();
}
}
and pixelate_f.glsl is:
uniform sampler2D texture;
uniform float pixel_threshold;
void main()
{
float factor = 1.0 / (pixel_threshold + 0.001);
vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;
gl_FragColor = texture2D(texture, pos) * gl_Color;
// gl_FragColor = vec4 (1.0, 1.0, 0.0, 1.0);//yellow
}
I'm seeing only green background.
Uncommenting line in cpp brings rect (rendering to texture works, sprite has texture properly set).
Uncommenting line in shader brings yellow box (shader works).
Where I'm doing wrong?
Thanks in advance
simps
void main()
{
float factor = 1.0 / (pixel_threshold + 0.001);//factor is 0.1 basically(approximation - good enough)
vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;// xy are 0.0..1.0 so they become 0.0..0.1 + 0.5 which is 0.5..0.6 and 0.0 after floor, then they get divided so they are still zero, and pixels at 0.0,0.0 in your render texture have zero alpha because you cleared it with transparent
gl_FragColor = texture2D(texture, pos) * gl_Color;//basically 0.0(corner of render texture) * 1.0(color) in all four channels
gl_FragColor.a=1.0;//now you can see rect
gl_FragColor.rb=vec2(1.0,1.0)-pos.xy;//now you can see 100% magenta rect because pos.xy are both 0.0
}
Whatever you're doing you got your algorithm wrong.
Try getting rid of gl_Color in the shader. I think that's supposed to be a color interpolated from vertices rather than a property of the texture (obviously you're already getting the texture color from texture2D()).
Doesn't matter, defaults to 1.0 in all four channels in sf::Sprite, does nothing when multiplied by other vec4.