21GLuint program_cubemap;
22GLuint program_render_cubemap;
24GLuint texture_cubemap;
44 program_cubemap=
read_program(
"gkit2_tutos/cubemap.glsl");
48 program_render_cubemap=
read_program(
"gkit2_tutos/render_cubemap.glsl");
54 if(mesh.vertex_count() == 0)
57 vertex_count= mesh.vertex_count();
60 mesh.bounds(pmin, pmax);
61 camera.lookat(pmin, pmax);
64 glGenVertexArrays(1, &vao);
65 glBindVertexArray(vao);
68 glGenBuffers(1, &vertex_buffer);
69 glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
70 glBufferData(GL_ARRAY_BUFFER, mesh.vertex_buffer_size(), mesh.vertex_buffer(), GL_STATIC_DRAW);
73 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
74 glEnableVertexAttribArray(0);
77 if(!mesh.has_normal())
79 printf(
"[oops] pas de normales...\n");
83 glGenBuffers(1, &normal_buffer);
84 glBindBuffer(GL_ARRAY_BUFFER, normal_buffer);
85 glBufferData(GL_ARRAY_BUFFER, mesh.normal_buffer_size(), mesh.normal_buffer(), GL_STATIC_DRAW);
87 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
88 glEnableVertexAttribArray(1);
93 glBindBuffer(GL_ARRAY_BUFFER, 0);
101 glGenTextures(1, &texture_cubemap);
102 glBindTexture(GL_TEXTURE_CUBE_MAP, texture_cubemap);
103 for(
int i= 0; i < 6; i++)
105 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X +i, 0,
107 GL_RGBA, GL_UNSIGNED_BYTE,
nullptr);
109 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
112 glGenTextures(1, &depth_cubemap);
113 glBindTexture(GL_TEXTURE_CUBE_MAP, depth_cubemap);
114 for(
int i= 0; i < 6; i++)
116 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X +i, 0,
117 GL_DEPTH_COMPONENT, w, h, 0,
118 GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,
nullptr);
121 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
125 glGenFramebuffers(1, &framebuffer);
126 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
127 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture_cubemap, 0);
128 glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depth_cubemap, 0);
130 glBindFramebuffer(GL_FRAMEBUFFER, 0);
134 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
135 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
138 glGenVertexArrays(1, &vao_null);
139 glBindVertexArray(vao_null);
143 glBindVertexArray(0);
146 glClearColor(0.2f, 0.2f, 0.2f, 1);
149 glDepthFunc(GL_LEQUAL);
152 glEnable(GL_DEPTH_TEST);
157 glDisable(GL_CULL_FACE);
166 glDeleteVertexArrays(1, &vao);
167 glDeleteVertexArrays(1, &vao_null);
168 glDeleteBuffers(1, &vertex_buffer);
169 glDeleteBuffers(1, &normal_buffer);
170 glDeleteTextures(1, &texture_cubemap);
171 glDeleteTextures(1, &depth_cubemap);
172 glDeleteFramebuffers(1, &framebuffer);
179 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
180 glViewport(0, 0, 1024, 1024);
184 float black[4]= { 0, 0, 0, 0 };
185 glClearBufferfv(GL_COLOR, 0, black);
188 glClearBufferfv(GL_DEPTH, 0, &one);
207 glBindVertexArray(vao);
208 glUseProgram(program_render_cubemap);
209 glUniformMatrix4fv(glGetUniformLocation(program_render_cubemap,
"projectionMatrix"), 1, GL_TRUE, projection.buffer());
210 glUniformMatrix4fv(glGetUniformLocation(program_render_cubemap,
"modelMatrix"), 1, GL_TRUE, model.buffer());
211 glUniformMatrix4fv(glGetUniformLocation(program_render_cubemap,
"viewMatrix"), 6, GL_TRUE, faces[0].buffer());
214 glDrawArraysInstanced(GL_TRIANGLES, 0, vertex_count, 6);
218 glBindFramebuffer(GL_FRAMEBUFFER, 0);
219 glBindTexture(GL_TEXTURE_CUBE_MAP, texture_cubemap);
220 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
223 glBindFramebuffer(GL_FRAMEBUFFER, 0);
226 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
230 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
233 if(mb & SDL_BUTTON(1))
235 camera.rotation(mx, my);
237 else if(mb & SDL_BUTTON(3))
241 else if(mb & SDL_BUTTON(2))
257 Transform mvp= projection * view * model;
261 glBindVertexArray(vao);
262 glUseProgram(program);
264 program_uniform(program,
"mvpMatrix", mvp);
265 program_uniform(program,
"modelMatrix", model);
266 program_uniform(program,
"camera_position",
Inverse(view)(
Point(0, 0, 0)));
269 glBindTexture(GL_TEXTURE_CUBE_MAP, texture_cubemap);
272 GLint location= glGetUniformLocation(program,
"texture0");
273 glUniform1i(location, 0);
276 glDrawArrays(GL_TRIANGLES, 0, vertex_count);
281 glBindVertexArray(vao_null);
282 glUseProgram(program_cubemap);
285 glBindTexture(GL_TEXTURE_CUBE_MAP, texture_cubemap);
288 GLint location= glGetUniformLocation(program_cubemap,
"texture0");
289 glUniform1i(location, 0);
292 program_uniform(program_cubemap,
"vpInvMatrix",
Inverse(projection * view));
293 program_uniform(program_cubemap,
"camera_position",
Inverse(view)(
Point(0, 0, 0)));
296 glDrawArrays(GL_TRIANGLES, 0, 3);
300 glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
302 glBindVertexArray(0);
308int main(
int argc,
char **argv )
321 if(GLEW_ARB_shader_viewport_layer_array)
322 printf(
"ARB_shader_viewport_layer_array supported\n");
326 printf(
"[error] ARB_shader_viewport_layer_array NOT supported...\n");
333 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 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 window_width()
renvoie la largeur de la fenetre de l'application.
Transform Inverse(const Transform &m)
renvoie l'inverse de la matrice.
Transform Identity()
construit la transformation identite.
Transform Perspective(const float fov, const float aspect, const float znear, const float zfar)
renvoie la matrice representant une transformation projection perspective.
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....
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)
representation d'un point 3d.
representation d'un vecteur 3d.