gKit2 light
Loading...
Searching...
No Matches
tuto_draw_cubemap.cpp File Reference

chargement et affichage d'une 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.
int main (int argc, char **argv)

Detailed Description

chargement et affichage d'une cubemap / envmap.

Definition in file tuto_draw_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_draw_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 return texture;
75}
Image flipY(const Image &image)
retourne l'image
Definition image_io.cpp:113
Image flipX(const Image &image)
retourne l'image
Definition image_io.cpp:130
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:335
Image copy(const Image &image, const unsigned xmin, const unsigned ymin, const unsigned width, const unsigned height)
renvoie un bloc de l'image
Definition image_io.cpp:146
stockage temporaire des donnees d'une image.
Definition image_io.h:55

◆ main()

int main ( int argc,
char ** argv )

Definition at line 160 of file tuto_draw_cubemap.cpp.

161{
162 // il ne reste plus qu'a creer un objet application et la lancer
163 TP tp;
164 tp.run();
165
166 return 0;
167}
int run()
execution de l'application.
Definition app.cpp:36
Definition alpha.cpp:58