22 template <
typename T >
23 struct alignas(8)
gvec2
28 gvec2( const ::vec2& v ) : x(v.x), y(v.y) {}
36 template <
typename T >
37 struct alignas(16)
gvec3
42 gvec3( const ::vec3& v ) : x(v.x), y(v.y), z(v.z) {}
43 gvec3(
const Point& v ) : x(v.x), y(v.y), z(v.z) {}
44 gvec3(
const Vector& v ) : x(v.x), y(v.y), z(v.z) {}
52 template <
typename T >
53 struct alignas(16)
gvec4
55 alignas(4) T x, y, z, w;
58 gvec4( const ::vec4& v ) : x(v.x), y(v.y), z(v.z), w(v.w) {}
68class VertexCompute :
public App
72 VertexCompute( ) :
App(1024, 640, 4,3) {}
77 printf(
" positions %d\n", m_mesh.vertex_count());
78 printf(
" triangles %d\n", m_mesh.triangle_count());
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);
92 glGenVertexArrays(1, &m_vao);
93 glBindVertexArray(m_vao);
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);
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);
105 m_program=
read_program(
"gkit2_tutos/pipeline_compute.glsl");
109 m_compute_program=
read_program(
"gkit2_tutos/vertex_compute.glsl");
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];
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;
123 printf(
"groups %d= %d threads\n", m_compute_groups, m_compute_groups*m_compute_threads);
127 m_mesh.bounds(pmin, pmax);
128 m_camera.lookat(pmin, pmax);
131 glClearColor(0.2f, 0.2f, 0.2f, 1.f);
134 glDepthFunc(GL_LESS);
135 glEnable(GL_DEPTH_TEST);
137 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
149 glDeleteBuffers(1, &m_buffer);
150 glDeleteBuffers(1, &m_transformed_buffer);
151 glDeleteVertexArrays(1, &m_vao);
158 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
162 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
163 if(mb & SDL_BUTTON(1))
164 m_camera.rotation(mx, my);
165 else if(mb & SDL_BUTTON(3))
167 else if(mb & SDL_BUTTON(2))
171 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_buffer);
172 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_transformed_buffer);
175 glUseProgram(m_compute_program);
181 Transform mvp= projection * view * model;
183 program_uniform(m_compute_program,
"mvpMatrix", mvp);
186 glDispatchCompute(m_compute_groups, 1, 1);
189 glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
192 glBindVertexArray(m_vao);
193 glUseProgram(m_program);
194 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_transformed_buffer);
196 glDrawArrays(GL_TRIANGLES, 0, m_mesh.vertex_count());
198 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, 0);
199 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
210 GLuint m_transformed_buffer;
212 GLuint m_compute_program;
213 int m_compute_threads;
214 int m_compute_groups;
218int main(
int argc,
char **argv )
int run()
execution de l'application.
App(const int width, const int height, const int major=3, const int minor=3)
constructeur, dimensions de la fenetre et version d'openGL.
representation d'un objet / maillage.
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
int init()
a deriver pour creer 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.
int quit()
a deriver pour detruire les objets openGL. renvoie -1 pour indiquer une erreur, 0 sinon.
int window_height()
renvoie la hauteur de la fenetre de l'application.
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().
int window_width()
renvoie la largeur de la fenetre de l'application.
float global_time()
renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.
Transform RotationY(const float a)
renvoie la matrice representation une rotation de a degree autour de l'axe Y.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
GLuint read_program(const char *filename, const char *definitions)
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
int release_program(const GLuint program)
detruit les shaders et le program.
representation d'un point 3d.
vecteur generique 4d, ou 3d homogene, utilitaire.
representation de l'indexation complete d'un sommet