Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Bug] Using the same shader for two different things.  (Read 1543 times)

0 Members and 1 Guest are viewing this topic.

Xorlium

  • Jr. Member
  • **
  • Posts: 84
    • View Profile
[Bug] Using the same shader for two different things.
« on: November 03, 2010, 08:39:08 pm »
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

Code: [Select]

#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
Code: [Select]

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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[Bug] Using the same shader for two different things.
« Reply #1 on: November 04, 2010, 10:51:39 pm »
It's fixed.

Thank you a lot for your detailed feedback :)
Laurent Gomila - SFML developer