1
Graphics / Re: Scrolling background - maybe a shader?
« on: April 20, 2012, 05:09:31 pm »
Okay, after knocking my head against the wall very roughly for a while, I came up with a working solution. In case anyone else is having the same problems with how to do it, here's the new update logic:
And the fragment shader used while rendering:
tex_offset is set to its corresponding m_offsets value with setParameter in my render function, and tex is set to sf::Shader::CurrentTexture there as well. And remember setRepeated( true ); on the textures.
This is the first time I've ever touched GLSL (or shaders in general), so if anyone knows a better/faster implementation then please let me know!
Code: [Select]
void TropoBackground::Update( float elapsedSeconds )
{
// Move layers
for ( int i = 0; i < LAYER_COUNT; ++i )
{
m_offsets[ i ] += m_parallaxSpeed[ i ] * elapsedSeconds;
}
}
And the fragment shader used while rendering:
Code: [Select]
uniform sampler2D tex;
uniform float tex_offset;
void main()
{
vec4 texel = texture2D(tex, vec2(gl_TexCoord[0].s, gl_TexCoord[0].t - frac(tex_offset)));
gl_FragColor = texel;
}
tex_offset is set to its corresponding m_offsets value with setParameter in my render function, and tex is set to sf::Shader::CurrentTexture there as well. And remember setRepeated( true ); on the textures.
This is the first time I've ever touched GLSL (or shaders in general), so if anyone knows a better/faster implementation then please let me know!