Nested loops in Fragment Shader not nesting

I have a fragment shader:

void main(void) {
	vec2 st     = gl_TexCoord[0].st;		// Get Coordinate
	vec2 offset = vec2(0);
	vec3 sample = vec3(0);				// Declare a sample to hold the convolution matrix sample
	int i;
	int j;

	for (i == 0; i < 5; i++)
		for (j == 0; j < 5; j++)
			offset = vec2(i-16,j-16);
			sample += texture2DRect(source, st+offset).rgba*(1./5);
    gl_FragColor =  vec4(sample,1.);					// Update Pixel

And it will run the j loop (5 samples) but it only returns i == static. If I pass offset as j,j it’ll obviously blur as a diagonal. Can you not perform nested loops in OpenGL? It’s also clearly only running the last loop since I have to divide the sample intensity by 5 not 25. Any help would be appreciated.

Should those comparisons be assignments?

1 Like

I’m surprised that it ran at all with the == instead of =.

Also, I would change that rgba*(1./5); to rgba*(1.0/5.0); Leaving the .0 off the 5 could result in an integer from that division operation on some graphics drivers, which would ultimately mean you were incrementing sample by zero.

1 Like

Ha! face palm Of course that’s it. I’m surprised it ran at all too! Very strange that it didn’t give me a compile error.

Thank you! I was sure a second set of eyes could see what the snag was.

There’s no reason for it not to run, the for takes that as a statement. You can actually write 0==3; as a valid line of code… I actually use it all over my code for artistic purposes.

At most it could give you a statement with no effect warning.