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

Author Topic: Shaders and texture rects  (Read 2198 times)

0 Members and 1 Guest are viewing this topic.

Synyster_Coder

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Shaders and texture rects
« on: July 15, 2014, 03:59:18 pm »
Hey guys,

So I have this problem:

I have a texture that is let's say 1024x1024.
I have a sprite with a subrect of 100,200,500x500.
I have a wave fragment shader that is this:

uniform sampler2D texture;                       //sf::Shader::CurrentTexture
uniform vec3 iResolution;                          //1000,800,1
uniform float time;                                    //A Clock returning miliseconds
uniform float waveWidth;                         //50
uniform float amplitude;                           //150
uniform float speed;                                //100
void main()
{
   vec2 colour = gl_FragCoord.xy / iResolution.xy;

   colour.y = 1.0 - colour.y;

   colour.y += sin(colour.x*waveWidth+(time/speed))/amplitude;

   gl_FragColor = texture2D(texture,colour);
}

The problem is that the whole texture not just the subrect is being scaled to the size of the window and having the wave effect.

I have tried adding this code:

glViewport(100,200,500,500);

But this didn't work.

Anyone have any ideas of how I could maintain the subrect of the sprite?

Thanks everyone!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Shaders and texture rects
« Reply #1 on: July 15, 2014, 04:23:12 pm »
Since you use the fragment position rather than texture coordinates (or anything else correlated to the sprite's subrect) for texture lookup, the results are not very surprising.
Laurent Gomila - SFML developer

Synyster_Coder

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Shaders and texture rects
« Reply #2 on: July 15, 2014, 04:28:08 pm »
Thank's for the help, so you are saying if I were to use gl_TexCoord[0] instead I would have better results?

Synyster_Coder

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Shaders and texture rects
« Reply #3 on: July 15, 2014, 04:32:32 pm »
That worked perfectly thank you very much!!

Just so everyone can see this is the new shader:

uniform sampler2D texture;
uniform float time;
uniform float waveWidth;
uniform float amplitude;
uniform float speed;
void main()
{
   vec2 colour = gl_TexCoord[0].xy;

   colour.y += sin(colour.x*waveWidth+(time/speed))/amplitude;

   gl_FragColor = texture2D(texture,colour);
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Shaders and texture rects
« Reply #4 on: July 15, 2014, 04:52:13 pm »
The variable name "colour" is misleading, since this is a set of coordinates, not a color. The actual color is obtained after looking up the texture at the specified coordinates ;)
Laurent Gomila - SFML developer

Synyster_Coder

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: Shaders and texture rects
« Reply #5 on: September 17, 2014, 12:13:36 pm »
How right you are, thanks!