gKit2 light
Loading...
Searching...
No Matches
Quads Class Reference
Inheritance diagram for Quads:

Public Member Functions

 Quads (const char *filename)
int init ()
 a deriver pour creer les objets openGL.
int quit ()
 a deriver pour detruire les objets openGL.
int render ()
 a deriver pour afficher les objets.
 Quads (const char *filename)
int init ()
 a deriver pour creer les objets openGL.
int quit ()
 a deriver pour detruire les objets openGL.
int render ()
 a deriver pour afficher les objets.
Public Member Functions inherited from AppTime
 AppTime (const int width, const int height, const int major=3, const int minor=3)
 constructeur, dimensions de la fenetre et version d'openGL.
virtual int update (const float time, const float delta)
 a deriver et redefinir pour animer les objets en fonction du temps.
Public Member Functions inherited from App
 App (const int width, const int height, const int major=3, const int minor=3)
 constructeur, dimensions de la fenetre et version d'openGL.
int run ()
 execution de l'application.

Protected Attributes

Mesh m_mesh
Orbiter m_camera
GLuint m_program
GLuint m_program_count
GLuint m_program_display
GLuint m_framebuffer
GLuint m_aatexture
GLuint m_aadepth
GLuint m_texture
Protected Attributes inherited from AppTime
std::chrono::high_resolution_clock::time_point m_cpu_start
std::chrono::high_resolution_clock::time_point m_cpu_stop
GLuint m_time_query [MAX_FRAMES]
GLint64 m_frame_time
int m_frame
Text m_console
Protected Attributes inherited from App
Window m_window
Context m_context
bool sync

Additional Inherited Members

Protected Member Functions inherited from AppTime
virtual int prerender ()
virtual int postrender ()
Protected Member Functions inherited from App
void vsync_off ()

Detailed Description

Definition at line 21 of file aaquads.cpp.

Constructor & Destructor Documentation

◆ Quads() [1/2]

Quads::Quads ( const char * filename)
inline

Definition at line 25 of file aaquads.cpp.

25 : AppTime(1024, 1024, 4,4)
26 {
27 m_mesh= read_indexed_mesh_fast( filename );
28 }
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
Mesh read_indexed_mesh_fast(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles indexes. utiliser glDrawElem...

◆ Quads() [2/2]

Quads::Quads ( const char * filename)
inline

Definition at line 25 of file quads.cpp.

25 : AppTime(1024, 640, 4,4)
26 //~ Quads( const char *filename ) : AppTime(2048, 1280, 4,4)
27 {
28 m_mesh= read_mesh( filename );
29 }
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition wavefront.cpp:14

Member Function Documentation

◆ init() [1/2]

int Quads::init ( )
inlinevirtual

a deriver pour creer les objets openGL.

Implements AppTime.

Definition at line 30 of file aaquads.cpp.

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 }
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
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

◆ quit() [1/2]

int Quads::quit ( )
inlinevirtual

a deriver pour detruire les objets openGL.

Implements AppTime.

Definition at line 79 of file aaquads.cpp.

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 }
int release_program(const GLuint program)
Definition program.cpp:238

◆ render() [1/2]

int Quads::render ( )
inlinevirtual

a deriver pour afficher les objets.

Implements AppTime.

Definition at line 93 of file aaquads.cpp.

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 }
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

◆ init() [2/2]

int Quads::init ( )
inlinevirtual

a deriver pour creer les objets openGL.

Implements AppTime.

Definition at line 31 of file quads.cpp.

32 {
33 if(m_mesh.vertex_count() == 0)
34 return -1; // pas de geometrie
35
36 Point pmin, pmax;
37 m_mesh.bounds(pmin, pmax);
38 m_camera.lookat(pmin, pmax);
39
40 // cree la texture, 1 canal, entiers 32bits non signes
42
43 //
44 m_program= read_program("gkit2_tutos/M2/quads.glsl");
45 program_print_errors(m_program);
46
47 m_program_display= read_program("gkit2_tutos/M2/quads_display.glsl");
48 program_print_errors(m_program_display);
49
50 // etat openGL par defaut
51 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
52
53 glClearDepth(1.f); // profondeur par defaut
54 glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la m_camera
55 glEnable(GL_DEPTH_TEST); // activer le ztest
56
57 glFrontFace(GL_CCW);
58 glCullFace(GL_BACK);
59 glEnable(GL_CULL_FACE); // active le back face culling
60
61 return 0; // ras, pas d'erreur
62 }

◆ quit() [2/2]

