GLSL:链接后没有活动的制服/属性

时间:2013-08-28 13:07:31

标签: c++ opengl glsl

将程序与附加的顶点和片段着色器连接起来并用值输入后,我发现没有任何反应,即没有渲染。经过一些记录,我发现该程序找不到我的任何制服。经过一些记录,我发现我的程序中没有活动的制服或属性。我也仔细检查了着色器源,并且所有变量都用于fo'sho(从而消除了任何“优化”)。我还检查了源解析,它也很好。因此,我不知道造成这种情况的原因。

这是我的解析/编译功能:

int _LoadProgramFromSource(GLuint shader,const char* filename)
{
    std::ifstream File;
    File.open(filename, std::ios::in);

    //check if file is valid
    if (!File)
    {
        g_Log.PrintError("Creating shader %s - file not found",filename);
        return -1;
    }

    const GLint len = _getFileLenght(File);
    if(len == 0) return 0;

    std::vector<std::string> lines;

    lines.push_back("#version 140"); // GLSL version control
    while(!File.eof()) {
        std::string in = "";
        std::getline(File, in);
        if(strcmp(in.substr(0,8).c_str(),"#version")) lines.push_back(in.c_str());
    }

    std::string fm = "";
    for(int i = 0; i < lines.size(); ++i) {
        std::cout << lines[i] << std::endl;
        fm += lines[i];
        if(fm[fm.size()-1]) fm += "\0";
    }

    const GLchar* fileMemory = fm.c_str();

    glShaderSource(shader, 1, &fileMemory, NULL); CHECK_GL_ERROR;
    glCompileShader(shader); CHECK_GL_ERROR;

    int status;
    glGetShaderiv(shader, GL_COMPILE_STATUS, &status); CHECK_GL_ERROR;
    if(status == GL_TRUE) {
        g_Log.PrintInfo("Shader %s compiled successfully.", filename);
        printLog(shader);
        return 0;
    }
    else {
        g_Log.PrintError("Shader %s could not compile.", filename);
        printLog(shader);
        return -1;
    }

}

这是关联的一个:

int SetTechnique(unsigned int hTechnique)
{
    GLint asdf; // temp
    m_activeTechnique = hTechnique;


    glLinkProgram(m_techniques[m_activeTechnique].m_program); CHECK_GL_ERROR;

    glGetProgramiv(m_techniques[m_activeTechnique].m_program,GL_LINK_STATUS,&asdf);
    if(asdf == GL_TRUE) {
        g_Log.PrintInfo("Program %d successfully linked. (%s | %s)",m_techniques[m_activeTechnique].m_program,m_techniques[m_activeTechnique].BufferVShader,m_techniques[m_activeTechnique].BufferPShader);
        glGetProgramiv(m_techniques[m_activeTechnique].m_program,GL_ACTIVE_UNIFORMS,&asdf); CHECK_GL_ERROR;
        g_Log.PrintInfo("Active uniforms: %d",asdf);
        glGetProgramiv(m_techniques[m_activeTechnique].m_program,GL_ACTIVE_ATTRIBUTES,&asdf); CHECK_GL_ERROR;
        g_Log.PrintInfo("Active attributes: %d",asdf);
    } else {
        g_Log.PrintError("Program not linked.");
        GLint tmp0;
        glGetProgramiv(m_techniques[m_activeTechnique].m_program, GL_INFO_LOG_LENGTH, &tmp0);
        if(tmp0) {
            GLchar* tmp1 = new GLchar[tmp0];
            GLsizei tmp2;
            glGetProgramInfoLog(m_techniques[m_activeTechnique].m_program, tmp0, &tmp2, tmp1);
            g_Log.PrintError("%s",tmp1);
            g_Log.PrintError("%s",m_techniques[m_activeTechnique].BufferVShader);
            g_Log.PrintError("%s",m_techniques[m_activeTechnique].BufferPShader);
            delete[] tmp1;
        }
    }

    if(m_techniques[m_activeTechnique].m_cull_off)
        glDisable(GL_CULL_FACE);
    else
        glEnable(GL_CULL_FACE);

    if(m_techniques[m_activeTechnique].m_ccwcull)
        glCullFace(GL_FRONT);
    else
        glCullFace(GL_BACK);

    return 0;
}

