1
Graphics / Re: Use multiple shaders on same sprite?
« 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.
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);
}
}
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);
}
}