~ | Imgui and other

This commit is contained in:
TheRedShip
2025-01-31 12:19:27 +01:00
parent 0b5de9f683
commit 49bc86433f
9 changed files with 156 additions and 118 deletions

View File

@ -99,16 +99,9 @@ int main(int argc, char **argv)
glBindBuffer(GL_UNIFORM_BUFFER, debugUBO);
glBufferData(GL_UNIFORM_BUFFER, sizeof(GPUDebug), nullptr, GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 2, debugUBO);
try
{
scene.loadTextures();
}
catch(const std::exception& e)
{
std::cerr << e.what() << std::endl;
if (!scene.loadTextures())
return (1);
}
shader.attach();

View File

@ -78,7 +78,7 @@ void BVH::subdivide(std::vector<Triangle> &triangles)
if (_primitive_count <= 4)
return ;
const int num_test_per_axis = 5;
const int num_test_per_axis = 10;
int best_axis = 0;
float best_pos = 0;
@ -207,37 +207,56 @@ int BVH::getLeaves()
return (count);
}
//get tri per leaf min max and average
BVHStats BVH::analyzeBVHLeaves(BVH *root)
BVHStats BVH::analyzeBVHLeaves(BVH *root, int current_depth)
{
if (!root)
return {0, 0, 0.0f};
// If this is a leaf node, return its stats
return {0, 0, 0.0f, INT_MAX, 0, 0.0f, 0};
// If this is a leaf node, return its stats
if (root->_is_leaf)
return {
root->_primitive_count, // min
root->_primitive_count, // max
(float)root->_primitive_count // average
root->_primitive_count, // min triangles
root->_primitive_count, // max triangles
(float)root->_primitive_count, // average triangles
current_depth, // min depth
current_depth, // max depth
(float)current_depth, // depth sum (will be averaged later)
1 // this leaf counts as 1 towards total leaves
};
// Get stats from left and right subtrees
BVHStats left = analyzeBVHLeaves(root->_left);
BVHStats right = analyzeBVHLeaves(root->_right);
BVHStats left = analyzeBVHLeaves(root->_left, current_depth + 1);
BVHStats right = analyzeBVHLeaves(root->_right, current_depth + 1);
// Combine the results
int min_count = std::min(left.min_triangles, right.min_triangles);
int max_count = std::max(left.max_triangles, right.max_triangles);
BVHStats combined;
// Calculate weighted average based on number of leaves in each subtree
float left_leaf_count = (left.average_triangles > 0) ? 1.0f : 0.0f;
float right_leaf_count = (right.average_triangles > 0) ? 1.0f : 0.0f;
float total_leaf_count = left_leaf_count + right_leaf_count;
float avg_count = 0.0f;
if (total_leaf_count > 0)
avg_count = (left.average_triangles * left_leaf_count +
right.average_triangles * right_leaf_count) / total_leaf_count;
// Triangle statistics
combined.min_triangles = std::min(left.min_triangles, right.min_triangles);
combined.max_triangles = std::max(left.max_triangles, right.max_triangles);
return {min_count, max_count, avg_count};
}
// Depth statistics
combined.min_depth = std::min(left.min_depth, right.min_depth);
combined.max_depth = std::max(left.max_depth, right.max_depth);
// Count total leaves
combined.total_leaves = left.total_leaves + right.total_leaves;
// Calculate weighted averages
if (combined.total_leaves > 0) {
// Average triangles
combined.average_triangles =
(left.average_triangles * left.total_leaves +
right.average_triangles * right.total_leaves) / combined.total_leaves;
// Average depth
combined.average_depth =
(left.average_depth * left.total_leaves +
right.average_depth * right.total_leaves) / combined.total_leaves;
} else {
combined.average_triangles = 0.0f;
combined.average_depth = 0.0f;
}
return combined;
}

View File

