Hi,
If I try to use the same sf::Shader to draw two different things, and then I try not to use the shader, I get a bug.
Here's a minimal example that doesn't work. For this example, you need three images of the same size. Make them very distinct, so that you can see the problem. I include main.cpp and then a file called MyShader.frag
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
if (sf::Shader::IsAvailable() == false)
return EXIT_SUCCESS;
sf::RenderWindow App(sf::VideoMode(1024, 768), "SFML Shader test");
App.UseVerticalSync(true);
sf::Image SomeImage, AnotherImage, Image;
SomeImage.LoadFromFile("someimage.png");
AnotherImage.LoadFromFile("anotherimage.png");
Image.LoadFromFile("image.png");
sf::Shader myShader;
myShader.LoadFromFile("MyShader.frag");
myShader.SetTexture("imageA", sf::Shader::CurrentTexture);
myShader.SetTexture("imageB", AnotherImage);
sf::Sprite sprite1,sprite2,sprite3;
sprite1.SetImage(SomeImage);
sprite2.SetImage(SomeImage);
sprite3.SetImage(Image);
sprite2.Move(SomeImage.GetWidth(),0);
sprite3.Move(SomeImage.GetWidth(),SomeImage.GetHeight());
// Start the game loop
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
// Escape key : exit
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
App.Close();
}
// Clear screen
App.Clear();
App.Draw(sprite1, myShader);
App.Draw(sprite2, myShader);
App.Draw(sprite3);
// Finally, display the rendered frame on screen
App.Display();
}
return EXIT_SUCCESS;
}
and MyShader.frag, which is a simple multiply shader
uniform sampler2D imageA;
uniform sampler2D imageB;
void main()
{
vec4 A = texture2D(imageA, gl_TexCoord[0].xy);
vec4 B = texture2D(imageB, gl_TexCoord[0].xy);
gl_FragColor = A*B;
}
This compiles and runs, but if you notice the sprite on the bottom-right, the one that is drawn without a shader, it takes the image of the other two (but not the shader effect).
This is very strange, because if I comment just one of the App.Draw(sprite1, myShader); (or with sprite2), then it works fine.
So basically, the fact that I draw sprite1 AND sprite2 (which have shaders) affects how sprite 3 is drawn, even if sprite3 has no shader!
Am I misunderstanding something?
BTW, I'm using latest sfml2 svn checkout, on Kubuntu Linux 10.10, x86_64, but this bug has been present for a while, I just hadn't written a minimal example in which this didn't work, because I was assuming I was doing something wrong.
Xorlium