GLSL questions

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:

  1. 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.

  2. Is there any tutorial focusing on GLSL only?

  3. 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:

  1. Look for a pixel with a Red value of 1.0 in te texture mask
  2. Once found, check the pixel below in the shadow texture
  3. 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,

I have not found a stand-alone GLSL tutorial

That’s because you can’t use GLSL standalone. By definition, any tutorial that involves GLSL must involve OpenGL. Furthermore, GLSL is designed to be useful in producing graphics. Shaders get data from rendering commands. They produce data in the form of images (or transform feedback data).

Sure, more recent stuff like image load/store, SSBOs, and compute shaders divorce GLSL a bit from rendering. But for the most part, most of the constructs of GLSL are designed around rendering.

How long should a shader be?

As long as it needs to be to do it’s job.

Is there any other way to achieve what I’m doing here in a more efficent way?

It’s not really clear what your shader is doing. You described your algorithm, but what’s unclear is what this is actually doing. Why is a particular invocation of a fragment shader iterating over a texture to find some particular location where a conditional statement is true? What is this attempting to achieve? Are you trying to do some kind of shadow-mapping or something? If so, that’s generally done with one texture access, not by scanning the texture.

You’re basically scanning the entire texture for every fragment being rendered. That’s not going to be fast.

I see!
Here is an example of what I wanted to achieve:

i.imgur.com/QxZVyo7.jpg

  1. I have a map, with a flat floor and walls. Every thing here is 2d, there is no 3d geometry, only 2d poligons that compose the map.

  2. Using the vertex of the polygons I cast shadows, to define the viewable area. The purple lines are part of the mask I use.

  3. Using the shader when drawing the shadows on top of the scenario, I avoid the walls to be also obscured.

  4. This way the shadows are cast dynamically along the walls as the field of view changes

Thanks!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.