gKit2 light
Loading...
Searching...
No Matches
fragments.cpp
Go to the documentation of this file.
1
3
4
5#include "wavefront_fast.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.
16Mesh 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
55class TP : public AppCamera
56{
57public:
58 // constructeur : donner les dimensions de l'image, et eventuellement la version d'openGL.
59 TP( const char *filename ) : AppCamera(1024, 640, 4, 3)
60 {
61 // charge un objet
62 mesh= read_mesh_fast( filename );
63 }
64
65 // creation des objets de l'application
66 int init( )
67 {
68 // decrire un repere / grille
69 repere= make_grid(10);
70
71 if(mesh.vertex_count() == 0)
72 return -1;
73
74 Point pmin, pmax;
75 mesh.bounds(pmin, pmax);
76 camera().lookat(pmin, pmax);
77
78 vao= mesh.create_buffers( USE_POSITION );
79 n= mesh.vertex_count();
80
81 program_record= read_program("gkit2_tutos/M2/fragment_record.glsl");
82 program_print_errors(program_record);
83
84 program_replay= read_program("gkit2_tutos/M2/fragment.glsl");
85 program_print_errors(program_replay);
86
87 //
88 size= 1024*1024*8;
89
90 glGenBuffers(1, &buffer);
91 glBindBuffer(GL_ARRAY_BUFFER, buffer);
92 glBufferData(GL_ARRAY_BUFFER, size * 6*sizeof(float), nullptr, GL_DYNAMIC_DRAW);
93
94 //
96
97 //
98 glGenVertexArrays(1, &vao_replay);
99
100 glBindVertexArray(vao_replay);
101 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (const void *) (1*sizeof(unsigned)));
102 glEnableVertexAttribArray(0);
103 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (const void *) (1*sizeof(unsigned) + 3*sizeof(float)));
104 glEnableVertexAttribArray(1);
105
106 // etat openGL par defaut
107 glBindVertexArray(0);
108 glBindBuffer(GL_ARRAY_BUFFER, 0);
109
110 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
111
112 glClearDepth(1.f); // profondeur par defaut
113 glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la camera
114 glEnable(GL_DEPTH_TEST); // activer le ztest
115
116 glFrontFace(GL_CCW);
117 glCullFace(GL_BACK);
118 glEnable(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 // clear
142 static bool init= false;
143 static bool zinit= false;
144 static unsigned replay= 0;
145
146 if(key_state('z'))
147 {
148 clear_key_state('z');
149 zinit= !zinit;
150 init= false; // force une nouvelle capture
151
152 if(zinit)
153 printf("Z prepass ON\n");
154 else
155 printf("Z prepass off\n");
156 }
157
158 //
159 static bool cull= true;
160 if(key_state('f'))
161 {
162 clear_key_state('f');
163 cull= !cull;
164 init= false; // force une nouvelle capture
165
166 if(cull)
167 glEnable(GL_CULL_FACE);
168 else
169 glDisable(GL_CULL_FACE);
170
171 if(cull)
172 printf("face culling ON\n");
173 else
174 printf("face culling off\n");
175 }
176
177 static bool reload= false;
178 static bool early_tests= true;
179 static const char *early_tests_definition= "";
180 if(key_state('e'))
181 {
182 clear_key_state('e');
183 early_tests= !early_tests;
184 init= false; // force une nouvelle capture
185 reload= true; // recompile les shaders
186
187 if(early_tests)
188 early_tests_definition= "#define EARLY_TESTS";
189 else
190 early_tests_definition= "#undef EARLY_TESTS";
191
192 if(early_tests)
193 printf("early fragment tests ON\n");
194 else
195 printf("early fragment test off\n");
196 }
197
198 static bool store_vertices= false;
199 static const char *store_vertices_definition= "";
200 if(key_state('x'))
201 {
202 clear_key_state('x');
203 store_vertices= !store_vertices;
204 init= false; // force une nouvelle capture
205 reload= true; // recompile les shaders
206
207 if(store_vertices)
208 store_vertices_definition= "#define STORE_VERTICES";
209 else
210 store_vertices_definition= "#undef STORE_VERTICES";
211
212 if(store_vertices)
213 printf("store vertices ON\n");
214 else
215 printf("store vertices off\n");
216 }
217
218 if(reload)
219 {
220 reload= false;
221
222 std::string definitions;
223 definitions.append(early_tests_definition).append("\n").append(store_vertices_definition);
224
225 reload_program(program_record, definitions.c_str());
226 program_print_errors(program_record);
227 }
228
229
230 if(!init)
231 {
232 if(zinit)
233 {
234 draw(mesh, model, view, projection);
235 }
236
237 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buffer);
238 {
239 float zero= 0;
240 glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32F, GL_RED, GL_FLOAT, &zero);
241 }
242
243 glBindImageTexture(0, texture, 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32UI);
244 {
245 GLuint zero= 0;
246 glClearTexImage(texture, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
247 }
248
249 replay= 0;
250
251 // passe 1 : record
252 glBindVertexArray(vao);
253 glUseProgram(program_record);
254
255 program_uniform(program_record, "mvpMatrix", mvp);
256 program_uniform(program_record, "mvMatrix", mv);
257 program_uniform(program_record, "counters", 0);
258
259 glDrawArrays(GL_TRIANGLES, 0, n);
260
261 glMemoryBarrier( GL_ALL_BARRIER_BITS);
262
263 if(key_state(' '))
264 {
265 clear_key_state(' ');
266 init= true;
267
268 // rappel de la config...
269 printf("capture: ");
270 if(zinit) printf("Z prepass ON, ");
271 else printf("Z prepass off, ");
272 if(store_vertices) printf("store vertices ON, ");
273 else printf("store vertices off, ");
274 if(cull) printf("face culling ON, ");
275 else printf("face culling off, ");
276 if(early_tests) printf("early fragment tests ON");
277 else printf("early fragment test off");
278 printf("\n");
279 }
280 }
281
282 if(init)
283 {
284 // passe 2 : replay
285 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
286
287 draw(repere, /* model */ Identity(), view, projection);
288
289 glBindVertexArray(vao_replay);
290 glUseProgram(program_replay);
291
292 program_uniform(program_replay, "mvpMatrix", mvp);
293
294 replay+= 4096;
295 if(replay > size)
296 replay= 0;
297 glDrawArrays(GL_POINTS, 0, replay);
298
299 if(key_state(' ')) // on recommence
300 {
301 clear_key_state(' ');
302 init= false;
303 }
304
305 static unsigned points= 1;
306 if(key_state(SDLK_KP_PLUS))
307 {
308 clear_key_state(SDLK_KP_PLUS);
309 points+= 1;
310 }
311 if(key_state(SDLK_KP_MINUS))
312 {
313 clear_key_state(SDLK_KP_MINUS);
314 points-= 1;
315 if(points < 1)
316 points= 1;
317 }
318 glPointSize(points);
319
320 static bool video= false;
321 if(key_state(SDLK_RETURN))
322 {
323 clear_key_state(SDLK_RETURN);
324 video= !video;
325
326 if(video) printf("start video capture...\n");
327 else printf("stop video capture.\n");
328 }
329
330 if(video)
331 capture("fragments");
332 }
333
334 return 1;
335 }
336
337protected:
338 Mesh repere;
339 Mesh mesh;
340
341 GLuint vao;
342 GLuint vao_replay;
343 unsigned n;
344
345 GLuint texture;
346 GLuint buffer;
347 unsigned size;
348
349 GLuint program_record;
350 GLuint program_replay;
351};
352
353
354int main( int argc, char **argv )
355{
356 const char *filename= "data/bigguy.obj";
357 if(argc > 1)
358 filename= argv[1];
359
360 // il ne reste plus qu'a creer un objet application et la lancer
361 window_resize(false);
362
363 TP tp( filename );
364 tp.run();
365
366 return 0;
367}
classe application.
Definition app_camera.h:19
const Orbiter & camera() const
renvoie l'orbiter gere par l'application.
Definition app_camera.h:37
AppCamera(const int width, const int height, const int major=3, const int minor=3)
constructeur, dimensions de la fenetre et version d'openGL.
Definition app_camera.cpp:5
representation d'un objet / maillage.
Definition mesh.h:121
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:97
Mesh & color(const vec4 &c)
definit la couleur du prochain sommet.
Definition mesh.cpp:66
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:48
Transform view() const
renvoie la transformation vue.
Definition orbiter.cpp:41
Definition alpha.cpp:58
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 init()
a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
Definition fragments.cpp:66
bool window_resize()
renvoie vrai si la fenetre peut etre redimensionnee.
Definition window.cpp:37
int window_height()
renvoie la hauteur de la fenetre de l'application.
Definition window.cpp:27
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
Definition window.cpp:69
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:63
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition window.cpp:23
Color Red()
utilitaire. renvoie une couleur rouge.
Definition color.cpp:28
Color Blue()
utilitaire. renvoie une couleur bleue.
Definition color.cpp:38
Color Green()
utilitaire. renvoie une couleur verte.
Definition color.cpp:33
Color White()
utilitaire. renvoie une couleur blanche.
Definition color.cpp:23
Transform Identity()
construit la transformation identite.
Definition mat.cpp:187
Mesh read_mesh_fast(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
@ USE_POSITION
inclut l'attribut position dans les buffers.
Definition mesh.h:112
int capture(const char *prefix)
Definition texture.cpp:205
GLuint read_program(const char *filename, const char *definitions)
Definition program.cpp:214
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
Definition program.cpp:459
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:151
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