PDA

View Full Version : GLSL If Else-If - what am I doing wrong?



Rainbird
11-20-2011, 04:14 PM
Hi, noob OpenGL/GLSL user here.

I have been pulling my hair out trying to debug this fragment shader if else-if statement:



void main(void)
{
// each stage and phase of shader calculates the output texture (texunit 0)

if ( stage.x == 1 ) // ********** STAGE: DRAW SOME POINTS
{
if (stage.y == 1 )
{
vec3 pos = texture2DRect(position, texCoord).xyz;
vec3 vel = texture2DRect(velocity, texCoord).xyz;
vec3 newPos = pos+vel/1000;
gl_FragData[0] = vec4 (newPos,1);
}
else if (stage.y == 2 )
{
vec3 vel = texture2DRect(velocity, texCoord).xyz;
vec3 newVel =vel*1.01;
gl_FragData[0] = vec4 (newVel,1);
}

return;
}

else if (stage.x == 2 ) // ********** STAGE: FLUID SIMULATION
{
advect(dt, rdx, velocityField, densityField);
return;
}
else if (stage.x == 0 ) // ********** STAGE: VIZUALIZATION
{
gl_FragColor = vec4(texture2DRect(densityField, texCoord).x*vec3(1,1,1),1);
return;
}

else
{
discard;
}
}


The problem seems to be with the condition:

else if (stage.x == 0 ) // ********** STAGE: VIZUALIZATION

stage is a uniform vec2. and I seem to be able to give it any value and the condition still triggers!

Basically, can anyone please have a quick scan and tell me if I am doing something obviously wrong? I know there are some differences between C and GLSL, so I'm probably missing something. I just can't seem to figure it out.

Rainbird
11-20-2011, 09:26 PM
ok, I think I fixed it I was passing integers instead of floats to the uniform vec2.

remdul
11-21-2011, 04:06 AM
Comparing floating point numbers with "==" operator is generally unsafe due to floating point accuracy issues.