gKit2 light
Files | Functions
utilitaires openGL

Files

file  program.h
 
file  texture.h
 
file  uniforms.h
 

Functions

GLuint read_program (const char *filename, const char *definitions="")
 
int release_program (const GLuint program)
 detruit les shaders et le program. More...
 
int reload_program (const GLuint program, const char *filename, const char *definitions="")
 
int program_format_errors (const GLuint program, std::string &errors)
 renvoie les erreurs de compilation. More...
 
int program_print_errors (const GLuint program)
 affiche les erreurs de compilation. More...
 
bool program_ready (const GLuint program)
 renvoie vrai si le programme est pret. (pas d'erreurs de compilation des shaders, pas d'erreur de link). More...
 
bool program_errors (const GLuint program)
 renvoie vrai si le programme n'est pas pret. More...
 
GLuint make_texture (const int unit, const Image &im, const GLenum texel_type=GL_RGBA32F)
 
GLuint make_texture (const int unit, const ImageData &im, const GLenum texel_type=GL_RGBA)
 
GLuint read_texture (const int unit, const char *filename, const GLenum texel_type=GL_RGBA)
 
int miplevels (const int width, const int height)
 renvoie le nombre de mipmap d'une image width x height. More...
 
int screenshot (const char *filename)
 enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp More...
 
int screenshot (const char *prefix, const int id)
 enregistre le contenu de la fenetre dans un fichier numerote prefixXXX.png. id est le numero de la capture. More...
 
int capture (const char *prefix)
 
GLuint make_texture (const int unit, const int width, const int height, const GLenum texel_type, const GLenum data_format=GL_RGBA, const GLenum data_type=GL_UNSIGNED_BYTE)
 creation de textures filtrables / mipmaps More...
 
GLuint make_flat_texture (const int unit, const int width, const int height, const GLenum texel_type, const GLenum data_format=GL_RGBA, const GLenum data_type=GL_UNSIGNED_BYTE)
 creation de textures non filtrables / 1 mipmap More...
 