这是日志中的一个小结果:

Program 120 successfully linked. (media/shaders/ship/LowReflect_VertexShader.vert | media/shaders/ship/LowReflect_PixelShader.frag)
Active uniforms: 0
Active attributes: 0
GLSL: warning! LightDiffuse No such uniform.
GLSL: warning! LightSpecular No such uniform.
GLSL: warning! LightAmbient No such uniform.
GLSL: warning! FogColor No such uniform.
GLSL: warning! FogDensity No such uniform.
GLSL: warning! WorldViewProjMatrix No such uniform.
GLSL: warning! WorldMatrix No such uniform.

......等等。

glxinfo | grep OpenGL
OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: GeForce 9600 GT/PCIe/SSE2
OpenGL core profile version string: 3.3.0 NVIDIA 325.15
OpenGL core profile shading language version string: 3.30 NVIDIA via Cg compiler
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile

顶点着色器:

// glslv output by Cg compiler
// cgc version 3.0.0016, build date Feb 10 2011
// command line args: -profile glslv -po version=140
// source file: LowReflect_VertexShader.cg
//vendor NVIDIA Corporation
//version 3.0.0.16
//profile glslv
//program main
//semantic MatSpecular
//semantic MatDiffuse
//semantic MatSpecularPower
//semantic LightDirection
//semantic CameraPosition
//semantic WorldViewProjMatrix
//semantic WorldMatrix
//var float4 MatSpecular :  : MatSpecular : -1 : 1
//var float4 MatDiffuse :  : MatDiffuse : -1 : 1
//var float MatSpecularPower :  : MatSpecularPower : -1 : 1
//var float4 LightDirection :  : LightDirection : -1 : 1
//var float4 CameraPosition :  : CameraPosition : -1 : 1
//var float4x4 WorldViewProjMatrix :  : WorldViewProjMatrix[0], 4 : -1 : 1
//var float4x4 WorldMatrix :  : WorldMatrix[0], 4 : -1 : 1
//var float4 IN.position : $vin.POSITION : $POSITION : 0 : 1
//var float3 IN.normal : $vin.NORMAL : $NORMAL : 0 : 1
//var float3 IN.tangent : $vin.TEXCOORD4 :  : 0 : 0
//var float3 IN.binormal : $vin.TEXCOORD8 :  : 0 : 0
//var float2 IN.coord : $vin.TEXCOORD0 : $TEXCOORD0 : 0 : 1
//var float2 IN.coord2 : $vin.TEXCOORD1 : $TEXCOORD1 : 0 : 1
//var float4 main.sposition : $vout.POSITION : POSITION : -1 : 1
//var float4 main.diffuse : $vout.TEXCOORD0 : $TEX0 : -1 : 1
//var float4 main.specular : $vout.TEXCOORD1 : $TEX1 : -1 : 1
//var float2 main.coord : $vout.TEXCOORD2 : $TEX2 : -1 : 1
//var float2 main.coord2 : $vout.TEXCOORD3 : $TEX3 : -1 : 1
//var float main.depth : $vout.TEXCOORD4 : $TEX4 : -1 : 1
//var float main.oClip0 : $vout.TEXCOORD5 : $TEX5 : -1 : 1

#version 140

struct Vertex {
    vec4 position;
    vec3 normal1;
    vec3 tangent;
    vec3 binormal;
    vec2 coord1;
    vec2 coord21;
};

struct Low_Fragment {
    vec4 sposition;
    vec4 diffuse;
    vec4 specular;
    vec2 coord;
    vec2 coord2;
    float depth;
    float oClip0;
};

