我有一个片段着色器,它有结构和这些结构的统一。当我尝试编译它们时,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;
}
代码有什么问题?
答案 0 :(得分: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 LightBase
和struct MaterialBase
的声明来重现您看到的错误 - 除了它们出现在第33和34行,它们是带有标记的标记{{ 1}}和lights_a
这让我相信你的问题是你实际上并没有编译你认为自己的程序。也许你正在从文件中读取它到内存中,但是在你调用glShaderSource之前内存会被破坏......