18GLuint
read_cubemap(
const int unit,
const char *filename,
const GLenum texel_type = GL_RGBA )
22 if(image.pixels.empty())
25 int w= image.width / 4;
26 int h= image.height / 3;
30 GLenum data_type= GL_UNSIGNED_BYTE;
31 if(image.channels == 3)
38 glGenTextures(1, &texture);
39 glActiveTexture(GL_TEXTURE0 + unit);
40 glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
44 struct {
int x, y; } faces[]= {
53 for(
int i= 0; i < 6; i++)
57 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X +i, 0,
59 data_format, data_type, face.data());
63 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
64 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
65 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
66 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
67 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
69 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
72 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
75 printf(
" cubemap faces %dx%d\n", w, h);
82Mesh make_grid(
const int n= 10 )
88 float w= float(n-1) / 2;
89 for(
int x= 0; x < n; x++)
95 for(
int z= 0; z < n; z++)
124 void init_dynamic_cubemap(
const int w,
const int h )
127 glGenTextures(1, &m_texture_cubemap);
128 glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture_cubemap);
129 for(
int i= 0; i < 6; i++)
131 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X +i, 0,
133 GL_RGBA, GL_UNSIGNED_BYTE,
nullptr);
137 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
138 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
139 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
140 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
141 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
143 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
146 glGenTextures(1, &m_depth_cubemap);
147 glBindTexture(GL_TEXTURE_CUBE_MAP, m_depth_cubemap);
148 for(
int i= 0; i < 6; i++)
150 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X +i, 0,
151 GL_DEPTH_COMPONENT, w, h, 0,
152 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
nullptr);
155 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
158 glGenFramebuffers(1, &m_framebuffer);
159 glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
160 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_texture_cubemap, 0);
161 glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_depth_cubemap, 0);
163 glBindFramebuffer(GL_FRAMEBUFFER, 0);
171 m_ground= make_grid(20);
173 m_program_render=
read_program(
"gkit2_tutos/render_cubemap.glsl");
175 m_program_draw=
read_program(
"gkit2_tutos/draw_cubemap.glsl");
182 m_object.bounds(pmin, pmax);
186 init_dynamic_cubemap(1024, 1024);
189 glGenVertexArrays(1, &m_vao);
191 glClearColor(0.2f, 0.2f, 0.2f, 1.f);
194 glDepthFunc(GL_LEQUAL);
195 glEnable(GL_DEPTH_TEST);
208 void scene( std::vector<Object>& objects )
214 for(
int i= 0; i < 100; i++)
216 float x= (i % 10 - 4.5) * 4;
217 float z= (i / 10 - 4.5) * 4;
218 float y= std::cos(x * z + offset);
220 objects[i].color=
Color(
White() * std::abs(y));
234 std::vector<Object> objects;
240 glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
241 glViewport(0, 0, 1024, 1024);
244 float black[4]= { 0.6, 0.6, 0.6, 1 };
245 glClearBufferfv(GL_COLOR, 0, black);
248 glClearBufferfv(GL_DEPTH, 0, &one);
264 glBindVertexArray(vao);
266 glUseProgram(m_program_render);
267 int location_mvp= glGetUniformLocation(m_program_render,
"mvpMatrix");
268 int location_model= glGetUniformLocation(m_program_render,
"modelMatrix");
270 for(
int i= 0; i < int(objects.size()); i++)
277 for(
int i= 0; i < 6; i++)
278 mvp[i]= projection * faces[i] * model;
281 glUniformMatrix4fv(location_mvp, 6, GL_TRUE, mvp[0].data());
282 glUniformMatrix4fv(location_model, 1, GL_TRUE, model.
data());
283 glDrawArraysInstanced(GL_TRIANGLES, 0, m_object.vertex_count(), 6);
287 glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture_cubemap);
288 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
294 glBindFramebuffer(GL_FRAMEBUFFER, 0);
296 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
299 Point camera_position= viewInv(
Point(0, 0, 0));
304 glUseProgram(m_program);
305 program_uniform(m_program,
"mvpMatrix", mvp * model);
306 program_uniform(m_program,
"modelMatrix", model);
307 program_uniform(m_program,
"camera_position", camera_position);
309 glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture_cubemap);
310 program_uniform(m_program,
"texture0",
int(0));
313 m_object.draw(m_program);
316 draw(m_ground,
Identity(), view, projection);
317 for(
int i= 0; i < int(objects.size()); i++)
318 draw(m_cube, objects[i].model, view, projection);
324 glUseProgram(m_program_draw);
325 glBindVertexArray(m_vao);
326 program_uniform(m_program_draw,
"invMatrix", inv);
327 program_uniform(m_program_draw,
"camera_position", camera_position);
329 glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture_cubemap);
330 program_uniform(m_program_draw,
"texture0",
int(0));
332 glDrawArrays(GL_TRIANGLES, 0, 3);
336 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
338 glBindVertexArray(0);
344 reload_program(m_program_render,
"gkit2_tutos/render_cubemap.glsl");
346 reload_program(m_program_draw,
"gkit2_tutos/draw_cubemap.glsl");
348 reload_program(m_program,
"gkit2_tutos/cubemap.glsl");
357 printf(
"screenshot %d\n", calls);
374 glDeleteVertexArrays(1, &m_vao);
376 glDeleteTextures(1, &m_texture_cubemap);
377 glDeleteTextures(1, &m_depth_cubemap);
378 glDeleteFramebuffers(1, &m_framebuffer);
387 GLuint m_program_render;
388 GLuint m_program_draw;
391 GLuint m_texture_cubemap;
392 GLuint m_depth_cubemap;
393 GLuint m_framebuffer;
397int main(
int argc,
char **argv )
const Orbiter & camera() const
renvoie l'orbiter gere par l'application.
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.
int run()
execution de l'application.
representation d'un objet / maillage.
unsigned int vertex(const vec3 &p)
insere un sommet de position p, et ses attributs (s'ils sont definis par color(), texcoord(),...
Mesh & color(const vec4 &c)
definit la couleur du prochain sommet.
void lookat(const Point ¢er, const float size)
observe le point center a une distance size.
Transform viewport() const
renvoie la transformation viewport actuelle. doit etre initialise par projection(width,...
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...
Transform view() const
renvoie la transformation vue.
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.
int window_height()
renvoie la hauteur de la fenetre 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().
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.
float global_time()
renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.
Color Red()
utilitaire. renvoie une couleur rouge.
Color Blue()
utilitaire. renvoie une couleur bleue.
Image flipY(const Image &image)
retourne l'image
Image flipX(const Image &image)
retourne l'image
Color Green()
utilitaire. renvoie une couleur verte.
Color White()
utilitaire. renvoie une couleur blanche.
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.
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Transform Perspective(const float fov, const float aspect, const float znear, const float zfar)
renvoie la matrice representant une transformation projection perspective.
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
Transform Lookat(const Point &from, const Point &to, const Vector &up)
renvoie la matrice representant le placement et l'orientation d'une camera pour observer le point to.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
@ USE_POSITION
inclut l'attribut position dans les buffers.
@ USE_NORMAL
inclut l'attribut normale dans les buffers.
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)
affiche les erreurs de compilation.
int release_program(const GLuint program)
detruit les shaders et le program.
representation d'une couleur (rgba) transparente ou opaque.
stockage temporaire des donnees d'une image.
representation d'un point 3d.
representation d'un vecteur 3d.
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.