Low_Fragment ret_0;
float TMP3;
float TMP2;
float TMP1;
float TMP0;
vec4 r0013;
vec4 r0023;
float TMP50;
vec3 a0057;
float TMP58;
float y0065;
in vec4 POSITION;
in vec4 NORMAL;
in vec4 TEXCOORD0;
in vec4 TEXCOORD1;
out vec4 TEX0;
out vec4 TEX1;
out vec4 TEX2;
out vec4 TEX3;
out vec4 TEX4;
out vec4 TEX5;
uniform vec4 MatSpecular;
uniform vec4 MatDiffuse;
uniform float MatSpecularPower;
uniform vec4 LightDirection;
uniform vec4 CameraPosition;
uniform mat4 WorldViewProjMatrix;
uniform mat4 WorldMatrix;

 // main procedure, the original name was main
void main()
{

    Low_Fragment OUT;
    vec3 view;
    vec3 normal;
    vec3 Reflect;
    float Diff;
    float Spec;

    r0013.x = dot(WorldViewProjMatrix[0], POSITION);
    r0013.y = dot(WorldViewProjMatrix[1], POSITION);
    r0013.z = dot(WorldViewProjMatrix[2], POSITION);
    r0013.w = dot(WorldViewProjMatrix[3], POSITION);
    r0023.y = dot(WorldMatrix[1], POSITION);
    view = (CameraPosition - POSITION).xyz;
    TMP0 = dot(view, view);
    TMP1 = inversesqrt(TMP0);
    view = TMP1*view;
    TMP0 = dot(NORMAL.xyz, NORMAL.xyz);
    TMP1 = inversesqrt(TMP0);
    normal = TMP1*NORMAL.xyz;
    TMP2 = dot(normal, view);
    Reflect = view - (2.00000000E+00*normal)*TMP2;
    Diff = dot(LightDirection.xyz, normal);
    TMP3 = min(1.00000000E+00, Diff);
    TMP50 = max(0.00000000E+00, TMP3);
    OUT.diffuse = TMP50*MatDiffuse;
    a0057 = (-LightDirection).xyz;
    Spec = dot(a0057, Reflect);
    TMP3 = min(1.00000000E+00, Spec);
    TMP58 = max(0.00000000E+00, TMP3);
    y0065 = float(int(MatSpecularPower));
    Spec = pow(TMP58, y0065);
    OUT.specular = Spec*MatSpecular;
    ret_0.sposition = r0013;
    ret_0.diffuse = OUT.diffuse;
    ret_0.specular = OUT.specular;
    ret_0.coord = TEXCOORD0.xy;
    ret_0.coord2 = TEXCOORD1.xy;
    ret_0.depth = r0013.z;
    ret_0.oClip0 = r0023.y;
    gl_Position = r0013;
    TEX0 = OUT.diffuse;
    TEX1 = OUT.specular;
    TEX2.xy = TEXCOORD0.xy;
    TEX3.xy = TEXCOORD1.xy;
    TEX4.x = r0013.z;
    TEX5.x = r0023.y;
    return;
}

片段着色器:

// glslf output by Cg compiler
// cgc version 3.0.0016, build date Feb 10 2011
// command line args: -profile glslf -po version=140
// source file: LowReflect_PixelShader.cg
//vendor NVIDIA Corporation
//version 3.0.0.16
//profile glslf
//program main
//semantic MatAmbient
//semantic LightDiffuse
//semantic LightSpecular
//semantic LightAmbient
//semantic FogColor
//semantic FogDensity
//semantic SampDiffuse : TEXUNIT0
//semantic SampSpecular : TEXUNIT1
//semantic SampLight : TEXUNIT3
//var float4 MatAmbient :  : MatAmbient : -1 : 1
//var float4 LightDiffuse :  : LightDiffuse : -1 : 1
//var float4 LightSpecular :  : LightSpecular : -1 : 1
//var float4 LightAmbient :  : LightAmbient : -1 : 1
//var float4 FogColor :  : FogColor : -1 : 1
//var float FogDensity :  : FogDensity : -1 : 1
//var sampler2D SampDiffuse : TEXUNIT0 : SampDiffuse 0 : -1 : 1
//var sampler2D SampSpecular : TEXUNIT1 : SampSpecular 1 : -1 : 1
//var sampler2D SampLight : TEXUNIT3 : SampLight 3 : -1 : 1
//var float4 IN.diffuse : $vin.TEXCOORD0 : $TEX0 : 0 : 1
//var float4 IN.specular : $vin.TEXCOORD1 : $TEX1 : 0 : 1
//var float2 IN.coord : $vin.TEXCOORD2 : $TEX2 : 0 : 1
//var float2 IN.coord2 : $vin.TEXCOORD3 : $TEX3 : 0 : 1
//var float IN.depth : $vin.TEXCOORD4 : $TEX4 : 0 : 1
//var float IN.oClip0 : $vin.TEXCOORD5 : $TEX5 : 0 : 1
//var float4 main.color : $vout.COLOR : $COL0 : -1 : 1

