22 TP( ) :
App(1024, 640)
25 SDL_GL_SetSwapInterval(0);
34 m_objet.bounds(pmin, pmax);
35 m_camera.lookat(pmin -
Vector(20, 20, 0), pmax +
Vector(20, 20, 0));
41 glGenVertexArrays(1, &m_vao);
42 glBindVertexArray(m_vao);
45 glGenBuffers(1, &m_vertex_buffer);
46 glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer);
47 glBufferData(GL_ARRAY_BUFFER, m_objet.vertex_buffer_size() + m_objet.normal_buffer_size(), 0, GL_STATIC_DRAW);
50 glBufferSubData(GL_ARRAY_BUFFER, 0, m_objet.vertex_buffer_size(), m_objet.vertex_buffer());
52 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
53 glEnableVertexAttribArray(0);
56 glBufferSubData(GL_ARRAY_BUFFER, m_objet.vertex_buffer_size(), m_objet.normal_buffer_size(), m_objet.normal_buffer());
58 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (
const GLvoid *) m_objet.vertex_buffer_size());
59 glEnableVertexAttribArray(2);
61 m_vertex_count= m_objet.vertex_count();
68 glBindBuffer(GL_ARRAY_BUFFER, 0);
71 glGenQueries(1, &m_time_query);
77 glClearColor(0.2f, 0.2f, 0.2f, 1.f);
81 glEnable(GL_DEPTH_TEST);
88 glDeleteQueries(1, &m_time_query);
92 glDeleteTextures(1, &m_texture);
97 int update(
const float time,
const float delta )
105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
110 if(mb & SDL_BUTTON(1))
111 m_camera.rotation(mx, my);
112 else if(mb & SDL_BUTTON(3))
114 else if(mb & SDL_BUTTON(2))
118 glBeginQuery(GL_TIME_ELAPSED, m_time_query);
122 std::chrono::high_resolution_clock::time_point cpu_start= std::chrono::high_resolution_clock::now();
128 mode= (mode + 1) % 3;
134 draw(m_objet, m_model, m_camera, m_texture);
139 for(
int y= -2; y <= 2; y++)
140 for(
int x= -2; x <= 2; x++)
143 draw(m_objet, t * m_model, m_camera, m_texture);
150 glBindVertexArray(m_vao);
151 glUseProgram(m_program);
159 program_uniform(m_program,
"mvpMatrix", mvp);
160 program_uniform(m_program,
"normalMatrix", mv.
normal());
162 glDrawArraysInstanced(GL_TRIANGLES, 0, m_vertex_count, 25);
165 std::chrono::high_resolution_clock::time_point cpu_stop= std::chrono::high_resolution_clock::now();
167 int cpu_time= std::chrono::duration_cast<std::chrono::microseconds>(cpu_stop - cpu_start).count();
169 glEndQuery(GL_TIME_ELAPSED);
174 std::chrono::high_resolution_clock::time_point wait_start= std::chrono::high_resolution_clock::now();
178 glGetQueryObjecti64v(m_time_query, GL_QUERY_RESULT, &gpu_time);
180 std::chrono::high_resolution_clock::time_point wait_stop= std::chrono::high_resolution_clock::now();
181 int wait_time= std::chrono::duration_cast<std::chrono::microseconds>(wait_stop - wait_start).count();
185 if(mode == 0)
printf(m_console, 0, 0,
"mode 0 : 1 draw");
186 if(mode == 1)
printf(m_console, 0, 0,
"mode 1 : 25 draws");
187 if(mode == 2)
printf(m_console, 0, 0,
"mode 2 : 1 draw / 25 instances");
188 printf(m_console, 0, 1,
"cpu %02dms %03dus",
int(cpu_time / 1000),
int(cpu_time % 1000));
189 printf(m_console, 0, 2,
"gpu %02dms %03dus",
int(gpu_time / 1000000),
int((gpu_time / 1000) % 1000));
190 printf(m_console, 0, 3,
"wait %02dms %03dus",
int(wait_time / 1000),
int(wait_time % 1000));
196 printf(
"cpu %02dms %03dus ",
int(cpu_time / 1000),
int(cpu_time % 1000));
197 printf(
"gpu %02dms %03dus\n",
int(gpu_time / 1000000),
int((gpu_time / 1000) % 1000));
213 GLuint m_vertex_buffer;
214 unsigned int m_vertex_count;
218int main(
int argc,
char **argv )
int run()
execution de l'application.
App(const int width, const int height, const int major=3, const int minor=3)
constructeur, dimensions de la fenetre et version d'openGL.
representation d'un objet / maillage.
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
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 update(const float time, const float delta)
a deriver et redefinir pour animer les objets en fonction du temps.
int init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
void clear(Text &text)
efface le contenu de la console.
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().
Text create_text()
cree une console. a detruire avec release_text( ).
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
void release_text(Text &text)
detruit une console.
int window_width()
renvoie la largeur de la fenetre de l'application.
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
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.
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
representation d'un point 3d.
representation d'un vecteur 3d.