22GLuint program_cubemap;
39 program=
read_program(
"gkit2_tutos/tuto5GL_cubemap.glsl");
43 program_cubemap=
read_program(
"gkit2_tutos/cubemap.glsl");
49 if(mesh.vertex_count() == 0)
52 vertex_count= mesh.vertex_count();
55 mesh.bounds(pmin, pmax);
56 camera.lookat(pmin, pmax);
59 glGenVertexArrays(1, &vao);
60 glBindVertexArray(vao);
63 glGenBuffers(1, &vertex_buffer);
64 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
65 glBufferData(GL_ARRAY_BUFFER, mesh.vertex_buffer_size(), mesh.vertex_buffer(), GL_STATIC_DRAW);
68 GLint position= glGetAttribLocation(program,
"position");
71 glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 0, 0);
72 glEnableVertexAttribArray(position);
76 if(!mesh.normal_buffer_size())
78 printf(
"[oops] pas de normales...\n");
82 glGenBuffers(1, &normal_buffer);
83 glBindBuffer(GL_ARRAY_BUFFER, normal_buffer);
84 glBufferData(GL_ARRAY_BUFFER, mesh.normal_buffer_size(), mesh.normal_buffer(), GL_STATIC_DRAW);
86 GLint normal= glGetAttribLocation(program,
"normal");
89 glVertexAttribPointer(normal, 3, GL_FLOAT, GL_FALSE, 0, 0);
90 glEnableVertexAttribArray(normal);
96 glBindBuffer(GL_ARRAY_BUFFER, 0);
105 int size= image.width;
108 GLenum data_type= GL_UNSIGNED_BYTE;
109 if(image.channels == 3)
112 data_format= GL_RGBA;
115 glGenTextures(1, &texture);
116 glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
119 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0,
120 GL_RGBA, size, size, 0,
121 data_format, data_type, image.data());
123 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0,
124 GL_RGBA, size, size, 0,
125 data_format, data_type, image.data());
127 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0,
128 GL_RGBA, size, size, 0,
129 data_format, data_type, image.data());
131 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0,
132 GL_RGBA, size, size, 0,
133 data_format, data_type, image.data());
135 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0,
136 GL_RGBA, size, size, 0,
137 data_format, data_type, image.data());
139 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0,
140 GL_RGBA, size, size, 0,
141 data_format, data_type, image.data());
149 int size= image.width / 6;
152 GLenum data_type= GL_UNSIGNED_BYTE;
153 if(image.channels == 3)
156 data_format= GL_RGBA;
159 glGenTextures(1, &texture);
160 glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
164 int faces[]= { 0, 1, 2, 3, 4, 5 };
167 for(
int i= 0; i < 6; i++)
174 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X +i, 0,
175 GL_RGBA, size, size, 0,
176 data_format, data_type, face.data());
187 int w= image.width / 4;
188 int h= image.height / 3;
192 GLenum data_type= GL_UNSIGNED_BYTE;
193 if(image.channels == 3)
196 data_format= GL_RGBA;
199 glGenTextures(1, &texture);
200 glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
204 struct {
int x, y; } faces[]= {
213 for(
int i= 0; i < 6; i++)
218 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X +i, 0,
220 data_format, data_type, face.data());
226 const char *filenames[]= {
227 "data/cubemap/cubemap_opensea/opensea_posx.png",
228 "data/cubemap/cubemap_opensea/opensea_negx.png",
229 "data/cubemap/cubemap_opensea/opensea_posy.png",
230 "data/cubemap/cubemap_opensea/opensea_negy.png",
231 "data/cubemap/cubemap_opensea/opensea_posz.png",
232 "data/cubemap/cubemap_opensea/opensea_negz.png"
242 glGenTextures(1, &texture);
243 glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
245 for(
int i= 0; i < 6; i++)
252 GLenum data_type= GL_UNSIGNED_BYTE;
253 if(image.channels == 3)
256 data_format= GL_RGBA;
258 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X +i, 0,
259 GL_RGBA, image.width, image.height, 0,
260 data_format, data_type, image.data());
264 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
267 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
269 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
272 glGenVertexArrays(1, &vao_null);
273 glBindVertexArray(vao_null);
277 glBindVertexArray(0);
280 glClearColor(0.2f, 0.2f, 0.2f, 1);
283 glDepthFunc(GL_LEQUAL);
286 glEnable(GL_DEPTH_TEST);
291 glDisable(GL_CULL_FACE);
299 glDeleteVertexArrays(1, &vao);
300 glDeleteVertexArrays(1, &vao_null);
301 glDeleteBuffers(1, &vertex_buffer);
302 glDeleteBuffers(1, &normal_buffer);
303 glDeleteTextures(1, &texture);
309 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
314 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
317 if(mb & SDL_BUTTON(1))
319 camera.rotation(mx, my);
321 else if(mb & SDL_BUTTON(3))
325 else if(mb & SDL_BUTTON(2))
342 Transform mvp= projection * view * model;
346 glBindVertexArray(vao);
347 glUseProgram(program);
349 program_uniform(program,
"mvpMatrix", mvp);
350 program_uniform(program,
"mMatrix", model);
351 program_uniform(program,
"camera_position",
Inverse(view)(
Point(0, 0, 0)));
354 glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
357 GLint location= glGetUniformLocation(program,
"texture0");
358 glUniform1i(location, 0);
361 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
366 glBindVertexArray(vao_null);
367 glUseProgram(program_cubemap);
370 glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
373 GLint location= glGetUniformLocation(program_cubemap,
"texture0");
374 glUniform1i(location, 0);
377 program_uniform(program_cubemap,
"vpInvMatrix",
Inverse(projection * view));
378 program_uniform(program_cubemap,
"camera_position",
Inverse(view)(
Point(0, 0, 0)));
381 glDrawArrays(GL_TRIANGLES, 0, 3);
392 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
394 glBindVertexArray(0);
400int main(
int argc,
char **argv )
415 printf(
"[error] init failed.\n");
representation d'un objet / maillage.
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
Context create_context(Window window)
cree et configure un contexte opengl
int window_height()
renvoie la hauteur de la fenetre de l'application.
void release_window(Window window)
destruction de la fenetre.
int run(Window window, int(*draw)())
boucle de gestion des evenements de l'application.
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
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().
Window create_window(const int w, const int h, const int major, const int minor)
creation d'une fenetre pour l'application.
void release_context(Context context)
detruit le contexte openGL.
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
int window_width()
renvoie la largeur de la fenetre de l'application.
Image flipY(const Image &image)
retourne l'image
Image flipX(const Image &image)
retourne l'image
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...
Image copy(const Image &image, const unsigned xmin, const unsigned ymin, const unsigned width, const unsigned height)
renvoie un bloc de l'image
Transform Inverse(const Transform &m)
renvoie l'inverse de la matrice.
Transform Identity()
construit la transformation identite.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
int screenshot(const char *filename)
enregistre le contenu de la fenetre dans un fichier. doit etre de type .png / .bmp
GLuint read_program(const char *filename, const char *definitions)
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
int release_program(const GLuint program)
detruit les shaders et le program.
int init(std::vector< const char * > &options)
stockage temporaire des donnees d'une image.
representation d'un point 3d.