fix matrix multiply problem

This commit is contained in:
Michael Zhang 2023-04-08 20:46:51 -05:00
parent 0212326d0a
commit ce9d7942cf
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B

View file

@ -117,6 +117,12 @@ key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
else if (key == GLFW_KEY_DOWN || key == GLFW_KEY_S)
scale_y -= SCALE_DELTA;
if (scale_x <= 0)
scale_x = SCALE_DELTA;
if (scale_y <= 0)
scale_y = SCALE_DELTA;
}
}
@ -298,7 +304,8 @@ int main(int argc, char **argv) {
// be the identity matrix; you will need define M according to the user's
// actions.
slow4x4MatrixMultiplyIntoColumnOrder(rotation_matrix, scale_matrix, M);
slow4x4MatrixMultiplyAllRowOrder(rotation_matrix, scale_matrix, M);
transposeMatrix(M);
// sanity check that your matrix contents are what you expect them to be
if (DEBUG_ON) {
@ -346,7 +353,15 @@ void printMatrix(string name, GLfloat *matrix) {
);
}
void transposeMatrix(GLfloat *matrix) {}
void transposeMatrix(GLfloat *matrix) {
for (int i = 0; i < 4; ++i) {
for (int j = i + 1; j < 4; ++j) {
GLfloat tmp = matrix[i * 4 + j];
matrix[i * 4 + j] = matrix[j * 4 + i];
matrix[j * 4 + i] = tmp;
}
}
}
void zeroInitGlfloats(GLfloat *arr, uint32_t len) {
for (auto i = 0; i < len; ++i) {
@ -354,29 +369,16 @@ void zeroInitGlfloats(GLfloat *arr, uint32_t len) {
}
}
void slow4x4MatrixMultiplyIntoColumnOrder(
GLfloat *left, GLfloat *right, GLfloat *result
) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
const GLfloat leftVal = left[i * 4 + k];
const GLfloat rightVal = right[j * 4 + k];
result[j * 4 + i] += leftVal * rightVal;
}
}
}
}
void slow4x4MatrixMultiplyAllRowOrder(
GLfloat *left, GLfloat *right, GLfloat *result
) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
result[i * 4 + j] = 0;
for (int k = 0; k < 4; ++k) {
const GLfloat leftVal = left[i * 4 + k];
const GLfloat rightVal = right[j * 4 + k];
result[j * 4 + i] += leftVal * rightVal;
const GLfloat rightVal = right[k + j * 4];
result[i * 4 + j] += leftVal * rightVal;
}
}
}