charge les textures associees a un ensemble de matieres, sans depasser une limite de taille, 1Go par defaut.
93{
94 std::vector<TextureData> textures;
95
96
97 size_t total_size= 0;
98 for(int i= 0; i < (int) materials.size(); i++)
99 {
102
104 {
105 data.use_diffuse= true;
107 total_size= total_size + data.diffuse_image.width * data.diffuse_image.height * data.diffuse_image.channels * data.diffuse_image.size;
108 }
110 {
111 data.use_ns= true;
113 total_size= total_size + data.ns_image.width * data.ns_image.height * data.ns_image.channels * data.ns_image.size;
114 }
115
116 if(total_size > max_size)
117 {
118
119 std::vector<unsigned char>().swap(data.diffuse_image.pixels);
120 std::vector<unsigned char>().swap(data.ns_image.pixels);
121 }
122
123 textures.emplace_back(data);
124 }
125
126 printf(
"using %dMB / %dMB\n",
int(total_size / 1024 / 1024),
int(max_size / 1024 / 1024));
127
128
129 int lod= 0;
130 for(; total_size > max_size ; lod++)
131 {
132 total_size= 0;
133 for(int i= 0; i < (int) textures.size(); i++)
134 {
135 if(textures[i].use_diffuse)
136 total_size= total_size + miplevel_size(textures[i].diffuse_image, lod);
137 if(textures[i].use_ns)
138 total_size= total_size + miplevel_size(textures[i].ns_image, lod);
139 }
140 }
141
142 printf(
" lod %d, %dMB\n", lod,
int(total_size / 1024 / 1024));
143
144
145 GLuint default_texture=
read_texture(0,
"data/grid.png");
146
147 printf(
"resizing textures...\n");
148
149 for(int i= 0; i < (int) textures.size(); i++)
150 {
153
154 if(data.use_diffuse && data.diffuse_image.width > 0)
155 {
156 if(data.diffuse_image.pixels.empty())
157 {
158
160 assert(data.diffuse_image.width > 0);
161 }
162
163 ImageData level= mipmap_resize(data.diffuse_image, lod);
165
167 }
168 else
170
171 if(data.use_ns && data.ns_image.width > 0)
172 {
173 if(data.ns_image.pixels.empty())
174 {
175
177 assert(data.ns_image.width > 0);
178 }
179
180 ImageData level= mipmap_resize(data.ns_image, lod);
182 }
183 else
185
186
187 std::vector<unsigned char>().swap(data.diffuse_image.pixels);
188 std::vector<unsigned char>().swap(data.ns_image.pixels);
189 }
190
191 return total_size;
192}
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().
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...
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
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
stockage temporaire des donnees d'une image.
representation d'une matiere texturee.
GLuint diffuse_texture
texture diffuse
std::string diffuse_filename
nom de la texture diffuse
GLuint ns_texture
texture exposant
std::string ns_filename
nom de la texture exposant
Color diffuse_texture_color
couleur moyenne de la texture