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

Public Member Functions

int init ()
 a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
int quit ()
 a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
int render ()
 a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.
Public Member Functions inherited from App
 App (const int width, const int height, const int major=3, const int minor=3, const int samples=0)
 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.
int run ()
 execution de l'application.

Protected Attributes

Mesh m_mesh
Orbiter m_camera
GLuint m_vao
GLuint m_buffer
GLuint m_transformed_buffer
GLuint m_program
GLuint m_compute_program
int m_compute_threads
int m_compute_groups
Protected Attributes inherited from App
Window m_window
Context m_context
bool sync

Additional Inherited Members

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

Detailed Description

Definition at line 68 of file tuto_vertex_compute.cpp.

Constructor & Destructor Documentation

◆ VertexCompute()

VertexCompute::VertexCompute ( )
inline

Definition at line 72 of file tuto_vertex_compute.cpp.

72: App(1024, 640, 4,3) {}
App(const int width, const int height, const int major=3, const int minor=3, const int samples=0)
constructeur, dimensions de la fenetre et version d'openGL.
Definition app.cpp:11

Member Function Documentation

◆ init()

int VertexCompute::init ( )
inlinevirtual

a deriver pour creer les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.

Implements App.

Definition at line 74 of file tuto_vertex_compute.cpp.

75 {
76 m_mesh= read_mesh("data/bigguy.obj");
77 printf(" positions %d\n", m_mesh.vertex_count());
78 printf(" triangles %d\n", m_mesh.triangle_count());
79
80 // construit le storage buffer contenant les positions, les normales et les texcoords, en utilisant les types alignes
81 struct vertex
82 {
83 glsl::vec3 position;
84 };
85
86 // recupere les attributs du mesh
87 std::vector<vertex> data(m_mesh.vertex_count());
88 for(int i= 0; i < m_mesh.vertex_count(); i++)
89 data[i].position= m_mesh.positions().at(i);
90
91 // vao par defaut, pas d'attribut. les positions des sommets sont dans le storage buffer...
92 glGenVertexArrays(1, &m_vao);
93 glBindVertexArray(m_vao);
94
95 // storage buffers
96 glGenBuffers(1, &m_buffer);
97 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer);
98 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vertex) * data.size(), data.data(), GL_STREAM_READ);
99
100 glGenBuffers(1, &m_transformed_buffer);
101 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_transformed_buffer);
102 glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vec4) * data.size(), std::vector<vec4>(data.size(), vec4()).data(), GL_STREAM_COPY);
103
104 //
105 m_program= read_program("gkit2_tutos/pipeline_compute.glsl");
106 program_print_errors(m_program);
107
108 // compute shader
109 m_compute_program= read_program("gkit2_tutos/vertex_compute.glsl");
110 program_print_errors(m_compute_program);
111
112 // recupere le nombre de threads de chaque groupe du compute shader
113 GLint threads[3]= { };
114 glGetProgramiv(m_compute_program, GL_COMPUTE_WORK_GROUP_SIZE, threads);
115 printf("threads / group x %d, y %d, z %d\n", threads[0], threads[1], threads[2]);
116 m_compute_threads= threads[0];
117
118 // nombre de groupes de threads a executer pour transformer les sommets du mesh
119 m_compute_groups= m_mesh.vertex_count() / m_compute_threads;
120 if(m_mesh.vertex_count() % m_compute_threads)
121 m_compute_groups= m_compute_groups +1;
122
123 printf("groups %d= %d threads\n", m_compute_groups, m_compute_groups*m_compute_threads);
124
125 //
126 Point pmin, pmax;
127 m_mesh.bounds(pmin, pmax);
128 m_camera.lookat(pmin, pmax);
129
130 // etat openGL par defaut
131 glClearColor(0.2f, 0.2f, 0.2f, 1.f); // couleur par defaut de la fenetre
132
133 glClearDepth(1.f); // profondeur par defaut
134 glDepthFunc(GL_LESS); // ztest, conserver l'intersection la plus proche de la camera
135 glEnable(GL_DEPTH_TEST); // activer le ztest
136
137 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // ne dessine que les aretes des triangles
138 glLineWidth(2);
139
140 return 0; // ras, pas d'erreur
141 }
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
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:218
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition program.cpp:446

