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

Author Topic: Use multiple shaders on same sprite?  (Read 13397 times)

0 Members and 2 Guests are viewing this topic.

addel

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Use multiple shaders on same sprite?
« Reply #15 on: June 30, 2013, 11:32:23 pm »
From other languages vector is used to store 2 values like a xy point but in C++ it is used as a array? Also i don't understand how i fill it with shaders as you don't give a example and looking at the tutorials i can only see shaders being applied by window.draw().

I also don't understand what is happening with the "sf::RenderTexture* front = &image1;" part but as a guess it looks like the RenderTexture's are being duplicated with a new name? I have not yet learned what the * or & symbols are doing and i am also not sure what the point of renaming it is either. Then there is the for loop on a 2 point thing to swap and combine them but what if the stack was more than 2 shaders?

So i don't really understand it. I tried to add in the missing parts to the code but am getting errors.

// Create a Texture
sf::Texture Texture001;
if (!Texture001.loadFromFile("assets/background.jpg")) { return EXIT_FAILURE; }
sf::Sprite Sprite001(Texture001); // Create a Sprite from the Texture

std::vector<sf::Shader> shaders; // we assume it is already filled

sf::RenderTexture image1;
sf::RenderTexture image2;

// Draw sprite into RenderTexture????
image1.draw(Sprite001);
image2.draw(Sprite001);

sf::RenderTexture* front = &image1;
sf::RenderTexture* back = &image2;

// draw the initial scene into "back"

// Apply Fragment Shader
if (sf::Shader::isAvailable()) {
    // Create the shader
    sf::Shader shader;

    // Load Fragment shader
    if (shader.loadFromFile("assets/pixelate.frag", sf::Shader::Fragment)){
        float x = static_cast<float>(sf::Mouse::getPosition(window).x) / window.getSize().x;
        shader.setParameter("pixel_threshold", x/10);
        // Draw shader in to back????
        window.draw(back, &shader);
    }
}

for (std::vector<sf::Shader>::iterator it = shaders.begin(); it != shaders.end(); ++it) {
    // draw "back" into "front"
    front->clear();
    front->draw(sf::Sprite(back->getTexture()), &*it);
    front->display();

    // swap front and back buffers
    std::swap(back, front);
}

// Apply Fragment Shader
if (sf::Shader::isAvailable()) {
    // Create the shader
    sf::Shader shader2;

    // Load Fragment shader
    if (shader2.loadFromFile("assets/blur.frag", sf::Shader::Fragment)){
        float x = static_cast<float>(sf::Mouse::getPosition(window).x) / window.getSize().x;
        shader2.setParameter("blur_radius", x* 0.01f);
        // Draw 2nd shader in to back????
        window.draw(back, &shader2);
    }
}
« Last Edit: June 30, 2013, 11:35:33 pm by addel »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Use multiple shaders on same sprite?
« Reply #16 on: July 01, 2013, 07:56:15 am »
Obviously, working with SFML requires to know a little of C++. If you don't even know what pointers and addresses are, stop right now, go buy a good C++ book and learn it properly. There's no point giving you a solution with code since you won't understand it, and won't be able to adapt it.
Laurent Gomila - SFML developer

 

anything