@ -284,7 +284,9 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
float x, y, z;
if(!(lineStream >> x >> y >> z))
throw std::runtime_error("OBJ: syntax error while getting material emission");
mat->emission = (x + y + z) / 3;
mat->emission = (x + y + z);
// if (mat->emission > 0.0)
// mat->color = glm::vec3(x, y, z);
}
else if(identifier == "Ni")
{

View File

@ -621,11 +621,13 @@ void Renderer::imguiRenderInfo(void)
void Renderer::renderImgui(void)
{
ImGui::Begin("Renderer");
if(rendering())
imguiRenderInfo();
else
imguiPathCreation();
ImGui::End();
// ImGui::Begin("Renderer");
if (ImGui::CollapsingHeader("Renderer"))
{
if(rendering())
imguiRenderInfo();
else
imguiPathCreation();
}
// ImGui::End();
}

View File

@ -194,10 +194,15 @@ void Scene::addBvh(std::vector<Triangle> &triangles, glm::vec3 offset, float sc
std::cout << "\tBVH size: " << bvh->getSize() << std::endl;
std::cout << "\tBVH leaves: " << bvh->getLeaves() << std::endl << std::endl;
BVHStats stats = bvh->analyzeBVHLeaves(bvh);
BVHStats stats = bvh->analyzeBVHLeaves(bvh, 0);
std::cout << "\tMin triangles per leaf: " << stats.min_triangles << std::endl;
std::cout << "\tMax triangles per leaf: " << stats.max_triangles << std::endl;
std::cout << "\tAverage triangles per leaf: " << stats.average_triangles << std::endl << std::endl;
std::cout << "\n\tMin depth: " << stats.min_depth << std::endl;
std::cout << "\tMax depth: " << stats.max_depth << std::endl;
std::cout << "\tAverage depth: " << stats.average_depth << std::endl;
}
@ -220,7 +225,7 @@ void Scene::addTexture(std::string path)
_textures.push_back(path);
}
void Scene::loadTextures()
bool Scene::loadTextures()
{
for (std::string &path : _textures)
{
@ -228,7 +233,10 @@ void Scene::loadTextures()
unsigned char* image = stbi_load(path.c_str(), &width, &height, &channels, STBI_rgb_alpha);
if (!image)
throw std::runtime_error("Failed to load texture " + path);
{
std::cout << "Failed to load texture " << path << std::endl;
return (false);
}
std::cout << "Loaded texture: " << path << " (" << width << "x" << height << ")" << std::endl;
@ -248,6 +256,8 @@ void Scene::loadTextures()
stbi_image_free(image);
}
return (true);
}
void Scene::updateLightAndObjects(int mat_id)

View File

