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 71 of file tuto_raytrace_compute.cpp.

Constructor & Destructor Documentation

◆ RT() [1/2]

RT::RT ( const char * filename)
inline

Definition at line 74 of file tuto_raytrace_compute.cpp.

74 : AppTime(1024, 640)
75 {
76 m_mesh= read_mesh(filename);
77 }
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 79 of file tuto_raytrace_compute.cpp.

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

152 {
153 release_program(m_program);
154 glDeleteTextures(1, &m_texture);
155 glDeleteBuffers(1, &m_buffer);
156 glDeleteFramebuffers(1, &m_blit_framebuffer);
157 return 0;
158 }
int release_program(const GLuint program)
Definition program.cpp:238

◆ render() [1/2]

int RT::render ( )
inlinevirtual

a deriver pour afficher les objets.

Implements AppTime.

Definition at line 160 of file tuto_raytrace_compute.cpp.

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

◆ m_camera

Orbiter RT::m_camera
protected

Definition at line 229 of file tuto_raytrace_compute.cpp.

◆ m_blit_framebuffer

GLuint RT::m_blit_framebuffer
protected

Definition at line 231 of file tuto_raytrace_compute.cpp.

◆ m_program

GLuint RT::m_program
protected

Definition at line 232 of file tuto_raytrace_compute.cpp.

◆ m_texture

GLuint RT::m_texture
protected

Definition at line 233 of file tuto_raytrace_compute.cpp.

◆ m_buffer

GLuint RT::m_buffer
protected

Definition at line 234 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: