gKit2 light
Loading...
Searching...
No Matches
tuto_storage_texture.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 StorageImage : public AppTime
22{
23public:
24 // application openGL 4.4
25 StorageImage( const char *filename ) : AppTime(1024, 640, 4,4)
26 {
27 m_mesh= read_mesh( 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_texture= make_uint_texture(0, window_width(), window_height());
41
42 glGenTextures(1, &m_texture);
43 glBindTexture(GL_TEXTURE_2D, m_texture);
44 glTexImage2D(GL_TEXTURE_2D, 0,
45 GL_R32UI, window_width(), window_height(), 0,
46 GL_RED_INTEGER, GL_UNSIGNED_INT, nullptr); // GL_RED_INTEGER, sinon normalisation implicite...
47
48 // pas la peine de construire les mipmaps / pas possible pour une texture int / uint
49 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
50
51 //
52 m_program= read_program("gkit2_tutos/tuto_storage_texture.glsl");
53 program_print_errors(m_program);
54
55 m_program_display= read_program("gkit2_tutos/storage_texture_display.glsl");
56 program_print_errors(m_program_display);
57
58 // etat openGL par defaut
59 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
60
61 glClearDepth(1.f); // profondeur par defaut
62 glDepthFunc(GL_LEQUAL); // ztest, conserver l'intersection la plus proche de la m_camera
63 glEnable(GL_DEPTH_TEST); // activer le ztest
64
65 glFrontFace(GL_CCW);
66 glCullFace(GL_BACK);
67 glEnable(GL_CULL_FACE); // active le back face culling
68
69 return 0; // ras, pas d'erreur
70 }
71
72 // destruction des objets de l'application
73 int quit( )
74 {
75 m_mesh.release();
76 release_program(m_program);
77 release_program(m_program_display);
78 glDeleteTextures(1, &m_texture);
79 return 0;
80 }
81
82 // dessiner une nouvelle image
83 int render( )
84 {
85 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
86
87 // effacer la texture image / compteur, le shader ajoute 1 a chaque fragment dessine...
88 GLuint zero= 0;
89 glClearTexImage(m_texture, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
90
91 //
92 static bool zinit= false;
93 if(key_state('z'))
94 {
95 clear_key_state('z');
96 zinit= !zinit;
97
98 if(zinit)
99 printf("Z prepass ON\n");
100 else
101 printf("Z prepass off\n");
102 }
103
104 static bool cull= true;
105 if(key_state('f'))
106 {
107 clear_key_state('f');
108
109 cull= !cull;
110 if(cull)
111 glEnable(GL_CULL_FACE);
112 else
113 glDisable(GL_CULL_FACE);
114
115 if(cull)
116 printf("face culling ON\n");
117 else
118 printf("face culling off\n");
119 }
120
121 static bool early_tests= false;
122 static const char *early_tests_definition= "";
123 static bool reload= false;
124 if(key_state('e'))
125 {
126 clear_key_state('e');
127
128 early_tests= !early_tests;
129 if(early_tests)
130 early_tests_definition= "#define EARLY_TESTS";
131 else
132 early_tests_definition= "#undef EARLY_TESTS";
133 reload= true;
134
135 if(early_tests)
136 printf("early fragment tests ON\n");
137 else
138 printf("early fragment test off\n");
139 }
140
141 if(reload || key_state('r'))
142 {
143 reload= false;
144 clear_key_state('r');
145
146 reload_program(m_program, early_tests_definition);
147 program_print_errors(m_program);
148
149 reload_program(m_program_display);
150 program_print_errors(m_program_display);
151 }
152
153 // transformations
154 Transform model= RotationY(global_time() / 60);
155 Transform view= m_camera.view();
156 Transform projection= m_camera.projection(window_width(), window_height(), 45);
157 Transform mv= view * model;
158 Transform mvp= projection * mv;
159
160 // Z prepass, si necessaire...
161 if(zinit)
162 {
163 draw(m_mesh, model, view, projection);
164 }
165
166 // passe 1 : compter le nombre de fragments par pixel
167 glUseProgram(m_program);
168 program_uniform(m_program, "mvpMatrix", mvp);
169
170 // selectionne la texture sur l'unite image 0, operations atomiques / lecture + ecriture
171 glBindImageTexture(0, m_texture, 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32UI);
172 program_uniform(m_program, "image", 0);
173
174 m_mesh.draw(m_program);
175
176 if(key_state(' ') == 0)
177 {
178 // attendre que les resultats de la passe 1 soient disponibles
179 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
180
181 // passe 2 : visualiser le compteur de fragments
182 glUseProgram(m_program_display);
183 program_uniform(m_program_display, "image", 0);
184
185 glDrawArrays(GL_TRIANGLES, 0, 3);
186 }
187
188 // gestion mouvements camera
189 update_camera( m_camera );
190
191 return 1;
192 }
193
194protected:
195 Mesh m_mesh;
196 Orbiter m_camera;
197
198 GLuint m_program;
199 GLuint m_program_display;
200 GLuint m_texture;
201};
202
203
204int main( int argc, char **argv )
205{
206 const char *filename= "data/bigguy.obj";
207 if(argc > 1)
208 filename= argv[1];
209
210 //~ window_msaa(8);
211 window_resize(false);
212
213 StorageImage app( filename );
214 app.run();
215
216 return 0;
217}
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 init()
a deriver pour creer les objets openGL.
int render()
a deriver pour afficher les objets.
int quit()
a deriver pour detruire les objets openGL.
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
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
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