gKit2 light
Classes | Functions
tuto_cubemap.cpp File Reference

reflets miroirs cubemap / envmap. More...

#include <memory>
#include "wavefront.h"
#include "texture.h"
#include "orbiter.h"
#include "program.h"
#include "uniforms.h"
#include "draw.h"
#include "app_camera.h"

Go to the source code of this file.

Classes

class  TP
 

Functions

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. More...
 
int main (int argc, char **argv)
 

Detailed Description

reflets miroirs cubemap / envmap.

Definition in file tuto_cubemap.cpp.

Function Documentation

◆ read_cubemap()

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.

Definition at line 17 of file tuto_cubemap.cpp.

18 {
19  // les 6 faces sur une croix
20  ImageData image= read_image_data(filename);
21  if(image.pixels.empty())
22  return 0;
23 
24  int w= image.width / 4;
25  int h= image.height / 3;
26  assert(w == h);
27 
28  GLenum data_format;
29  GLenum data_type= GL_UNSIGNED_BYTE;
30  if(image.channels == 3)
31  data_format= GL_RGB;
32  else // par defaut
33  data_format= GL_RGBA;
34 
35  // creer la texture
36  GLuint texture= 0;
37  glGenTextures(1, &texture);
38  glActiveTexture(GL_TEXTURE0 + unit);
39  glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
40 
41  // creer les 6 faces
42  // chaque face de la cubemap est un rectangle [image.width/4 x image.height/3] dans l'image originale
43  struct { int x, y; } faces[]= {
44  {0, 1}, // X+
45  {2, 1}, // X-
46  {1, 2}, // Y+
47  {1, 0}, // Y-
48  {1, 1}, // Z+
49  {3, 1}, // Z-
50  };
51 
52  for(int i= 0; i < 6; i++)
53  {
54  ImageData face= flipX(flipY(copy(image, faces[i].x*w, faces[i].y*h, w, h)));
55 
56  glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X +i, 0,
57  texel_type, w, h, 0,
58  data_format, data_type, face.data());
59  }
60 
61  // parametres de filtrage
62  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
63  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
64  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
65  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
66  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
67 
68  glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
69 
70  // filtrage "correct" sur les bords du cube...
71  glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
72  //~ glDisable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
73 
74  printf(" cubemap faces %dx%d\n", w, h);
75 
76  return texture;
77 }
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
Image flipY(const Image &image)
retourne l'image
Definition: image_io.cpp:295
Image flipX(const Image &image)
retourne l'image
Definition: image_io.cpp:312
Image copy(const Image &image, const int xmin, const int ymin, const int width, const int height)
renvoie un bloc de l'image
Definition: image_io.cpp:328
stockage temporaire des donnees d'une image.
Definition: image_io.h:38