1

在似乎是我的片段着色器中,我有这两个错误:

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.

4

1 回答 1

3

我想这是

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

不能normal用标量 ( 1) 为向量 ( ) 加点。

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

vec3 reflection = reflect(-1, normal);
于 2012-11-24T08:24:04.970 回答