Render in for loop

Hello everyone,

i want to do something like this in my shader:


int counterA=0;
for(int i=0;i<4;i++){
	if(texCoord.s<=((i+0.0)/4)+epsilon && texCoord.s>=((i+0.0)/4)-epsilon && texCoord.t >0.5){
		if(counterA==0){
			glFragColor=vec4(1,1,0,1);
	        }
		else{
		        glFragColor=vec4(0,1,0,1);
		}
		counterA++;
	}
	else{
		glFragColor=vec4(0,0,0,1);
	}
}

I’d expect to see a yellow slice on the left side on the top half of the window and and the rest of the top window should be green. The lower half should be completely black.
But what i get is a yellow slice on the right / top (i=3), the rest is black. For i=0,1,2 it is never entering.
What do I do wrong?

Thanks for your help!

You will never get green, because counterA is always zero. Was the “counter++” supposed to increment counterA instead?

Ohh man, your right. Such a stupid mistake :frowning:
None the less, i still have the same problem. its just the yellow slice for i=3 painted. the rest is black.

Note that the built-in output variable is called gl_FragColor (with ‘_’ after gl).

If you suspect the GLSL compiler to muck up something, you could try to assign the shader output (either built-in gl_FragColor or your own ‘out’ variable) only after the loop and use a temporary variable inside the loop - the theory being that assignment to output variables is something the compiler likely tracks specially and may get confused about if there is complex control flow involved.

Hi :slight_smile:

Actually it does work for me without the “_”.

Besides that, im not that experienced with opengl. Thats probably why i dont get your answer.
You suggest i should do the glFragColor outside the loop. But that doesnt change the fact that it is not in the right if cases and just the last incremented i (i=4) is recognized correctly. Does it?

Sry for my confusion :frowning:

Actually it does work for me without the “_”.

That could be a coincidence. Have you checked the compile and link info log (glGetShaderInfoLog, glGetProgramInfoLog) for helpful messages?

I meant writing it like this:


vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
int counterA = 0;
for(int i=0;i<4;i++)
{
    if(texCoord.s<=((i+0.0)/4)+epsilon && texCoord.s>=((i+0.0)/4)-epsilon && texCoord.t >0.5)
    {
        if(counterA==0)
        {
            color = vec4(1,1,0,1);
        }
        else
        {
            color = vec4(0,1,0,1);
        }

        counterA++;
    }
    else
    {
        color = vec4(0,0,0,1);
    }
}
gl_FragColor = color;

In theory this should do the same thing, except that there is only one location where an assignment to gl_FragColor happens. In the (perhaps unlikely) case that your original code runs into a GLSL compiler bug, this form or something like it could be a workaround.

[QUOTE=carsten neumann;1262594]
In theory this should do the same thing, except that there is only one location where an assignment to gl_FragColor happens. In the (perhaps unlikely) case that your original code runs into a GLSL compiler bug, this form or something like it could be a workaround.[/QUOTE]

It does the same thing, but sadly still the wrong one. Just one yellow slice on the left side :frowning:

Do you have an other idea?
I assumed that it would be a common problem to increment through the area u want to be painted?

Thanks a lot for help so far and have a nice day :slight_smile:

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