@ -180,94 +180,103 @@ void Window::imGuiRender()
{
bool has_changed = false;
ImGui::Begin("Camera");
ImGui::Begin("Settings");
ImGui::Text("Fps: %d", int(_fps));
ImGui::Text("Frame: %d", _frameCount);
ImGui::Text("Objects: %lu", _scene->getObjectData().size() + _scene->getTriangleData().size());
ImGui::Separator();
if (ImGui::Checkbox("Accumulate", &accumulate))
_frameCount = 0;
ImGui::Spacing();
has_changed |= ImGui::SliderInt("Bounce", &_scene->getCamera()->getBounce(), 0, 20);
has_changed |= ImGui::SliderFloat("FOV", &_scene->getCamera()->getFov(), 1.0f, 180.0f);
has_changed |= ImGui::SliderFloat("Aperture", &_scene->getCamera()->getAperture(), 0.0f, 1.0f);
has_changed |= ImGui::SliderFloat("Focus", &_scene->getCamera()->getFocus(), 0.0f, 150.0f);
ImGui::End();
ImGui::Begin("Material");
for (unsigned int i = 0; i < _scene->getMaterialData().size(); i++)
if (ImGui::CollapsingHeader("Camera"))
{
GPUMaterial &mat = _scene->getMaterialData()[i];
ImGui::PushID(i);
ImGui::Text("Material %d", i);
has_changed |= ImGui::ColorEdit3("Color", &mat.color[0]);
if (ImGui::SliderFloat("Emission", &mat.emission, 0.0f, 10.0f))
{
has_changed = 1;
_scene->updateLightAndObjects(i);
}
if (mat.type == 0)
{
has_changed |= ImGui::SliderFloat("Roughness", &mat.roughness, 0.0f, 1.0f);
has_changed |= ImGui::SliderFloat("Metallic", &mat.metallic, 0.0f, 1.0f);
}
else if (mat.type == 1)
has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 5.0f);
else if (mat.type == 2)
{
has_changed |= ImGui::SliderFloat("Transparency", &mat.roughness, 0.0f, 1.0f);
has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 2.0f);
has_changed |= ImGui::SliderFloat("Proba", &mat.metallic, 0., 1.);
}
has_changed |= ImGui::SliderInt("Type", &mat.type, 0, 2);
if (ImGui::Checkbox("Accumulate", &accumulate))
_frameCount = 0;
ImGui::PopID();
has_changed |= ImGui::SliderInt("Bounce", &_scene->getCamera()->getBounce(), 0, 20);
has_changed |= ImGui::SliderFloat("FOV", &_scene->getCamera()->getFov(), 1.0f, 180.0f);
has_changed |= ImGui::SliderFloat("Aperture", &_scene->getCamera()->getAperture(), 0.0f, 1.0f);
has_changed |= ImGui::SliderFloat("Focus", &_scene->getCamera()->getFocus(), 0.0f, 150.0f);
}
if (ImGui::CollapsingHeader("Material"))
{
ImGui::BeginChild("Header", ImVec2(0, 400), true, 0);
for (unsigned int i = 0; i < _scene->getMaterialData().size(); i++)
{
GPUMaterial &mat = _scene->getMaterialData()[i];
ImGui::PushID(i);
ImGui::Text("Material %d", i);
has_changed |= ImGui::ColorEdit3("Color", &mat.color[0]);
if (ImGui::SliderFloat("Emission", &mat.emission, 0.0f, 10.0f))
{
has_changed = 1;
_scene->updateLightAndObjects(i);
}
if (mat.type == 0)
{
has_changed |= ImGui::SliderFloat("Roughness", &mat.roughness, 0.0f, 1.0f);
has_changed |= ImGui::SliderFloat("Metallic", &mat.metallic, 0.0f, 1.0f);
}
else if (mat.type == 1)
has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 5.0f);
else if (mat.type == 2)
{
has_changed |= ImGui::SliderFloat("Transparency", &mat.roughness, 0.0f, 1.0f);
has_changed |= ImGui::SliderFloat("Refraction", &mat.refraction, 1.0f, 2.0f);
has_changed |= ImGui::SliderFloat("Proba", &mat.metallic, 0., 1.);
}
has_changed |= ImGui::SliderInt("Type", &mat.type, 0, 2);
ImGui::PopID();
ImGui::Separator();
}
ImGui::EndChild();
}
if (ImGui::CollapsingHeader("Fog"))
{
has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getVolume().enabled));
ImGui::Separator();
if (ImGui::SliderFloat("Absorption", &_scene->getVolume().sigma_a.x, 0., 0.1))
{
_scene->getVolume().sigma_a = glm::vec3(_scene->getVolume().sigma_a.x);
_scene->getVolume().sigma_t = _scene->getVolume().sigma_a + _scene->getVolume().sigma_s;
has_changed = true;
}
if (ImGui::SliderFloat("Scattering", &_scene->getVolume().sigma_s.x, 0., 0.5))
{
_scene->getVolume().sigma_s = glm::vec3(_scene->getVolume().sigma_s.x);
_scene->getVolume().sigma_t = _scene->getVolume().sigma_a + _scene->getVolume().sigma_s;
has_changed = true;
}
if (ImGui::SliderFloat("G", &_scene->getVolume().g, 0., 1.))
has_changed = true;
}
ImGui::End();
ImGui::Begin("Fog settings");
has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getVolume().enabled));
ImGui::Separator();
if (ImGui::SliderFloat("Absorption", &_scene->getVolume().sigma_a.x, 0., 0.1))
if (ImGui::CollapsingHeader("Debug"))
{
_scene->getVolume().sigma_a = glm::vec3(_scene->getVolume().sigma_a.x);
_scene->getVolume().sigma_t = _scene->getVolume().sigma_a + _scene->getVolume().sigma_s;
has_changed = true;
has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getDebug().enabled));
ImGui::Separator();
has_changed |= ImGui::SliderInt("Debug mode", &_scene->getDebug().mode, 0, 2);
has_changed |= ImGui::SliderInt("Box treshold", &_scene->getDebug().box_treshold, 1, 2000);
has_changed |= ImGui::SliderInt("Triangle treshold", &_scene->getDebug().triangle_treshold, 1, 2000);
}
if (ImGui::SliderFloat("Scattering", &_scene->getVolume().sigma_s.x, 0., 0.5))
{
_scene->getVolume().sigma_s = glm::vec3(_scene->getVolume().sigma_s.x);
_scene->getVolume().sigma_t = _scene->getVolume().sigma_a + _scene->getVolume().sigma_s;
has_changed = true;
}
if (ImGui::SliderFloat("G", &_scene->getVolume().g, 0., 1.))
has_changed = true;
ImGui::End();
ImGui::Begin("Debug BVH");
has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getDebug().enabled));
ImGui::Separator();
has_changed |= ImGui::SliderInt("Debug mode", &_scene->getDebug().mode, 0, 2);
has_changed |= ImGui::SliderInt("Box treshold", &_scene->getDebug().box_treshold, 1, 2000);
has_changed |= ImGui::SliderInt("Triangle treshold", &_scene->getDebug().triangle_treshold, 1, 2000);
ImGui::End();
_renderer->renderImgui();;
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());