This commit is contained in:
Michael Zhang 2023-04-08 23:55:13 -05:00
parent d5962f10e3
commit d327791de9
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B

View file

@ -33,6 +33,7 @@ using namespace std;
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
// Forward-declaring some functions for later implementation // Forward-declaring some functions for later implementation
void resetEverything();
void copyMatrix(GLfloat *a, GLfloat *b); void copyMatrix(GLfloat *a, GLfloat *b);
/// Print the matrix /// Print the matrix
@ -54,7 +55,7 @@ void slow4x4MatrixMultiplyIntoColumnOrder(
GLfloat *left, GLfloat *right, GLfloat *result GLfloat *left, GLfloat *right, GLfloat *result
); );
GLfloat *slow4x4MatrixMultiplyAllRowOrder(GLfloat *left, GLfloat *right); GLfloat *matrixMultiply(GLfloat *left, GLfloat *right);
void handleScaling(int key, int scancode, int action, int mods); void handleScaling(int key, int scancode, int action, int mods);
void handleRotate(double xpos, double ypos); void handleRotate(double xpos, double ypos);
@ -115,10 +116,13 @@ static void error_callback(int error, const char *description) {
static void static void
key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
// checks to see if the escape key was pressed // checks to see if the escape key was pressed
if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_PRESS) if ((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action == GLFW_RELEASE)
// closes the window // closes the window
glfwSetWindowShouldClose(window, GL_TRUE); glfwSetWindowShouldClose(window, GL_TRUE);
if (key == GLFW_KEY_R && action == GLFW_RELEASE)
resetEverything();
// Handle ctrl key press // Handle ctrl key press
if (key == GLFW_KEY_LEFT_CONTROL) if (key == GLFW_KEY_LEFT_CONTROL)
is_ctrl_held = action == GLFW_PRESS; is_ctrl_held = action == GLFW_PRESS;
@ -336,10 +340,8 @@ int main(int argc, char **argv) {
// be the identity matrix; you will need define M according to the user's // be the identity matrix; you will need define M according to the user's
// actions. // actions.
GLfloat *result1 = GLfloat *result1 = matrixMultiply(scale_matrix, rotation_matrix);
slow4x4MatrixMultiplyAllRowOrder(scale_matrix, rotation_matrix); GLfloat *result2 = matrixMultiply(translation_matrix, result1);
GLfloat *result2 =
slow4x4MatrixMultiplyAllRowOrder(translation_matrix, result1);
copyMatrix(result2, M); copyMatrix(result2, M);
transposeMatrix(M); transposeMatrix(M);
@ -384,6 +386,14 @@ int main(int argc, char **argv) {
} // end main } // end main
void resetEverything() {
scale_x = 1.0;
scale_y = 1.0;
translate_x = 0;
translate_y = 0;
rotation_angle = 0;
}
void printMatrix(string name, GLfloat *matrix) { void printMatrix(string name, GLfloat *matrix) {
printf( printf(
"%s = [%f %f %f %f\n" "%s = [%f %f %f %f\n"
@ -418,7 +428,7 @@ void zeroInitGlfloats(GLfloat *arr, uint32_t len) {
} }
} }
GLfloat *slow4x4MatrixMultiplyAllRowOrder(GLfloat *left, GLfloat *right) { GLfloat *matrixMultiply(GLfloat *left, GLfloat *right) {
GLfloat *result = (GLfloat *)malloc(16 * sizeof(GLfloat)); GLfloat *result = (GLfloat *)malloc(16 * sizeof(GLfloat));
for (int i = 0; i < 4; ++i) { for (int i = 0; i < 4; ++i) {