gKit2 light
Loading...
Searching...
No Matches
RT Struct Reference
Inheritance diagram for RT:

Public Member Functions

 RT (const char *filename)
int init ()
 a deriver pour creer les objets openGL.
int quit ()
 a deriver pour detruire les objets openGL.
int render ()
 a deriver pour afficher les objets.
 RT (const char *filename)
int init ()
 a deriver pour creer les objets openGL.
int quit ()
 a deriver pour detruire les objets openGL.
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.
virtual int update (const float time, const float delta)
 a deriver et redefinir pour animer les objets en fonction du temps.
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_blit_framebuffer
GLuint m_program
GLuint m_texture
GLuint m_buffer
GLuint m_vao
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 70 of file tuto_raytrace_compute.cpp.

Constructor & Destructor Documentation

◆ RT() [1/2]

RT::RT ( const char * filename)
inline

Definition at line 73 of file tuto_raytrace_compute.cpp.

73 : AppTime(1024, 640)
74 {
75 m_mesh= read_mesh(filename);
76 }
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
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes....
Definition wavefront.cpp:14

◆ RT() [2/2]

RT::RT ( const char * filename)
inline

Definition at line 75 of file tuto_raytrace_fragment.cpp.

75 : AppTime(1024, 640)
76 {
77 m_mesh= read_mesh(filename);
78 }

Member Function Documentation

◆ init() [1/2]

int RT::init ( )
inlinevirtual

a deriver pour creer les objets openGL.

Implements AppTime.

Definition at line 78 of file tuto_raytrace_compute.cpp.

79 {
80 if(m_mesh.triangle_count() == 0)
81 return -1;
82
83 Point pmin, pmax;
84 m_mesh.bounds(pmin, pmax);
85 m_camera.lookat(pmin, pmax);
86
87 // utilise un compute shader qui :
88 // 1. cree un rayon pour chaque pixel de l'image
89 // 2. calcule les intersections du rayon avec tous les triangles. (et garde la plus proche...)
90 // 3. ecrit le resultat dans une image.
91
92 // il faut donc creer le buffer et la texture, les entrees / sorties du shader
93
94 // recupere les triangles du mesh
95 // structure declaree par le shader, en respectant l'alignement std430
96 struct triangle
97 {
98 glsl::vec3 a;
99 glsl::vec3 ab;
100 glsl::vec3 ac;
101 };
102
103 std::vector<triangle> data;
104 data.reserve(m_mesh.triangle_count());
105 for(int i= 0; i < m_mesh.triangle_count(); i++)
106 {
107 TriangleData t= m_mesh.triangle(i);
108 data.push_back( { Point(t.a), Point(t.b) - Point(t.a), Point(t.c) - Point(t.a) } );
109 }
110
111 // cree et initialise le storage buffer
112 glGenBuffers(1, &m_buffer);
113 glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_buffer);
114 glBufferData(GL_SHADER_STORAGE_BUFFER, data.size() * sizeof(triangle), data.data(), GL_STATIC_READ);
115
116 // texture / image resultat
117 // cree la texture, 4 canaux, entiers 8bits normalises, standard
118 glGenTextures(1, &m_texture);
119 glBindTexture(GL_TEXTURE_2D, m_texture);
120 glTexImage2D(GL_TEXTURE_2D, 0,
121 GL_RGBA8, window_width(), window_height(), 0,
122 GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
123
124 // pas la peine de construire les mipmaps, le shader ne va ecrire que le mipmap 0
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
126 // oui, c'est une texture tout a fait normale.
127
128 // pour afficher l'image resultat, 2 solutions :
129 // 1. utiliser un shader qui copie un pixel de la texture vers un pixel du framebuffer par defaut / la fenetre,
130 // 2. ou copier directement la texture sur le framebuffer par defaut / la fenetre, en utilisant glBlitFramebuffer
131
132 // pour changer, on va utiliser glBlitFramebuffer,
133 // mais il faut configurer un framebuffer...
134 glGenFramebuffers(1, &m_blit_framebuffer);
135 glBindFramebuffer(GL_READ_FRAMEBUFFER, m_blit_framebuffer);
136 glFramebufferTexture(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, m_texture, 0);
137 // selectionner la texture du framebuffer a copier...
138 glReadBuffer(GL_COLOR_ATTACHMENT0);
139
140 // compute shader
141 m_program= read_program("gkit2_tutos/M2/raytrace_compute.glsl");
142 program_print_errors(m_program);
143
144 // nettoyage
145 glBindFramebuffer(GL_FRAMEBUFFER, 0);
146 glBindTexture(GL_TEXTURE_2D, 0);
147 return 0;
148 }
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
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
vec3 c
positions
Definition mesh.h:96

