ShaderProgram.cc 2 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
////////////////////////////////////////////////////////////////////////////////
// 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);

33
#ifdef ACGL_CHECK_CRITICAL_GL_ERRORS
Robert Menzel's avatar
Robert Menzel committed
34
35
    if ( openGLRareErrorOccured() )
    {
36
37
        // it's uncommon that glLinkProgram creates errors,
        // linking errors create no gl errors but a GL_LINK_STATUS of GL_FALSE
Robert Menzel's avatar
Robert Menzel committed
38
39
40
        return false;
    }

41
42
43
    // check for program link errors:
    GLint programError;
    glGetProgramiv(mContext, GL_LINK_STATUS, &programError);
Robert Menzel's avatar
Robert Menzel committed
44

45
    if (programError != GL_TRUE)
Robert Menzel's avatar
Robert Menzel committed
46
47
    {
        // yes, errors :-(
48
49
50
51
52
53
54
55
56
57
58
59
60
61
        error() << "Program could not get linked:" << std::endl;
    }

    GLsizei length = 0;
    glGetProgramiv(mContext, GL_INFO_LOG_LENGTH, &length);
    if (length > 1)
    {
        // error log or warnings:
        GLchar* pInfo = new char[length + 1];
        glGetProgramInfoLog(mContext,  length, &length, pInfo);
        openGLRareError();
        warning() << "Linker log: " << std::string(pInfo) << std::endl;
        delete[] pInfo;
        return (programError == GL_TRUE); // if the log contains only warnings we return true
Robert Menzel's avatar
Robert Menzel committed
62
    }
63
#endif
Robert Menzel's avatar
Robert Menzel committed
64
65
    return true;
}