我的GLSL隐式转换错误位于何处?

时间:2012-11-24 08:21:08

标签: casting compiler-errors glsl opengl-3

在我的片段着色器看来,我有两个错误:

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

然而,在花了大约30分钟左右查看着色器程序中使用的函数后,看起来我正在做这个,并且我的着色器中的大多数向量都是vec4 - 基本上,任何rgba计算。我想弄清楚的是我收到这个错误的原因......

我也检查了我的顶点着色器,只是为了确定,我似乎找不到任何与之匹配的东西。

错误是什么?

片段着色器

#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;

}

顶点着色器

#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;
}

更新

我追踪的两条线如下:

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

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

在我没有.0f后缀的情况下传递整个整数之前有什么奇怪的事情,因为我已经改变了,所以消息现在是implicit cast from "float" to "vec3",而不是"int"。然而,根据GLSL规范/手册页,这些函数的两个都返回GLSL的genType

1 个答案:

答案 0 :(得分:2)

我认为它

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

您无法使用标量(normal)对矢量(1)进行点缀。

看起来还有另一个,也在片段着色器中:

vec3 reflection = reflect(-1, normal);