◆ quit() [1/2]

int RT::quit ( )
inlinevirtual

a deriver pour detruire les objets openGL.

Implements AppTime.

Definition at line 150 of file tuto_raytrace_compute.cpp.

151 {
152 release_program(m_program);
153 glDeleteTextures(1, &m_texture);
154 glDeleteBuffers(1, &m_buffer);
155 glDeleteFramebuffers(1, &m_blit_framebuffer);
156 return 0;
157 }
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:240

◆ render() [1/2]

int RT::render ( )
inlinevirtual

a deriver pour afficher les objets.

Implements AppTime.

Definition at line 159 of file tuto_raytrace_compute.cpp.

160 {
161 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
162 glViewport(0, 0, window_width(), window_height());
163 glClear(GL_COLOR_BUFFER_BIT);
164
165 if(key_state('f'))
166 {
167 clear_key_state('f');
168 // recentrer la camera
169 Point pmin, pmax;
170 m_mesh.bounds(pmin, pmax);
171 m_camera.lookat(pmin, pmax);
172 }
173
174 // deplace la camera
175 int mx, my;
176 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
177 if(mb & SDL_BUTTON(1)) // le bouton gauche est enfonce
178 m_camera.rotation(mx, my);
179 else if(mb & SDL_BUTTON(3)) // le bouton droit est enfonce
180 m_camera.move(mx);
181 else if(mb & SDL_BUTTON(2)) // le bouton du milieu est enfonce
182 m_camera.translation((float) mx / (float) window_width(), (float) my / (float) window_height());
183
184 // recupere les transformations standards.
185 Transform m= Identity();
186 Transform v= m_camera.view();
187 Transform p= m_camera.projection(window_width(), window_height(), 45);
188 Transform im= Viewport(window_width(), window_height());
189 // ou Transform im= m_camera.viewport();
190 // compose toutes les transformations, jusqu'au repere image
191 Transform T= im * p * v * m;
192
193 // config pipeline
194 glUseProgram(m_program);
195
196 // storage buffer 0
197 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_buffer);
198
199 // image texture 0, ecriture seule, mipmap 0 + format rgba8 classique
200 glBindImageTexture(0, m_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8);
201 // configurer le shader
202 program_uniform(m_program, "image", 0);
203
204 // uniforms
205 program_uniform(m_program, "invMatrix", T.inverse());
206
207 // nombre de groupes de shaders pour executer un compute shader par pixel de l'image resultat. on utilise un domaine 2d...
208 // le shader declare un groupe de threads de 8x8.
209 int nx= window_width() / 8;
210 int ny= window_height() / 8;
211 // on suppose que les dimensions de l'image sont multiples de 8...
212 // sinon calculer correctement le nombre de groupes pour x et y.
213
214 // go !!
215 glDispatchCompute(nx, ny, 1);
216
217 // attendre le resultat
218 glMemoryBarrier(GL_ALL_BARRIER_BITS);
219
220 // afficher le resultat
221 // copier la texture resultat vers le framebuffer par defaut / de la fenetre
222 glBindFramebuffer(GL_READ_FRAMEBUFFER, m_blit_framebuffer);
223
224 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
225 glBlitFramebuffer(
226 0,0, window_width(),window_height(),
228 GL_COLOR_BUFFER_BIT, GL_NEAREST);
229
230 return 1;
231 }
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
Definition window.cpp:59
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
Transform Viewport(const float width, const float height)
renvoie la matrice representant une transformation viewport.
Definition mat.cpp:357
Transform Identity()
construit la transformation identite.
Definition mat.cpp:187
Transform inverse() const
renvoie l'inverse de la matrice.
Definition mat.cpp:399

◆ init() [2/2]

int RT::init ( )
inlinevirtual

a deriver pour creer les objets openGL.

Implements AppTime.

Definition at line 80 of file tuto_raytrace_fragment.cpp.

