Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions examples/viewer/viewer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -1088,9 +1099,15 @@ static void Draw(const std::vector<DrawObject>& drawObjects,
}
}

static void Init() {
static void Init() { FitToScene(); }

// 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;
Expand Down Expand Up @@ -1126,6 +1143,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";

Expand Down Expand Up @@ -1154,14 +1172,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];
Comment on lines +1175 to +1176

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chained assignments for scene_bmin/scene_bmax on single lines reduce readability and make diffs harder to review. Consider formatting these as one assignment per line (or using std::copy) for consistency with the surrounding style.

Suggested change
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_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];

Copilot uses AI. Check for mistakes.

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);
Expand All @@ -1178,23 +1201,23 @@ 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);

build_rotmatrix(mat, curr_quat);
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);
Comment on lines +1214 to +1215

Copilot AI Mar 27, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scene_maxExtent can become 0 (e.g., degenerate geometry where bmin==bmax on all axes), which would make glScalef(1.0f / scene_maxExtent, ...) divide by zero and propagate INF/NaN through the modelview matrix. Consider clamping scene_maxExtent to a small positive epsilon (or falling back to 1.0f) after computing it, and/or early-out when bounds are invalid.

Suggested change
glScalef(1.0f / scene_maxExtent, 1.0f / scene_maxExtent,
1.0f / scene_maxExtent);
float scale = 1.0f;
if (scene_maxExtent > 0.0f) {
scale = 1.0f / scene_maxExtent;
}
glScalef(scale, scale, scale);

Copilot uses AI. Check for mistakes.

#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);

Expand Down