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 // Based on example code from: Interactive Computer Graphics: A Top-Down
// Approach with Shader-Based OpenGL (6th Edition), by Ed Angel // Approach with Shader-Based OpenGL (6th Edition), by Ed Angel
#include "GLFW/glfw3.h"
#include "glad/glad.h"
#include <iostream>
#include <math.h> #include <math.h>
#include <sstream>
#include <stdio.h> #include <stdio.h>
#include <stdlib.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 \ #define DEBUG_ON \
0 // repetitive of the debug flag in the shader loading code, included here 0 // repetitive of the debug flag in the shader loading code, included here
@ -18,6 +27,18 @@
// compiles them // compiles them
#include "ShaderStuff.hpp" #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 // initialize some basic structure types
@ -90,54 +111,29 @@ void init(void) {
FloatType2D vertices[NVERTICES]; FloatType2D vertices[NVERTICES];
GLuint vao[1], buffer, program, location1, location2; GLuint vao[1], buffer, program, location1, location2;
// clang-format off
// set up some hard-coded colors and geometry // set up some hard-coded colors and geometry
// this part can be customized to read in an object description from a file // this part can be customized to read in an object description from a file
colors[0].r = 1; colors[0].r = 1; colors[0].g = 1; colors[0].b = 1; // white
colors[0].g = 1; colors[1].r = 1; colors[1].g = 0; colors[1].b = 0; // red
colors[0].b = 1; // white colors[2].r = 1; colors[2].g = 0; colors[2].b = 0; // red
colors[1].r = 1; colors[3].r = 1; colors[3].g = 1; colors[3].b = 1; // white
colors[1].g = 0; colors[4].r = 0; colors[4].g = 0; colors[4].b = 1; // blue
colors[1].b = 0; // red colors[5].r = 0; colors[5].g = 0; colors[5].b = 1; // blue
colors[2].r = 1; colors[6].r = 1; colors[6].g = 1; colors[6].b = 1; // white
colors[2].g = 0; colors[7].r = 0; colors[7].g = 1; colors[7].b = 1; // cyan
colors[2].b = 0; // red colors[8].r = 0; colors[8].g = 1; colors[8].b = 1; // cyan
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].x = 0; vertices[0].y = 0.25; // center
vertices[0].y = 0.25; // center vertices[1].x = 0.25; vertices[1].y = 0.5; // upper right
vertices[1].x = 0.25; vertices[2].x = -0.25; vertices[2].y = 0.5; // upper left
vertices[1].y = 0.5; // upper right vertices[3].x = 0; vertices[3].y = 0.25; // center (again)
vertices[2].x = -0.25; vertices[4].x = 0.25; vertices[4].y = -0.5; // low-lower right
vertices[2].y = 0.5; // upper left vertices[5].x = 0.5; vertices[5].y = -0.25; // mid-lower right
vertices[3].x = 0; vertices[6].x = 0; vertices[6].y = 0.25; // center (again)
vertices[3].y = 0.25; // center (again) vertices[7].x = -0.5; vertices[7].y = -0.25; // low-lower left
vertices[4].x = 0.25; vertices[8].x = -0.25; vertices[8].y = -0.5; // mid-lower left
vertices[4].y = -0.5; // low-lower right // clang-format on
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
// Create and bind a vertex array object // Create and bind a vertex array object
glGenVertexArrays(1, vao); glGenVertexArrays(1, vao);
@ -277,3 +273,17 @@ int main(int argc, char **argv) {
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} // end main } // 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;
}
}
}
}