15 bool use_diffuse=
false;
22unsigned miplevel_size(
const ImageData& image,
const int lod )
24 unsigned w= std::max(
unsigned(1), image.width / (1<<lod));
25 unsigned h= std::max(
unsigned(1), image.height / (1<<lod));
26 return image.channels * image.size * w * h;
30unsigned offset(
const unsigned width,
const unsigned stride,
const unsigned x,
const unsigned y,
const unsigned c )
32 return (y * width + x) * stride + c;
38 assert(image.size == 1);
41 level.pixels= image.pixels;
43 unsigned w= level.width;
44 unsigned h= level.height;
45 unsigned stride= level.channels * level.size;
46 unsigned row_stride= image.width;
47 for(
int l= 0; l < lod; l++)
49 w= std::max(
unsigned(1), w / 2);
50 h= std::max(
unsigned(1), h / 2);
52 for(
unsigned y= 0; y < h; y++)
53 for(
unsigned x= 0; x < w; x++)
54 for(
unsigned i= 0; i < image.channels; i++)
57 m= m + level.pixels[offset(row_stride, stride, 2*x, 2*y, i)];
58 m= m + level.pixels[offset(row_stride, stride, 2*x +1, 2*y, i)];
59 m= m + level.pixels[offset(row_stride, stride, 2*x, 2*y +1, i)];
60 m= m + level.pixels[offset(row_stride, stride, 2*x +1, 2*y +1, i)];
62 level.pixels[offset(w, stride, x, y, i)]= m / 4;
78 assert(image.size == 1);
80 float color[4]= {0, 0, 0, 0};
82 unsigned stride= image.channels * image.size;
83 for(
unsigned y= 0; y < image.height; y++)
84 for(
unsigned x= 0; x < image.width; x++)
85 for(
unsigned i= 0; i < image.channels; i++)
86 color[i]= color[i] + (
float) image.pixels[offset(image.width, stride, x, y, i)] / 255.f;
88 return Color(color[0], color[1], color[2], color[3]) / (image.width * image.height);
92int read_textures( std::vector<MaterialData>& materials,
const size_t max_size )
94 std::vector<TextureData> textures;
98 for(
int i= 0; i < (int) materials.size(); i++)
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;
113 total_size= total_size + data.ns_image.width * data.ns_image.height * data.ns_image.channels * data.ns_image.size;
116 if(total_size > max_size)
119 std::vector<unsigned char>().swap(data.diffuse_image.pixels);
120 std::vector<unsigned char>().swap(data.ns_image.pixels);
123 textures.emplace_back(data);
126 printf(
"using %dMB / %dMB\n",
int(total_size / 1024 / 1024),
int(max_size / 1024 / 1024));
130 for(; total_size > max_size ; lod++)
133 for(
int i= 0; i < (int) textures.size(); i++)
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);
142 printf(
" lod %d, %dMB\n", lod,
int(total_size / 1024 / 1024));
145 GLuint default_texture=
read_texture(0,
"data/grid.png");
147 printf(
"resizing textures...\n");
149 for(
int i= 0; i < (int) textures.size(); i++)
154 if(data.use_diffuse && data.diffuse_image.width > 0)
156 if(data.diffuse_image.pixels.empty())
160 assert(data.diffuse_image.width > 0);
163 ImageData level= mipmap_resize(data.diffuse_image, lod);
171 if(data.use_ns && data.ns_image.width > 0)
173 if(data.ns_image.pixels.empty())
177 assert(data.ns_image.width > 0);
180 ImageData level= mipmap_resize(data.ns_image, lod);
187 std::vector<unsigned char>().swap(data.diffuse_image.pixels);
188 std::vector<unsigned char>().swap(data.ns_image.pixels);
196 for(
int i= 0; i < (int) materials.size(); i++)
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)
void release_textures(std::vector< MaterialData > &materials)
detruit les textures.
int read_textures(std::vector< MaterialData > &materials, const size_t max_size)
charge les textures associees a un ensemble de matieres, sans depasser une limite de taille,...
charge les textures utiilisees par un ensemble de matieres.
representation des donnees d'un fichier wavefront .obj
representation d'une couleur (rgba) transparente ou opaque.
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