Implement matrix multiply

This commit is contained in:
Michael Zhang 2023-04-08 17:42:05 -05:00
parent 0c56cc6b8e
commit f6e0d12287
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B

View file

@ -2,13 +2,22 @@
// Based on example code from: Interactive Computer Graphics: A Top-Down
// Approach with Shader-Based OpenGL (6th Edition), by Ed Angel
#include "GLFW/glfw3.h"
#include "glad/glad.h"
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <x86intrin.h>
#include <xmmintrin.h>
#include <iostream>
#include <istream>
#include <optional>
#include <sstream>
// Unfortunately this must be included first because it does some annoying check
// that vomits if GLFW has already been included (dumb)
#include "glad/glad.h"
#include "GLFW/glfw3.h"
#define DEBUG_ON \
0 // repetitive of the debug flag in the shader loading code, included here
@ -18,6 +27,18 @@
// compiles them
#include "ShaderStuff.hpp"
//----------------------------------------------------------------------------
// Forward-declaring some functions for later implementation
/// Multiplies two matrices and puts them into result.
///
/// - Left matrix must be ROW-order
/// - Right matrix must be COLUMN-order
///
/// Result will be row-order
void slow4x4MatrixMultiplyIntoRowOrder(GLfloat *left, GLfloat *right,
GLfloat *result);
//----------------------------------------------------------------------------
// initialize some basic structure types
@ -90,54 +111,29 @@ void init(void) {
FloatType2D vertices[NVERTICES];
GLuint vao[1], buffer, program, location1, location2;
// clang-format off
// set up some hard-coded colors and geometry
// this part can be customized to read in an object description from a file
colors[0].r = 1;
colors[0].g = 1;
colors[0].b = 1; // white
colors[1].r = 1;
colors[1].g = 0;
colors[1].b = 0; // red
colors[2].r = 1;
colors[2].g = 0;
colors[2].b = 0; // red
colors[3].r = 1;
colors[3].g = 1;
colors[3].b = 1; // white
colors[4].r = 0;
colors[4].g = 0;
colors[4].b = 1; // blue
colors[5].r = 0;
colors[5].g = 0;
colors[5].b = 1; // blue
colors[6].r = 1;
colors[6].g = 1;
colors[6].b = 1; // white
colors[7].r = 0;
colors[7].g = 1;
colors[7].b = 1; // cyan
colors[8].r = 0;
colors[8].g = 1;
colors[8].b = 1; // cyan
colors[0].r = 1; colors[0].g = 1; colors[0].b = 1; // white
colors[1].r = 1; colors[1].g = 0; colors[1].b = 0; // red
colors[2].r = 1; colors[2].g = 0; colors[2].b = 0; // red
colors[3].r = 1; colors[3].g = 1; colors[3].b = 1; // white
colors[4].r = 0; colors[4].g = 0; colors[4].b = 1; // blue
colors[5].r = 0; colors[5].g = 0; colors[5].b = 1; // blue
colors[6].r = 1; colors[6].g = 1; colors[6].b = 1; // white
colors[7].r = 0; colors[7].g = 1; colors[7].b = 1; // cyan
colors[8].r = 0; colors[8].g = 1; colors[8].b = 1; // cyan
vertices[0].x = 0;
vertices[0].y = 0.25; // center
vertices[1].x = 0.25;
vertices[1].y = 0.5; // upper right
vertices[2].x = -0.25;
vertices[2].y = 0.5; // upper left
vertices[3].x = 0;
vertices[3].y = 0.25; // center (again)
vertices[4].x = 0.25;
vertices[4].y = -0.5; // low-lower right
vertices[5].x = 0.5;
vertices[5].y = -0.25; // mid-lower right
vertices[6].x = 0;
vertices[6].y = 0.25; // center (again)
vertices[7].x = -0.5;
vertices[7].y = -0.25; // low-lower left
vertices[8].x = -0.25;
vertices[8].y = -0.5; // mid-lower left
vertices[0].x = 0; vertices[0].y = 0.25; // center
vertices[1].x = 0.25; vertices[1].y = 0.5; // upper right
vertices[2].x = -0.25; vertices[2].y = 0.5; // upper left
vertices[3].x = 0; vertices[3].y = 0.25; // center (again)
vertices[4].x = 0.25; vertices[4].y = -0.5; // low-lower right
vertices[5].x = 0.5; vertices[5].y = -0.25; // mid-lower right
vertices[6].x = 0; vertices[6].y = 0.25; // center (again)
vertices[7].x = -0.5; vertices[7].y = -0.25; // low-lower left
vertices[8].x = -0.25; vertices[8].y = -0.5; // mid-lower left
// clang-format on
// Create and bind a vertex array object
glGenVertexArrays(1, vao);
@ -277,3 +273,17 @@ int main(int argc, char **argv) {
exit(EXIT_SUCCESS);
} // end main
void slow4x4MatrixMultiplyIntoRowOrder(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 int resultIdx = i * 4 + j;
const GLfloat leftVal = left[i * 4 + k];
const GLfloat rightVal = right[j * 4 + k];
result[i * 4 + j] += leftVal * rightVal;
}
}
}
}