Alright I tested it now and I get something I really don't understand at all.
Thats how I understood: I should print my stuff on texture1 and then I pass this texture to the shader.
After this I draw my shape on texture2 with the shader. So I don't use the same texture for reading and drawing.
However if I draw the shape it has the color I cleared texture1 with (green) and not magenta (which I set in the shader if the pixel is black)
Thats the code:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Test", sf::Style::Close);
window.setFramerateLimit(60);
sf::Event windowEvent;
sf::RenderTexture texture, temp;
sf::Shader shader;
sf::RectangleShape test(sf::Vector2f(50, 50));
test.setFillColor(sf::Color::Black);
test.setPosition(400, 300);
if(!texture.create(800, 600)) window.close();
if(!temp.create(800, 600)) window.close();
if(!shader.loadFromFile("test.frag", sf::Shader::Fragment)) window.close();
while(window.isOpen())
{
while(window.pollEvent(windowEvent))
{
if(windowEvent.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Blue);
texture.clear(sf::Color::Green);
temp.clear(sf::Color::Black);
texture.draw(test);
//texture.display();
shader.setParameter("texture", texture.getTexture());
temp.draw(test, &shader);
window.draw(sf::Sprite(temp.getTexture()));
window.display();
}
return EXIT_SUCCESS;
}
shader:
uniform sampler2D texture;
void main()
{
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
if(pixel == vec4(0.0, 0.0, 0.0, 1.0))
pixel = vec4(1.0, 0.0, 1.0, 1.0);
gl_FragColor = pixel;
}
Pixel is read from texture1 at position 0.0 0.0 because the rectangle shape vertices don't have texture coordinates so it default to 0.0 0.0 in SFML.
Also don't comment out that display in texture, it's needed.
If you replace
temp.draw(test, &shader);
with
temp.draw(sf::Sprite(texture.getTexture()), &shader);
It'll draw a green background with magenta rectangle on it(you can also use current texture then instead of setting it to texture.getTexture()).