Uniform.hh 6.94 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.                                                       //
////////////////////////////////////////////////////////////////////////////////

6
7
#ifndef ACGL_OPENGL_OBJECTS_UNIFORM_HH
#define ACGL_OPENGL_OBJECTS_UNIFORM_HH
Robert Menzel's avatar
Robert Menzel committed
8

Robert Menzel's avatar
Robert Menzel committed
9
10
11
12
13
14
15
16
17
18
/*
 * Uniforms do not map to OpenGL objects directly, to set a uniform value in a
 * ShaderProgram, use the setUniform() / setProgramUniform() methods there with
 * the raw values you want to ste.
 *
 * This class provides the ability to save uniform values on the client side and
 * wait with the uploading until the ShaderProgram gets used. It gets used in
 * combination with the ShaderProgramObject/RenderObject (see there).
 */

Robert Menzel's avatar
Robert Menzel committed
19
20
#include <ACGL/ACGL.hh>

21
#include <ACGL/Base/Macros.hh>
Robert Menzel's avatar
Robert Menzel committed
22
#include <ACGL/OpenGL/GL.hh>
Robert Menzel's avatar
Robert Menzel committed
23
#include <ACGL/Math/Math.hh>
24
#include <ACGL/OpenGL/Objects/Texture.hh>
Robert Menzel's avatar
Robert Menzel committed
25
26
27


namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
28
namespace OpenGL{
Robert Menzel's avatar
Robert Menzel committed
29

30
31
32
33
// ================================================================================================== \/
// ================================================================================== BASIC INTERFACE \/
// ================================================================================================== \/
class Uniform
Robert Menzel's avatar
Robert Menzel committed
34
35
{
public:
36
    //! Apply the uniform to a specified location
Robert Menzel's avatar
Robert Menzel committed
37
38
39
    virtual void apply(GLint) const = 0;
};

40
41
42
43
44
ACGL_SHARED_TYPEDEF(Uniform)

// ================================================================================================= \/
// ===================================================================================== C-SIDE DATA \/
// ================================================================================================= \/
Robert Menzel's avatar
Robert Menzel committed
45
template<typename T>
46
class UniformData
Robert Menzel's avatar
Robert Menzel committed
47
{
48
49
50
51
52
53
    // ===================================================================================================== \/
    // ============================================================================================ TYPEDEFS \/
    // ===================================================================================================== \/
public:
    typedef T DATA_TYPE;

Robert Menzel's avatar
Robert Menzel committed
54
55
56
57
    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
58
    UniformData(void) : mValue() {}
Robert Menzel's avatar
Robert Menzel committed
59
60
61
62
63
64
65
66
67
68
69
70
71
    virtual ~UniformData(void) {}

    // ==================================================================================================== \/
    // ============================================================================================ SETTERS \/
    // ==================================================================================================== \/
public:
    inline void setValue (const T& _value) { mValue = _value; }

    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
public:
    inline const T& getValue (void) const { return mValue; }
72

Robert Menzel's avatar
Robert Menzel committed
73
74
75
76
77
78
79
    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
protected:
    T mValue;
};

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// ===================================================================================================== \/
// ===================================================================================== IMPLEMENTATIONS \/
// ===================================================================================================== \/
class Uniform1i : public UniformData<GLint>, public Uniform
{
public:
    Uniform1i(void) : UniformData<GLint>() {}
    virtual ~Uniform1i(void) {}
    void apply(GLint _location) const { glUniform1i(_location, mValue); }
};

ACGL_SHARED_TYPEDEF(Uniform1i)

//=========================

class Uniform1f : public UniformData<GLfloat>, public Uniform
Robert Menzel's avatar
Robert Menzel committed
96
97
{
public:
98
    Uniform1f(void) : UniformData<GLfloat>() {}
Robert Menzel's avatar
Robert Menzel committed
99
100
101
102
    virtual ~Uniform1f(void) {}
    void apply(GLint _location) const { glUniform1f(_location, mValue); }
};

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
ACGL_SHARED_TYPEDEF(Uniform1f)

//=========================

class Uniform2f : public UniformData<glm::vec2>, public Uniform
{
public:
    Uniform2f(void) : UniformData<glm::vec2>() {}
    virtual ~Uniform2f(void) {}
    void apply(GLint _location) const { glUniform2fv(_location, 1, glm::value_ptr(mValue)); }
};

ACGL_SHARED_TYPEDEF(Uniform2f)

//=========================

class Uniform3f : public UniformData<glm::vec3>, public Uniform
{
public:
    Uniform3f(void) : UniformData<glm::vec3>() {}
    virtual ~Uniform3f(void) {}
    void apply(GLint _location) const { glUniform3fv(_location, 1, glm::value_ptr(mValue)); }
};

ACGL_SHARED_TYPEDEF(Uniform3f)

//=========================

class Uniform4f : public UniformData<glm::vec4>, public Uniform
{
public:
    Uniform4f(void) : UniformData<glm::vec4>() {}
    virtual ~Uniform4f(void) {}
    void apply(GLint _location) const { glUniform4fv(_location, 1, glm::value_ptr(mValue)); }
};

ACGL_SHARED_TYPEDEF(Uniform4f)

//=========================

class UniformMatrix2f : public UniformData<glm::mat2>, public Uniform
{
public:
    UniformMatrix2f(void) : UniformData<glm::mat2>() {}
    virtual ~UniformMatrix2f(void) {}
    void apply(GLint _location) const { glUniformMatrix2fv(_location, 1, GL_FALSE, glm::value_ptr(mValue)); }
};

ACGL_SHARED_TYPEDEF(UniformMatrix2f)

//=========================

class UniformMatrix3f : public UniformData<glm::mat3>, public Uniform
{
public:
    UniformMatrix3f(void) : UniformData<glm::mat3>() {}
    virtual ~UniformMatrix3f(void) {}
    void apply(GLint _location) const { glUniformMatrix3fv(_location, 1, GL_FALSE, glm::value_ptr(mValue)); }
};

ACGL_SHARED_TYPEDEF(UniformMatrix3f)

//=========================

class UniformMatrix4f : public UniformData<glm::mat4>, public Uniform
{
public:
    UniformMatrix4f(void) : UniformData<glm::mat4>() {}
    virtual ~UniformMatrix4f(void) {}
    void apply(GLint _location) const { glUniformMatrix4fv(_location, 1, GL_FALSE, glm::value_ptr(mValue)); }
};

ACGL_SHARED_TYPEDEF(UniformMatrix4f)

//=========================

class UniformTexture : public UniformData<ConstSharedTexture>
{
public:
    UniformTexture(void) : UniformData<ConstSharedTexture>() {}
    virtual ~UniformTexture(void) {}
    void apply(GLint _location, GLenum _unit) const { glUniform1i(_location, _unit); mValue->bind(_unit); }
};

ACGL_SHARED_TYPEDEF(UniformTexture)

Robert Menzel's avatar
Robert Menzel committed
189
} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
190
191
} // ACGL

192
#endif // ACGL_OPENGL_OBJECTS_UNIFORM_HH