gKit2 light
Loading...
Searching...
No Matches
quads.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.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, 640, 4,4)
26 //~ Quads( const char *filename ) : AppTime(2048, 1280, 4,4)
27 {
28 m_mesh= read_mesh( filename );
29 }
30
31 int init( )
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 }
63
64 // destruction des objets de l'application
65 int quit( )
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 }
73
74 // dessiner une nouvelle image
75 int render( )
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 }
185
186protected:
187 Mesh m_mesh;
188 Orbiter m_camera;
189
190 GLuint m_program;
191 GLuint m_program_display;
192 GLuint m_texture;
193};
194
195
196int main( int argc, char **argv )
197{
198 const char *filename= "data/bigguy.obj";
199 //~ const char *filename= "cc4bigguy.obj";
200 if(argc > 1)
201 filename= argv[1];
202
203 window_msaa(8);
204 window_resize(false);
205
206 Quads app( filename );
207 app.run();
208
209 return 0;
210}
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 quads.cpp:65
int render()
a deriver pour afficher les objets.
Definition quads.cpp:75
int init()
a deriver pour creer les objets openGL.
Definition quads.cpp:31
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_msaa()
renvoie le nombre de samples MSAA.
Definition window.cpp:43
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_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition wavefront.cpp:14
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
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