gKit2 light
Loading...
Searching...
No Matches
aaquads.cpp
Go to the documentation of this file.
1
3
4#include "app_time.h"
5#include "app_camera.h" // gestion camera
6
7#include "vec.h"
8#include "mat.h"
9
10#include "draw.h"
11#include "program.h"
12#include "uniforms.h"
13#include "texture.h"
14
15#include "mesh.h"
16#include "wavefront_fast.h"
17
18#include "orbiter.h"
19
20
21class Quads : public AppTime
22{
23public:
24 // application openGL 4.4
25 Quads( const char *filename ) : AppTime(1024, 1024, 4,4)
26 {
27 m_mesh= read_indexed_mesh_fast( filename );
28 }
29
30 int init( )
31 {
32 if(m_mesh.vertex_count() == 0)
33 return -1; // pas de geometrie
34
35 Point pmin, pmax;
36 m_mesh.bounds(pmin, pmax);
37 m_camera.lookat(pmin, pmax);
38
39 // cree la texture, 1 canal, entiers 32bits non signes
40 m_aatexture= make_uint_texture(0, 8192, 8192); // ou 8x8 sur une image de base 1024x1024
41 m_aadepth= make_depth_texture(0, 8192, 8192); // ou 8x8 sur une image de base 1024x1024
42
43 glGenFramebuffers(1, &m_framebuffer);
44 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_framebuffer);
45 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_aatexture, 0);
46 glFramebufferTexture(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_aadepth, 0);
47
48 GLenum buffers[]= { GL_COLOR_ATTACHMENT0 };
49 glDrawBuffers(1, buffers);
50
51 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
52
53 // resultat
54 m_texture= make_uint_texture(0, 1024, 1024);
55
56 //
57 m_program= read_program("gkit2_tutos/M2/aaquads.glsl");
58 program_print_errors(m_program);
59 m_program_count= read_program("gkit2_tutos/M2/aaquads_count.glsl");
60 program_print_errors(m_program_count);
61
62 m_program_display= read_program("gkit2_tutos/M2/aaquads_display.glsl");
63 program_print_errors(m_program_display);
64
65 // etat openGL par defaut
66 glClearColor(0.2, 0.2, 0.2, 1); // couleur par defaut de la fenetre
67 glClearDepth(1); // profondeur par defaut
68 glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la m_camera
69 glEnable(GL_DEPTH_TEST); // activer le ztest
70
71 glFrontFace(GL_CCW);
72 glCullFace(GL_BACK);
73 glEnable(GL_CULL_FACE); // active le back face culling
74
75 return 0; // ras, pas d'erreur
76 }
77
78 // destruction des objets de l'application
79 int quit( )
80 {
81 m_mesh.release();
82 release_program(m_program);
83 release_program(m_program_count);
84 release_program(m_program_display);
85 glDeleteTextures(1, &m_texture);
86 glDeleteTextures(1, &m_aatexture);
87 glDeleteTextures(1, &m_aadepth);
88 glDeleteFramebuffers(1, &m_framebuffer);
89 return 0;
90 }
91
92 // dessiner une nouvelle image
93 int render( )
94 {
95 static bool zinit= false;
96 if(key_state('z'))
97 {
98 clear_key_state('z');
99 zinit= !zinit;
100
101 if(zinit)
102 printf("Z prepass ON\n");
103 else
104 printf("Z prepass off\n");
105 }
106 // plus tres utile...
107
108 static bool cull= true;
109 if(key_state('f'))
110 {
111 clear_key_state('f');
112
113 cull= !cull;
114 if(cull)
115 glEnable(GL_CULL_FACE);
116 else
117 glDisable(GL_CULL_FACE);
118
119 if(cull)
120 printf("face culling ON\n");
121 else
122 printf("face culling off\n");
123 }
124
125 static bool early_tests= false;
126 static const char *early_tests_definition= "";
127 static bool reload= false;
128 if(key_state('e'))
129 {
130 clear_key_state('e');
131
132 early_tests= !early_tests;
133 if(early_tests)
134 early_tests_definition= "#define EARLY_TESTS";
135 else
136 early_tests_definition= "#undef EARLY_TESTS";
137 reload= true;
138
139 if(early_tests)
140 printf("early fragment tests ON\n");
141 else
142 printf("early fragment test off\n");
143 }
144
145 if(reload || key_state('r'))
146 {
147 reload= false;
148 clear_key_state('r');
149
150 reload_program(m_program, early_tests_definition);
151 program_print_errors(m_program);
152
153 reload_program(m_program_count);
154 program_print_errors(m_program_count);
155
156 reload_program(m_program_display);
157 program_print_errors(m_program_display);
158 }
159
160
161 // passe 1 : stocker les ids des triangles
162 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_framebuffer);
163 glViewport(0, 0, 8192, 8192);
164
165 float one[]= { 1 };
166 glClearBufferfv(GL_DEPTH, /* draw buffer */ 0, one); // profondeur par defaut
167 GLuint zero[]= { 0, 0, 0, 0 };
168 glClearBufferuiv(GL_COLOR, /* draw buffer */ 0, zero); // valeur par defaut
169
170 // transformations
171 Transform view= m_camera.view();
172 Transform projection= m_camera.projection(8192, 8192, 60);
173
174 Transform model;
175 if(key_state(' '))
176 model= RotationY(global_time() / 60);
177
178 Transform mv= view * model;
179 Transform mvp= projection * mv;
180
181 // Z prepass, si necessaire...
182 if(zinit)
183 {
184 draw(m_mesh, model, view, projection);
185 }
186
187 glUseProgram(m_program);
188 program_uniform(m_program, "mvpMatrix", mvp);
189
190 m_mesh.draw(m_program);
191
192 //
193 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
194 glViewport(0, 0, window_width(), window_height());
195 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
196
197 glClearTexImage(m_texture, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, zero);
198
199 {
200 // passe 2 : compter le nombre de triangles par pixel
201 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
202 glUseProgram(m_program_count);
203
204 glBindImageTexture(0, m_texture, 0, GL_TRUE, 0, GL_WRITE_ONLY, GL_R32UI);
205 program_uniform(m_program, "counters", 0);
206
207 glBindImageTexture(1, m_aatexture, 0, GL_TRUE, 0, GL_READ_ONLY, GL_R32UI);
208 program_uniform(m_program, "primitives", 1);
209
210 glDispatchCompute(1024, 1024, 1);
211
212 // passe 3 : visualiser le compteur de fragments
213 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
214 glUseProgram(m_program_display);
215
216 program_uniform(m_program_display, "image", 0);
217
218 glDrawArrays(GL_TRIANGLES, 0, 3);
219 }
220
221 // gestion mouvements camera
222 update_camera( m_camera );
223
224 return 1;
225 }
226
227protected:
228 Mesh m_mesh;
229 Orbiter m_camera;
230
231 GLuint m_program;
232 GLuint m_program_count;
233 GLuint m_program_display;
234
235 GLuint m_framebuffer;
236 GLuint m_aatexture;
237 GLuint m_aadepth;
238
239 GLuint m_texture;
240};
241
242
243int main( int argc, char **argv )
244{
245 const char *filename= "data/bigguy.obj";
246 //~ const char *filename= "data/ccbigguy.obj";
247 if(argc > 1)
248 filename= argv[1];
249
250 Quads app( filename );
251 app.run();
252
253 return 0;
254}
AppTime(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_time.cpp:8
representation d'un objet / maillage.
Definition mesh.h:121
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
Definition orbiter.h:17
int quit()
a deriver pour detruire les objets openGL.
Definition aaquads.cpp:79
int render()
a deriver pour afficher les objets.
Definition aaquads.cpp:93
int init()
a deriver pour creer les objets openGL.
Definition aaquads.cpp:30
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
float global_time()
renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.
Definition window.cpp:145
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_indexed_mesh_fast(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles indexes. utiliser glDrawElem...
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
int release_program(const GLuint program)
Definition program.cpp:238
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
GLuint make_depth_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:146
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