Search code examples
castingcompiler-errorsglslopengl-3

Where are my GLSL implicit cast errors located?


In what appears to be my fragment shader, I have these two errors:

0(47) : error C7011: implicit cast from "int" to "vec3"
0(55) : error C7011: implicit cast from "int" to "vec3"

Yet, after spending a good 30 minutes or so looking over the functions being used in the shader programs, it looks like I'm doing this properly, and the majority of the vectors in my shaders are vec4 - basically, any rgba calculations. What I'm trying to figure out is why I'm receiving this error...

I've checked my vertex shader as well, just to be sure, and I can't seem to find anything which matches up with that either.

What is the error?

Fragment Shader

#version 330 core

in vec2 UV;

in vec3 Position_worldSpace;
in vec3 Normal_cameraSpace;
in vec3 EyeDirection_cameraSpace;
in vec3 LightDirection_cameraSpace;

out vec4 Color;

uniform sampler2D TextureSampler;
uniform mat4 ViewMatrix;
uniform mat4 ModelMatrix;
uniform vec3 LightPosition_worldSpace;

void main() 
{
    // Light emission properties
    // Make them uniforms for flexibility/customization

    vec4 LightColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
    float LightPower = 50.0f;

    // Material properties
    vec4 MaterialDiffuseColor  = texture(TextureSampler, UV).rgba;
    vec4 MaterialAmbientColor  = vec4(0.1f, 0.1f, 0.1f, 1.0f) * MaterialDiffuseColor;
    vec4 MaterialSpecularColor = vec4(0.3f, 0.3f, 0.3f, 1.0f);

    float DistanceToLight = length(LightPosition_worldSpace - Position_worldSpace);     

    // Normal of the computed fragment, in camera space

    vec3 normal = normalize(Normal_cameraSpace);

    // Light Direction, from the fragment to the light itself

    vec3 ld = normalize(LightDirection_cameraSpace);

    // Cosine of the angle between the normal and the light direction,
    // clamped above 0
    //  - light is at the vertical of the triangle -> 1
    //  - light is perpendicular to the triangle   -> 0
    //  - light is behind the triangle             -> 0

    float cosTheta = clamp(dot(normal, 1), 0, 1);

    // Eye vector (towards the camera)

    vec3 norm_EyeDir = normalize(EyeDirection_cameraSpace);

    // direction in which the triangle reflects the light

    vec3 reflection = reflect(-1, normal);

    // Cosine of the angle between the Eye vector and
    // the Reflect vector, clamped to 0
    //  - Looking into the reflection -> 1
    //  - Looking elsewhere           -> less than 1

    float cosAlpha = clamp(dot(norm_EyeDir, reflection), 0, 1);

    float DistSquared = DistanceToLight * DistanceToLight;

    Color = 
        // Ambient : simulates indirect lighting
        MaterialAmbientColor + 
        // Diffuse : "color" of the object
        MaterialDiffuseColor * LightColor * LightPower * cosTheta / DistSquared +
        // Specular : reflective highlight, like a mirror
        MaterialSpecularColor * LightColor * LightPower * pow(cosAlpha, 5) / DistSquared;

}

Vertex Shader

#version 330 core

// Input data, which is different for all executions of this shader

layout(location = 0) in vec3 VertexPosition_modelSpace;
layout(location = 1) in vec2 VertexUV;
layout(location = 2) in vec3 VertexNormal_modelSpace;

// Output data ; will be interpolated for each fragment

out vec2 UV;

out vec3 Position_worldSpace;
out vec3 Normal_cameraSpace;
out vec3 EyeDirection_cameraSpace;
out vec3 LightDirection_cameraSpace;

// Values which stay constant for the whole mesh

uniform mat4 MvpMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ModelMatrix;

uniform vec3 LightPosition_worldSpace;

void main()
{
    vec4 vPos = vec4(VertexPosition_modelSpace, 1.0f);

    // Output position of the vertex, in clip space : MvpMatrix * position
    gl_Position = MvpMatrix * vPos;

    // Position of the vertex, in worldSpace : ModelMatrix * position
    Position_worldSpace = (ModelMatrix * vPos).xyz;


    // Vector that goes from the vertex to the camera, in camera space.
    // In camera space, the camera is at the origin (0, 0, 0).

    vec3 VertexPosition_cameraSpace = (ViewMatrix * ModelMatrix * vPos).xyz;

    EyeDirection_cameraSpace = vec3(0.0f, 0.0f, 0.0f) - VertexPosition_cameraSpace;

    // Vector that goes from the vertex to the light, in camera space.
    // M is identity, thus it's ommitted 
    vec3 LightPosition_cameraSpace = (ViewMatrix * vec4(LightPosition_worldSpace, 1.0f)).xyz;
    LightDirection_cameraSpace = LightPosition_cameraSpace + EyeDirection_cameraSpace;

    // Normal of the vertex, in camera space

    // This is only correct if ModelMatrix does not scale the model; use its transpose if doesn't work properly.
    Normal_cameraSpace = (ViewMatrix * ModelMatrix * vec4(VertexNormal_modelSpace, 0.0f)).xyz; 

    // UV of the vertex.

    UV = VertexUV;
}

Update

The two lines I tracked down are as follows:

vec3 reflection = reflect(-1.0f, normal);

float cosTheta = clamp( dot(normal, 1.0f), 0.0f, 1.0f);

What's weird is before I was passing whole integers in without the .0f suffix, and since I've changed that, the messages are now implicit cast from "float" to "vec3", as opposed to "int". Yet, according to the GLSL specification/man pages, both of these functions return GLSL's genType.


Solution

  • I think its

    float cosTheta = clamp(dot(normal, 1), 0, 1);
    

    You can't dot a vector (normal) with a scalar (1).

    And it looks like there's another one, also in the Fragment Shader:

    vec3 reflection = reflect(-1, normal);