1

After linking program with both vertex and fragment shaders attached and feeding it with values, I noticed that nothing happens at all, i.e. nothing is rendered. After some logging, I found out that the program cannot find any of my uniforms. After some more logging, I found out that there are no active uniforms or attributes in my program. I double checked the shader sources as well, and all the variables are used fo'sho (thus eliminating any "optimalisations"). I also checked the source parsing and it's fine too. I, therefore, have no idea what may be causing this.

This is my parsing/compiling function:

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;
    }

}

And this is the linking one:

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;
}

This is a little substract from the log:

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.

...and so on.

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

Vertex shader:

// 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;
}

Fragment shader:

// 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

I will provide additional information on the fly, if you need any. Thanks in advance.

matuzalem
  • 347
  • 1
  • 2
  • 14

1 Answers1

1

Rather than explain in detail why your loading code is awful, I'll just provide correct code that does what your code is trying and failing to do.

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

This doesn't prefix your shader with #version, but that's already in your shader files. The istreambuf_iterator class is in the <iterator> header.

Nicol Bolas
  • 378,677
  • 53
  • 635
  • 829
  • Thanks man, worked perfectly. Still, I would appreciate an explanation why my code was wrong. Also, calm down a bit, everybody has to start somewhere. – matuzalem Aug 28 '13 at 13:29
  • 2
    I was just reading this earlier today too... http://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring (in short, apparently it's faster to reserve() first) – jozxyqk Aug 28 '13 at 13:30
  • It took me a while to decipher that mess of C++ stream I/O in the original question. I take it the problem here was neglecting to insert newline characters when re-constructing the file line-by-line? – Andon M. Coleman Aug 29 '13 at 09:52
  • 1
    @AndonM.Coleman: Probably. I didn't bother to decipher it, as there was way too much that was wrong. – Nicol Bolas Aug 29 '13 at 09:55
  • 1
    @NicolBolas rather than flaming less experienced programmers for their wrong code, you should provide explanation of what was wrong and why. Your attitude is simply wrong. – matuzalem Sep 02 '13 at 12:30