gKit2 light
envmap.cpp
1 
2 #include "image_io.h"
3 #include "image_hdr.h"
4 
5 #include "envmap.h"
6 
7 
8 Envmap read_cubemap( const char *filename )
9 {
10  Image image;
11  if(is_hdr_image(filename))
12  image= read_image_hdr(filename);
13  else
14  image= read_image(filename);
15 
16  if(image.size() == 0)
17  return Envmap();
18 
19  return Envmap(image);
20 }
21 
22 Envmap read_cubemap_faces( const std::array<const char *, 6>& filenames )
23 {
24  std::array<Image, 6> faces;
25  for(int i= 0; i < 6; i++)
26  {
27  Image image;
28  if(is_hdr_image(filenames[i]))
29  image= read_image_hdr(filenames[i]);
30  else
31  image= read_image(filenames[i]);
32 
33  faces[i]= image;
34  }
35 
36  return Envmap(faces);
37 }
38 
39 Envmap read_cubemap_faces( const char *prefix )
40 {
41  std::array<const char *, 6> suffixes= {
42  "posx",
43  "negx",
44  "posy",
45  "negy",
46  "posz",
47  "negz"
48  };
49 
50  char tmp[1024];
51  std::array<Image, 6> faces;
52  for(int i= 0; i < 6; i++)
53  {
54  sprintf(tmp, prefix, suffixes[i]);
55 
56  Image image;
57  if(is_hdr_image(tmp))
58  image= read_image_hdr(tmp);
59  else
60  image= read_image(tmp);
61 
62  faces[i]= image;
63  }
64 
65  return Envmap(faces);
66 }
67 
68 
69 int write_cubemap( const Envmap& envmap, const char *filename )
70 {
71  Image image= envmap.cross();
72  if(is_hdr_image(filename))
73  return write_image_hdr(image, filename);
74  else
75  return write_image(image, filename);
76 }
77 
78 int write_cubemap_faces( const Envmap& envmap, const char *prefix )
79 {
80  std::array<const char *, 6> suffixes= {
81  "posx",
82  "negx",
83  "posy",
84  "negy",
85  "posz",
86  "negz"
87  };
88 
89  char tmp[1024];
90  std::array<Image, 6> faces= envmap.faces();
91  for(int i= 0; i < 6; i++)
92  {
93  sprintf(tmp, prefix, suffixes[i]);
94 
95  if(is_hdr_image(tmp))
96  write_image_hdr(faces[i], tmp);
97  else
98  write_image(faces[i], tmp);
99  }
100 
101  return 0;
102 }
representation d'une image.
Definition: image.h:21
unsigned size() const
renvoie le nombre de pixels de l'image.
Definition: image.h:102
int write_image(const Image &image, const char *filename)
enregistre une image dans un fichier png.
Definition: image_io.cpp:85
Image read_image(const char *filename)
Definition: image_io.cpp:18
bool is_hdr_image(const char *filename)
renvoie vrai si le nom de fichier se termine par .hdr.
Definition: image_hdr.cpp:10
Image read_image_hdr(const char *filename)
Definition: image_hdr.cpp:16
int write_image_hdr(const Image &image, const char *filename)
enregistre une image dans un fichier .hdr.
Definition: image_hdr.cpp:56
representation d'une cubemap / envmap.
Definition: envmap.h:13
Image cross() const
renvoie une image contenant les 6 faces de la cubemap.
Definition: envmap.h:85
std::array< Image, 6 > faces() const
renvoie les 6 faces de la cubemap.
Definition: envmap.h:117
GLuint read_cubemap(const int unit, const char *filename, const GLenum texel_type=GL_RGBA)
charge une image, decoupe les 6 faces et renvoie une texture cubemap.