Shader.hh 4.77 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

7
8
#ifndef ACGL_OPENGL_OBJECTS_SHADER_HH
#define ACGL_OPENGL_OBJECTS_SHADER_HH
Robert Menzel's avatar
Robert Menzel committed
9

Robert Menzel's avatar
Robert Menzel committed
10
/**
Robert Menzel's avatar
Robert Menzel committed
11
12
13
14
15
16
 * A Shader ist just one OpenGL shader like a fragment or vertex shader. To use these
 * a ShaderProgram is needed that links together multiple Shaders for the different
 * pipelinestages.
 *
 * So normally you want to work with ShaderPrograms instead of Shaders (switch Programs,
 * set uniforms etc).
Philip Trettner's avatar
Philip Trettner committed
17
18
 *
 * Custimizing shader parsing is done via custom ShaderParser classes, see Creator/ShaderParser.hh
Robert Menzel's avatar
Robert Menzel committed
19
20
 */

Robert Menzel's avatar
Robert Menzel committed
21
22
#include <vector>
#include <string>
Robert Menzel's avatar
Robert Menzel committed
23
24
25

#include <ACGL/ACGL.hh>

26
#include <ACGL/Base/Macros.hh>
Robert Menzel's avatar
Robert Menzel committed
27
#include <ACGL/OpenGL/GL.hh>
28
#include <ACGL/OpenGL/Tools.hh>
29
#include <ACGL/OpenGL/Debug.hh>
Philip Trettner's avatar
Philip Trettner committed
30
#include <ACGL/OpenGL/Creator/ShaderParser.hh>
Robert Menzel's avatar
Robert Menzel committed
31
32

namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
33
namespace OpenGL{
Robert Menzel's avatar
Robert Menzel committed
34
35
36

class Shader
{
37
38
    ACGL_NOT_COPYABLE(Shader)

Robert Menzel's avatar
Robert Menzel committed
39
40
41
42
43
    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
    Shader(GLenum _type)
44
    :   mObjectName(0),
Robert Menzel's avatar
Robert Menzel committed
45
46
        mType(_type)
    {
47
        mObjectName = glCreateShader(mType);
48
49
50
        if (mObjectName == 0) {
            ACGL::Utils::error() << "couldn't create Shader object! Requested type = " << (unsigned int) mType << std::endl;
        }
Robert Menzel's avatar
Robert Menzel committed
51
52
53
54
55
    }

    virtual ~Shader(void)
    {
        // "DeleteShader will silently ignore the value zero." - GL Spec
56
        glDeleteShader(mObjectName);
Robert Menzel's avatar
Robert Menzel committed
57
    }
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

    // ===================================================================================================== \/
    // =========================================================================================== KHR_DEBUG \/
    // ===================================================================================================== \/
public:
    // Sets and gets a label visible inside of a OpenGL debugger if KHR_debug is supported at runtime *and*
    // if ACGL_OPENGL_DEBUGGER_SUPPORT was defined during compile time. Does nothing otherwise!
#ifdef ACGL_OPENGL_DEBUGGER_SUPPORT
    void setObjectLabel( const std::string &_label ) { setObjectLabelT<GL_SHADER>(getObjectName(),_label); }
    std::string getObjectLabel() { return getObjectLabelT<GL_SHADER>(getObjectName()); }
#else
    void setObjectLabel( const std::string & ) {}
    std::string getObjectLabel() { return ""; }
#endif

Robert Menzel's avatar
Robert Menzel committed
73
74
75
76
    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
public:
77
78
    inline GLuint getObjectName(void) const { return mObjectName; }
    inline GLenum getType      (void) const { return mType;       }
Robert Menzel's avatar
Robert Menzel committed
79
80
81
82
83

    // ==================================================================================================== \/
    // ============================================================================================ METHODS \/
    // ==================================================================================================== \/
public:
84
    bool setFromFile (SharedShaderParser const& _sp);
85
86
    bool setSource   (const std::string &_source, bool _checkForCompileErrors = true);
    bool setSources  (const std::vector<std::string> &_sources, bool _checkForCompileErrors = true );
Robert Menzel's avatar
Robert Menzel committed
87
88

protected:
89
    // could get reactivated if needed, might get removed later (thus protected):
90
    //bool setFromFileNoImportParsing(const std::string& _filename);
91

92
    bool compile() const;
93
94
95
    // get a log and a bool whether the log contains an error (or just a warning), not done
    // automatically by compile() but called by all public source setting functions:
    void getCompileLog( std::string &_log, bool &_wasErrorLog ) const;
Robert Menzel's avatar
Robert Menzel committed
96
97
98
99
100

    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
protected:
101
    GLuint              mObjectName;
102
    GLenum              mType;
Robert Menzel's avatar
Robert Menzel committed
103
104
};

105

Robert Menzel's avatar
Robert Menzel committed
106
ACGL_SMARTPOINTER_TYPEDEFS(Shader)
Robert Menzel's avatar
Robert Menzel committed
107
108

} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
109
110
} // ACGL

111
#endif // ACGL_OPENGL_OBJECTS_SHADER_HH