From d1eab0d16fadb572f797f345df26fef8d1d1a0a6 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 8 Apr 2023 17:46:57 -0500 Subject: [PATCH] Implement the arrow keys controlling the scaling --- assignment-2a/src/HW2a.cpp | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/assignment-2a/src/HW2a.cpp b/assignment-2a/src/HW2a.cpp index a83bf5e..795e668 100755 --- a/assignment-2a/src/HW2a.cpp +++ b/assignment-2a/src/HW2a.cpp @@ -59,6 +59,10 @@ GLint window_width = 500; GLint window_height = 500; GLdouble pi = 4.0 * atan(1.0); +// Added state +GLdouble scale_x = 1.0, scale_y = 1.0; +const GLdouble SCALE_DELTA = 0.05; + GLFWcursor *hand_cursor, *arrow_cursor; // some different cursors GLint NVERTICES = 9; // part of the hard-coded model @@ -74,9 +78,25 @@ static void error_callback(int error, const char *description) { // keyboard input will be handled static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { - if (key == GLFW_KEY_ESCAPE && - action == GLFW_PRESS) // checks to see if the escape key was pressed - glfwSetWindowShouldClose(window, GL_TRUE); // closes the window + // checks to see if the escape key was pressed + if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_PRESS) + // closes the window + glfwSetWindowShouldClose(window, GL_TRUE); + + // arrow keys control the scaling of the object + if (action == GLFW_PRESS || action == GLFW_REPEAT) { + if (key == GLFW_KEY_LEFT || key == GLFW_KEY_A) + scale_x -= SCALE_DELTA; + + else if (key == GLFW_KEY_RIGHT || key == GLFW_KEY_D) + scale_x += SCALE_DELTA; + + else if (key == GLFW_KEY_UP || key == GLFW_KEY_W) + scale_y += SCALE_DELTA; + + else if (key == GLFW_KEY_DOWN || key == GLFW_KEY_S) + scale_y -= SCALE_DELTA; + } } //---------------------------------------------------------------------------- @@ -240,9 +260,13 @@ int main(int argc, char **argv) { // define/re-define the modelview matrix. In this template, we define M to // be the identity matrix; you will need define M according to the user's // actions. - for (i = 0; i < 16; i++) { + /* for (i = 0; i < 16; i++) { M[i] = (i % 5 == 0); - } + }*/ + M[0] = scale_x; + M[5] = scale_y; + M[10] = 1.0; + M[15] = 1.0; // sanity check that your matrix contents are what you expect them to be if (DEBUG_ON)