0

我有一个片段着色器,它有结构和这些结构的统一。当我尝试编译它们时,OpenGL 给了我这个错误:

0(30) : error C0000: syntax error, unexpected identifier, expecting '{' at token "lights_a"
0(31) : error C0000: syntax error, unexpected identifier, expecting '{' at token "material"

我不知道这里有什么问题。我一直在寻找问题...我查看了第 30 行和第 31 行,我做了我能想象的一切,但没有成功。

这是代码:

#version 330 core
struct LightBase
{
    int renderit;

    vec4 ambient_light;
    vec4 specular_light;
    vec4 diffuse_light;

    float radius;

    vec3 light_position;
    vec3 light_direction;

    int light_type;
};

struct MaterialBase
{
    vec4 ambient_affect;
    vec4 specular_affect;
    vec4 diffuse_affect;
    float shining;

    float mirror;

    int light_affect;
};

in vec3 VertexPos;
in vec3 Normal;
uniform int light_quantity;
uniform LightBase lights_a[50];
uniform MaterialBase material;
uniform float usingTex;
uniform sampler2D texturemap;
in vec2 UVs;
in vec4 Colors;
out vec4 color;

void main() {

    vec4 texture_u = texture(texturemap,UVs).rgba * usingTex;
    vec4 color_u = Colors * (1.0f-usingTex);

    vec4 final_color = color_u+texture_u;
    // Light
    for(int i=0;i<light_quantity;i++) {
        if(lights_a[i].renderit==1) {
            if(lights_a[i].light_type==1) {

                float attenuation = max(0.0,1.0-dot(lights_a[i].light_direction,lights_a[i].light_direction));

                vec3 L = normalize(lights_a[i].light_direction);
                vec3 N = normalize(Normal);
                vec3 V = normalize(-VertexPos);
                vec3 R = normalize(-reflect(L,N));

                float nDotL = max(0.0,dot(N,L));
                float rDotV = max(0.0,dot(R,V));

                float ambient_result = lights_a[i].ambient_light * material.ambient_affect * attenuation;
                float diffuse_result = lights_a[i].diffuse_light * material.diffuse_affect * nDotL * attenuation;
                float specular_result = lights_a[i].specular_light * material.specular_affect * pow(rDotV,material.shining) * attenuation;

                vec4 this_colour = (ambient_result + diffuse_result + specular_result) * final_color;
                    final_color = this_colour;
            }
    }
    }


    color = final_color;
}

代码有什么问题?

4

1 回答 1

3

当我编译你的代码时,我没有看到错误——只有几个警告:

0(62) : error C7011: implicit cast from "vec4" to "float"
0(63) : error C7011: implicit cast from "vec4" to "float"
0(64) : error C7011: implicit cast from "vec4" to "float"

我几乎可以通过注释掉struct LightBaseand的声明来重现您看到的错误——除了它们出现在第 33和struct MaterialBase34 行。lights_amaterial

这让我相信你的问题是你实际上并没有编译你认为的程序。也许您正在将它从文件读取到内存中,但是在您调用 glShaderSource 之前,内存会以某种方式损坏......

于 2013-03-10T20:14:05.767 回答