属性被编译器删除,但我不明白为什么

时间:2015-12-07 13:24:08

标签: opengl shader

我正在尝试编写一组着色器来执行具有多个灯光和纹理的每顶点照明。我想我正在使用着色器编译器优化'v_texture',这是我的一个属性变量,因为glVertexAttribLocation无法找到它。

这是我的顶点着色器代码:

attribute vec3 v_position;
attribute vec3 v_normal;
attribute vec2 v_texture;

varying vec4 color;
varying vec2 texCoord;

const int MAX_LIGHTS = 8;

uniform struct lightSource
    {
    vec4 position;
    vec4 color;
    vec3 coneDirection;
    float coneAngle;
    float ambientFactor;
    } sceneLights[MAX_LIGHTS];

uniform mat4 modelView;
uniform mat4 Projection;
uniform float shininess;
uniform int numLights;

vec4 applyLight(lightSource light)
{
vec4 outColor;
float attenuation;

vec3 surfacePos = (modelView * vec4(v_position.xyz, 1.0)).xyz;

vec3 toLight;

if(light.position.w == 0.0)
    {
    toLight = normalize(light.position.xyz);
    attenuation = 1.0;
    }

else
    {
    toLight = normalize(light.position.xyz - surfacePos);

    float distanceToLight = length(light.position.xyz - surfacePos);
    float lightAngle = degrees(acos(dot(-surfacePos, normalize(light.coneDirection))));

    if(lightAngle > light.coneAngle)
        {
        attenuation = 0.0;
        }

    else
        {
        attenuation = 1.0/(1.0 + (0.1 * pow(distanceToLight, 2.0)));
        }
    }

vec3 Eye = normalize(-surfacePos);
vec3 Halfway = normalize(toLight + Eye);

vec3 worldNormal = normalize(modelView * vec4(v_normal, 0.0)).xyz;

vec4 ambient = vec4(light.ambientFactor * vec3(light.color.xyz), 1.0);

float Kd = max(dot(toLight, worldNormal), 0.0);
vec4 diffuse = Kd * light.color;

float Ks = pow(max(dot(worldNormal, Halfway), 0.0), shininess);
vec4 specular = Ks * light.color;
if(dot(toLight, worldNormal) < 0.0)
    {
    specular = vec4(0.0, 0.0, 0.0, 0.0);
    }

outColor = ambient + (attenuation * (diffuse + specular));
outColor.a = 1.0;
return outColor;
}

void main(void)
{
vec4 colorSum;

colorSum = vec4(0, 0, 0, 0);

for(int i = 0; i < MAX_LIGHTS; i++)
    {
    if(i >= numLights)
        {
        break;
        }
    colorSum += applyLight(sceneLights[i]);  
    }

colorSum.xyzw = normalize(colorSum.xyzw);

vec3 gammaCorrection = vec3(1.0/2.2);
color = vec4(pow(vec3(colorSum.xyz), gammaCorrection), 1.0);

texCoord = v_texture;

gl_Position = Projection * modelView * vec4(v_position.xyz, 1.0);
}

片段着色器:

varying vec4 color;
varying vec2 texCoord;

uniform sampler2D texSampler;

void main(void)
    {
    gl_FragColor = (color * texture2D(texSampler, texCoord.xy));
    }

我从上到下都是代码,但我无法看到问题出在哪里。我将v_texture分配给texCoord,并将其传递给frag着色器,在那里我将它与顶点着色器的最终光照结果一起使用以产生最终颜色。当我编译它时,我正在检查着色器错误,而我没有得到任何东西。我现在唯一的猜测是,gl_FragColor =(color * texture2D(texSampler,texCoord.xy))可能不是一个有效的语句,但那不应该抓住它吗?

glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &shader_status);
if (!shader_status) 
    {
    GLchar InfoLog[1024];
    glGetShaderInfoLog(fragment_shader, sizeof(InfoLog), NULL, InfoLog);
    fprintf(stderr, "Fragment Shader %d: '%s'\n", fragment_shader, InfoLog);
    }

编辑:我可能应该首先将它放在这里,但是这个片段是我的C ++程序的initialize()函数中的着色器部分。 glGetAttribLocation调用失败的唯一一个是v_texture。

// Begin shader loading.
    //compile the shaders
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
GLint shader_status;
pass=new char [2048];
unsigned int len;

//Give a maximum number of attempts to load the vertex shader 
int attempts=10;
//Load the vertex shader
do
    {

    loader.load("lighting.vsh");

    shaderCode = loader.hold.c_str();
    len=loader.length;
    pass=shaderCode;


    attempts-=1;
    }
while(len!=pass.length()&& attempts>0);

//Pass the temperary variable to a pointer
tmp = pass.c_str();


// Vertex shader first
glShaderSource(vertex_shader, 1,&tmp, NULL);
glCompileShader(vertex_shader);
//check the compile status
glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &shader_status);
if (!shader_status) 
    {
    GLchar InfoLog[1024];
    glGetShaderInfoLog(vertex_shader, sizeof(InfoLog), NULL, InfoLog);
    fprintf(stderr, "Vertex Shader %d: '%s'\n", vertex_shader, InfoLog);
    }

const char *fs=loader.load("lighting.fsh");


// Now the Fragment shader
glShaderSource(fragment_shader, 1, &fs, NULL);
glCompileShader(fragment_shader);
//check the compile status
glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &shader_status);
if (!shader_status) 
    {
    GLchar InfoLog[1024];
    glGetShaderInfoLog(fragment_shader, sizeof(InfoLog), NULL, InfoLog);
    fprintf(stderr, "Fragment Shader %d: '%s'\n", fragment_shader, InfoLog);
    }

//Now we link the 2 shader objects into a program
//This program is what is run on the GPU
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
//check if everything linked ok
glGetProgramiv(program, GL_LINK_STATUS, &shader_status);
  if(!shader_status)
    {
    std::cerr << "[F] THE SHADER PROGRAM FAILED TO LINK" << std::endl;
    return false;
    }

//Now we set the locations of the attributes and uniforms
//this allows us to access them easily while rendering
loc_position = glGetAttribLocation(program,
                const_cast<const char*>("v_position"));

if(loc_position == -1)
    {
    std::cerr << "Error: POSITION NOT FOUND IN SHADER" << std::endl;
    return false;
    }

loc_normals = glGetAttribLocation(program, const_cast<const char*>("v_normal"));

if(loc_normals == -1)
    {
    std::cerr << "Error: NORMALS NOT FOUND IN SHADER" << std:: endl;
    return false;
    }


loc_texture = glGetAttribLocation(program, const_cast<const char*>("v_texture"));

if(loc_texture == -1)
    {
    std::cerr << "[F] TEXTURE NOT FOUND IN SHADER" << std::endl;
    return false;
    }


// Begin light initialization.

0 个答案:

没有答案