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

Author Topic: Sprite and Shader  (Read 1523 times)

0 Members and 1 Guest are viewing this topic.

Sakurazaki

  • Newbie
  • *
  • Posts: 31
  • `お嬢様を守ります!
    • MSN Messenger - sakurazaki.setsunita@gmail.com
    • View Profile
Sprite and Shader
« on: June 04, 2013, 06:33:30 pm »
Hi there,

I've got one Sprite which loads an spritesheet and a shader, and it's texture changes on animation
by seting an intrect.

So when rendering, I calculate the new intrect for the animation and then draw it to rendertarget (which is renderwindow by now) like this:

window->draw(*this, &shader);

This is the code to my shader:

uniform vec2 lightpos;
uniform vec3 lightColor;
uniform float screenHeight;
uniform vec3 lightAttenuation;
uniform float radius;

uniform sampler2D tex;

void main()
{              
        vec2 pixel=gl_FragCoord.xy;            
        pixel.y=screenHeight-pixel.y;  
        vec2 aux=lightpos-pixel;
        float distance=length(aux);
        float attenuation=1.0/(lightAttenuation.x+lightAttenuation.y*distance+lightAttenuation.z*distance*distance);   
        vec4 color=vec4(attenuation,attenuation,attenuation,1.0)*vec4(lightColor,1.0); 
        gl_FragColor = color * texture2D(tex,gl_TexCoord[0].st);
}

and how I load it to sf::Shader:

sf::Shader shader; // member of my sprite holding class.
        shader.loadFromFile("assets/shaders/lightfx.frag", sf::Shader::Fragment);
        shader.setParameter("lightpos", sf::Vector2f(150.f, 50.f));
        shader.setParameter("lightColor", sf::Vector3f(255.f, 255.f, 255.f));
        shader.setParameter("screenHeight", static_cast<float>(window->getSize().y));
        shader.setParameter("lightAttenuation", sf::Vector3f(50.f, 25.f, 15.f));
        shader.setParameter("radius", 500.f);
        shader.setParameter("texture", sf::Shader::CurrentTexture);

The lighting is fixed by now cause I'm still developing lighting system but this should work.

I want to know why it doesn't seem to abe applied the shader to my sprite.
Am I doing something wrong?

EDIT: Forgot to comment that the output from this is just sprite being as black color square. and If i change the attenuation to 0, it shows the lightColor square, but I want it to "diffuminate" over the sprite.

Thanks!
« Last Edit: June 04, 2013, 07:51:25 pm by Sakurazaki »
こんな私も変われるのならもし変われるのなら白になる

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite and Shader
« Reply #1 on: June 04, 2013, 07:33:15 pm »
Two uniforms are misspelled.

Also, I wouldn't name the sampler texture, since that's the name of a builtin GLSL function.

And what kind of relative path begins with a backslash? You should only use forward slashes anyway. But relative paths usually begin with the name of a file/directory or one or two dots.
« Last Edit: June 04, 2013, 07:35:47 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Sakurazaki

  • Newbie
  • *
  • Posts: 31
  • `お嬢様を守ります!
    • MSN Messenger - sakurazaki.setsunita@gmail.com
    • View Profile
Re: Sprite and Shader
« Reply #2 on: June 04, 2013, 07:41:03 pm »
I already fixed the 2 misspell attributes (Thanks i didnt notice) and changed the path to normal /.

Yet the drawing is exactly the same.

Edit: I should add that what I want to do is to make mi sprite to get the lighting and blend over it
some degradation of transparent-black + shadowing. Not sure if I'm doing it correctly
« Last Edit: June 04, 2013, 07:46:20 pm by Sakurazaki »
こんな私も変われるのならもし変われるのなら白になる