#version 140

struct Low_Fragment {
    vec4 diffuse;
    vec4 specular;
    vec2 coord;
    vec2 coord2;
    float depth;
    float oClip0;
};

struct Low_Pixel {
    vec4 color;
};

Low_Pixel ret_0;
float TMP1;
vec4 TMP0;
float c0016;
float TMP21;
float t0026;
in vec4 TEX0;
in vec4 TEX1;
in vec4 TEX2;
in vec4 TEX3;
in vec4 TEX4;
in vec4 TEX5;
out vec4 COL0;
uniform vec4 MatAmbient;
uniform vec4 LightDiffuse;
uniform vec4 LightSpecular;
uniform vec4 LightAmbient;
uniform vec4 FogColor;
uniform float FogDensity;
uniform sampler2D SampDiffuse;
uniform sampler2D SampSpecular;
uniform sampler2D SampLight;

 // main procedure, the original name was main
void main()
{

    Low_Pixel OUT;
    vec4 DiffuseTex;
    vec4 SpecularTex;
    vec4 Diffuse;
    vec4 Specular;
    vec4 Ambient;
    float FogFactor;

    if (TEX5.x < 0.00000000E+00) { // if begin
        discard;
    } // end if
    DiffuseTex = texture(SampDiffuse, TEX2.xy);
    c0016 = DiffuseTex.w - 8.00000012E-01;
    if (c0016 < 0.00000000E+00) { // if begin
        discard;
    } // end if
    SpecularTex = texture(SampSpecular, TEX2.xy);
    TMP0 = texture(SampLight, TEX3.xy);
    DiffuseTex.xyz = DiffuseTex.xyz*TMP0.xxx;
    SpecularTex.xyz = SpecularTex.xyz*TMP0.xxx;
    Diffuse = TEX0*DiffuseTex*LightDiffuse;
    Specular = TEX1*SpecularTex*LightSpecular;
    Ambient = MatAmbient*DiffuseTex*LightAmbient;
    OUT.color = Diffuse + Specular + Ambient;
    OUT.color.w = DiffuseTex.w;
    TMP1 = TEX4.x*FogDensity*TEX4.x*FogDensity;
    TMP21 = pow(2.71828198E+00, TMP1);
    FogFactor = 1.00000000E+00/TMP21;
    t0026 = 1.00000000E+00 - FogFactor;
    OUT.color.xyz = OUT.color.xyz + t0026*(FogColor.xyz - OUT.color.xyz);
    ret_0.color = OUT.color;
    COL0 = OUT.color;
    return;
} // main end

如果您需要,我会即时提供其他信息。提前谢谢。

1 个答案:

答案 0 :(得分:1)

我没有详细解释为什么你的加载代码很糟糕,我只是提供正确的代码来执行代码尝试和无法执行的操作。

std::string shader;
{
  std::ifstream File(filename, std::ios::in);
  if(File)
    shader.assign(std::istreambuf_iterator<char>(File), std::istreambuf_iterator<char>());
}

这不会为着色器添加#version前缀,但这已经在着色器文件中。 istreambuf_iterator类位于<iterator>标题中。