gKit2 light
Loading...
Searching...
No Matches
tuto_draw_cubemap.cpp
Go to the documentation of this file.
1
3
4#include <memory>
5
6#include "wavefront.h"
7#include "texture.h"
8
9#include "orbiter.h"
10#include "program.h"
11#include "uniforms.h"
12#include "draw.h"
13
14#include "app_camera.h"
15
17GLuint read_cubemap( const int unit, const char *filename, const GLenum texel_type = GL_RGBA )
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}
76
77class TP : public AppCamera
78{
79public:
80 // constructeur : donner les dimensions de l'image, et eventuellement la version d'openGL.
81 TP( ) : AppCamera(1024, 640) {}
82
83 // creation des objets de l'application
84 int init( )
85 {
86 m_objet= read_mesh("data/cube.obj");
87 m_texture= read_cubemap(0, "gkit2_tutos/cubemap_debug_cross.png");
88 m_program= read_program("gkit2_tutos/draw_cubemap.glsl");
89 program_print_errors(m_program);
90
91 // init camera
92 Point pmin, pmax;
93 m_objet.bounds(pmin, pmax);
94 camera().lookat(pmin, pmax);
95
96 // etat openGL par defaut
97 glGenVertexArrays(1, &m_vao);
98
99 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
100
101 glClearDepth(1.f); // profondeur par defaut
102 glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la camera
103 glEnable(GL_DEPTH_TEST); // activer le ztest
104
105 return 0; // ras, pas d'erreur
106 }
107
108 // destruction des objets de l'application
109 int quit( )
110 {
111 m_objet.release();
112 release_program(m_program);
113 glDeleteVertexArrays(1, &m_vao);
114 glDeleteTextures(1, &m_texture);
115 return 0;
116 }
117
118 // dessiner une nouvelle image
119 int render( )
120 {
121 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
122
123 glUseProgram(m_program);
124 glBindVertexArray(m_vao);
125
126 Transform view= camera().view();
127 Transform projection= camera().projection();
128 Transform viewport= camera().viewport();
129 // inverse de la composition des transformations repere monde vers repere image
130 Transform inv= Inverse(viewport * projection * view);
131
132 Transform viewInv= Inverse(view);
133 Point camera_position= viewInv(Point(0, 0, 0)); // coordonnees de la camera, dans le repere camera... c'est l'origine
134
135 program_uniform(m_program, "invMatrix", inv);
136 program_uniform(m_program, "camera_position", camera_position);
137
138 glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture);
139 program_uniform(m_program, "texture0", int(0));
140
141 glDrawArrays(GL_TRIANGLES, 0, 3);
142
143 draw(m_objet, Identity(), view, projection);
144
145 // nettoyage
146 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
147 glUseProgram(0);
148 glBindVertexArray(0);
149 return 1;
150 }
151
152protected:
153 Mesh m_objet;
154 GLuint m_texture;
155 GLuint m_program;
156 GLuint m_vao;
157};
158
159
160int main( int argc, char **argv )
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}
classe application.
Definition app_camera.h:19
const Orbiter & camera() const
renvoie l'orbiter gere par l'application.
Definition app_camera.h:37
AppCamera(const int width, const int height, const int major=3, const int minor=3, const int samples=0)
constructeur, dimensions de la fenetre et version d'openGL.
Definition app_camera.cpp:5
int run()
execution de l'application.
Definition app.cpp:36
representation d'un objet / maillage.
Definition mesh.h:121
void lookat(const Point &center, const float size)
observe le point center a une distance size.
Definition orbiter.cpp:7
Transform viewport() const
renvoie la transformation viewport actuelle. doit etre initialise par projection(width,...
Definition orbiter.cpp:84
Transform projection(const int width, const int height, const float fov)
fixe la projection reglee pour une image d'aspect width / height, et une demi ouverture de fov degres...
Definition orbiter.cpp:48
Transform view() const
renvoie la transformation vue.
Definition orbiter.cpp:41
Definition alpha.cpp:58
int render()
a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
int init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
Image flipY(const Image &image)
retourne l'image
Definition image_io.cpp:112
Image flipX(const Image &image)
retourne l'image
Definition image_io.cpp:129
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:315
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:145
Transform Inverse(const Transform &m)
renvoie l'inverse de la matrice.
Definition mat.cpp:197
Transform Identity()
construit la transformation identite.
Definition mat.cpp:187
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition wavefront.cpp:14
GLuint read_program(const char *filename, const char *definitions)
Definition program.cpp:218
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition program.cpp:446
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:225
stockage temporaire des donnees d'une image.
Definition image_io.h:53
representation d'un point 3d.
Definition vec.h:21
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition mat.h:21
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.