GLSL着色器不会编译,有人可以帮我找出原因吗?

时间:2015-03-18 21:15:15

标签: c++ opengl glsl

我已经盯着这个太久了,我对GLSL来说太新了,不知道出了什么问题。我所知道的是,在检查顶点着色器是否编译时,它表示它不能这样做。如果有人可以帮助我找出我做错了什么,那就太棒了。

textureShader.vert

#version 140

uniform mat4 mvpMatrix;

attribute vec3 position;
attribute vec2 textCoord;

varying vec2 TextCoord;
varying vec3 lightDir,normal;

void main()
{
    normal = normalize(gl_NormalMatrix * gl_Normal);

    lightDir = normalize(vec3(gl_LightSource[0].position));
    gl_TexCoord[0] = gl_MultiTexCoord0;

    gl_Position = position;
}

textureShader.frag

#version 140

uniform sampler2D texUnit;

varying vec2 TextCoord;
varying vec3 lightDir,normal;

void main()
{
    vec3 ct,cf;
    vec4 texel;
    float intensity,at,af;
    intensity = max(dot(lightDir,normalize(normal)),0.0);

    cf = intensity * (gl_FrontMaterial.diffuse).rgb + gl_FrontMaterial.ambient.rgb;
    af = gl_FrontMaterial.diffuse.a;
    texel = texture2D(texUnit, TextCoord);

    ct = texel.rgb;
    at = texel.a;
    gl_FragColor = vec4(ct * cf, at * af);

}

我正在做什么来检查编译。 DBOUT是一个写入Visual Studio输出框的函数。

glCompileShader(shader_vp);
validateShader(shader_vp, vsFile);

GLint compiled;
glGetShaderiv(shader_vp, GL_COMPILE_STATUS, &compiled);
if (!compiled){
    DBOUT("Couldn't Compile Vertex Shader: Aborting Mission\n");
    abort();
}


glCompileShader(shader_fp);
validateShader(shader_fp, fsFile);

glGetShaderiv(shader_fp, GL_COMPILE_STATUS, &compiled);

if (!compiled){
    DBOUT("Couldn't Compile Fragment Shader: Aborting Mission\n");
    abort();
}

我收到的输出:

Couldn't Compile Vertex Shader: Aborting Mission
Debug Error!

解决

所以每个人都帮忙我去编译。我不得不替换这些行:

    gl_TexCoord[0] = gl_MultiTexCoord0;

    gl_Position = position;

有了这些:

    TextCoord = vec2(textCoord);

    gl_Position = mvpMatrix * vec4(position,1.0f);

谢谢大家!

2 个答案:

答案 0 :(得分:3)

我没有查看您的着色器,但您可以从编译器收到错误消息,例如:

auto error = GLint();

::glGetShaderiv(id, GL_COMPILE_STATUS, &error);

if(error != GL_TRUE)
{       
    auto length = GLint();

    ::glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length);

    if(length > 0)
    {
        auto log = std::vector<GLchar>(length, 0);

        ::glGetShaderInfoLog(id, length, nullptr, &log[0]);

        auto message = std::string(log.begin(), log.end());
        ...
    } 
}

答案 1 :(得分:1)

着色器中可能出现的一个错误可能是:

  

gl_Position = position;

gl_Position应该是vec4类型,但是position是vec3的一个属性,你可能忘了做类似的事情:

  

gl_Position = mvpMatrix * vec4(position,1.0f);