gKit2 light
uniforms.cpp
1 
2 #include <cstdio>
3 #include <cassert>
4 
5 #include <set>
6 
7 #include "program.h"
8 #include "uniforms.h"
9 
10 
11 static
12 int location( const GLuint program, const char *uniform, const int array_size= 0 )
13 {
14  if(program == 0)
15  return -1;
16 
17  // recuperer l'identifiant de l'uniform dans le program
18  char error[4096]= { 0 };
19  GLint location= glGetUniformLocation(program, uniform);
20  if(location < 0)
21  {
22  #ifdef GL_VERSION_4_3
23  {
24  char label[1024];
25  glGetObjectLabel(GL_PROGRAM, program, sizeof(label), nullptr, label);
26 
27  sprintf(error, "uniform( %s %u, '%s' ): not found.", label, program, uniform);
28  }
29  #else
30  sprintf(error, "uniform( program %u, '%s'): not found.", program, uniform);
31  #endif
32 
33  static std::set<std::string> log;
34  if(log.insert(error).second == true)
35  // pas la peine d'afficher le message 60 fois par seconde...
36  printf("%s\n", error);
37 
38  return -1;
39  }
40 
41 #ifndef GK_RELEASE
42  // verifier que le program est bien en cours d'utilisation, ou utiliser glProgramUniform, mais c'est gl 4
43  GLuint current;
44  glGetIntegerv(GL_CURRENT_PROGRAM, (GLint *) &current);
45  if(current != program)
46  {
47  #ifdef GL_VERSION_4_3
48  {
49  char label[1024];
50  glGetObjectLabel(GL_PROGRAM, program, sizeof(label), nullptr, label);
51  char labelc[1024];
52  glGetObjectLabel(GL_PROGRAM, current, sizeof(labelc), nullptr, labelc);
53 
54  sprintf(error, "uniform( %s %u, '%s' ): invalid current shader program( %s %u )", label, program, uniform, labelc, current);
55  }
56  #else
57  sprintf(error, "uniform( program %u, '%s' ): invalid current shader program( %u )...", program, uniform, current);
58  #endif
59 
60  printf("%s\n", error);
61  glUseProgram(program);
62  }
63 
64  if(location >= 0 && array_size > 0)
65  {
66  #ifdef GL_VERSION_4_3
67  // verifier que le tableau d'uniform fait la bonne taille...
68  GLuint index= glGetProgramResourceIndex(program, GL_UNIFORM, uniform);
69  if(index != GL_INVALID_INDEX)
70  {
71  GLenum props[]= { GL_ARRAY_SIZE };
72  GLint value= 0;
73  glGetProgramResourceiv(program, GL_UNIFORM, index, 1, props, 1, nullptr, &value);
74  if(value != array_size)
75  {
76  char label[1024];
77  glGetObjectLabel(GL_PROGRAM, program, sizeof(label), nullptr, label);
78 
79  printf("uniform( %s %u, '%s' array [%d] ): invalid array size [%d]...\n", label, location, uniform, value, array_size);
80  }
81  }
82  #endif
83  }
84 #endif
85 
86  return location;
87 }
88 
89 void program_uniform( const GLuint program, const char *uniform, const unsigned int v )
90 {
91  glUniform1ui( location(program, uniform), v );
92 }
93 
94 void program_uniform( const GLuint program, const char *uniform, const std::vector<unsigned>& v )
95 {
96  assert(v.size());
97  glUniform1uiv( location(program, uniform, v.size()), v.size(), v.data() );
98 }
99 
100 void program_uniform( const GLuint program, const char *uniform, const int v )
101 {
102  glUniform1i( location(program, uniform), v );
103 }
104 
105 void program_uniform( const GLuint program, const char *uniform, const std::vector<int>& v )
106 {
107  assert(v.size());
108  glUniform1iv( location(program, uniform, v.size()), v.size(), v.data() );
109 }
110 
111 void program_uniform( const GLuint program, const char *uniform, const float v )
112 {
113  glUniform1f( location(program, uniform), v );
114 }
115 
116 void program_uniform( const GLuint program, const char *uniform, const std::vector<float>& v )
117 {
118  assert(v.size());
119  glUniform1fv( location(program, uniform, v.size()), v.size(), v.data() );
120 }
121 
122 void program_uniform( const GLuint program, const char *uniform, const vec2& v )
123 {
124  glUniform2fv( location(program, uniform), 1, &v.x );
125 }
126 
127 void program_uniform( const GLuint program, const char *uniform, const std::vector<vec2>& v )
128 {
129  assert(v.size());
130  glUniform2fv( location(program, uniform, v.size()), v.size(), &v[0].x );
131 }
132 
133 void program_uniform( const GLuint program, const char *uniform, const vec3& v )
134 {
135  glUniform3fv( location(program, uniform), 1, &v.x );
136 }
137 
138 void program_uniform( const GLuint program, const char *uniform, const std::vector<vec3>& v )
139 {
140  assert(v.size());
141  glUniform3fv( location(program, uniform, v.size()), v.size(), &v[0].x );
142 }
143 
144 void program_uniform( const GLuint program, const char *uniform, const Point& a )
145 {
146  glUniform3fv( location(program, uniform), 1, &a.x );
147 }
148 
149 void program_uniform( const GLuint program, const char *uniform, const std::vector<Point>& a )
150 {
151  assert(a.size());
152  glUniform3fv( location(program, uniform, a.size()), a.size(), &a[0].x );
153 }
154 
155 void program_uniform( const GLuint program, const char *uniform, const Vector& v )
156 {
157  glUniform3fv( location(program, uniform), 1, &v.x );
158 }
159 
160 void program_uniform( const GLuint program, const char *uniform, const std::vector<Vector>& v )
161 {
162  assert(v.size());
163  glUniform3fv( location(program, uniform, v.size()), v.size(), &v[0].x );
164 }
165 
166 void program_uniform( const GLuint program, const char *uniform, const vec4& v )
167 {
168  glUniform4fv( location(program, uniform), 1, &v.x );
169 }
170 
171 void program_uniform( const GLuint program, const char *uniform, const std::vector<vec4>& v )
172 {
173  assert(v.size());
174  glUniform4fv( location(program, uniform, v.size()), v.size(), &v[0].x );
175 }
176 
177 void program_uniform( const GLuint program, const char *uniform, const Color& c )
178 {
179  glUniform4fv( location(program, uniform), 1, &c.r );
180 }
181 
182 void program_uniform( const GLuint program, const char *uniform, const std::vector<Color>& c )
183 {
184  assert(c.size());
185  glUniform4fv( location(program, uniform, c.size()), c.size(), &c[0].r );
186 }
187 
188 void program_uniform( const GLuint program, const char *uniform, const Transform& v )
189 {
190  glUniformMatrix4fv( location(program, uniform), 1, GL_TRUE, v.data() );
191 }
192 
193 void program_uniform( const GLuint program, const char *uniform, const std::vector<Transform>& v )
194 {
195  glUniformMatrix4fv( location(program, uniform, v.size()), v.size(), GL_TRUE, v[0].data() );
196 }
197 
198 void program_use_texture( const GLuint program, const char *uniform, const int unit, const GLuint texture, const GLuint sampler )
199 {
200  // verifie que l'uniform existe
201  int id= location(program, uniform);
202  if(id < 0)
203  return;
204 
205  // selectionne l'unite de texture
206  glActiveTexture(GL_TEXTURE0 + unit);
207  // configure la texture
208  glBindTexture(GL_TEXTURE_2D, texture);
209 
210  // les parametres de filtrage
211  glBindSampler(unit, sampler);
212 
213  // transmet l'indice de l'unite de texture au shader
214  glUniform1i(id, unit);
215 }
void label(Widgets &w, const char *format,...)
cree un texte. meme fonctionnement que printf().
Definition: widgets.cpp:142
bool value(Widgets &w, const char *label, int &value, const int value_min, const int value_max, const int value_step)
valeur editable par increment.
Definition: widgets.cpp:191
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition: text.cpp:140
void program_uniform(const GLuint program, const char *uniform, const std::vector< unsigned > &v)
affecte un tableau de valeurs a un uniform du shader program.
Definition: uniforms.cpp:94
void program_use_texture(const GLuint program, const char *uniform, const int unit, const GLuint texture, const GLuint sampler)
configure le pipeline et le shader program pour utiliser une texture, et des parametres de filtrage,...
Definition: uniforms.cpp:198
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:14
representation d'un point 3d.
Definition: vec.h:21
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition: mat.h:21
const float * data() const
renvoie l'adresse de la premiere valeur de la matrice.
Definition: mat.h:75
representation d'un vecteur 3d.
Definition: vec.h:59
vecteur generique, utilitaire.
Definition: vec.h:131
vecteur generique, utilitaire.
Definition: vec.h:146
vecteur generique 4d, ou 3d homogene, utilitaire.
Definition: vec.h:168