Shader.cc 3.95 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
8
#include <ACGL/OpenGL/Objects/Shader.hh>
#include <ACGL/OpenGL/Tools.hh>
#include <ACGL/Base/StringOperations.hh>
Robert Menzel's avatar
Robert Menzel committed
9
10
11
12

#include <iostream>
#include <fstream>

13
14
#include <tr1/regex>

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

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)
{
    std::istringstream stream(_source);
    std::string line = "";

    mAttributes.clear();

    while (stream.good())
    {
53
        std::getline(stream, line);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

        if(this->mType == GL_VERTEX_SHADER)
        {
            if(StringOperations::startsWith(line, "in"))
            {
                std::vector<std::string> tokens = StringOperations::split(line, ' ');
                if(tokens.size() > 2)
                {
                    std::vector<std::string> tokensOfToken = StringOperations::split(tokens[2], ';');
                    if(tokensOfToken.size() > 0)
                    {
                        mAttributes.push_back(tokensOfToken[0]);
                    }
                }
            }
        }
        else if(this->mType == GL_FRAGMENT_SHADER)
Robert Menzel's avatar
Robert Menzel committed
71
        {
72
            if(StringOperations::startsWith(line, "out"))
Robert Menzel's avatar
Robert Menzel committed
73
            {
74
75
                std::vector<std::string> tokens = StringOperations::split(line, ' ');
                if(tokens.size() > 2)
Robert Menzel's avatar
Robert Menzel committed
76
                {
77
78
79
80
81
                    std::vector<std::string> tokensOfToken = StringOperations::split(tokens[2], ';');
                    if(tokensOfToken.size() > 0)
                    {
                        mFragmentData.push_back(tokensOfToken[0]);
                    }
Robert Menzel's avatar
Robert Menzel committed
82
83
84
85
86
87
88
89
90
                }
            }
        }
    }
    return compile(_source.c_str());
}

bool Shader::compile(const char* _pProgramText) const
{
91
    glShaderSource(mContext, 1, &_pProgramText, NULL); // can't create OpenGL errors
Robert Menzel's avatar
Robert Menzel committed
92
93
    glCompileShader(mContext);
    
94
95
96
97
98
#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
99
    {
100
        error() << "glCompileShader failed, that can only mean, that the context used is not a valid shader object!" << std::endl;
Robert Menzel's avatar
Robert Menzel committed
101
102
103
104
105
106
107
108
109
110
111
112
        return false;
    }

    // check for shader compile errors:
    GLint shaderError;
    glGetShaderiv(mContext, GL_COMPILE_STATUS, &shaderError);
    if(shaderError != GL_TRUE)
    {
        // yes, errors
        error() << "Shader compile error: " << std::endl;
    }

113
    // a log gets always printed (could be warnings)
Robert Menzel's avatar
Robert Menzel committed
114
115
116
117
118
119
120
121
122
123
    GLsizei length = 0;
    glGetShaderiv(mContext, GL_INFO_LOG_LENGTH, &length);
    if(length > 1)
    {
        // a compile log can get produced even if there were no errors, but warnings!
        GLchar* pInfo = new char[length + 1];
        glGetShaderInfoLog(mContext,  length, &length, pInfo);
        error() << "Compile log: " << std::string(pInfo) << std::endl;
        delete[] pInfo;
    }
124
    openGLRareError(); // glGetShader- calls normaly shoudn't create errors
Robert Menzel's avatar
Robert Menzel committed
125
    return (shaderError == GL_TRUE); // return true if we encountered no errors
126
127
128
#else
    return true; // if no error checking was done we assume it went ok
#endif
Robert Menzel's avatar
Robert Menzel committed
129
}