sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
sf::Vertex vertices[] =
{
sf::Vertex(sf::Vector2f( 0, 0), sf::Color::Blue),
sf::Vertex(sf::Vector2f(200, 0), sf::Color::Red),
sf::Vertex(sf::Vector2f(200, 200), sf::Color::Red),
sf::Vertex(sf::Vector2f( 0, 200), sf::Color::Yellow),
};
sf::Shader shader;
//the example shader from the SDK
shader.loadFromFile("pixelate.frag", sf::Shader::Fragment);
shader.setParameter("pixel_threshold", .01f);
window.draw(vertices, 4, sf::Quads, &shader);
I'm probably not understanding something which is why I'm asking this question. Why doesn't this work?
The only thing that happens to the Quad is that it gets turned all black. I'm thinking it has something to do with textures since:
sf::Texture texture;
texture.create(800,600);
window.draw(vertices, 4, sf::Quads);
texture.update(window);
sf::Sprite sprite(texture);
window.draw(sprite, &shader);
this workaround works, but is probably bad since it uses the whole window. texture.update is overloaded, but I have no idea how to change a vertex array to an array of pixels or sf::Image (If it's even the correct way to do this).
This is the shader code just in case:
uniform sampler2D texture;
uniform float pixel_threshold;
void main()
{
float factor = 1.0 / (pixel_threshold + 0.001);
vec2 pos = floor(gl_TexCoord[0].xy * factor + 0.5) / factor;
gl_FragColor = texture2D(texture, pos) * gl_Color;
}
...
shader.setCurrentTexture("texture");
sf::RenderStates states;
states.shader = &shader;
states.texture = &texture;
window.draw(vertices, 4, sf::Quads, states);
shader.setCurrentTexture("texture");
I'm not sure I understand this. sf::Shader doesn't seem to have that function.
shader.setCurrentTexture("texture");
That was random..
shader.setParameter("texture",sf::Shader::CurrentTexture);
That was random..
shader.setParameter("texture",sf::Shader::CurrentTexture);
That's what I thought he meant, but it doesn't solve my problem either.
Basically, I'm wondering if I can apply the shader to whatever the vertices are drawing - in this case, the quad with interpolated colors. I think this doesn't work because the colors itself doesn't count as a texture which is why I had to draw it to the screen first, then store that whole image as a texture, create a sprite with that texture, then draw that sprite with the shader.
What I'm wondering is if I can apply a shader directly to the vertices without having to use textures. And if not, is there a way to not use the whole screen and just use the vertices as a texture.
And I thought this was my biggest clue!
shader.loadFromFile("pixelate.frag", sf::Shader::Fragment);
Well in any case, I'm trying to apply the pixelate effect (from the example) to the vertices without using textures.