SuperTuxKart
Loading...
Searching...
No Matches
sp_uniform_assigner.hpp
1// SuperTuxKart - a fun racing game with go-kart
2// Copyright (C) 2018 SuperTuxKart-Team
3//
4// This program is free software; you can redistribute it and/or
5// modify it under the terms of the GNU General Public License
6// as published by the Free Software Foundation; either version 3
7// of the License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18#ifndef HEADER_SP_UNIFORM_ASSIGNER_HPP
19#define HEADER_SP_UNIFORM_ASSIGNER_HPP
20
21#include "graphics/gl_headers.hpp"
22#include "utils/no_copy.hpp"
23
24#ifdef DEBUG
25#include "utils/log.hpp"
26#endif
27
28#include <typeinfo>
29#include <typeindex>
30#include <array>
31#include <string>
32#include <vector>
33
34#include <matrix4.h>
35#include <SColor.h>
36#include <vector2d.h>
37#include <vector3d.h>
38
39namespace SP
40{
41
43{
44private:
45 const GLuint m_location;
46
47 const std::type_index m_type;
48
49 mutable bool m_assigned;
50
51public:
52 // ------------------------------------------------------------------------
53 SPUniformAssigner(const std::type_index& ti, GLuint location)
54 : m_location(location), m_type(ti), m_assigned(false) {}
55 // ------------------------------------------------------------------------
56 bool runtimeChecking(const std::type_info& ti) const
57 {
58#ifdef DEBUG
59 if (m_type != ti)
60 {
61 Log::error("SPUniformAssigner", "%s doesn't match %s which is the"
62 " type of this SPUniformAssigner", ti.name(), m_type.name());
63 return false;
64 }
65#endif
66 return true;
67 }
68 // ------------------------------------------------------------------------
69 void getValue(const GLuint& p, irr::core::matrix4& mat) const
70 {
71 if (runtimeChecking(typeid(mat)))
72 {
73#ifndef SERVER_ONLY
74 glGetUniformfv(p, m_location, mat.pointer());
75#endif
76 }
77 }
78 // ------------------------------------------------------------------------
79 void getValue(const GLuint& p, std::array<float, 4>& v) const
80 {
81 if (runtimeChecking(typeid(v)))
82 {
83#ifndef SERVER_ONLY
84 glGetUniformfv(p, m_location, v.data());
85#endif
86 }
87 }
88 // ------------------------------------------------------------------------
89 void getValue(const GLuint& p, irr::core::vector3df& v) const
90 {
91 if (runtimeChecking(typeid(v)))
92 {
93#ifndef SERVER_ONLY
94 glGetUniformfv(p, m_location, &v.X);
95#endif
96 }
97 }
98 // ------------------------------------------------------------------------
99 void getValue(const GLuint& p, irr::core::vector2df& v) const
100 {
101 if (runtimeChecking(typeid(v)))
102 {
103#ifndef SERVER_ONLY
104 glGetUniformfv(p, m_location, &v.X);
105#endif
106 }
107 }
108 // ------------------------------------------------------------------------
109 void getValue(const GLuint& p, float& v) const
110 {
111 if (runtimeChecking(typeid(v)))
112 {
113#ifndef SERVER_ONLY
114 glGetUniformfv(p, m_location, &v);
115#endif
116 }
117 }
118 // ------------------------------------------------------------------------
119 void getValue(const GLuint& p, int& v) const
120 {
121 if (runtimeChecking(typeid(v)))
122 {
123#ifndef SERVER_ONLY
124 glGetUniformiv(p, m_location, &v);
125#endif
126 }
127 }
128 // ------------------------------------------------------------------------
129 void setValue(const irr::core::matrix4& mat) const
130 {
131 if (runtimeChecking(typeid(mat)))
132 {
133#ifndef SERVER_ONLY
134 glUniformMatrix4fv(m_location, 1, GL_FALSE, mat.pointer());
135 m_assigned = true;
136#endif
137 }
138 }
139 // ------------------------------------------------------------------------
140 void setValue(const std::array<float, 4>& v) const
141 {
142 if (runtimeChecking(typeid(v)))
143 {
144#ifndef SERVER_ONLY
145 glUniform4f(m_location, v[0], v[1], v[2], v[3]);
146 m_assigned = true;
147#endif
148 }
149 }
150 // ------------------------------------------------------------------------
151 void setValue(const irr::core::vector3df& v) const
152 {
153 if (runtimeChecking(typeid(v)))
154 {
155#ifndef SERVER_ONLY
156 glUniform3f(m_location, v.X, v.Y, v.Z);
157 m_assigned = true;
158#endif
159 }
160 }
161 // ------------------------------------------------------------------------
162 void setValue(const irr::core::vector2df& v) const
163 {
164 if (runtimeChecking(typeid(v)))
165 {
166#ifndef SERVER_ONLY
167 glUniform2f(m_location, v.X, v.Y);
168 m_assigned = true;
169#endif
170 }
171 }
172 // ------------------------------------------------------------------------
173 void setValue(float v) const
174 {
175 if (runtimeChecking(typeid(v)))
176 {
177#ifndef SERVER_ONLY
178 glUniform1f(m_location, v);
179 m_assigned = true;
180#endif
181 }
182 }
183 // ------------------------------------------------------------------------
184 void setValue(int v) const
185 {
186 if (runtimeChecking(typeid(v)))
187 {
188#ifndef SERVER_ONLY
189 glUniform1i(m_location, v);
190 m_assigned = true;
191#endif
192 }
193 }
194 // ------------------------------------------------------------------------
195 void reset() const
196 {
197 if (m_assigned)
198 {
199 m_assigned = false;
200#ifndef SERVER_ONLY
201 if (m_type == typeid(int))
202 {
203 glUniform1i(m_location, 0);
204 }
205 else if (m_type == typeid(float))
206 {
207 glUniform1f(m_location, 0.0f);
208 }
209 else if (m_type == typeid(irr::core::matrix4))
210 {
211 static const char zeroes[64] = {};
212 glUniformMatrix4fv(m_location, 1, GL_FALSE, (float*)zeroes);
213 }
214 else if (m_type == typeid(std::array<float, 4>))
215 {
216 glUniform4f(m_location, 0.0f, 0.0f, 0.0f,0.0f);
217 }
218 else if (m_type == typeid(irr::core::vector3df))
219 {
220 glUniform3f(m_location, 0.0f, 0.0f, 0.0f);
221 }
222 else if (m_type == typeid(irr::core::vector2df))
223 {
224 glUniform2f(m_location, 0.0f, 0.0f);
225 }
226#endif
227 }
228 }
229};
230
231}
232
233#endif
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Definition: sp_uniform_assigner.hpp:43