14
14
15
15
#include < nfd.h>
16
16
17
+ #include < string>
17
18
#include < thread>
18
- #include < ctime>
19
19
#include < cstdlib>
20
20
21
21
void Tracer::Run ()
@@ -163,7 +163,7 @@ void Tracer::Run()
163
163
if (ImGui::TreeNodeEx (" Scene" , ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanFullWidth))
164
164
{
165
165
ImGui::Text (" Light Pos" );
166
- if (ImGui::DragFloat3 (" ##lightPos" , &m_LightPos[0 ], 0 .01f , -1 .0f , 1 .0f ))
166
+ if (ImGui::DragFloat3 (" ##lightPos" , &m_LightPos[0 ], 0 .01f , -100 .0f , 100 .0f ))
167
167
resetFrameIdx = true ;
168
168
169
169
ImGui::Spacing ();
@@ -186,26 +186,63 @@ void Tracer::Run()
186
186
ImGui::Spacing ();
187
187
ImGui::Spacing ();
188
188
189
- if (ImGui::Button (" Add a sphere" ))
189
+ if (ImGui::Button (" Add sphere" ))
190
190
{
191
- resetFrameIdx = true ;
192
-
193
191
m_Scene.spheres .push_back (Sphere ());
194
192
195
- glDeleteBuffers (1 , &ssbo );
193
+ glDeleteBuffers (1 , &m_SceneSSBO );
196
194
197
- glGenBuffers (1 , &ssbo );
198
- glBindBuffer (GL_SHADER_STORAGE_BUFFER, ssbo );
195
+ glGenBuffers (1 , &m_SceneSSBO );
196
+ glBindBuffer (GL_SHADER_STORAGE_BUFFER, m_SceneSSBO );
199
197
glBufferData (GL_SHADER_STORAGE_BUFFER, sizeof (Sphere) * m_Scene.spheres .size (), m_Scene.spheres .data (), GL_DYNAMIC_DRAW);
200
- glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 0 , ssbo );
198
+ glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 0 , m_SceneSSBO );
201
199
202
200
glBindBuffer (GL_SHADER_STORAGE_BUFFER, 0 );
203
201
}
204
202
205
203
ImGui::Spacing ();
206
- ImGui::Spacing ();
207
- ImGui::Spacing ();
204
+
205
+ if (ImGui::Button (" Add material" ))
206
+ {
207
+ m_Scene.materials .push_back (Material{});
208
+
209
+ glDeleteBuffers (1 , &m_MatSSBO);
210
+
211
+ glGenBuffers (1 , &m_MatSSBO);
212
+ glBindBuffer (GL_SHADER_STORAGE_BUFFER, m_MatSSBO);
213
+ glBufferData (GL_SHADER_STORAGE_BUFFER, sizeof (Material) * m_Scene.materials .size (), m_Scene.materials .data (), GL_DYNAMIC_DRAW);
214
+ glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 1 , m_MatSSBO);
215
+
216
+ glBindBuffer (GL_SHADER_STORAGE_BUFFER, 0 );
217
+ }
208
218
219
+ ImGui::Separator ();
220
+
221
+ if (ImGui::TreeNodeEx (" Materials" , ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanFullWidth))
222
+ {
223
+ for (int i = 0 ; i < m_Scene.materials .size (); i++)
224
+ {
225
+ if (ImGui::TreeNodeEx ((" Material #" + std::to_string (i)).c_str (), ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanFullWidth))
226
+ {
227
+ ImGui::Text (" Albedo" );
228
+ if (ImGui::ColorEdit3 ((" ##albedo" + std::to_string (i)).c_str (), &m_Scene.materials [i].albedo [0 ]))
229
+ resetFrameIdx = true ;
230
+
231
+ ImGui::Spacing ();
232
+ ImGui::Spacing ();
233
+ ImGui::Spacing ();
234
+
235
+ ImGui::Text (" Roughness" );
236
+ if (ImGui::DragFloat ((" ##rougness" + std::to_string (i)).c_str (), &m_Scene.materials [i].roughness , 0 .1f , 0 .0f , 1 .0f ))
237
+ resetFrameIdx = true ;
238
+
239
+ ImGui::TreePop ();
240
+ }
241
+ }
242
+
243
+ ImGui::TreePop ();
244
+ }
245
+
209
246
for (int i = 0 ; i < m_Scene.spheres .size (); i++)
210
247
{
211
248
if (ImGui::TreeNodeEx ((" Sphere " + std::to_string (i + 1 )).c_str (), ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanFullWidth))
@@ -217,27 +254,24 @@ void Tracer::Run()
217
254
ImGui::Spacing ();
218
255
ImGui::Spacing ();
219
256
ImGui::Spacing ();
220
-
221
- ImGui::Text (" Sphere Albedo" );
222
- if (ImGui::ColorEdit3 ((" ##sphereAlbedo" + std::to_string (i)).c_str (), &m_Scene.spheres [i].albedo [0 ]))
223
- resetFrameIdx = true ;
224
-
225
- ImGui::Spacing ();
226
- ImGui::Spacing ();
227
- ImGui::Spacing ();
228
257
229
258
ImGui::Text (" Sphere Radius" );
230
- if (ImGui::DragFloat ((" ##sphereRadius" + std::to_string (i)).c_str (), &m_Scene.spheres [i].radius , 0 .1f , 0 .2f , 100 .0f ))
259
+ if (ImGui::DragFloat ((" ##sphereRadius" + std::to_string (i)).c_str (), &m_Scene.spheres [i].radius , 0 .1f , 0 .2f , 1000 .0f ))
231
260
resetFrameIdx = true ;
232
-
233
- ImGui::Spacing ();
234
- ImGui::Spacing ();
235
- ImGui::Spacing ();
236
-
237
- ImGui::Text (" Sphere Roughness " );
238
- if (ImGui::DragFloat ((" ##sphereRoughness " + std::to_string (i)).c_str (), &m_Scene.spheres [i].roughness , 0 . 1f , 0 . 0f , 1 .0f ))
261
+
262
+ ImGui::Spacing ();
263
+ ImGui::Spacing ();
264
+ ImGui::Spacing ();
265
+
266
+ ImGui::Text (" Material Index " );
267
+ if (ImGui::DragInt ((" ##matIdx " + std::to_string (i)).c_str (), &m_Scene.spheres [i].materialIdx , 1 .0f ))
239
268
resetFrameIdx = true ;
240
269
270
+ if (m_Scene.spheres [i].materialIdx >= m_Scene.materials .size ())
271
+ {
272
+ m_Scene.spheres [i].materialIdx = 0 ;
273
+ }
274
+
241
275
ImGui::TreePop ();
242
276
}
243
277
}
@@ -339,13 +373,21 @@ void Tracer::Init()
339
373
// Scene
340
374
{
341
375
m_Scene.spheres .push_back (Sphere{});
376
+ m_Scene.materials .push_back (Material{});
342
377
}
343
- // SSBO
378
+ // SSBOs
344
379
{
345
- glGenBuffers (1 , &ssbo );
346
- glBindBuffer (GL_SHADER_STORAGE_BUFFER, ssbo );
380
+ glGenBuffers (1 , &m_SceneSSBO );
381
+ glBindBuffer (GL_SHADER_STORAGE_BUFFER, m_SceneSSBO );
347
382
glBufferData (GL_SHADER_STORAGE_BUFFER, sizeof (Sphere) * m_Scene.spheres .size (), m_Scene.spheres .data (), GL_DYNAMIC_DRAW);
348
- glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 0 , ssbo);
383
+ glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 0 , m_SceneSSBO);
384
+
385
+ glBindBuffer (GL_SHADER_STORAGE_BUFFER, 0 );
386
+
387
+ glGenBuffers (1 , &m_MatSSBO);
388
+ glBindBuffer (GL_SHADER_STORAGE_BUFFER, m_MatSSBO);
389
+ glBufferData (GL_SHADER_STORAGE_BUFFER, sizeof (Material) * m_Scene.materials .size (), m_Scene.materials .data (), GL_DYNAMIC_DRAW);
390
+ glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 1 , m_MatSSBO);
349
391
350
392
glBindBuffer (GL_SHADER_STORAGE_BUFFER, 0 );
351
393
}
@@ -360,16 +402,23 @@ void Tracer::Cleanup()
360
402
361
403
m_Shader.Destroy ();
362
404
405
+ glDeleteBuffers (1 , &m_MatSSBO);
406
+ glDeleteBuffers (1 , &m_SceneSSBO);
407
+
363
408
glDeleteBuffers (1 , &vbo);
364
409
glDeleteVertexArrays (1 , &vao);
365
410
m_Window.Destroy ();
366
411
}
367
412
368
413
void Tracer::Render (int width, int height)
369
414
{
370
- glBindBuffer (GL_SHADER_STORAGE_BUFFER, ssbo );
415
+ glBindBuffer (GL_SHADER_STORAGE_BUFFER, m_SceneSSBO );
371
416
glBufferSubData (GL_SHADER_STORAGE_BUFFER, 0 , sizeof (Sphere) * m_Scene.spheres .size (), m_Scene.spheres .data ());
372
- glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 0 , ssbo);
417
+ glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 0 , m_SceneSSBO);
418
+
419
+ glBindBuffer (GL_SHADER_STORAGE_BUFFER, m_MatSSBO);
420
+ glBufferSubData (GL_SHADER_STORAGE_BUFFER, 0 , sizeof (Material) * m_Scene.materials .size (), m_Scene.materials .data ());
421
+ glBindBufferBase (GL_SHADER_STORAGE_BUFFER, 1 , m_MatSSBO);
373
422
374
423
m_Shader.Bind ();
375
424
@@ -380,7 +429,6 @@ void Tracer::Render(int width, int height)
380
429
m_Shader.PutTex (" t_Skybox" , 0 );
381
430
m_Shader.PutTex (" t_PrevFrame" , 1 );
382
431
383
- m_Shader.PutInt (" u_SphereCount" , m_Scene.spheres .size ());
384
432
m_Shader.PutInt (" u_MaxBounces" , m_MaxBounces);
385
433
m_Shader.PutInt (" u_FrameIdx" , m_FrameIdx);
386
434
m_Shader.PutInt (" u_Accumulate" , (int )m_Accumulate);
@@ -415,4 +463,4 @@ void Tracer::WriteToPngFile(std::string fileName, unsigned char* pixels, int wid
415
463
stbi_write_png (fileName.c_str (), width, height, channels, flippedPixels, width * channels);
416
464
417
465
delete[] flippedPixels;
418
- }
466
+ }
0 commit comments