1
\$\begingroup\$

I'm trying to use if statement in my fragment shader.

I want to compare my in int finalLightType[4];

Here is my vertex shader code and fragment shader code:

Vertex:

#version 400 core

in vec3 position;
in vec2 textureVector;
in vec3 normalVector;

out vec3 color;
out vec3 surfaceNormal;
out vec2 textureCoords;
out float visibility;
out vec3 toLightVector[4];
out int finalLightType[4];

uniform vec3 mainColor;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;

uniform float tilingX;
uniform float tilingY;

uniform float density = 0.01;
uniform float gradient = 1.5;

uniform int lightType[4];
uniform float lightRange[4];
uniform vec3 lightPosition[4];

void main(void)
{       
    finalLightType[0] = 1;
    color = mainColor;

    textureCoords = textureVector;
    textureCoords.x = textureCoords.x * tilingX;
    textureCoords.y = textureCoords.y * tilingY;

    vec4 worldPosition = transformationMatrix * vec4(position,1.0);

    vec4 positionRelativeToCam = viewMatrix * worldPosition;

    float distance1 = length(positionRelativeToCam.xyz);

    visibility = exp(-pow((distance1 * density), gradient));
    visibility = clamp(visibility,0.0,1.0);

    surfaceNormal = (transformationMatrix * vec4(normalVector,0.0)).xyz;

    gl_Position = projectionMatrix * positionRelativeToCam;
}

You can see out int finalLightType[4]; and finalLightType[0] = 1;

Fragment:

#version 400 core

in vec3 color;
in vec3 surfaceNormal;
in vec2 textureCoords;
in float visibility;
in vec3 toLightVector[4];
in int finalLightType[4];

out vec4 out_Color;

uniform bool textured;

uniform sampler2D sampler;

uniform vec3 bgColor;
uniform vec3 ambientColor;

uniform vec3 lightColor[4];
uniform float lightIntensity[4];
uniform vec3 lightDir[4];
uniform float lightSpotAngle[4];

void main(void)
{   
    vec3 unitSurfaceNormal = normalize(surfaceNormal);

    if(finalLightType[0] == 0)
    {
    }

    if(textured == true)
    {
        out_Color = texture2D(sampler,textureCoords);
    }
    else
    {
        out_Color = vec4(color,1.0);
    }

    out_Color = mix(vec4(bgColor,1.0),out_Color,visibility);

    vec3 convAmbientColor = vec3(1.0,1.0,1.0) - ambientColor;

    out_Color = out_Color - vec4(convAmbientColor,1.0);

    out_Color.w = 1.0;
}

Everything looks good, but if I start my game I receive really strange effect (Uncolored, untextured, transformed on the front of the camera object):

CASE1

But.. If I try to compare another int same way, but not in variable, but local variable (Or even uniform variable, they works fine too):

#version 400 core

in vec3 color;
in vec3 surfaceNormal;
in vec2 textureCoords;
in float visibility;
in vec3 toLightVector[4];
in int finalLightType[4];

out vec4 out_Color;

uniform bool textured;

uniform sampler2D sampler;

uniform vec3 bgColor;
uniform vec3 ambientColor;

uniform vec3 lightColor[4];
uniform float lightIntensity[4];
uniform vec3 lightDir[4];
uniform float lightSpotAngle[4];

void main(void)
{   
    vec3 unitSurfaceNormal = normalize(surfaceNormal);

    int lightTyp = 0;

    if(lightTyp == 0)
    {
    }

    if(textured == true)
    {
        out_Color = texture2D(sampler,textureCoords);
    }
    else
    {
        out_Color = vec4(color,1.0);
    }

    out_Color = mix(vec4(bgColor,1.0),out_Color,visibility);

    vec3 convAmbientColor = vec3(1.0,1.0,1.0) - ambientColor;

    out_Color = out_Color - vec4(convAmbientColor,1.0);

    out_Color.w = 1.0;
}

CASE2

It only happens if I compare in variables, everything is works fine with uniform variables, local variables, etc.. (Also tried with varying variable).

Edit:

This object after I run my game is always on the front of the camera and never rotates or whatever. I can't also see other object like a cube that should appear something (probably my camera is inside this cube and I can't see it because of culling faces).

\$\endgroup\$
  • \$\begingroup\$ Do you get any errors returned after shader compilation? \$\endgroup\$ – Kromster Feb 28 '16 at 9:34
  • \$\begingroup\$ No. Everything compiles without any errors. \$\endgroup\$ – skprime Feb 28 '16 at 9:36
4
\$\begingroup\$

As per the GLSL Language Specifications section 4.3.4 "input variables":

Fragment shader inputs that are signed or unsigned integers, integer vectors, or any double-precision floating-point type must be qualified with the interpolation qualifier "flat".

That is, these types cannot be smoothly interpolated between vertices the way that input variables are by default. Therefore, the GLSL specification states that your shader must explicitly state that it doesn't expect them to be smoothed.

You can do this by replacing your current variable declarations with these lines:

flat out int finalLightType[4];

and:

flat in int finalLightType[4];

It surprises me that you don't get compile errors from your current code; my understanding is that most drivers will outright refuse to compile your original GLSL code at all.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.