ShaderProgram.cc 1.79 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

#include <ACGL/Resource/ShaderProgram.hh>
#include <ACGL/GLUtils/Tools.hh>
#include <ACGL/Utils/Log.hh>

using namespace ACGL::GLUtils;
using namespace ACGL::GLUtils::Tools;
using namespace ACGL::Utils;
using namespace ACGL::Resource;

void ShaderProgram::bindAttributeLocations() const
{
    for (SharedShaderVec::size_type i = 0; i < mShaders.size(); ++i)
    {
        const Shader::AttributeVec& attribs = mShaders[i]->getAttributes();
        for (Shader::AttributeVec::size_type k = 0; k < attribs.size(); ++k)
        {
            glBindAttribLocation(mContext, k, attribs[k].c_str());
        }
    }
}

bool ShaderProgram::link(void) const
{
    bindAttributeLocations();

    glLinkProgram(mContext);

    if ( openGLRareErrorOccured() )
    {
        // it's uncommon that this fails, compile errors (the most common errors)
        // are not checked here!
        return false;
    }

    // check for shader compile errors:
    GLint error;
    glGetProgramiv(mContext, GL_LINK_STATUS, &error);

    if (error != GL_TRUE)
    {
        // yes, errors :-(
        GLsizei length = 0;
        glGetProgramiv(mContext, GL_INFO_LOG_LENGTH, &length);
        if (length > 1)
        {
            GLchar* pInfo = new char[length + 1];
            glGetProgramInfoLog(mContext,  length, &length, pInfo);
            openGLRareError();
            message() << "Linker Error: " << std::string(pInfo) << std::endl;
            delete[] pInfo;
            return false;
        }
    }
    return true;
}