From 4fd8c8ab2e516d3adafbfaf0394d4f0daa8cea8f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 27 Mar 2026 19:44:12 +0000 Subject: [PATCH 1/4] Initial plan From 04b113ddb742f463f969b36e163423378c786557 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 27 Mar 2026 19:49:28 +0000 Subject: [PATCH 2/4] Add 'fit to scene' feature to examples/viewer - Press 'F' key to reset camera to view entire model - Initial camera position set to fit the scene on load - Enable centering translation to center objects at origin - Store bounding box data in globals for FitToScene() access - Update help text to document 'F' key Agent-Logs-Url: https://github.com/tinyobjloader/tinyobjloader/sessions/3b05e7bf-a2b5-4b2d-b308-718537dfc75a Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com> --- examples/viewer/viewer.cc | 63 +++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/examples/viewer/viewer.cc b/examples/viewer/viewer.cc index 2e6bd952..24dfec04 100644 --- a/examples/viewer/viewer.cc +++ b/examples/viewer/viewer.cc @@ -157,6 +157,10 @@ float g_angleY = 0.0f; // in degree bool g_show_wire = true; bool g_cull_face = false; +float scene_bmin[3] = {0.0f, 0.0f, 0.0f}; +float scene_bmax[3] = {0.0f, 0.0f, 0.0f}; +float scene_maxExtent = 1.0f; + GLFWwindow* window; static std::string GetBaseDir(const std::string& filepath) { @@ -923,6 +927,8 @@ static void reshapeFunc(GLFWwindow* window, int w, int h) { height = h; } +static void FitToScene(); + static void keyboardFunc(GLFWwindow* window, int key, int scancode, int action, int mods) { (void)window; @@ -959,6 +965,11 @@ static void keyboardFunc(GLFWwindow* window, int key, int scancode, int action, g_cull_face = !g_cull_face; } + if (key == GLFW_KEY_F) { + // fit to scene + FitToScene(); + } + // init_frame = true; } } @@ -1104,6 +1115,26 @@ static void Init() { up[2] = 0.0f; } +// Reset camera to fit the entire scene in view. +static void FitToScene() { + trackball(curr_quat, 0, 0, 0, 0); + + g_angleX = 0.0f; + g_angleY = 0.0f; + + eye[0] = 0.0f; + eye[1] = 0.0f; + eye[2] = 3.0f; + + lookat[0] = 0.0f; + lookat[1] = 0.0f; + lookat[2] = 0.0f; + + up[0] = 0.0f; + up[1] = 1.0f; + up[2] = 0.0f; +} + int main(int argc, char** argv) { if (argc < 2) { std::cout << "Needs input.obj\n" << std::endl; @@ -1126,6 +1157,7 @@ int main(int argc, char** argv) { std::cout << "W : Toggle wireframe\n"; std::cout << "C : Toggle face culling\n"; + std::cout << "F : Fit to scene\n"; //std::cout << "K, J, H, L, P, N : Move camera\n"; std::cout << "Q, Esc : quit\n"; @@ -1154,14 +1186,19 @@ int main(int argc, char** argv) { return -1; } - float maxExtent = 0.5f * (bmax[0] - bmin[0]); - if (maxExtent < 0.5f * (bmax[1] - bmin[1])) { - maxExtent = 0.5f * (bmax[1] - bmin[1]); + scene_bmin[0] = bmin[0]; scene_bmin[1] = bmin[1]; scene_bmin[2] = bmin[2]; + scene_bmax[0] = bmax[0]; scene_bmax[1] = bmax[1]; scene_bmax[2] = bmax[2]; + + scene_maxExtent = 0.5f * (bmax[0] - bmin[0]); + if (scene_maxExtent < 0.5f * (bmax[1] - bmin[1])) { + scene_maxExtent = 0.5f * (bmax[1] - bmin[1]); } - if (maxExtent < 0.5f * (bmax[2] - bmin[2])) { - maxExtent = 0.5f * (bmax[2] - bmin[2]); + if (scene_maxExtent < 0.5f * (bmax[2] - bmin[2])) { + scene_maxExtent = 0.5f * (bmax[2] - bmin[2]); } + FitToScene(); + while (glfwWindowShouldClose(window) == GL_FALSE) { glfwPollEvents(); glClearColor(0.1f, 0.2f, 0.3f, 1.0f); @@ -1178,9 +1215,9 @@ int main(int argc, char** argv) { up[1], up[2]); float center[3]; - center[0] = 0.5 * (bmax[0] + bmin[0]); - center[1] = 0.5 * (bmax[1] + bmin[1]); - center[2] = 0.5 * (bmax[2] + bmin[2]); + center[0] = 0.5f * (scene_bmax[0] + scene_bmin[0]); + center[1] = 0.5f * (scene_bmax[1] + scene_bmin[1]); + center[2] = 0.5f * (scene_bmax[2] + scene_bmin[2]); float rotm[4][4]; turntable(g_angleX, g_angleY, center, rotm); @@ -1188,13 +1225,13 @@ int main(int argc, char** argv) { glMultMatrixf(&mat[0][0]); // Fit to -1, 1 - glScalef(1.0f / maxExtent, 1.0f / maxExtent, 1.0f / maxExtent); + glScalef(1.0f / scene_maxExtent, 1.0f / scene_maxExtent, + 1.0f / scene_maxExtent); -#if 0 // Centerize object. - glTranslatef(-0.5 * (bmax[0] + bmin[0]), -0.5 * (bmax[1] + bmin[1]), - -0.5 * (bmax[2] + bmin[2])); -#endif + glTranslatef(-0.5f * (scene_bmax[0] + scene_bmin[0]), + -0.5f * (scene_bmax[1] + scene_bmin[1]), + -0.5f * (scene_bmax[2] + scene_bmin[2])); Draw(gDrawObjects, materials, textures); From 05afe287e3990d51a0d46fd34ef58ac48bf8b4c8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 17:59:01 +0000 Subject: [PATCH 3/4] Deduplicate camera reset via FitToScene Agent-Logs-Url: https://github.com/tinyobjloader/tinyobjloader/sessions/bc775a18-3d4d-4633-8094-588a51daab3d Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com> --- examples/viewer/viewer.cc | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/examples/viewer/viewer.cc b/examples/viewer/viewer.cc index 24dfec04..25b463fb 100644 --- a/examples/viewer/viewer.cc +++ b/examples/viewer/viewer.cc @@ -1099,21 +1099,9 @@ static void Draw(const std::vector& drawObjects, } } -static void Init() { - trackball(curr_quat, 0, 0, 0, 0); - - eye[0] = 0.0f; - eye[1] = 0.0f; - eye[2] = 3.0f; - - lookat[0] = 0.0f; - lookat[1] = 0.0f; - lookat[2] = 0.0f; +static void FitToScene(); - up[0] = 0.0f; - up[1] = 1.0f; - up[2] = 0.0f; -} +static void Init() { FitToScene(); } // Reset camera to fit the entire scene in view. static void FitToScene() { From 98840c2b0df2c6a3fa5ec9bbc7cb3299abbe0043 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 20 May 2026 17:59:53 +0000 Subject: [PATCH 4/4] Remove duplicate FitToScene declaration Agent-Logs-Url: https://github.com/tinyobjloader/tinyobjloader/sessions/bc775a18-3d4d-4633-8094-588a51daab3d Co-authored-by: syoyo <18676+syoyo@users.noreply.github.com> --- examples/viewer/viewer.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/viewer/viewer.cc b/examples/viewer/viewer.cc index 25b463fb..5d236854 100644 --- a/examples/viewer/viewer.cc +++ b/examples/viewer/viewer.cc @@ -1099,8 +1099,6 @@ static void Draw(const std::vector& drawObjects, } } -static void FitToScene(); - static void Init() { FitToScene(); } // Reset camera to fit the entire scene in view.