From 67520399cee699b084090864816f6c3b1f7ca7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BAlio=20Pinto?= Date: Mon, 4 Mar 2024 18:17:13 +0000 Subject: [PATCH] fix: center image to window --- engine/src/main.cpp | 54 +++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/engine/src/main.cpp b/engine/src/main.cpp index 14f02b9..9703b54 100644 --- a/engine/src/main.cpp +++ b/engine/src/main.cpp @@ -14,36 +14,24 @@ float cameraAngle = 90.0f; float cameraAngleY = 0.0f; Configuration c; +void reshape(int w, int h) { + float aspect_ratio = (float)w / (float)h; -void changeSize(int w, int h) { - // Prevent a divide by zero, when window is too short - // (you can�t make a window with zero width). - if (h == 0) h = 1; + // Set the viewport to the entire window + glViewport(0, 0, w, h); - float ratio = c.window.width * 1.0f / c.window.height; - // Set the projection matrix as current - glMatrixMode(GL_PROJECTION); - // Load the identity matrix - glLoadIdentity(); - // Set the viewport to be the entire window - glViewport(0, 0, c.window.width, c.window.height); - // Set the perspective - gluPerspective(c.camera.fov, ratio, c.camera.near, c.camera.far); - // return to the model view matrix mode - glMatrixMode(GL_MODELVIEW); + // Set up the projection matrix + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(c.camera.fov, aspect_ratio, c.camera.near, c.camera.far); + + // Reset the modelview matrix + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); } -void renderScene(void) { - // clear buffers - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // set camera - glLoadIdentity(); - // gluLookAt(0.0f, 0.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f); - gluLookAt(c.camera.position.x, c.camera.position.y, c.camera.position.z, - c.camera.lookAt.x, c.camera.lookAt.y, c.camera.lookAt.z, - c.camera.up.x, c.camera.up.y, c.camera.up.z); +void drawAxis(void) { - // put drawing instructions here glBegin(GL_LINES); // x-axis (red) glColor3f(50.0f, 0.0f, 0.0f); @@ -59,6 +47,20 @@ void renderScene(void) { glVertex3f(0.0f, 0.0f, 50.0f); glEnd(); +} +void renderScene(void) { + // clear buffers + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + // set camera + glLoadIdentity(); + // gluLookAt(0.0f, 0.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f); + gluLookAt(c.camera.position.x, c.camera.position.y, c.camera.position.z, + c.camera.lookAt.x, c.camera.lookAt.y, c.camera.lookAt.z, + c.camera.up.x, c.camera.up.y, c.camera.up.z); + + // put drawing instructions here + drawAxis(); + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); for (std::string model : c.models) { drawFile(model.data()); @@ -103,7 +105,7 @@ int main(int argc, char** argv) { // put callback registry here glutIdleFunc(renderScene); glutDisplayFunc(renderScene); - glutReshapeFunc(changeSize); + glutReshapeFunc(reshape); glutSpecialFunc(processSpecialKeys);