Shader.cc 2.71 KB
Newer Older
1
2
3
4
5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
Robert Menzel's avatar
Robert Menzel committed
6

Robert Menzel's avatar
Robert Menzel committed
7
8
9
#include <ACGL/OpenGL/Objects/Shader.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/Base/StringOperations.hh>
Robert Menzel's avatar
Robert Menzel committed
10
11
12
13

#include <iostream>
#include <fstream>

Robert Menzel's avatar
Robert Menzel committed
14
using namespace ACGL::Base;
Robert Menzel's avatar
Robert Menzel committed
15
using namespace ACGL::Utils;
Robert Menzel's avatar
Robert Menzel committed
16
using namespace ACGL::OpenGL;
Robert Menzel's avatar
Robert Menzel committed
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

bool Shader::setFromFile(const std::string& _filename)
{
    std::string line = "";
    std::string fileContent = "";
    
    std::ifstream fileStream(_filename.c_str(), std::ifstream::in);
    
    if(fileStream.is_open())
    {
        while (fileStream.good())
        {
            std::getline(fileStream,line);
            fileContent += line + "\n";
        }
        fileStream.close();
    }
    else
    {
        error() << "Failed to open file: " << _filename << std::endl;
        return false;
    }
    
    return setSource(fileContent);
}

bool Shader::setSource(const std::string& _source)
{
    return compile(_source.c_str());
}

bool Shader::compile(const char* _pProgramText) const
{
50
51
    glShaderSource(mObjectName, 1, &_pProgramText, NULL); // can't create OpenGL errors
    glCompileShader(mObjectName);
Robert Menzel's avatar
Robert Menzel committed
52
    
53
54
55
56
57
#ifdef ACGL_CHECK_CRITICAL_GL_ERRORS
    // problems with the shader creation are always a bad thing...
    // from here on only error checks are performed:
    //
    if ( openGLRareErrorOccured() )
Robert Menzel's avatar
Robert Menzel committed
58
    {
59
        error() << "glCompileShader failed, that can only mean, that the object name used is not a valid shader object!" << std::endl;
Robert Menzel's avatar
Robert Menzel committed
60
61
62
63
64
        return false;
    }

    // check for shader compile errors:
    GLint shaderError;
65
    glGetShaderiv(mObjectName, GL_COMPILE_STATUS, &shaderError);
Robert Menzel's avatar
Robert Menzel committed
66
67
68
69
70
71
    if(shaderError != GL_TRUE)
    {
        // yes, errors
        error() << "Shader compile error: " << std::endl;
    }

72
    // a log gets always printed (could be warnings)
Robert Menzel's avatar
Robert Menzel committed
73
    GLsizei length = 0;
74
    glGetShaderiv(mObjectName, GL_INFO_LOG_LENGTH, &length);
Robert Menzel's avatar
Robert Menzel committed
75
76
77
78
    if(length > 1)
    {
        // a compile log can get produced even if there were no errors, but warnings!
        GLchar* pInfo = new char[length + 1];
79
        glGetShaderInfoLog(mObjectName,  length, &length, pInfo);
Robert Menzel's avatar
Robert Menzel committed
80
81
82
        error() << "Compile log: " << std::string(pInfo) << std::endl;
        delete[] pInfo;
    }
83
    openGLRareError(); // glGetShader- calls normaly shoudn't create errors
Robert Menzel's avatar
Robert Menzel committed
84
    return (shaderError == GL_TRUE); // return true if we encountered no errors
85
86
87
#else
    return true; // if no error checking was done we assume it went ok
#endif
Robert Menzel's avatar
Robert Menzel committed
88
}