22 TP( ) :
App(1024, 640) {}
30 m_objet.
bounds(pmin, pmax);
37 glGenVertexArrays(1, &m_vao);
38 glBindVertexArray(m_vao);
41 glGenBuffers(1, &m_vertex_buffer);
42 glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffer);
48 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
49 glEnableVertexAttribArray(0);
54 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (
const GLvoid *) m_objet.
vertex_buffer_size());
55 glEnableVertexAttribArray(2);
64 glBindBuffer(GL_ARRAY_BUFFER, 0);
67 glGenQueries(1, &m_time_query);
73 glClearColor(0.2f, 0.2f, 0.2f, 1.f);
77 glEnable(GL_DEPTH_TEST);
84 glDeleteQueries(1, &m_time_query);
88 glDeleteTextures(1, &m_texture);
93 int update(
const float time,
const float delta )
101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
106 if(mb & SDL_BUTTON(1))
108 else if(mb & SDL_BUTTON(3))
110 else if(mb & SDL_BUTTON(2))
114 glBeginQuery(GL_TIME_ELAPSED, m_time_query);
118 std::chrono::high_resolution_clock::time_point cpu_start= std::chrono::high_resolution_clock::now();
124 mode= (mode + 1) % 3;
130 draw(m_objet, m_model, m_camera, m_texture);
135 for(
int y= -2; y <= 2; y++)
136 for(
int x= -2; x <= 2; x++)
139 draw(m_objet, t * m_model, m_camera, m_texture);
146 glBindVertexArray(m_vao);
147 glUseProgram(m_program);
158 glDrawArraysInstanced(GL_TRIANGLES, 0, m_vertex_count, 25);
161 std::chrono::high_resolution_clock::time_point cpu_stop= std::chrono::high_resolution_clock::now();
163 long long int cpu_time= std::chrono::duration_cast<std::chrono::nanoseconds>(cpu_stop - cpu_start).count();
165 glEndQuery(GL_TIME_ELAPSED);
170 std::chrono::high_resolution_clock::time_point wait_start= std::chrono::high_resolution_clock::now();
174 glGetQueryObjecti64v(m_time_query, GL_QUERY_RESULT, &gpu_time);
176 std::chrono::high_resolution_clock::time_point wait_stop= std::chrono::high_resolution_clock::now();
177 long long int wait_time= std::chrono::duration_cast<std::chrono::nanoseconds>(wait_stop - wait_start).count();
181 if(mode == 0)
printf(m_console, 0, 0,
"mode 0 : 1 draw");
182 if(mode == 1)
printf(m_console, 0, 0,
"mode 1 : 25 draws");
183 if(mode == 2)
printf(m_console, 0, 0,
"mode 2 : 1 draw / 25 instances");
184 printf(m_console, 0, 1,
"cpu %02dms %03dus", (
int) (cpu_time / 1000000), (
int) ((cpu_time / 1000) % 1000));
185 printf(m_console, 0, 2,
"gpu %02dms %03dus", (
int) (gpu_time / 1000000), (
int) ((gpu_time / 1000) % 1000));
186 printf(m_console, 0, 3,
"wait %02dms %03dus", (
int) (wait_time / 1000000), (
int) ((wait_time / 1000) % 1000));
192 printf(
"cpu %02dms %03dus ", (
int) (cpu_time / 1000000), (
int) ((cpu_time / 1000) % 1000));
193 printf(
"gpu %02dms %03dus\n", (
int) (gpu_time / 1000000), (
int) ((gpu_time / 1000) % 1000));
209 GLuint m_vertex_buffer;
210 unsigned int m_vertex_count;
214 int main(
int argc,
char **argv )
const float * normal_buffer() const
renvoie l'adresse de la normale du premier sommet. par convention, la normale est un vec3...
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet...
Text create_text()
cree une console. a detruire avec release_text( ).
int init()
a deriver pour creer les objets openGL.
const float * vertex_buffer() const
renvoie l'adresse de la position du premier sommet. permet de construire les vertex buffers openGL...
void bounds(Point &pmin, Point &pmax)
renvoie min et max les coordonnees des extremites des positions des sommets de l'objet (boite engloba...
void release_text(Text &text)
detruit une console.
std::size_t normal_buffer_size() const
renvoie la longueur (en octets) du normal buffer.
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
representation d'un objet / maillage.
void move(const float z)
rapproche / eloigne la camera du centre.
int update(const float time, const float delta)
a deriver et redefinir pour animer les objets en fonction du temps.
std::size_t vertex_buffer_size() const
renvoie la longueur (en octets) du vertex buffer.
int run()
execution de l'application.
GLuint read_texture(const int unit, const char *filename, const GLenum texel_type)
void program_uniform(const GLuint program, const char *uniform, const unsigned int v)
affecte une valeur a un uniform du shader program. uint.
void draw(Mesh &m, const Transform &model, const Transform &view, const Transform &projection, const GLuint texture)
applique une texture a la surface de l'objet. ne fonctionne que si les coordonnees de textures sont f...
int quit()
a deriver pour detruire les objets openGL.
Transform view() const
renvoie la transformation vue.
int window_width()
renvoie la largeur de la fenetre de l'application.
representation d'un vecteur 3d.
void release()
detruit les objets openGL.
Transform projection(const float width, const float height, const float fov) const
renvoie la projection reglee pour une image d'aspect width / height, et une ouverture de fov degres...
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
void lookat(const Point ¢er, const float size)
observe le point center a une distance size.
GLuint read_program(const char *filename, const char *definitions)
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
Transform Translation(const Vector &v)
renvoie la matrice representant une translation par un vecteur.
void translation(const float x, const float y)
deplace le centre / le point observe.
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().
void rotation(const float x, const float y)
change le point de vue / la direction d'observation.
int window_height()
renvoie la hauteur de la fenetre de l'application.
representation d'un point 3d.
void clear(Text &text)
efface le contenu de la console.
int vertex_count() const
renvoie le nombre de sommets.
int render()
a deriver pour afficher les objets.
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes. utiliser glDrawArrays pour l'afficher. a detruire avec Mesh::release( ).