GLuint make_depth_texture (const int unit, const int width, const int height, const GLenum texel_type=GL_DEPTH_COMPONENT)
 creation de textures pour stocker des donnees (autres qu'une couleur). More...
 
GLuint make_uint_texture (const int unit, const int width, const int height, const GLenum texel_type=GL_R32UI)
 creation de textures pour stocker des donnees (autres qu'une couleur). More...
 
GLuint make_float_texture (const int unit, const int width, const int height, const GLenum texel_type=GL_R32F)
 creation de textures pour stocker des donnees (autres qu'une couleur). More...
 
GLuint make_vec2_texture (const int unit, const int width, const int height, const GLenum texel_type=GL_RG32F)
 creation de textures pour stocker des donnees (autres qu'une couleur). More...
 
GLuint make_vec3_texture (const int unit, const int width, const int height, const GLenum texel_type=GL_RGB32F)
 creation de textures pour stocker des donnees (autres qu'une couleur). More...
 
GLuint make_vec4_texture (const int unit, const int width, const int height, const GLenum texel_type=GL_RGBA32F)
 creation de textures pour stocker des donnees (autres qu'une couleur). More...
 
void program_uniform (const GLuint program, const char *uniform, const unsigned v)
 affecte une valeur a un uniform du shader program. uint. More...
 
void program_uniform (const GLuint program, const char *uniform, const int v)
 affecte une valeur a un uniform du shader program. int. More...
 
void program_uniform (const GLuint program, const char *uniform, const float v)
 affecte une valeur a un uniform du shader program. float. More...
 
void program_uniform (const GLuint program, const char *uniform, const vec2 &v)
 affecte une valeur a un uniform du shader program. vec2. More...
 
void program_uniform (const GLuint program, const char *uniform, const vec3 &v)
 affecte une valeur a un uniform du shader program. vec3. More...
 
void program_uniform (const GLuint program, const char *uniform, const Point &v)
 affecte une valeur a un uniform du shader program. Point. More...
 
void program_uniform (const GLuint program, const char *uniform, const Vector &v)
 affecte une valeur a un uniform du shader program. Vector. More...
 
void program_uniform (const GLuint program, const char *uniform, const vec4 &v)
 affecte une valeur a un uniform du shader program. vec4. More...
 
void program_uniform (const GLuint program, const char *uniform, const Color &c)
 affecte une valeur a un uniform du shader program. Color. More...
 
void program_uniform (const GLuint program, const char *uniform, const Transform &v)
 affecte une valeur a un uniform du shader program. Transform. More...
 
void program_uniform (const GLuint program, const char *uniform, const std::vector< unsigned > &c)
 affecte un tableau de valeurs a un uniform du shader program. More...
 
void program_uniform (const GLuint program, const char *uniform, const std::vector< int > &c)
 affecte un tableau de valeurs a un uniform du shader program. More...
 
void program_uniform (const GLuint program, const char *uniform, const std::vector< float > &c)
 affecte un tableau de valeurs a un uniform du shader program. More...
 
void program_uniform (const GLuint program, const char *uniform, const std::vector< vec2 > &c)
 affecte un tableau de valeurs a un uniform du shader program. More...
 
void program_uniform (const GLuint program, const char *uniform, const std::vector< vec3 > &c)
 affecte un tableau de valeurs a un uniform du shader program. More...
 
void program_uniform (const GLuint program, const char *uniform, const std::vector< vec4 > &c)
 affecte un tableau de valeurs a un uniform du shader program. More...
 
void program_uniform (const GLuint program, const char *uniform, const std::vector< Color > &c)
 affecte un tableau de valeurs a un uniform du shader program. More...
 
void program_uniform (const GLuint program, const char *uniform, const std::vector< Transform > &v)
 affecte un tableau de valeurs a un uniform du shader program. More...
 
void program_use_texture (const GLuint program, const char *uniform, const int unit, const GLuint texture, const GLuint sampler=0)
 configure le pipeline et le shader program pour utiliser une texture, et des parametres de filtrage, eventuellement. More...
 

Detailed Description

Function Documentation

◆ read_program()

GLuint read_program ( const char *  filename,
const char *  definitions = "" 
)

cree un shader program. a detruire avec release_program( ).
charge un seul fichier, les shaders sont separes par #ifdef VERTEX_SHADER / #endif et #ifdef FRAGMENT_SHADER / #endif.
renvoie l'identifiant openGL du program et le program est selectionne (cf glUseProgram( )).

Parameters
filenamenom du fichier source.
definitionschaine de caracteres pouvant comporter plusieurs lignes "#define what value\n".

Definition at line 204 of file program.cpp.

205 {
206  GLuint program= glCreateProgram();
207  reload_program(program, filename, definitions);
208  return program;
209 }
int reload_program(const GLuint program, const char *filename, const char *definitions)
Definition: program.cpp:149

◆ release_program()

int release_program ( const GLuint  program)

detruit les shaders et le program.

Definition at line 211 of file program.cpp.

212 {
213  if(program == 0)
214  return -1;
215 
216  // recupere les shaders
217  int shaders_max= 0;
218  glGetProgramiv(program, GL_ATTACHED_SHADERS, &shaders_max);
219 
220  if(shaders_max > 0)
221  {
222  std::vector<GLuint> shaders(shaders_max, 0);
223  glGetAttachedShaders(program, shaders_max, NULL, &shaders.front());
224  for(int i= 0; i < shaders_max; i++)
225  {
226  glDetachShader(program, shaders[i]);
227  glDeleteShader(shaders[i]);
228  }
229  }
230 
231  glDeleteProgram(program);
232  return 0;
233 }

◆ reload_program()

int reload_program ( const GLuint  program,
const char *  filename,
const char *  definitions = "" 
)

recharge les sources et recompile un shader program.

Parameters
programshader program a modifier
filenamenom du fichier source a charger
definitionscf read_program

Definition at line 149 of file program.cpp.

150 {
151  if(program == 0)
152  return -1;
153 
154  // supprime les shaders attaches au program
155  int shaders_max= 0;
156  glGetProgramiv(program, GL_ATTACHED_SHADERS, &shaders_max);
157  if(shaders_max > 0)
158  {
159  std::vector<GLuint> shaders(shaders_max, 0);
160  glGetAttachedShaders(program, shaders_max, NULL, &shaders.front());
161  for (int i = 0; i < shaders_max; i++)
162  {
163  glDetachShader(program, shaders[i]);
164  glDeleteShader(shaders[i]);
165  }
166  }
167 
168 #ifdef GL_VERSION_4_3
169  glObjectLabel(GL_PROGRAM, program, -1, filename);
170 #endif
171 
172  // prepare les sources
173  std::string common_source= read(filename);
174  for(int i = 0; i < shader_keys_max; i++)
175  {
176  if(common_source.find(shader_keys[i]) != std::string::npos)
177  {
178  // cree et compile les shaders detectes dans le source
179  std::string source= prepare_source(common_source, std::string(definitions).append("#define ").append(shader_keys[i]).append("\n"));
180  GLuint shader= compile_shader(program, shader_types[i], source);
181  //~ printf(" %s...\n", shader_string(shader_types[i]));
182  if(shader == 0)
183  printf("[error] compiling %s...\n%s\n", shader_string(shader_types[i]), definitions);
184  }
185  }
186 
187  // linke les shaders
188  glLinkProgram(program);
189 
190  // verifie les erreurs
191  GLint status;
192  glGetProgramiv(program, GL_LINK_STATUS, &status);
193  if(status == GL_FALSE)
194  {
195  printf("[error] linking program %u '%s'...\n", program, filename);
196  return -1;
197  }
198 
199  // pour etre coherent avec les autres fonctions de creation, active l'objet gl qui vient d'etre cree.
200  glUseProgram(program);
201  return 0;
202 }
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

◆ program_format_errors()

int program_format_errors ( const GLuint  program,
std::string &  errors 
)

renvoie les erreurs de compilation.

Definition at line 366 of file program.cpp.

367 {
368  errors.clear();
369 
370  if(program == 0)
371  {
372  errors.append("[error] no program...\n");
373  return -1;
374  }
375 
376  GLint status;
377  glGetProgramiv(program, GL_LINK_STATUS, &status);
378  if(status == GL_TRUE)
379  return 0;
380 
381  int first_error= INT_MAX;
382  // recupere les shaders
383  int shaders_max= 0;
384  glGetProgramiv(program, GL_ATTACHED_SHADERS, &shaders_max);
385  if(shaders_max == 0)
386  {
387  errors.append("[error] no shaders...\n");
388  return 0;
389  }
390 
391  std::vector<GLuint> shaders(shaders_max, 0);
392  glGetAttachedShaders(program, shaders_max, NULL, &shaders.front());
393  for(int i= 0; i < shaders_max; i++)
394  {
395  GLint value;
396  glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &value);
397  if(value == GL_FALSE)
398  {
399  // recupere les erreurs de compilation des shaders
400  glGetShaderiv(shaders[i], GL_INFO_LOG_LENGTH, &value);
401  std::vector<char>log(value+1, 0);
402  glGetShaderInfoLog(shaders[i], (GLsizei) log.size(), NULL, &log.front());
403 
404  // recupere le source
405  glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &value);
406  std::vector<char> source(value+1, 0);
407  glGetShaderSource(shaders[i], (GLsizei) source.size(), NULL, &source.front());
408 
409  glGetShaderiv(shaders[i], GL_SHADER_TYPE, &value);
410  errors.append("[error] compiling ").append(shader_string(value)).append("...\n");
411 
412  // formatte les erreurs
413  int last_error= print_errors(errors, &log.front(), &source.front());
414  first_error= std::min(first_error, last_error);
415  }
416  }
417 
418  // recupere les erreurs de link du program
419  {
420  GLint value= 0;
421  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &value);
422 
423  std::vector<char>log(value+1, 0);
424  glGetProgramInfoLog(program, (GLsizei) log.size(), NULL, &log.front());
425 
426  errors.append("[error] linking program...\n").append(log.begin(), log.end());
427  }
428 
429  return first_error;
430 }
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
Point min(const Point &a, const Point &b)
renvoie la plus petite composante de chaque point. x, y, z= min(a.x, b.x), min(a.y,...
Definition: vec.cpp:30

◆ program_print_errors()

int program_print_errors ( const GLuint  program)

affiche les erreurs de compilation.

Definition at line 432 of file program.cpp.

433 {
434  std::string errors;
435  int code= program_format_errors(program, errors);
436  if(errors.size() > 0)
437  printf("%s\n", errors.c_str());
438  return code;
439 }
int program_format_errors(const GLuint program, std::string &errors)
renvoie les erreurs de compilation.
Definition: program.cpp:366

◆ program_ready()

bool program_ready ( const GLuint  program)

renvoie vrai si le programme est pret. (pas d'erreurs de compilation des shaders, pas d'erreur de link).

Definition at line 236 of file program.cpp.

237 {
238  if(program == 0)
239  return false;
240 
241  GLint status= GL_FALSE;
242  glGetProgramiv(program, GL_LINK_STATUS, &status);
243 
244 #ifndef GK_RELEASE
245  #ifdef GL_VERSION_4_3
246  char label[1024];
247  glGetObjectLabel(GL_PROGRAM, program, sizeof(label), nullptr, label);
248 
249  if(status == GL_FALSE) // n'affiche le messsage qu'en cas d'erreur...
250  printf("program %u '%s' %s...\n", program, label, (status == GL_TRUE) ? "ready" : "not ready");
251  #else
252  printf("program %u %s...\n", program, (status == GL_TRUE) ? "ready" : "not ready");
253  #endif
254 #endif
255 
256  return (status == GL_TRUE);
257 }
void label(Widgets &w, const char *format,...)
cree un texte. meme fonctionnement que printf().
Definition: widgets.cpp:142

◆ program_errors()

bool program_errors ( const GLuint  program)

renvoie vrai si le programme n'est pas pret.

Definition at line 259 of file program.cpp.

260 {
261  if(program == 0)
262  return true;
263 
264  GLint status;
265  glGetProgramiv(program, GL_LINK_STATUS, &status);
266 
267 #ifndef GK_RELEASE
268  #ifdef GL_VERSION_4_3
269  char label[1024];
270  glGetObjectLabel(GL_PROGRAM, program, sizeof(label), nullptr, label);
271 
272  if(status == GL_FALSE) // n'affiche le messsage qu'en cas d'erreur...
273  printf("program %u '%s' %s...\n", program, label, (status == GL_TRUE) ? "ready" : "not ready");
274  #else
275  printf("program %u %s...\n", program, (status == GL_TRUE) ? "ready" : "not ready");
276  #endif
277 #endif
278 
279  return (status == GL_FALSE);
280 }

◆ make_texture() [1/3]

GLuint make_texture ( const int  unit,
const Image im,
const GLenum  texel_type = GL_RGBA32F 
)

cree une texture a partir d'une image im. a detruire avec glDeleteTextures( ).

Parameters
texel_typepermet de choisir la representation interne des valeurs de la texture.

Definition at line 82 of file texture.cpp.

83 {
84  if(im == Image::error())
85  return 0;
86 
87  // cree la texture openGL
88  GLuint texture;
89  glGenTextures(1, &texture);
90  glActiveTexture(GL_TEXTURE0 + unit);
91  glBindTexture(GL_TEXTURE_2D, texture);
92 
93  // fixe les parametres de filtrage par defaut
94  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
95  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
96  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
97  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
98 
99  // transfere les donnees dans la texture, 4 float par texel
100  glTexImage2D(GL_TEXTURE_2D, 0,
101  texel_type, im.width(), im.height(), 0,
102  GL_RGBA, GL_FLOAT, im.data());
103 
104  // prefiltre la texture
105  glGenerateMipmap(GL_TEXTURE_2D);
106  return texture;
107 }
int height() const
renvoie la hauteur de l'image.
Definition: image.h:100
const void * data() const
renvoie un pointeur sur le stockage des couleurs des pixels.
Definition: image.h:84
static Image & error()
Definition: image.h:126
int width() const
renvoie la largeur de l'image.
Definition: image.h:98

◆ make_texture() [2/3]

GLuint make_texture ( const int  unit,
const ImageData im,
const GLenum  texel_type = GL_RGBA 
)

cree une texture a partir des donnees d'une image, cf image_io.h. a detruire avec glDeleteTextures( ).

Parameters
texel_typepermet de choisir la representation interne des valeurs de la texture.

Definition at line 109 of file texture.cpp.

110 {
111  if(im.pixels.empty())
112  return 0;
113 
114  // cree la texture openGL
115  GLuint texture;
116  glGenTextures(1, &texture);
117  glActiveTexture(GL_TEXTURE0 + unit);
118  glBindTexture(GL_TEXTURE_2D, texture);
119 
120  // fixe les parametres de filtrage par defaut
121  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
122  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
123  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
124  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
125 
126  GLenum format;
127  switch(im.channels)
128  {
129  case 1: format= GL_RED; break;
130  case 2: format= GL_RG; break;
131  case 3: format= GL_RGB; break;
132  case 4: format= GL_RGBA; break;
133  default: format= GL_RGBA;
134  }
135 
136  GLenum type;
137  switch(im.size)
138  {
139  case 1: type= GL_UNSIGNED_BYTE; break;
140  case 4: type= GL_FLOAT; break;
141  default: type= GL_UNSIGNED_BYTE;
142  }
143 
144  // transfere les donnees dans la texture
145  glTexImage2D(GL_TEXTURE_2D, 0,
146  texel_type, im.width, im.height, 0,
147  format, type, im.data());
148 
149  // prefiltre la texture
150  glGenerateMipmap(GL_TEXTURE_2D);
151  return texture;
152 }

◆ read_texture()

GLuint read_texture ( const int  unit,
const char *  filename,
const GLenum  texel_type = GL_RGBA 
)

cree une texture a partir d'un fichier filename. a detruire avec glDeleteTextures( ).

Parameters
texel_typepermet de choisir la representation interne des valeurs de la texture.

Definition at line 154 of file texture.cpp.

155 {
156  ImageData image= read_image_data(filename);
157  return make_texture(unit, image, texel_type);
158 }
ImageData read_image_data(const char *filename)
charge les donnees d'un fichier png. renvoie une image initialisee par defaut en cas d'echec.
Definition: image_io.cpp:216
GLuint make_texture(const int unit, const int width, const int height, const GLenum texel_type, const GLenum data_format, const GLenum data_type)
creation de textures filtrables / mipmaps
Definition: texture.cpp:25
stockage temporaire des donnees d'une image.
Definition: image_io.h:38

◆ miplevels()

int miplevels ( const int  width,
const int  height 
)

renvoie le nombre de mipmap d'une image width x height.

Definition at line 10 of file texture.cpp.

11 {
12  int w= width;
13  int h= height;
14  int levels= 1;
15  while(w > 1 || h > 1)
16  {
17  w= std::max(1, w / 2);
18  h= std::max(1, h / 2);
19  levels= levels + 1;
20  }
21 
22  return levels;
23 }
Point max(const Point &a, const Point &b)
renvoie la plus grande composante de chaque point. x, y, z= max(a.x, b.x), max(a.y,...
Definition: vec.cpp:35

◆ screenshot() [1/2]

int screenshot ( const char *  filename)

enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp

Definition at line 194 of file texture.cpp.

195 {
196  // recupere le contenu de la fenetre / framebuffer par defaut
197  glFinish();
198 
199  glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
200  glReadBuffer(GL_BACK);
201 
202  // recupere les dimensions de la fenetre
203  GLint viewport[4];
204  glGetIntegerv(GL_VIEWPORT, viewport);
205 
206  // transfere les pixels
207  ImageData image(viewport[2], viewport[3], 4);
208  glReadPixels(0, 0, image.width, image.height,
209  GL_RGBA, GL_UNSIGNED_BYTE, image.data());
210  // ecrit l'image
211  return write_image_data(image, filename);
212 }
int write_image_data(ImageData &image, const char *filename)
enregistre des donnees dans un fichier png.
Definition: image_io.cpp:229

◆ screenshot() [2/2]

int screenshot ( const char *  prefix,
const int  id 
)

enregistre le contenu de la fenetre dans un fichier numerote prefixXXX.png. id est le numero de la capture.

Definition at line 214 of file texture.cpp.

215 {
216  char tmp[4096];
217  sprintf(tmp,"%s%02d.png", prefix, id);
218  return screenshot(tmp);
219 }
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp
Definition: texture.cpp:194

◆ capture()

int capture ( const char *  prefix)

capture video. enregistre le contenu de la fenetre dans un fichier prefix%04d.bmp.

pour obtenir une video 30 images par secondes, compresser avec : avconv -r 30 -f image2 -i prefix%04d.bmp -c:v libx264 -crf 19 video.m4v

verifier que le codec x264 est installe : avconv -codecs | grep x264

s'il n'est pas installe : sudo apt-get install libavcodec-extra-53 ou la version actuelle : sudo apt-get install libavcodec-extra-*

exemple d'utilisation : cf shader_kit.cpp

static bool video= false;
if(key_state(SDLK_RETURN))
{
clear_key_state(SDLK_RETURN);
video= !video;
if(video) printf("start video capture...\n");
else printf("stop video capture.\n");
}
if(video) capture("shader_kit");
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
Definition: window.cpp:48
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
Definition: window.cpp:42
int capture(const char *prefix)
Definition: texture.cpp:221

Definition at line 221 of file texture.cpp.

222 {
223  static int id= 1;
224 
225  char tmp[4096];
226  sprintf(tmp,"%s%04d.bmp", prefix, id);
227 
228  if(id % 30 == 0)
229  printf("capture frame '%s'...\n", tmp);
230 
231  id++;
232  return screenshot(tmp);
233 }

◆ make_texture() [3/3]

GLuint make_texture ( const int  unit,
const int  width,
const int  height,
const GLenum  texel_type,
const GLenum  data_format = GL_RGBA,
const GLenum  data_type = GL_UNSIGNED_BYTE 
)

creation de textures filtrables / mipmaps

Definition at line 25 of file texture.cpp.

26 {
27  // cree la texture openGL
28  GLuint texture;
29  glGenTextures(1, &texture);
30  glActiveTexture(GL_TEXTURE0 + unit);
31  glBindTexture(GL_TEXTURE_2D, texture);
32 
33  // fixe les parametres de filtrage par defaut
34  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
35  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
36  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
37  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
38 
39  // transfere les donnees dans la texture, 4 float par texel
40  glTexImage2D(GL_TEXTURE_2D, 0,
41  texel_type, width, height, 0,
42  data_format, data_type, nullptr);
43 
44  // prefiltre la texture / alloue les mipmaps
45  glGenerateMipmap(GL_TEXTURE_2D);
46  return texture;
47 }

◆ make_flat_texture()

GLuint make_flat_texture ( const int  unit,
const int  width,
const int  height,
const GLenum  texel_type,
const GLenum  data_format = GL_RGBA,
const GLenum  data_type = GL_UNSIGNED_BYTE 
)

creation de textures non filtrables / 1 mipmap

Definition at line 49 of file texture.cpp.

50 {
51  // cree la texture openGL
52  GLuint texture;
53  glGenTextures(1, &texture);
54  glActiveTexture(GL_TEXTURE0 + unit);
55  glBindTexture(GL_TEXTURE_2D, texture);
56 
57  // fixe les parametres de filtrage par defaut
58  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
59  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
60  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
61  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
62 
63  // 1 seul mipmap
64  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
65 
66  // transfere les donnees dans la texture, 4 float par texel
67  glTexImage2D(GL_TEXTURE_2D, 0,
68  texel_type, width, height, 0,
69  data_format, data_type, nullptr);
70 
71  // prefiltre la texture / alloue les mipmaps
72  glGenerateMipmap(GL_TEXTURE_2D);
73  return texture;
74 }

◆ make_depth_texture()

GLuint make_depth_texture ( const int  unit,
const int  width,
const int  height,
const GLenum  texel_type = GL_DEPTH_COMPONENT 
)

creation de textures pour stocker des donnees (autres qu'une couleur).

Definition at line 162 of file texture.cpp.

163 {
164  return make_flat_texture(unit, width, height, texel_type, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT);
165 }
GLuint make_flat_texture(const int unit, const int width, const int height, const GLenum texel_type, const GLenum data_format, const GLenum data_type)
creation de textures non filtrables / 1 mipmap
Definition: texture.cpp:49

◆ make_uint_texture()

GLuint make_uint_texture ( const int  unit,
const int  width,
const int  height,
const GLenum  texel_type = GL_R32UI 
)

creation de textures pour stocker des donnees (autres qu'une couleur).

Definition at line 167 of file texture.cpp.

168 {
169  return make_flat_texture(unit, width, height, texel_type, GL_RED_INTEGER, GL_UNSIGNED_INT);
170 }

◆ make_float_texture()

GLuint make_float_texture ( const int  unit,
const int  width,
const int  height,
const GLenum  texel_type = GL_R32F 
)

creation de textures pour stocker des donnees (autres qu'une couleur).

Definition at line 172 of file texture.cpp.

173 {
174  return make_flat_texture(unit, width, height, texel_type, GL_RED, GL_FLOAT);
175 }

◆ make_vec2_texture()

GLuint make_vec2_texture ( const int  unit,
const int  width,
const int  height,
const GLenum  texel_type = GL_RG32F 
)

creation de textures pour stocker des donnees (autres qu'une couleur).

Definition at line 177 of file texture.cpp.

178 {
179  return make_flat_texture(unit, width, height, texel_type, GL_RG, GL_FLOAT);
180 }

◆ make_vec3_texture()

GLuint make_vec3_texture ( const int  unit,
const int  width,
const int  height,
const GLenum  texel_type = GL_RGB32F 
)

creation de textures pour stocker des donnees (autres qu'une couleur).

Definition at line 182 of file texture.cpp.

183 {
184  return make_flat_texture(unit, width, height, texel_type, GL_RGB, GL_FLOAT);
185 }

◆ make_vec4_texture()

GLuint make_vec4_texture ( const int  unit,
const int  width,
const int  height,
const GLenum  texel_type = GL_RGBA32F 
)

creation de textures pour stocker des donnees (autres qu'une couleur).

Definition at line 187 of file texture.cpp.

188 {
189  return make_flat_texture(unit, width, height, texel_type, GL_RGBA, GL_FLOAT);
190 }

◆ program_uniform() [1/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const unsigned  v 
)

affecte une valeur a un uniform du shader program. uint.

◆ program_uniform() [2/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const int  v 
)

affecte une valeur a un uniform du shader program. int.

Definition at line 100 of file uniforms.cpp.

101 {
102  glUniform1i( location(program, uniform), v );
103 }

◆ program_uniform() [3/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const float  v 
)

affecte une valeur a un uniform du shader program. float.

Definition at line 111 of file uniforms.cpp.

112 {
113  glUniform1f( location(program, uniform), v );
114 }

◆ program_uniform() [4/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const vec2 v 
)

affecte une valeur a un uniform du shader program. vec2.

Definition at line 122 of file uniforms.cpp.

123 {
124  glUniform2fv( location(program, uniform), 1, &v.x );
125 }

◆ program_uniform() [5/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const vec3 v 
)

affecte une valeur a un uniform du shader program. vec3.

Definition at line 133 of file uniforms.cpp.

134 {
135  glUniform3fv( location(program, uniform), 1, &v.x );
136 }

◆ program_uniform() [6/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const Point v 
)

affecte une valeur a un uniform du shader program. Point.

Definition at line 144 of file uniforms.cpp.

145 {
146  glUniform3fv( location(program, uniform), 1, &a.x );
147 }

◆ program_uniform() [7/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const Vector v 
)

affecte une valeur a un uniform du shader program. Vector.

Definition at line 155 of file uniforms.cpp.

156 {
157  glUniform3fv( location(program, uniform), 1, &v.x );
158 }

◆ program_uniform() [8/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const vec4 v 
)

affecte une valeur a un uniform du shader program. vec4.

Definition at line 166 of file uniforms.cpp.

167 {
168  glUniform4fv( location(program, uniform), 1, &v.x );
169 }

◆ program_uniform() [9/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const Color c 
)

affecte une valeur a un uniform du shader program. Color.

Definition at line 177 of file uniforms.cpp.

178 {
179  glUniform4fv( location(program, uniform), 1, &c.r );
180 }

◆ program_uniform() [10/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const Transform v 
)

affecte une valeur a un uniform du shader program. Transform.

Definition at line 188 of file uniforms.cpp.

189 {
190  glUniformMatrix4fv( location(program, uniform), 1, GL_TRUE, v.data() );
191 }
const float * data() const
renvoie l'adresse de la premiere valeur de la matrice.
Definition: mat.h:75

◆ program_uniform() [11/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const std::vector< unsigned > &  c 
)

affecte un tableau de valeurs a un uniform du shader program.

Definition at line 94 of file uniforms.cpp.

95 {
96  assert(v.size());
97  glUniform1uiv( location(program, uniform, v.size()), v.size(), v.data() );
98 }

◆ program_uniform() [12/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const std::vector< int > &  c 
)

affecte un tableau de valeurs a un uniform du shader program.

Definition at line 105 of file uniforms.cpp.

106 {
107  assert(v.size());
108  glUniform1iv( location(program, uniform, v.size()), v.size(), v.data() );
109 }

◆ program_uniform() [13/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const std::vector< float > &  c 
)

affecte un tableau de valeurs a un uniform du shader program.

Definition at line 116 of file uniforms.cpp.

117 {
118  assert(v.size());
119  glUniform1fv( location(program, uniform, v.size()), v.size(), v.data() );
120 }

◆ program_uniform() [14/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const std::vector< vec2 > &  c 
)

affecte un tableau de valeurs a un uniform du shader program.

Definition at line 127 of file uniforms.cpp.

128 {
129  assert(v.size());
130  glUniform2fv( location(program, uniform, v.size()), v.size(), &v[0].x );
131 }

◆ program_uniform() [15/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const std::vector< vec3 > &  c 
)

affecte un tableau de valeurs a un uniform du shader program.

Definition at line 138 of file uniforms.cpp.

139 {
140  assert(v.size());
141  glUniform3fv( location(program, uniform, v.size()), v.size(), &v[0].x );
142 }

◆ program_uniform() [16/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const std::vector< vec4 > &  c 
)

affecte un tableau de valeurs a un uniform du shader program.

Definition at line 171 of file uniforms.cpp.

172 {
173  assert(v.size());
174  glUniform4fv( location(program, uniform, v.size()), v.size(), &v[0].x );
175 }

◆ program_uniform() [17/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const std::vector< Color > &  c 
)

affecte un tableau de valeurs a un uniform du shader program.

Definition at line 182 of file uniforms.cpp.

183 {
184  assert(c.size());
185  glUniform4fv( location(program, uniform, c.size()), c.size(), &c[0].r );
186 }

◆ program_uniform() [18/18]

void program_uniform ( const GLuint  program,
const char *  uniform,
const std::vector< Transform > &  v 
)

affecte un tableau de valeurs a un uniform du shader program.

Definition at line 193 of file uniforms.cpp.

194 {
195  glUniformMatrix4fv( location(program, uniform, v.size()), v.size(), GL_TRUE, v[0].data() );
196 }

◆ program_use_texture()

void program_use_texture ( const GLuint  program,
const char *  uniform,
const int  unit,
const GLuint  texture,
const GLuint  sampler = 0 
)

configure le pipeline et le shader program pour utiliser une texture, et des parametres de filtrage, eventuellement.

Definition at line 198 of file uniforms.cpp.

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 }