81 {
82 if(m_mesh.vertex_count() == 0)
83 return -1;
84
85 Point pmin, pmax;
86 m_mesh.bounds(pmin, pmax);
87 m_camera.lookat(pmin, pmax);
88
89 glGenVertexArrays(1, &m_vao);
90 glBindVertexArray(m_vao);
91
92 glGenBuffers(1, &m_buffer);
93 glBindBuffer(GL_UNIFORM_BUFFER, m_buffer);
94
95 //
96 struct triangle
97 {
98 glsl::vec3 a;
99 glsl::vec3 ab;
100 glsl::vec3 ac;
101 };
102
103 std::vector<triangle> data;
104 data.reserve(m_mesh.triangle_count());
105 for(int i= 0; i < m_mesh.triangle_count(); i++)
106 {
107 TriangleData t= m_mesh.triangle(i);
108 data.push_back( { Point(t.a), Point(t.b) - Point(t.a), Point(t.c) - Point(t.a) } );
109 }
110
111 // alloue le buffer
112 // alloue au moins 1024 triangles, cf le shader
113 if(data.size() < 1024)
114 data.resize(1024);
115 glBufferData(GL_UNIFORM_BUFFER, data.size() * sizeof(triangle), data.data(), GL_STATIC_READ);
116
117 //
118 m_program= read_program("gkit2_tutos/M2/raytrace.glsl");
119 program_print_errors(m_program);
120
121 // associe l'uniform buffer a l'entree 0
122 GLint index= glGetUniformBlockIndex(m_program, "triangleData");
123 glUniformBlockBinding(m_program, index, 0);
124
125 return 0;
126 }

◆ quit() [2/2]

int RT::quit ( )
inlinevirtual

a deriver pour detruire les objets openGL.

Implements AppTime.

Definition at line 128 of file tuto_raytrace_fragment.cpp.

129 {
130 return 0;
131 }

◆ render() [2/2]

int RT::render ( )
inlinevirtual

a deriver pour afficher les objets.

Implements AppTime.

Definition at line 133 of file tuto_raytrace_fragment.cpp.

134 {
135 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
136
137 if(key_state('f'))
138 {
139 clear_key_state('f');
140 Point pmin, pmax;
141 m_mesh.bounds(pmin, pmax);
142 m_camera.lookat(pmin, pmax);
143 }
144
145 // deplace la camera
146 int mx, my;
147 unsigned int mb= SDL_GetRelativeMouseState(&mx, &my);
148 if(mb & SDL_BUTTON(1)) // le bouton gauche est enfonce
149 m_camera.rotation(mx, my);
150 else if(mb & SDL_BUTTON(3)) // le bouton droit est enfonce
151 m_camera.move(mx);
152 else if(mb & SDL_BUTTON(2)) // le bouton du milieu est enfonce
153 m_camera.translation((float) mx / (float) window_width(), (float) my / (float) window_height());
154
155 Transform m;
156 Transform v= m_camera.view();
157 Transform p= m_camera.projection(window_width(), window_height(), 45);
158 Transform mvp= p * v * m;
159
160 // config pipeline
161 glBindVertexArray(m_vao);
162 glUseProgram(m_program);
163
164 // uniform buffer 0
165 glBindBufferBase(GL_UNIFORM_BUFFER, 0, m_buffer);
166 program_uniform(m_program, "triangle_count", std::min((int) m_mesh.triangle_count(), 1024) );
167
168 program_uniform(m_program, "mvpInvMatrix", mvp.inverse());
169 glDrawArrays(GL_TRIANGLES, 0, 3);
170
171 return 1;
172 }

Member Data Documentation

◆ m_mesh

Mesh RT::m_mesh
protected

Definition at line 234 of file tuto_raytrace_compute.cpp.

◆ m_camera

Orbiter RT::m_camera
protected

Definition at line 235 of file tuto_raytrace_compute.cpp.

◆ m_blit_framebuffer

GLuint RT::m_blit_framebuffer
protected

Definition at line 237 of file tuto_raytrace_compute.cpp.

◆ m_program

GLuint RT::m_program
protected

Definition at line 238 of file tuto_raytrace_compute.cpp.

◆ m_texture

GLuint RT::m_texture
protected

Definition at line 239 of file tuto_raytrace_compute.cpp.

◆ m_buffer

GLuint RT::m_buffer
protected

Definition at line 240 of file tuto_raytrace_compute.cpp.

◆ m_vao

GLuint RT::m_vao
protected

Definition at line 179 of file tuto_raytrace_fragment.cpp.


The documentation for this struct was generated from the following files: