gKit2 light
tuto_stream.cpp
Go to the documentation of this file.
1 
3 
4 #include <cstdlib>
5 #include <ctime>
6 #include <chrono>
7 
8 #include "vec.h"
9 #include "mat.h"
10 
11 #include "mesh.h"
12 #include "wavefront.h"
13 
14 #include "program.h"
15 #include "uniforms.h"
16 
17 #include "orbiter.h"
18 #include "app_time.h"
19 
20 
21 class DrawInstanceBuffer : public AppTime
22 {
23 public:
24  // constructeur : donner les dimensions de l'image, et eventuellement la version d'openGL.
25  DrawInstanceBuffer( ) : AppTime(1024, 640, 4,4) {}
26 
27  int init( )
28  {
29  // charge un mesh
30  Mesh mesh= read_mesh("data/cube.obj");
31  if(mesh == Mesh::error()) return -1;
32 
33  Point pmin, pmax;
34  mesh.bounds(pmin, pmax);
35  m_camera.lookat(pmin - Vector(40, 40, 40), pmax + Vector(40, 40, 40));
36 
37  m_vertex_count= mesh.vertex_count();
38 
39  // cree les buffers et le vao
40  glGenVertexArrays(1, &m_vao);
41  glBindVertexArray(m_vao);
42 
43  // buffer : positions + normals
44  glGenBuffers(1, &m_buffer);
45  glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
46 
47  size_t size= mesh.vertex_buffer_size() + mesh.normal_buffer_size();
48  glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_STATIC_DRAW);
49 
50  // transfere les positions des sommets
51  size_t offset_positions= 0;
52  size= mesh.vertex_buffer_size();
53  glBufferSubData(GL_ARRAY_BUFFER, offset_positions, size, mesh.vertex_buffer());
54  // configure l'attribut 0, vec3 position
55  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, /* stride */ 0, (const GLvoid *) offset_positions);
56  glEnableVertexAttribArray(0);
57 
58  // transfere les normales des sommets
59  size_t offset_normals= offset_positions + size;
60  size= mesh.normal_buffer_size();
61  glBufferSubData(GL_ARRAY_BUFFER, offset_normals, size, mesh.normal_buffer());
62  // configure l'attribut 2, vec3 normal
63  glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, /* stride */ 0, (const GLvoid *) offset_normals);
64  glEnableVertexAttribArray(2);
65 
66  // remarque : oui, c'est mal et la suite montre comment le faire correctement !!
67 
68  // instance buffer, position aleatoire des cubes...
69  srand(time(nullptr));
70 
71  for(int i= 0; i < 1024*1024; i++)
72  {
73  float x= rand() % 256 - 128;
74  float y= rand() % 256 - 128;
75  float z= rand() % 256 - 128;
76 
77  m_positions.push_back(vec3(x *4, y *4, z *4));
78  }
79 
80 
81  m_instance_count= int(m_positions.size());
82 
83  printf("buffer %dKB\n", int(sizeof(vec3) * m_positions.size() / 1024));
84 
85  // cree et initialise le buffer d'instance
86  // version openGL 3, buffer dynamique + glBufferSubData()
87  glGenBuffers(1, &m_instance_buffer);
88  glBindBuffer(GL_ARRAY_BUFFER, m_instance_buffer);
89  glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * m_positions.size(), m_positions.data(), GL_DYNAMIC_DRAW);
90 
91  // configure le vao pour l'attribut d'instance
92  // configure l'attribut 1, vec3 instance_position
93  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, /* stride */ 0, /* offset */ 0);
94  glVertexAttribDivisor(1, 1); // !! c'est la seule difference entre un attribut de sommet et un attribut d'instance !!
95  glEnableVertexAttribArray(1);
96 
97  // openGL 4.4, buffer dynamique explicite + map persistant
98  glGenBuffers(1, &m_instance_storage);
99  glBindBuffer(GL_ARRAY_BUFFER, m_instance_storage);
100  glBufferStorage(GL_ARRAY_BUFFER, sizeof(vec3) * m_positions.size(), nullptr, GL_DYNAMIC_STORAGE_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT);
101 
102  m_storage= glMapBufferRange(GL_ARRAY_BUFFER, 0, sizeof(vec3) * m_positions.size(),
103  GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_PERSISTENT_BIT);
104  if(m_storage == nullptr)
105  return -1;
106 
107  // cree et initialise un autre vao
108  glGenVertexArrays(1, &m_vao_storage);
109  glBindVertexArray(m_vao_storage);
110 
111  glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
112  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, /* stride */ 0, (const GLvoid *) offset_positions);
113  glEnableVertexAttribArray(0);
114  glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, /* stride */ 0, (const GLvoid *) offset_normals);
115  glEnableVertexAttribArray(2);
116 
117  glBindBuffer(GL_ARRAY_BUFFER, m_instance_storage);
118  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, /* stride */ 0, /* offset */ 0);
119  glVertexAttribDivisor(1, 1); // !! c'est la seule difference entre un attribut de sommet et un attribut d'instance !!
120  glEnableVertexAttribArray(1);
121 
122  glBindVertexArray(0);
123 
124  //
125  mesh.release();
126  glBindVertexArray(0);
127  glBindBuffer(GL_ARRAY_BUFFER, 0);
128 
129  // shaders
130  m_program= read_program("tutos/instance_buffer.glsl");
131  program_print_errors(m_program);
132 
133  // etat openGL par defaut
134  glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
135 
136  glClearDepth(1.f); // profondeur par defaut
137  glDepthFunc(GL_LESS); // ztest, conserver l'intersection la plus proche de la camera
138  glEnable(GL_DEPTH_TEST); // activer le ztest
139 
140  return 0; // ras, pas d'erreur
141  }
142 
143  // destruction des objets de l'application
144  int quit( )
145  {
146  release_program(m_program);
147  glDeleteVertexArrays(1, &m_vao);
148  glDeleteBuffers(1, &m_buffer);
149  glDeleteBuffers(1, &m_instance_buffer);
150  return 0;
151  }
152 
153  int update( const float time, const float delta )
154  {
155  m_model= RotationY(time / 20);
156  return 0;
157  }
158 
159  // dessiner une nouvelle image
160  int render( )
161  {
162  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
163 
164  // deplace la camera
165  int mx, my;
166  unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
167  if(mb & SDL_BUTTON(1)) // le bouton gauche est enfonce
168  m_camera.rotation(mx, my);
169  else if(mb & SDL_BUTTON(3)) // le bouton droit est enfonce
170  m_camera.move(mx);
171  else if(mb & SDL_BUTTON(2)) // le bouton du milieu est enfonce
172  m_camera.translation((float) mx / (float) window_width(), (float) my / (float) window_height());
173 
174  static int mode= 0;
175  if(key_state(SDLK_SPACE))
176  {
177  clear_key_state(SDLK_SPACE);
178  mode= (mode + 1) % 5;
179  printf("mode %d\n", mode);
180 
181  // alterne entre les 4 solutions de transfert...
182  }
183 
184  // modifie le contenu du buffer d'instances
185  if(mode == 0)
186  {
187  // strategie 1 :
188  // recreer le buffer + transferer les donnees
189  glBindBuffer(GL_ARRAY_BUFFER, m_instance_buffer);
190  glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * m_positions.size(), m_positions.data(), GL_DYNAMIC_DRAW);
191 
192  glBindVertexArray(m_vao);
193  }
194  else if(mode == 1)
195  {
196  // strategie 2 :
197  // transferer les donnees
198  glBindBuffer(GL_ARRAY_BUFFER, m_instance_buffer);
199  glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vec3) * m_positions.size(), m_positions.data());
200 
201  glBindVertexArray(m_vao);
202  }
203  else if(mode == 2)
204  {
205  // strategie 3 :
206  // invalider les donnees + transfert
207  glInvalidateBufferData(m_instance_buffer);
208  glBindBuffer(GL_ARRAY_BUFFER, m_instance_buffer);
209  glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vec3) * m_positions.size(), m_positions.data());
210 
211  glBindVertexArray(m_vao);
212  }
213  else if(mode == 3)
214  {
215  // strategie 4 :
216  // persistant map + flush + barrier
217 
218  std::chrono::high_resolution_clock::time_point copy_start= std::chrono::high_resolution_clock::now();
219  memcpy(m_storage, m_positions.data(), sizeof(vec3) * m_positions.size());
220 
221  std::chrono::high_resolution_clock::time_point copy_stop= std::chrono::high_resolution_clock::now();
222  auto copy_time= std::chrono::duration_cast<std::chrono::microseconds>(copy_stop - copy_start).count();
223  printf("memcpy %dK %02dus = %.2fK/s\n", int(sizeof(vec3) * m_positions.size()) / 1024, int(copy_time), float(sizeof(vec3) * m_positions.size() / 1024) / (float(copy_time) / 1000000));
224 
225  glBindBuffer(GL_ARRAY_BUFFER, m_instance_storage);
226  glFlushMappedBufferRange(GL_ARRAY_BUFFER, 0, sizeof(vec3) * m_positions.size());
227 
228  glMemoryBarrier(GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT);
229 
230  glBindVertexArray(m_vao_storage);
231  }
232  else
233  {
234  // pas de modifications
235  glBindVertexArray(m_vao);
236  }
237 
238  // draw
239  glUseProgram(m_program);
240 
241  Transform m= m_model;
242  Transform v= m_camera.view();
243  Transform p= m_camera.projection(window_width(), window_height(), 45);
244  Transform mvp= p * v * m;
245  Transform mv= v * m;
246 
247  program_uniform(m_program, "mvpMatrix", mvp);
248  program_uniform(m_program, "normalMatrix", mv.normal());
249 
250  glDrawArraysInstanced(GL_TRIANGLES, 0, m_vertex_count, std::min(m_instance_count, 4096));
251 
252  return 1;
253  }
254 
255 protected:
256  std::vector<vec3> m_positions;
257 
258  // solution openGL3
259  GLuint m_vao;
260  GLuint m_instance_buffer;
261  // solution openGL4
262  GLuint m_vao_storage;
263  GLuint m_instance_storage;
264  void *m_storage;
265 
266  Transform m_model;
267  Orbiter m_camera;
268  GLuint m_buffer;
269 
270  GLuint m_program;
271  int m_vertex_count;
272  int m_instance_count;
273 };
274 
275 
276 int main( int argc, char **argv )
277 {
279  tp.run();
280 
281  return 0;
282 }
283 
int run()
execution de l'application.
Definition: app.cpp:36
int render()
a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
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.
Definition: tuto_stream.cpp:27
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
representation d'un objet / maillage.
Definition: mesh.h:112
static Mesh & error()
Definition: mesh.h:348
const float * vertex_buffer() const
renvoie l'adresse de la position du premier sommet. permet de construire les vertex buffers openGL....
Definition: mesh.h:296
void bounds(Point &pmin, Point &pmax) const
renvoie min et max les coordonnees des extremites des positions des sommets de l'objet (boite engloba...
Definition: mesh.cpp:501
std::size_t vertex_buffer_size() const
renvoie la longueur (en octets) du vertex buffer.
Definition: mesh.h:298
int vertex_count() const
renvoie le nombre de sommets.
Definition: mesh.h:291
void release()
detruit les objets openGL.
Definition: mesh.cpp:62
std::size_t normal_buffer_size() const
renvoie la longueur (en octets) du normal buffer.
Definition: mesh.h:303
const float * normal_buffer() const
renvoie l'adresse de la normale du premier sommet. par convention, la normale est un vec3,...
Definition: mesh.h:301
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
Definition: orbiter.h:17
void lookat(const Point &center, const float size)
observe le point center a une distance size.
Definition: orbiter.cpp:7
void move(const float z)
rapproche / eloigne la camera du centre.
Definition: orbiter.cpp:33
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...
Definition: orbiter.cpp:47
void translation(const float x, const float y)
deplace le centre / le point observe.
Definition: orbiter.cpp:27
void rotation(const float x, const float y)
change le point de vue / la direction d'observation.
Definition: orbiter.cpp:21
Transform view() const
renvoie la transformation vue.
Definition: orbiter.cpp:40
int window_height()
renvoie la hauteur de la fenetre de l'application.
Definition: window.cpp:29
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
Definition: window.cpp:48
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().
Definition: text.cpp:140
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
Definition: window.cpp:42
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition: window.cpp:25
Point min(const Point &a, const Point &b)
renvoie la plus petite composante de chaque point. x, y, z= min(a.x, b.x), min(a.y,...
Definition: vec.cpp:30
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Definition: mat.cpp:242
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition: wavefront.cpp:14
GLuint read_program(const char *filename, const char *definitions)
Definition: program.cpp:204
void program_uniform(const GLuint program, const char *uniform, const std::vector< unsigned > &v)
affecte un tableau de valeurs a un uniform du shader program.
Definition: uniforms.cpp:94
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition: program.cpp:432
int release_program(const GLuint program)
detruit les shaders et le program.
Definition: program.cpp:211
representation d'un point 3d.
Definition: vec.h:21
representation d'une transformation, une matrice 4x4, organisee par ligne / row major.
Definition: mat.h:21
Transform normal() const
renvoie la transformation a appliquer aux normales d'un objet transforme par la matrice m.
Definition: mat.cpp:181
representation d'un vecteur 3d.
Definition: vec.h:59
vecteur generique, utilitaire.
Definition: vec.h:146