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( ) : 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_fast("data/bigguy.obj");
69 //~ mesh= read_mesh_fast("/home/jciehl/scenes/flying-world/export.obj");
70 //~ mesh= read_mesh_fast("/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( USE_POSITION );
77 n= mesh.vertex_count();
78
79 program_record= read_program("gkit2_tutos/M2/fragment_record.glsl");
80 program_print_errors(program_record);
81
82 program_replay= read_program("gkit2_tutos/M2/fragment.glsl");
83 program_print_errors(program_replay);
84
85 //
86 size= 1024*1024*8;
87
88 glGenBuffers(1, &buffer);
89 glBindBuffer(GL_ARRAY_BUFFER, buffer);
90 glBufferData(GL_ARRAY_BUFFER, size * 6*sizeof(float), nullptr, GL_DYNAMIC_DRAW);
91
92 //
94
95 //
96 glGenVertexArrays(1, &vao_replay);
97
98 glBindVertexArray(vao_replay);
99 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (const void *) (1*sizeof(unsigned)));
100 glEnableVertexAttribArray(0);
101 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (const void *) (1*sizeof(unsigned) + 3*sizeof(float)));
102 glEnableVertexAttribArray(1);
103
104 // etat openGL par defaut
105 glBindVertexArray(0);
106 glBindBuffer(GL_ARRAY_BUFFER, 0);
107
108 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
109
110 glClearDepth(1.f); // profondeur par defaut
111 glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la camera
112 glEnable(GL_DEPTH_TEST); // activer le ztest
113
114 glFrontFace(GL_CCW);
115 glCullFace(GL_BACK);
116 glEnable(GL_CULL_FACE);
117
118 return 0; // pas d'erreur, sinon renvoyer -1
119 }
120
121 // destruction des objets de l'application
122 int quit( )
123 {
124 repere.release();
125 return 0; // pas d'erreur
126 }
127
128 // dessiner une nouvelle image
129 int render( )
130 {
131 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132
133 Transform model= Identity();
134 Transform view= camera().view();
135 Transform projection= camera().projection();
136 Transform mv= view * model;
137 Transform mvp= projection * mv;
138
139 // clear
140 static bool init= false;
141 static bool zinit= false;
142 static unsigned replay= 0;
143
144 if(key_state('z'))
145 {
146 clear_key_state('z');
147 zinit= !zinit;
148 init= false; // force une nouvelle capture
149
150 if(zinit)
151 printf("Z prepass ON\n");
152 else
153 printf("Z prepass off\n");
154 }
155
156 //
157 static bool cull= true;
158 if(key_state('f'))
159 {
160 clear_key_state('f');
161
162 cull= !cull;
163 if(cull)
164 glEnable(GL_CULL_FACE);
165 else
166 glDisable(GL_CULL_FACE);
167
168 if(cull)
169 printf("face culling ON\n");
170 else
171 printf("face culling off\n");
172 }
173
174 static bool early_tests= true;
175 static const char *early_tests_definition= "";
176 if(key_state('e'))
177 {
178 clear_key_state('e');
179
180 early_tests= !early_tests;
181 if(early_tests)
182 early_tests_definition= "#define EARLY_TESTS";
183 else
184 early_tests_definition= "#undef EARLY_TESTS";
185
186 if(early_tests)
187 printf("early fragment tests ON\n");
188 else
189 printf("early fragment test off\n");
190
191 reload_program(program_record, "gkit2_tutos/M2/fragment_record.glsl", early_tests_definition);
192 program_print_errors(program_record);
193 }
194
195 if(!init)
196 {
197 if(zinit)
198 {
199 draw(mesh, model, view, projection);
200 }
201
202 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buffer);
203 {
204 float zero= 0;
205 glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R32F, GL_RED, GL_FLOAT, &zero);
206 }
207
208 glBindImageTexture(0, texture, 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32UI);
209 {
210 GLuint zero= 0;
211 glClearTexImage(texture, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
212 }
213
214 replay= 0;
215
216 // passe 1 : record
217 glBindVertexArray(vao);
218 glUseProgram(program_record);
219
220 program_uniform(program_record, "mvpMatrix", mvp);
221 program_uniform(program_record, "mvMatrix", mv);
222 program_uniform(program_record, "counters", 0);
223
224 glDrawArrays(GL_TRIANGLES, 0, n);
225
226 glMemoryBarrier( GL_ALL_BARRIER_BITS);
227
228 if(key_state(' '))
229 {
230 clear_key_state(' ');
231 init= true;
232
233 // rappel de la config...
234 printf("capture: ");
235 if(zinit) printf("Z prepass ON, ");
236 else printf("Z prepass off, ");
237 if(cull) printf("face culling ON, ");
238 else printf("face culling off, ");
239 if(early_tests) printf("early fragment tests ON");
240 else printf("early fragment test off");
241 printf("\n");
242 }
243 }
244
245 if(init)
246 {
247 // passe 2 : replay
248 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
249
250 draw(repere, /* model */ Identity(), view, projection);
251
252 glBindVertexArray(vao_replay);
253 glUseProgram(program_replay);
254
255 program_uniform(program_replay, "mvpMatrix", mvp);
256
257 replay+= 4096;
258 if(replay > size)
259 replay= 0;
260 glDrawArrays(GL_POINTS, 0, replay);
261
262 if(key_state(' ')) // on recommence
263 {
264 clear_key_state(' ');
265 init= false;
266 }
267
268 static unsigned points= 1;
269 if(key_state(SDLK_KP_PLUS))
270 {
271 clear_key_state(SDLK_KP_PLUS);
272 points+= 1;
273 }
274 if(key_state(SDLK_KP_MINUS))
275 {
276 clear_key_state(SDLK_KP_MINUS);
277 points-= 1;
278 if(points < 1)
279 points= 1;
280 }
281 glPointSize(points);
282
283 static bool video= false;
284 if(key_state(SDLK_RETURN))
285 {
286 clear_key_state(SDLK_RETURN);
287 video= !video;
288
289 if(video) printf("start video capture...\n");
290 else printf("stop video capture.\n");
291 }
292
293 if(video)
294 capture("fragments");
295 }
296
297 return 1;
298 }
299
300protected:
301 Mesh repere;
302 Mesh mesh;
303
304 GLuint vao;
305 GLuint vao_replay;
306 unsigned n;
307
308 GLuint texture;
309 GLuint buffer;
310 unsigned size;
311
312 GLuint program_record;
313 GLuint program_replay;
314};
315
316
317int main( int argc, char **argv )
318{
319 // il ne reste plus qu'a creer un objet application et la lancer
320 TP tp;
321 tp.run();
322
323 return 0;
324}
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
int run()
execution de l'application.
Definition app.cpp:36
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:62
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:59
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:53
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:217
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
Definition program.cpp:461
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