ShaderProgram.cc 2.32 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1 2 3 4 5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

Robert Menzel's avatar
Robert Menzel committed
6 7
#include <ACGL/OpenGL/Objects/ShaderProgram.hh>
#include <ACGL/OpenGL/Tools.hh>
Robert Menzel's avatar
Robert Menzel committed
8

Robert Menzel's avatar
Robert Menzel committed
9 10
using namespace ACGL::OpenGL;
using namespace ACGL::Base;
Robert Menzel's avatar
Robert Menzel committed
11 12 13 14
using namespace ACGL::Utils;

void ShaderProgram::bindAttributeLocations() const
{
15
    for (ConstSharedShaderVec::size_type i = 0; i < mShaders.size(); ++i)
Robert Menzel's avatar
Robert Menzel committed
16
    {
17
        for (Shader::AttributeNameVec::size_type k = 0; k < mShaders[i]->getAttributeNames().size(); ++k)
Robert Menzel's avatar
Robert Menzel committed
18
        {
19
            glBindAttribLocation(mContext, k, mShaders[i]->getAttributeNames()[k].c_str());
Robert Menzel's avatar
Robert Menzel committed
20 21 22 23
        }
    }
}

24 25 26 27
void ShaderProgram::bindFragmentDataLocations() const
{
    for (ConstSharedShaderVec::size_type i = 0; i < mShaders.size(); ++i)
    {
28
        for (Shader::FragmentDataNameVec::size_type k = 0; k < mShaders[i]->getFragmentDataNames().size(); ++k)
29
        {
30
            glBindFragDataLocation(mContext, k, mShaders[i]->getFragmentDataNames()[k].c_str());
31 32 33 34
        }
    }
}

Robert Menzel's avatar
Robert Menzel committed
35 36 37
bool ShaderProgram::link(void) const
{
    bindAttributeLocations();
38
    bindFragmentDataLocations();
Robert Menzel's avatar
Robert Menzel committed
39 40 41

    glLinkProgram(mContext);

42
#ifdef ACGL_CHECK_CRITICAL_GL_ERRORS
Robert Menzel's avatar
Robert Menzel committed
43 44
    if ( openGLRareErrorOccured() )
    {
45 46
        // 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
47 48 49
        return false;
    }

50 51 52
    // check for program link errors:
    GLint programError;
    glGetProgramiv(mContext, GL_LINK_STATUS, &programError);
Robert Menzel's avatar
Robert Menzel committed
53

54
    if (programError != GL_TRUE)
Robert Menzel's avatar
Robert Menzel committed
55 56
    {
        // yes, errors :-(
57 58 59 60 61 62 63 64 65 66 67 68 69 70
        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
71
    }
72
#endif
Robert Menzel's avatar
Robert Menzel committed
73 74
    return true;
}