◆ quit()

int VertexCompute::quit ( )
inlinevirtual

a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.

Implements App.

Definition at line 144 of file tuto_vertex_compute.cpp.

145 {
146 m_mesh.release();
147 release_program(m_program);
148 release_program(m_compute_program);
149 glDeleteBuffers(1, &m_buffer);
150 glDeleteBuffers(1, &m_transformed_buffer);
151 glDeleteVertexArrays(1, &m_vao);
152 return 0;
153 }
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:225

◆ render()

int VertexCompute::render ( )
inlinevirtual

a deriver pour afficher les objets. renvoie 1 pour continuer, 0 pour fermer l'application.

Implements App.

Definition at line 156 of file tuto_vertex_compute.cpp.

157 {
158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
159
160 // deplace la camera
161 int mx, my;
162 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
163 if(mb & SDL_BUTTON(1)) // le bouton gauche est enfonce
164 m_camera.rotation(mx, my);
165 else if(mb & SDL_BUTTON(3)) // le bouton droit est enfonce
166 m_camera.move(mx);
167 else if(mb & SDL_BUTTON(2)) // le bouton du milieu est enfonce
168 m_camera.translation((float) mx / (float) window_width(), (float) my / (float) window_height());
169
170 // selectionne les buffers
171 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_buffer);
172 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_transformed_buffer);
173
174 // etape 1 : utilise le compute shader pour transformer les positions des sommets du mesh.
175 glUseProgram(m_compute_program);
176
177 // uniforms du compute shader
178 Transform model= RotationY(global_time() / 60);
179 Transform view= m_camera.view();
180 Transform projection= m_camera.projection(window_width(), window_height(), 45);
181 Transform mvp= projection * view * model;
182
183 program_uniform(m_compute_program, "mvpMatrix", mvp);
184
185 // go !!
186 glDispatchCompute(m_compute_groups, 1, 1);
187
188 // etape 2 : synchronisation, attendre les resultats du compute shader
189 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
190
191 // etape 3 : utilise les sommets transformes (par le compute shader) pour afficher le mesh.
192 glBindVertexArray(m_vao);
193 glUseProgram(m_program);
194 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_transformed_buffer);
195
196 glDrawArrays(GL_TRIANGLES, 0, m_mesh.vertex_count());
197
198 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
199 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
200
201 return 1;
202 }
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
float global_time()
renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.
Definition window.cpp:126
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 VertexCompute::m_mesh
protected

Definition at line 205 of file tuto_vertex_compute.cpp.

◆ m_camera

Orbiter VertexCompute::m_camera
protected

Definition at line 206 of file tuto_vertex_compute.cpp.

◆ m_vao

GLuint VertexCompute::m_vao
protected

Definition at line 208 of file tuto_vertex_compute.cpp.

◆ m_buffer

GLuint VertexCompute::m_buffer
protected

Definition at line 209 of file tuto_vertex_compute.cpp.

◆ m_transformed_buffer

GLuint VertexCompute::m_transformed_buffer
protected

Definition at line 210 of file tuto_vertex_compute.cpp.

◆ m_program

GLuint VertexCompute::m_program
protected

Definition at line 211 of file tuto_vertex_compute.cpp.

◆ m_compute_program

GLuint VertexCompute::m_compute_program
protected

Definition at line 212 of file tuto_vertex_compute.cpp.

◆ m_compute_threads

int VertexCompute::m_compute_threads
protected

Definition at line 213 of file tuto_vertex_compute.cpp.

◆ m_compute_groups

int VertexCompute::m_compute_groups
protected

Definition at line 214 of file tuto_vertex_compute.cpp.


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