int Quads::quit ( )
inlinevirtual

a deriver pour detruire les objets openGL.

Implements AppTime.

Definition at line 65 of file quads.cpp.

66 {
67 m_mesh.release();
68 release_program(m_program);
69 release_program(m_program_display);
70 glDeleteTextures(1, &m_texture);
71 return 0;
72 }

◆ render() [2/2]

int Quads::render ( )
inlinevirtual

a deriver pour afficher les objets.

Implements AppTime.

Definition at line 75 of file quads.cpp.

76 {
77 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
78
79 // effacer la texture image / compteur, le shader ajoute 1 a chaque fragment dessine...
80 GLuint zero= 0;
81 glClearTexImage(m_texture, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
82
83 //
84 static bool zinit= false;
85 if(key_state('z'))
86 {
87 clear_key_state('z');
88 zinit= !zinit;
89
90 if(zinit)
91 printf("Z prepass ON\n");
92 else
93 printf("Z prepass off\n");
94 }
95
96 static bool cull= true;
97 if(key_state('f'))
98 {
99 clear_key_state('f');
100
101 cull= !cull;
102 if(cull)
103 glEnable(GL_CULL_FACE);
104 else
105 glDisable(GL_CULL_FACE);
106
107 if(cull)
108 printf("face culling ON\n");
109 else
110 printf("face culling off\n");
111 }
112
113 static bool early_tests= false;
114 static const char *early_tests_definition= "";
115 static bool reload= false;
116 if(key_state('e'))
117 {
118 clear_key_state('e');
119
120 early_tests= !early_tests;
121 if(early_tests)
122 early_tests_definition= "#define EARLY_TESTS";
123 else
124 early_tests_definition= "#undef EARLY_TESTS";
125 reload= true;
126
127 if(early_tests)
128 printf("early fragment tests ON\n");
129 else
130 printf("early fragment test off\n");
131 }
132
133 if(reload || key_state('r'))
134 {
135 reload= false;
136 clear_key_state('r');
137
138 reload_program(m_program, early_tests_definition);
139 program_print_errors(m_program);
140
141 reload_program(m_program_display);
142 program_print_errors(m_program_display);
143 }
144
145 // transformations
146 Transform model= RotationY(global_time() / 60);
147 Transform view= m_camera.view();
148 Transform projection= m_camera.projection(window_width(), window_height(), 45);
149 Transform mv= view * model;
150 Transform mvp= projection * mv;
151
152 // Z prepass, si necessaire...
153 if(zinit)
154 {
155 draw(m_mesh, model, view, projection);
156 }
157
158 // passe 1 : compter le nombre de fragments par pixel
159 glUseProgram(m_program);
160 program_uniform(m_program, "mvpMatrix", mvp);
161
162 // selectionne la texture sur l'unite image 0, operations atomiques / lecture + ecriture
163 glBindImageTexture(0, m_texture, 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32UI);
164 program_uniform(m_program, "image", 0);
165
166 m_mesh.draw(m_program);
167
168 if(key_state(' ') == 0)
169 {
170 // attendre que les resultats de la passe 1 soient disponibles
171 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
172
173 // passe 2 : visualiser le compteur de fragments
174 glUseProgram(m_program_display);
175 program_uniform(m_program_display, "image", 0);
176
177 glDrawArrays(GL_TRIANGLES, 0, 3);
178 }
179
180 // gestion mouvements camera
181 update_camera( m_camera );
182
183 return 1;
184 }

Member Data Documentation

◆ m_mesh

Mesh Quads::m_mesh
protected

Definition at line 228 of file aaquads.cpp.

◆ m_camera

Orbiter Quads::m_camera
protected

Definition at line 229 of file aaquads.cpp.

◆ m_program

GLuint Quads::m_program
protected

Definition at line 231 of file aaquads.cpp.

◆ m_program_count

GLuint Quads::m_program_count
protected

Definition at line 232 of file aaquads.cpp.

◆ m_program_display

GLuint Quads::m_program_display
protected

Definition at line 233 of file aaquads.cpp.

◆ m_framebuffer

GLuint Quads::m_framebuffer
protected

Definition at line 235 of file aaquads.cpp.

◆ m_aatexture

GLuint Quads::m_aatexture
protected

Definition at line 236 of file aaquads.cpp.

◆ m_aadepth

GLuint Quads::m_aadepth
protected

Definition at line 237 of file aaquads.cpp.

◆ m_texture

GLuint Quads::m_texture
protected

Definition at line 239 of file aaquads.cpp.


The documentation for this class was generated from the following files: