gKit2 light
texture.cpp
1 
2 #include <cassert>
3 #include <cstdio>
4 #include <algorithm>
5 
6 #include "texture.h"
7 #include "image_io.h"
8 
9 
10 int miplevels( const int width, const int height )
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 }
24 
25 GLuint make_texture( const int unit, const Image& im, const GLenum texel_type )
26 {
27  if(im == Image::error())
28  return 0;
29 
30  // cree la texture openGL
31  GLuint texture;
32  glGenTextures(1, &texture);
33  glActiveTexture(GL_TEXTURE0 + unit);
34  glBindTexture(GL_TEXTURE_2D, texture);
35 
36  // fixe les parametres de filtrage par defaut
37  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
38  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
39  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
40  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
41 
42  // transfere les donnees dans la texture, 4 float par texel
43  glTexImage2D(GL_TEXTURE_2D, 0,
44  texel_type, im.width(), im.height(), 0,
45  GL_RGBA, GL_FLOAT, im.buffer());
46 
47  // prefiltre la texture
48  glGenerateMipmap(GL_TEXTURE_2D);
49  return texture;
50 }
51 
52 GLuint make_texture( const int unit, const ImageData& im, const GLenum texel_type )
53 {
54  if(im.data.empty())
55  return 0;
56 
57  // cree la texture openGL
58  GLuint texture;
59  glGenTextures(1, &texture);
60  glActiveTexture(GL_TEXTURE0 + unit);
61  glBindTexture(GL_TEXTURE_2D, texture);
62 
63  // fixe les parametres de filtrage par defaut
64  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
65  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
66  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
67  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
68 
69  GLenum format;
70  switch(im.channels)
71  {
72  case 1: format= GL_RED; break;
73  case 2: format= GL_RG; break;
74  case 3: format= GL_RGB; break;
75  case 4: format= GL_RGBA; break;
76  default: format= GL_RGBA;
77  }
78 
79  GLenum type;
80  switch(im.size)
81  {
82  case 1: type= GL_UNSIGNED_BYTE; break;
83  case 4: type= GL_FLOAT; break;
84  default: type= GL_UNSIGNED_BYTE;
85  }
86 
87  // transfere les donnees dans la texture
88  glTexImage2D(GL_TEXTURE_2D, 0,
89  texel_type, im.width, im.height, 0,
90  format, type, im.buffer());
91 
92  // prefiltre la texture
93  glGenerateMipmap(GL_TEXTURE_2D);
94  return texture;
95 }
96 
97 
98 GLuint read_texture( const int unit, const char *filename, const GLenum texel_type )
99 {
100  ImageData image= read_image_data(filename);
101  return make_texture(unit, image, texel_type);
102 }
103 
104 
105 int screenshot( const char *filename )
106 {
107  // recupere le contenu de la fenetre / framebuffer par defaut
108  glFinish();
109 
110  glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
111  glReadBuffer(GL_BACK);
112 
113  // recupere les dimensions de la fenetre
114  GLint viewport[4];
115  glGetIntegerv(GL_VIEWPORT, viewport);
116 
117  // transfere les pixels
118  ImageData image(viewport[2], viewport[3], 4);
119  glReadPixels(0, 0, image.width, image.height,
120  GL_RGBA, GL_UNSIGNED_BYTE, image.buffer());
121 
122  // ecrit l'image
123  return write_image_data(image, filename);
124 }
125 
126 int capture( const char *prefix )
127 {
128  static int id= 1;
129 
130  char tmp[4096];
131  sprintf(tmp,"%s%04d.bmp", prefix, id);
132 
133  if(id % 30 == 0)
134  printf("capture frame '%s'...\n", tmp);
135 
136  id++;
137  return screenshot(tmp);
138 }
139 
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:144
const void * buffer() const
renvoie un pointeur sur le stockage des couleurs des pixels.
Definition: image.h:54
stockage temporaire des donnees d'une image.
Definition: image_io.h:23
static Image & error()
Definition: image.h:75
int write_image_data(ImageData &image, const char *filename)
enregistre des donnees dans un fichier png.
Definition: image_io.cpp:222
GLuint make_texture(const int unit, const Image &im, const GLenum texel_type)
Definition: texture.cpp:25
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
Definition: texture.cpp:98
int capture(const char *prefix)
Definition: texture.cpp:126
int miplevels(const int width, const int height)
renvoie le nombre de mipmap d'une image width x height.
Definition: texture.cpp:10
int height() const
renvoie la hauteur de l'image.
Definition: image.h:63
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
representation d'une image.
Definition: image.h:18
int width() const
renvoie la largeur de l'image.
Definition: image.h:61
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier filename nom de l'image a ecrire. doit etre de type .png / .bmp
Definition: texture.cpp:105