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 "program.h"
11#include "uniforms.h"
12#include "texture.h"
13
14#include "mesh.h"
15#include "wavefront.h"
16
17#include "orbiter.h"
18
19
20class StorageImage : public AppTime
21{
22public:
23 // application openGL 4.3
24 StorageImage( ) : AppTime(1024, 640, 4, 3) {}
25
26 int init( )
27 {
28 m_mesh= read_mesh("data/bigguy.obj");
29
30 Point pmin, pmax;
31 m_mesh.bounds(pmin, pmax);
32 m_camera.lookat(pmin, pmax);
33
34 // cree la texture, 1 canal, entiers 32bits non signes
35 glGenTextures(1, &m_texture);
36 glBindTexture(GL_TEXTURE_2D, m_texture);
37 glTexImage2D(GL_TEXTURE_2D, 0,
38 GL_R32UI, window_width(), window_height(), 0,
39 GL_RED_INTEGER, GL_UNSIGNED_INT, nullptr); // GL_RED_INTEGER, sinon normalisation implicite...
40
41 // pas la peine de construire les mipmaps / pas possible pour une texture int / uint
42 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
43
44 //
45 m_program= read_program("gkit2_tutos/tuto_storage_texture.glsl");
46 program_print_errors(m_program);
47
48 m_program_display= read_program("gkit2_tutos/storage_texture_display.glsl");
49 program_print_errors(m_program_display);
50
51 // etat openGL par defaut
52 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
53
54 glClearDepth(1.f); // profondeur par defaut
55 glDepthFunc(GL_LESS); // ztest, conserver l'intersection la plus proche de la m_camera
56 glEnable(GL_DEPTH_TEST); // activer le ztest
57
58 glFrontFace(GL_CCW);
59 glCullFace(GL_BACK);
60 glEnable(GL_CULL_FACE); // active le back face culling
61
62 return 0; // ras, pas d'erreur
63 }
64
65 // destruction des objets de l'application
66 int quit( )
67 {
68 m_mesh.release();
69 release_program(m_program);
70 glDeleteTextures(1, &m_texture);
71 return 0;
72 }
73
74 int update( const float time, const float delta )
75 {
76
77 return 0;
78 }
79
80 // dessiner une nouvelle image
81 int render( )
82 {
83 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
84
85 // effacer la texture image / compteur, le shader ajoute 1 a chaque fragment dessine...
86 GLuint zero= 0;
87 glClearTexImage(m_texture, 0, GL_RED_INTEGER, GL_UNSIGNED_INT, &zero);
88
89 // gestion mouvements camera
90 update_camera( m_camera );
91
92 //
93 static bool cull= true;
94 if(key_state('f'))
95 {
96 clear_key_state('f');
97
98 cull= !cull;
99 if(cull)
100 glEnable(GL_CULL_FACE);
101 else
102 glDisable(GL_CULL_FACE);
103
104 if(cull)
105 printf("face culling ON\n");
106 else
107 printf("face culling off\n");
108 }
109
110
111 static bool early_tests= true;
112 static const char *early_tests_definition= "";
113 static bool reload= false;
114 if(key_state('e'))
115 {
116 clear_key_state('e');
117
118 early_tests= !early_tests;
119 if(early_tests)
120 early_tests_definition= "#define EARLY_TESTS";
121 else
122 early_tests_definition= "#undef EARLY_TESTS";
123 reload= true;
124
125 if(early_tests)
126 printf("early fragment tests ON\n");
127 else
128 printf("early fragment test off\n");
129 }
130
131 if(reload || key_state('r'))
132 {
133 reload= false;
134 clear_key_state('r');
135
136 reload_program(m_program, "gkit2_tutos/tuto_storage_texture.glsl", early_tests_definition);
137 program_print_errors(m_program);
138
139 reload_program(m_program_display, "gkit2_tutos/storage_texture_display.glsl");
140 program_print_errors(m_program_display);
141 }
142
143 // passe 1 : compter le nombre de fragments par pixel
144 glUseProgram(m_program);
145
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 program_uniform(m_program, "mvpMatrix", mvp);
153
154 // selectionne la texture sur l'unite image 0, operations atomiques / lecture + ecriture
155 glBindImageTexture(0, m_texture, 0, GL_TRUE, 0, GL_READ_WRITE, GL_R32UI);
156 program_uniform(m_program, "image", 0);
157
158 m_mesh.draw(m_program);
159
160 if(key_state(' ') == 0)
161 {
162 // passe 2 : afficher le compteur
163 glUseProgram(m_program_display);
164
165 // attendre que les resultats de la passe 1 soit disponible
166 glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
167
168 // RE-selectionne la texture sur l'unite image 0 / LECTURE SEULE
169 glBindImageTexture(0, m_texture, 0, GL_TRUE, 0, GL_READ_ONLY, GL_R32UI);
170 program_uniform(m_program_display, "image", 0);
171
172 glDrawArrays(GL_TRIANGLES, 0, 3);
173 }
174
175 return 1;
176 }
177
178protected:
179 Mesh m_mesh;
180 Orbiter m_camera;
181
182 GLuint m_program;
183 GLuint m_program_display;
184 GLuint m_texture;
185};
186
187
188int main( int argc, char **argv )
189{
190 StorageImage app;
191 app.run();
192
193 return 0;
194}
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
int run()
execution de l'application.
Definition app.cpp:36
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 update(const float time, const float delta)
a deriver et redefinir pour animer les objets en fonction du temps.
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.
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
float global_time()
renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.
Definition window.cpp:139
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:217
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
Definition program.cpp:461
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:240
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