Uniform.hh 7.38 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>
25
#include <vector>
Robert Menzel's avatar
Robert Menzel committed
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;
};

Robert Menzel's avatar
Robert Menzel committed
40
ACGL_SMARTPOINTER_TYPEDEFS(Uniform)
41
42
43
44

// ================================================================================================= \/
// ===================================================================================== 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
// ===================================================================================================== \/
// ===================================================================================== IMPLEMENTATIONS \/
// ===================================================================================================== \/
class Uniform1i : public UniformData<GLint>, public Uniform
{
public:
    Uniform1i(void) : UniformData<GLint>() {}
    virtual ~Uniform1i(void) {}
    void apply(GLint _location) const { glUniform1i(_location, mValue); }
};

Robert Menzel's avatar
Robert Menzel committed
91
ACGL_SMARTPOINTER_TYPEDEFS(Uniform1i)
92
93
94
95

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

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); }
};

Robert Menzel's avatar
Robert Menzel committed
103
ACGL_SMARTPOINTER_TYPEDEFS(Uniform1f)
104
105
106

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

107
class Uniform1fv : public UniformData<std::vector<GLfloat> >, public Uniform
108
109
110
111
112
113
114
115
116
117
118
119
120
121
{
public:
    virtual ~Uniform1fv(void) {}
    void apply(GLint _location) const
    {
        if(!mValue.empty())
            glUniform1fv(_location, (GLsizei)mValue.size(), (GLfloat*)&mValue[0]);
    }
};

ACGL_SMARTPOINTER_TYPEDEFS(Uniform1fv)

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

122
123
124
125
126
127
128
129
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)); }
};

Robert Menzel's avatar
Robert Menzel committed
130
ACGL_SMARTPOINTER_TYPEDEFS(Uniform2f)
131

132

133
134
135
136
137
138
139
140
141
142
//=========================

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)); }
};

Robert Menzel's avatar
Robert Menzel committed
143
ACGL_SMARTPOINTER_TYPEDEFS(Uniform3f)
144
145
146
147
148
149
150
151
152
153
154

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

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)); }
};

Robert Menzel's avatar
Robert Menzel committed
155
ACGL_SMARTPOINTER_TYPEDEFS(Uniform4f)
156
157
158
159
160
161
162
163
164
165
166

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

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)); }
};

Robert Menzel's avatar
Robert Menzel committed
167
ACGL_SMARTPOINTER_TYPEDEFS(UniformMatrix2f)
168
169
170
171
172
173
174
175
176
177
178

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

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)); }
};

Robert Menzel's avatar
Robert Menzel committed
179
ACGL_SMARTPOINTER_TYPEDEFS(UniformMatrix3f)
180
181
182
183
184
185
186
187
188
189
190

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

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)); }
};

Robert Menzel's avatar
Robert Menzel committed
191
ACGL_SMARTPOINTER_TYPEDEFS(UniformMatrix4f)
192
193
194
195
196
197
198
199
200
201
202

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

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); }
};

Robert Menzel's avatar
Robert Menzel committed
203
ACGL_SMARTPOINTER_TYPEDEFS(UniformTexture)
204

Robert Menzel's avatar
Robert Menzel committed
205
} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
206
207
} // ACGL

208
#endif // ACGL_OPENGL_OBJECTS_UNIFORM_HH