Hello SFML!
I'm having trouble getting my sf::Shader to work on a sf::RectangleShape that has a texture attached to it via setTexture( &texture).
Is there something I am missing in order to get that to work?
I'm using a simple fragment shader that I got from the forums somewhere here. Here is the shader:
uniform sampler2D texture;
uniform vec3 light;
uniform vec4 lcolor;
void main() {
float distance = sqrt(pow(gl_FragCoord.x - light.x, 2) +
pow(gl_FragCoord.y - light.y, 2));
if(floor(light.x) == floor(gl_FragCoord.x) &&
floor(light.y) == floor(gl_FragCoord.y))
distance = 0.1;
if(distance>light.z)
distance = light.z;
vec2 pos = gl_TexCoord[0].xy;
//vec4 c2 = lcolor;
//c2.a = 1.0 - (distance / light.z);
// Why doesn't this work for texures???
gl_FragColor = mix(texture2D(texture,pos),
lcolor,
1.0 - (distance/light.z));
}
And what happens is this:
The rectangle shape on the left, with no texture, is properly lighted. On the right, with my wall.png as a texture, it still shown bright as day.
I'm pretty new to GLSL and wanted to use SFML because: 1. I really like it and have used it before, 2. OpenGL is a lot more code / scary to me.
Here is how I load the shader, create the rectangles and apply the texture:
sf::Shader shader;
if (!shader.loadFromFile("light.frag", sf::Shader::Fragment))
return -1;
sf::RenderStates renderState;
renderState.shader = &shader;
shader.setParameter("texture", sf::Shader::CurrentTexture);
sf::RectangleShape rect(sf::Vector2f(800,600));
sf::RectangleShape r2(sf::Vector2f(200,200));
r2.setPosition(200, 200);
r2.setFillColor(sf::Color::Cyan);
sf::RectangleShape r3(sf::Vector2f(320, 64));
r3.setPosition(600, 100);
sf::Texture t;
t.loadFromFile("wall.png");
r3.setTexture(&t);
Here is a snippet for the drawing:
window.draw(shadow(r2, em, sf::Color::Black));
window.draw(r2, renderState);
// why do the bricks not vanish?
window.draw(shadow(r3, em, sf::Color::Black));
window.draw(r3, renderState);
window.display();
NB:
- The shadow method is what's creating those faux-hard shadows, again courtesy of the sfml forums (thanks, it's awesome, I have the link on my other computer).
- The "light" parameter for the shader is set earlier based on the mouse position.
Any help / advice would be much appreciated.
Thanks!