gKit2 light
fragments.cpp
Go to the documentation of this file.
1 
3 
4 
5 #include "wavefront.h"
6 #include "texture.h"
7 
8 #include "program.h"
9 #include "uniforms.h"
10 
11 #include "draw.h"
12 #include "app_camera.h" // classe Application a deriver
13 
14 
15 // utilitaire. creation d'une grille / repere.
16 Mesh make_grid( const int n= 10 )
17 {
18  Mesh grid= Mesh(GL_LINES);
19 
20  // grille
21  grid.color(White());
22  for(int x= 0; x < n; x++)
23  {
24  float px= float(x) - float(n)/2 + .5f;
25  grid.vertex(Point(px, 0, - float(n)/2 + .5f));
26  grid.vertex(Point(px, 0, float(n)/2 - .5f));
27  }
28 
29  for(int z= 0; z < n; z++)
30  {
31  float pz= float(z) - float(n)/2 + .5f;
32  grid.vertex(Point(- float(n)/2 + .5f, 0, pz));
33  grid.vertex(Point(float(n)/2 - .5f, 0, pz));
34  }
35 
36  // axes XYZ
37  grid.color(Red());
38  grid.vertex(Point(0, .1, 0));
39  grid.vertex(Point(1, .1, 0));
40 
41  grid.color(Green());
42  grid.vertex(Point(0, .1, 0));
43  grid.vertex(Point(0, 1, 0));
44 
45  grid.color(Blue());
46  grid.vertex(Point(0, .1, 0));
47  grid.vertex(Point(0, .1, 1));
48 
49  glLineWidth(2);
50 
51  return grid;
52 }
53 
54 
55 class TP : public AppCamera
56 {
57 public:
58  // constructeur : donner les dimensions de l'image, et eventuellement la version d'openGL.
59  TP( ) : AppCamera(1024, 640) {}
60 
61  // creation des objets de l'application
62  int init( )
63  {
64  // decrire un repere / grille
65  repere= make_grid(10);
66 
67  // charge un objet
68  //~ mesh= read_mesh("data/cube.obj");
69  //~ mesh= read_mesh("/home/jciehl/scenes/flying-world/export.obj");
70  mesh= read_mesh("/home/jciehl/scenes/bistro/exterior.obj");
71 
72  Point pmin, pmax;
73  mesh.bounds(pmin, pmax);
74  camera().lookat(pmin, pmax);
75 
76  //~ vao= mesh.create_buffers( false, false, false, false ); // positions
77  vao= mesh.create_buffers( true, true, true, true ); // positions
78  n= mesh.vertex_count();
79 
80  program_record= read_program("tutos/M2/fragment_record.glsl");
81  program_print_errors(program_record);
82 
83  program_replay= read_program("tutos/M2/fragment.glsl");
84  program_print_errors(program_replay);
85 
86  //
87  size= 1024*1024*8;
88 
89  glGenBuffers(1, &buffer);
90  glBindBuffer(GL_ARRAY_BUFFER, buffer);
91  glBufferData(GL_ARRAY_BUFFER, size * 6*sizeof(float), nullptr, GL_DYNAMIC_DRAW);
92 
93  //
95 
96  //
97  glGenVertexArrays(1, &vao_replay);
98 
99  glBindVertexArray(vao_replay);
100  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (const void *) (4*sizeof(unsigned)));
101  glEnableVertexAttribArray(0);
102  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (const void *) (4*sizeof(unsigned) + 3*sizeof(float)));
103  glEnableVertexAttribArray(1);
104 
105  // etat openGL par defaut
106  glBindVertexArray(0);
107  glBindBuffer(GL_ARRAY_BUFFER, 0);
108 
109  glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
110 
111  glClearDepth(1.f); // profondeur par defaut
112  glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la camera
113  glEnable(GL_DEPTH_TEST); // activer le ztest
114 
115  glFrontFace(GL_CCW);
116  glCullFace(GL_BACK);
117  glEnable(GL_CULL_FACE);
118  //~ glDisable(GL_CULL_FACE);
119 
120  return 0; // pas d'erreur, sinon renvoyer -1
121  }
122 
123  // destruction des objets de l'application
124  int quit( )
125  {
126  repere.release();
127  return 0; // pas d'erreur
128  }
129 
130  // dessiner une nouvelle image
131  int render( )
132  {
133  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
134 
135  Transform model= Identity();
136  Transform view= camera().view();
137  Transform projection= camera().projection();
138  Transform mv= view * model;
139  Transform mvp= projection * mv;
140 
141  //~ draw(repere, /* model */ Identity(), view, projection);
142 
143  // clear
144  static unsigned init= 0;
145  static unsigned zinit= 0;
146 
147  if(key_state('z'))
148  {
149  clear_key_state('z');
150  zinit= (zinit +1) %2;
151 
152  init= 0; // force une nouvelle captire
153 
154  if(zinit)
155  printf("Zpre pass ON\n");
156  else
157  printf("Zpre pass OFF\n");
158  }
159 
160  if(!init)
161  {
162  if(zinit)
163  {
164  draw(mesh, model, view, projection);
165  }
166 
167  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buffer);
168  {
169  float zero= 0;
170  glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32F, GL_RED, GL_FLOAT, &zero);
171  }
172 
173  /* indirect draw parameters
174  uint count= 0;
175  uint instance_count= 1;
176  uint vertex_base= 0;
177  uint instance_base= 0;
178  */
179  unsigned indirect[]= { 0, 1, 0, 0};
180  glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(indirect), indirect);
181 
182  glBindImageTexture(0, texture, 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32UI);
183  {
184  GLuint zero= 0;
185  glClearTexImage(texture, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
186  }
187 
188  // passe 1 : record
189  glBindVertexArray(vao);
190  glUseProgram(program_record);
191 
192  program_uniform(program_record, "mvpMatrix", mvp);
193  program_uniform(program_record, "mvMatrix", mv);
194  program_uniform(program_record, "counters", 0);
195 
196  glDrawArrays(GL_TRIANGLES, 0, n);
197 
198  glMemoryBarrier( GL_ALL_BARRIER_BITS);
199 
200  if(key_state(' '))
201  {
202  clear_key_state(' ');
203  init= 1;
204  }
205  }
206 
207  if(init)
208  {
209  // passe 2 : replay
210  static unsigned replay= 0;
211 
212  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
213 
214  draw(repere, /* model */ Identity(), view, projection);
215 
216  glBindVertexArray(vao_replay);
217 
218  glBindBuffer(GL_PARAMETER_BUFFER_ARB, buffer);
219  glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer);
220 
221  glUseProgram(program_replay);
222 
223  program_uniform(program_replay, "mvpMatrix", mvp);
224 
225  replay+= 4096;
226  if(replay > size)
227  replay= 0;
228  glDrawArrays(GL_POINTS, 0, replay);
229 
230 
231  static unsigned points= 1;
232  if(key_state(SDLK_KP_PLUS))
233  {
234  clear_key_state(SDLK_KP_PLUS);
235  points+= 1;
236  }
237  if(key_state(SDLK_KP_MINUS))
238  {
239  clear_key_state(SDLK_KP_MINUS);
240  points-= 1;
241  if(points < 1)
242  points= 1;
243  }
244  glPointSize(points);
245 
246  static bool video= false;
247  if(key_state(SDLK_RETURN))
248  {
249  clear_key_state(SDLK_RETURN);
250  video= !video;
251 
252  if(video) printf("start video capture...\n");
253  else printf("stop video capture.\n");
254  }
255 
256  if(video)
257  capture("fragments");
258  }
259 
260  return 1;
261  }
262 
263 protected:
264  Mesh repere;
265  Mesh mesh;
266 
267  GLuint vao;
268  GLuint vao_replay;
269  unsigned n;
270 
271  GLuint texture;
272  GLuint buffer;
273  unsigned size;
274 
275  GLuint program_record;
276  GLuint program_replay;
277 };
278 
279 
280 int main( int argc, char **argv )
281 {
282  // il ne reste plus qu'a creer un objet application et la lancer
283  TP tp;
284  tp.run();
285 
286  return 0;
287 }
classe application.
Definition: app_camera.h:19
const Orbiter & camera() const
renvoie l'orbiter gere par l'application.
Definition: app_camera.h:37
int run()
execution de l'application.
Definition: app.cpp:36
representation d'un objet / maillage.
Definition: mesh.h:112
unsigned int vertex(const vec3 &p)
insere un sommet de position p, et ses attributs (s'ils sont definis par color(), texcoord(),...
Definition: mesh.cpp:111
GLuint create_buffers(const bool use_texcoord, const bool use_normal, const bool use_color, const bool use_material_index)
construit les buffers et le vertex array object necessaires pour dessiner l'objet avec openGL....
Definition: mesh.cpp:581
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:503
int vertex_count() const
renvoie le nombre de sommets.
Definition: mesh.h:291
void release()
detruit les objets openGL.
Definition: mesh.cpp:64
Mesh & color(const vec4 &c)
definit la couleur du prochain sommet.
Definition: mesh.cpp:80
void lookat(const Point &center, const float size)
observe le point center a une distance size.
Definition: orbiter.cpp:7
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
Transform view() const
renvoie la transformation vue.
Definition: orbiter.cpp:40
Definition: alpha.cpp:59
int render()
a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
Definition: fragments.cpp:131
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
Definition: fragments.cpp:124
int init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
Definition: fragments.cpp:62
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
Color Red()
utilitaire. renvoie une couleur rouge.
Definition: color.cpp:57
Color Blue()
utilitaire. renvoie une couleur bleue.
Definition: color.cpp:67
Color Green()
utilitaire. renvoie une couleur verte.
Definition: color.cpp:62
Color White()
utilitaire. renvoie une couleur blanche.
Definition: color.cpp:52
Transform Identity()
construit la transformation identite.
Definition: mat.cpp:187
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition: wavefront.cpp:14
int capture(const char *prefix)
Definition: texture.cpp:215
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
GLuint make_uint_texture(const int unit, const int width, const int height, const GLenum texel_type)
creation de textures pour stocker des donnees (autres qu'une couleur).
Definition: texture.cpp:161
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