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 int width, const int height, const GLenum texel_type, const GLenum data_format, const GLenum data_type )
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 }
48 
49 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 )
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 }
75 
76 GLuint make_flat_depth_texture( const int unit, const int width, const int height, const GLenum texel_type )
77 {
78  return make_flat_texture(unit, width, height, texel_type, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT);
79 }
80 
81 
82 GLuint make_texture( const int unit, const Image& im, const GLenum texel_type )
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 }
108 
109 GLuint make_texture( const int unit, const ImageData& im, const GLenum texel_type )
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 }
153 
154 GLuint read_texture( const int unit, const char *filename, const GLenum texel_type )
155 {
156  ImageData image= read_image_data(filename);
157  return make_texture(unit, image, texel_type);
158 }
159 
160 
161 // creation des textures
162 GLuint make_depth_texture( const int unit, const int width, const int height, const GLenum texel_type )
163 {
164  return make_flat_texture(unit, width, height, texel_type, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT);
165 }
166 
167 GLuint make_uint_texture( const int unit, const int width, const int height, const GLenum texel_type )
168 {
169  return make_flat_texture(unit, width, height, texel_type, GL_RED_INTEGER, GL_UNSIGNED_INT);
170 }
171 
172 GLuint make_float_texture( const int unit, const int width, const int height, const GLenum texel_type )
173 {
174  return make_flat_texture(unit, width, height, texel_type, GL_RED, GL_FLOAT);
175 }
176 
177 GLuint make_vec2_texture( const int unit, const int width, const int height, const GLenum texel_type )
178 {
179  return make_flat_texture(unit, width, height, texel_type, GL_RG, GL_FLOAT);
180 }
181 
182 GLuint make_vec3_texture( const int unit, const int width, const int height, const GLenum texel_type )
183 {
184  return make_flat_texture(unit, width, height, texel_type, GL_RGB, GL_FLOAT);
185 }
186 
187 GLuint make_vec4_texture( const int unit, const int width, const int height, const GLenum texel_type )
188 {
189  return make_flat_texture(unit, width, height, texel_type, GL_RGBA, GL_FLOAT);
190 }
191 
192 
193 //
194 int screenshot( const char *filename )
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 }
213 
214 int screenshot( const char *prefix, const int id )
215 {
216  char tmp[4096];
217  sprintf(tmp,"%s%02d.png", prefix, id);
218  return screenshot(tmp);
219 }
220 
221 int capture( const char *prefix )
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 }
234 
representation d'une image.
Definition: image.h:21
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
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
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
int write_image_data(ImageData &image, const char *filename)
enregistre des donnees dans un fichier png.
Definition: image_io.cpp:229
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
GLuint make_vec2_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
Definition: texture.cpp:177
int capture(const char *prefix)
Definition: texture.cpp:221
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
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp
Definition: texture.cpp:194
GLuint make_vec3_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
Definition: texture.cpp:182
GLuint make_float_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
Definition: texture.cpp:172
GLuint make_vec4_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
Definition: texture.cpp:187
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
Definition: texture.cpp:154
int miplevels(const int width, const int height)
renvoie le nombre de mipmap d'une image width x height.
Definition: texture.cpp:10
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
GLuint make_uint_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
Definition: texture.cpp:167
GLuint make_depth_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
Definition: texture.cpp:162
stockage temporaire des donnees d'une image.
Definition: image_io.h:38