jmasterx
jmasterx

Reputation: 54193

Declaring certain variables in shader causes it to stop working? (GLSL)

I'm using GLSL.

I have a simple fragment shader here:

 "uniform sampler2D backBuffer;",
 "uniform float r;",
 "uniform float g;",
 "uniform float b;",
 "uniform float ratio;",
 "void main() {",
 "  vec4 color;",
 "  float avg, dr, dg, db, multiplier;",
 "  color = texture2D(backBuffer, vec2(gl_TexCoord[0].x * 1,gl_TexCoord[0].y * 1));",
 "  avg = (color.r + color.g + color.b) / 3.0;",
 "  dr = avg * r;",
 "  dg = avg * g;",
 "  db = avg * b;",
 "  color.r =  color.r * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
"   color.g =  color.g * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
"   color.b =  color.b * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
 "  gl_FragColor = color;",
 "}"

Now it works just fine.

However, for some very strange reason, adding any more variables such as a vec2 or float causes it to have no effect on my scene:

 "uniform sampler2D backBuffer;",
 "uniform float r;",
 "uniform float g;",
 "uniform float b;",
 "uniform float ratio;",
 "void main() {",
 "  vec4 color;",
 "  float avg, dr, dg, db, multiplier;",
 "  vec2 divisors;",
 "  color = texture2D(backBuffer, vec2(gl_TexCoord[0].x * 1,gl_TexCoord[0].y * 1));",
 "  avg = (color.r + color.g + color.b) / 3.0;",
 "  dr = avg * r;",
 "  dg = avg * g;",
 "  db = avg * b;",
 "  color.r =  color.r * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
"   color.g =  color.g * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
"   color.b =  color.b * (gl_TexCoord[0].x * gl_TexCoord[0].y);",
 "  gl_FragColor = color;",
 "}"

In this one I added a vec2 called divisors, that's all I did and the shader no longer does anything to the pixels.

Why is this? Is there something I do not understand about variable declaration in GLSL?

Thanks

Upvotes: 0

Views: 347

Answers (1)

UncleZeiv
UncleZeiv

Reputation: 18488

I notice that each line is a quoted string separated by commas. In C/C++ you would usually just juxtapose quoted strings when creating a single big string, so I wonder if you are doing something strange like initializing an array of strings and not taking into account that its size has changed after adding a new line?

Upvotes: 2

Related Questions