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

Public Member Functions

int init ()
 a deriver pour creer les objets openGL.
int quit ()
 a deriver pour detruire les objets openGL.
int update (const float time, const float delta)
 a deriver et redefinir pour animer les objets en fonction du temps.
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.
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_display
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 20 of file tuto_storage_texture.cpp.

Constructor & Destructor Documentation

◆ StorageImage()

StorageImage::StorageImage ( )
inline

Definition at line 24 of file tuto_storage_texture.cpp.

24: AppTime(1024, 640, 4, 3) {}
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

Member Function Documentation

◆ init()

int StorageImage::init ( )
inlinevirtual

a deriver pour creer les objets openGL.

Implements AppTime.

Definition at line 26 of file tuto_storage_texture.cpp.

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 }
int window_height()
renvoie la hauteur de la fenetre de l'application.
Definition window.cpp:27
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition window.cpp:23
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

◆ quit()

int StorageImage::quit ( )
inlinevirtual

a deriver pour detruire les objets openGL.

Implements AppTime.

Definition at line 66 of file tuto_storage_texture.cpp.

67 {
68 m_mesh.release();
69 release_program(m_program);
70 glDeleteTextures(1, &m_texture);
71 return 0;
72 }
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:240

◆ update()

int StorageImage::update ( const float time,
const float delta )
inlinevirtual

a deriver et redefinir pour animer les objets en fonction du temps.

Reimplemented from AppTime.

Definition at line 74 of file tuto_storage_texture.cpp.

75 {
76
77 return 0;
78 }

◆ render()

int StorageImage::render ( )
inlinevirtual

a deriver pour afficher les objets.

Implements AppTime.

Definition at line 81 of file tuto_storage_texture.cpp.

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

Member Data Documentation

◆ m_mesh

Mesh StorageImage::m_mesh
protected

Definition at line 179 of file tuto_storage_texture.cpp.

◆ m_camera

Orbiter StorageImage::m_camera
protected

Definition at line 180 of file tuto_storage_texture.cpp.

◆ m_program

GLuint StorageImage::m_program
protected

Definition at line 182 of file tuto_storage_texture.cpp.

◆ m_program_display

GLuint StorageImage::m_program_display
protected

Definition at line 183 of file tuto_storage_texture.cpp.

◆ m_texture

GLuint StorageImage::m_texture
protected

Definition at line 184 of file tuto_storage_texture.cpp.


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