Hello,
I’m completely new to OpenGL and GLSL.
I’m using SFML, which handles openGL on it’s own, and I have working with it just fine.
My problem came up when I needed to use shaders.
I have been looking for a tutorial on GLSL, but all tutorials are OpenGL tutorials. I have not found a stand-alone GLSL tutorial, and all I have done with shaders is just reverse engineering sample shaders, and I have managed to do what I wanted to, but have have a lot of open questions which I have not found an answer yet:
-
How long should a shader be?
Most examples shaders I have seen are really really short, but I don’t know if it’s for the shake of simplicity (they are just examples), or if all shders should be as brief as possible.
I have done 3 different shaders, that pretty much occludes vision depending on some pre processed masks. The longest shader has around 150 lines of code, including several loops. This ran just fine on my computer, but when I tried to run my code in a different PC with lower specs, it was really really slow. -
Is there any tutorial focusing on GLSL only?
-
This is one of my shaders. Pretty much the other shaders follows the same idea. Is there any other way to achieve what I’m doing here in a more efficent way?
The idea is the following:
- Look for a pixel with a Red value of 1.0 in te texture mask
- Once found, check the pixel below in the shadow texture
- If there is no shadow check the Blue value on the mask’s pixel and set the current pixel as transparent.
uniform sampler2D texture;
uniform sampler2D filterTexture;
uniform vec2 textureSize;
uniform float cellSize;
uniform sampler2D shadowTexture;
void main()
{
vec2 position;
vec4 filterPixel;
vec4 shadowPixel;
vec4 pixel = texture2D(texture, gl_TexCoord[0].xy );
for( float i=0.0 ; i<=cellSize*2.0 ; i++)
{
position = gl_TexCoord[0].xy;
position.y = position.y - (i/textureSize.y);
filterPixel = texture2D( filterTexture, position );
position.y = position.y - (1.0/textureSize.y);
shadowPixel = texture2D( texture, position );
if( filterPixel.r == 1.0 )
{
if ( shadowPixel.r == 0.0 && shadowPixel.g == 0.0 && shadowPixel.b == 0.0 && shadowPixel.a == 0.0)
{
if( filterPixel.b == 1.0 ){
pixel.a = 0.0;
break;
}
else if( i<=cellSize )
{
pixel.a = 0.0;
break;
}
}
}
}
gl_FragColor = pixel;
}
My biggest (and slowest) shader makes several loops like this, which seems to be the cause of the slow down.
Any help with any ofthese points would be great!
Thanks,