gKit2 light
Loading...
Searching...
No Matches
texture.cpp
1
2#include <cassert>
3#include <cstdio>
4#include <algorithm>
5
6#include "texture.h"
7#include "image_io.h"
8
9
10GLuint make_texture( const int unit, const int width, const int height, const GLenum texel_type, const GLenum data_format, const GLenum data_type )
11{
12 // cree la texture openGL
13 GLuint texture;
14 glGenTextures(1, &texture);
15 glActiveTexture(GL_TEXTURE0 + unit);
16 glBindTexture(GL_TEXTURE_2D, texture);
17
18 // fixe les parametres de filtrage par defaut
19 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
20 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
21 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
22 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
23
24 // transfere les donnees dans la texture, 4 float par texel
25 glTexImage2D(GL_TEXTURE_2D, 0,
26 texel_type, width, height, 0,
27 data_format, data_type, nullptr);
28
29 // prefiltre la texture / alloue les mipmaps
30 glGenerateMipmap(GL_TEXTURE_2D);
31 return texture;
32}
33
34GLuint make_flat_texture( const int unit, const int width, const int height, const GLenum texel_type, const GLenum data_format, const GLenum data_type )
35{
36 // cree la texture openGL
37 GLuint texture;
38 glGenTextures(1, &texture);
39 glActiveTexture(GL_TEXTURE0 + unit);
40 glBindTexture(GL_TEXTURE_2D, texture);
41
42 // fixe les parametres de filtrage par defaut
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
47
48 // 1 seul mipmap
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
50
51 // transfere les donnees dans la texture, 4 float par texel
52 glTexImage2D(GL_TEXTURE_2D, 0,
53 texel_type, width, height, 0,
54 data_format, data_type, nullptr);
55
56 // prefiltre la texture / alloue les mipmaps
57 glGenerateMipmap(GL_TEXTURE_2D);
58 return texture;
59}
60
61GLuint make_texture( const int unit, const Image& im, const GLenum texel_type )
62{
63 if(im.size() == 0)
64 return 0;
65
66 // cree la texture openGL
67 GLuint texture;
68 glGenTextures(1, &texture);
69 glActiveTexture(GL_TEXTURE0 + unit);
70 glBindTexture(GL_TEXTURE_2D, texture);
71
72 // fixe les parametres de filtrage par defaut
73 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
74 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
75 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
76 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
77
78 // transfere les donnees dans la texture, 4 float par texel
79 glTexImage2D(GL_TEXTURE_2D, 0,
80 texel_type, im.width(), im.height(), 0,
81 GL_RGBA, GL_FLOAT, im.data());
82
83 // prefiltre la texture
84 glGenerateMipmap(GL_TEXTURE_2D);
85 return texture;
86}
87
88GLuint make_texture( const int unit, const ImageData& im, const GLenum texel_type )
89{
90 if(im.pixels.empty())
91 return 0;
92
93 // cree la texture openGL
94 GLuint texture;
95 glGenTextures(1, &texture);
96 glActiveTexture(GL_TEXTURE0 + unit);
97 glBindTexture(GL_TEXTURE_2D, texture);
98
99 // fixe les parametres de filtrage par defaut
100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
104
105 GLenum format;
106 switch(im.channels)
107 {
108 case 1: format= GL_RED; break;
109 case 2: format= GL_RG; break;
110 case 3: format= GL_RGB; break;
111 case 4: format= GL_RGBA; break;
112 default: format= GL_RGBA;
113 }
114
115 GLenum type;
116 switch(im.size)
117 {
118 case 1: type= GL_UNSIGNED_BYTE; break;
119 case 4: type= GL_FLOAT; break;
120 default: type= GL_UNSIGNED_BYTE;
121 }
122
123 // transfere les donnees dans la texture
124 glTexImage2D(GL_TEXTURE_2D, 0,
125 texel_type, im.width, im.height, 0,
126 format, type, im.data());
127
128 // prefiltre la texture
129 glGenerateMipmap(GL_TEXTURE_2D);
130 return texture;
131}
132
133GLuint read_texture( const int unit, const char *filename, const GLenum texel_type )
134{
135 ImageData image= read_image_data(filename);
136 return make_texture(unit, image, texel_type);
137}
138
139GLuint read_srgb_texture( const int unit, const char *filename, const GLenum texel_type )
140{
141 return read_texture(unit, filename, texel_type );
142}
143
144
145// creation des textures
146GLuint make_depth_texture( const int unit, const int width, const int height, const GLenum texel_type )
147{
148 return make_flat_texture(unit, width, height, texel_type, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT);
149}
150
151GLuint make_uint_texture( const int unit, const int width, const int height, const GLenum texel_type )
152{
153 return make_flat_texture(unit, width, height, texel_type, GL_RED_INTEGER, GL_UNSIGNED_INT);
154}
155
156GLuint make_float_texture( const int unit, const int width, const int height, const GLenum texel_type )
157{
158 return make_flat_texture(unit, width, height, texel_type, GL_RED, GL_FLOAT);
159}
160
161GLuint make_vec2_texture( const int unit, const int width, const int height, const GLenum texel_type )
162{
163 return make_flat_texture(unit, width, height, texel_type, GL_RG, GL_FLOAT);
164}
165
166GLuint make_vec3_texture( const int unit, const int width, const int height, const GLenum texel_type )
167{
168 return make_flat_texture(unit, width, height, texel_type, GL_RGB, GL_FLOAT);
169}
170
171GLuint make_vec4_texture( const int unit, const int width, const int height, const GLenum texel_type )
172{
173 return make_flat_texture(unit, width, height, texel_type, GL_RGBA, GL_FLOAT);
174}
175
176
177//
178int screenshot( const char *filename )
179{
180 // recupere le contenu de la fenetre / framebuffer par defaut
181 glFinish();
182
183 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
184 glReadBuffer(GL_BACK);
185
186 // recupere les dimensions de la fenetre
187 GLint viewport[4];
188 glGetIntegerv(GL_VIEWPORT, viewport);
189
190 // transfere les pixels
191 ImageData image(viewport[2], viewport[3], 4);
192 glReadPixels(0, 0, image.width, image.height,
193 GL_RGBA, GL_UNSIGNED_BYTE, image.data());
194 // ecrit l'image
195 return write_image_data(image, filename);
196}
197
198int screenshot( const char *prefix, const int id )
199{
200 char tmp[4096];
201 sprintf(tmp,"%s%02d.png", prefix, id);
202 return screenshot(tmp);
203}
204
205int capture( const char *prefix )
206{
207 static int id= 1;
208
209 char tmp[4096];
210 sprintf(tmp,"%s%04d.bmp", prefix, id);
211
212 if(id % 30 == 0)
213 printf("capture frame '%s'...\n", tmp);
214
215 id++;
216 return screenshot(tmp);
217}
218
representation d'une image.
Definition image.h:21
unsigned width() const
renvoie la largeur de l'image.
Definition image.h:100
unsigned size() const
renvoie le nombre de pixels de l'image.
Definition image.h:104
unsigned height() const
renvoie la hauteur de l'image.
Definition image.h:102
const float * data() const
renvoie un const pointeur sur le stockage des couleurs des pixels.
Definition image.h:86
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
int write_image_data(ImageData &image, const char *filename, const bool flipY)
enregistre des donnees dans un fichier png.
Definition image_io.cpp:346
ImageData read_image_data(const void *buffer, const unsigned size, const bool flipY)
charge les donnees d'un fichier png stocke en memoire. renvoie une image initialisee par defaut en ca...
Definition image_io.cpp:315
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:161
int capture(const char *prefix)
Definition texture.cpp:205
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:10
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp
Definition texture.cpp:178
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:166
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:156
GLuint read_srgb_texture(const int unit, const char *filename, const GLenum texel_type)
Definition texture.cpp:139
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:171
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
Definition texture.cpp:133
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:34
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:151
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:146
stockage temporaire des donnees d'une image.
Definition image_io.h:53