opengl_learn

step-by-step introduction to OpenGL
git clone https://0xdd.org/code/opengl_learn.git
Log | Files | Refs | README | LICENSE

ogl_mat4f_multiply.c (2344B)


      1 /*
      2 2018 David DiPaola
      3 licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/)
      4 */
      5 
      6 #include <GL/glew.h>
      7 
      8 #include "ogl.h"
      9 
     10 void
     11 ogl_mat4f_multiply(
     12 	struct ogl_mat4f a, struct ogl_mat4f b,
     13 	struct ogl_mat4f * out_c
     14 ) {
     15 	GLfloat
     16 		a_1_1 = a.values[ 0], a_1_2 = a.values[ 1], a_1_3 = a.values[ 2], a_1_4 = a.values[ 3],
     17 		a_2_1 = a.values[ 4], a_2_2 = a.values[ 5], a_2_3 = a.values[ 6], a_2_4 = a.values[ 7],
     18 		a_3_1 = a.values[ 8], a_3_2 = a.values[ 9], a_3_3 = a.values[10], a_3_4 = a.values[11],
     19 		a_4_1 = a.values[12], a_4_2 = a.values[13], a_4_3 = a.values[14], a_4_4 = a.values[15]
     20 	;
     21 
     22 	GLfloat
     23 		b_1_1 = b.values[ 0], b_1_2 = b.values[ 1], b_1_3 = b.values[ 2], b_1_4 = b.values[ 3],
     24 		b_2_1 = b.values[ 4], b_2_2 = b.values[ 5], b_2_3 = b.values[ 6], b_2_4 = b.values[ 7],
     25 		b_3_1 = b.values[ 8], b_3_2 = b.values[ 9], b_3_3 = b.values[10], b_3_4 = b.values[11],
     26 		b_4_1 = b.values[12], b_4_2 = b.values[13], b_4_3 = b.values[14], b_4_4 = b.values[15]
     27 	;
     28 
     29 	/* from https://en.wikipedia.org/wiki/Matrix_multiplication#Definition */
     30 	GLfloat * values = (*out_c).values;
     31 	values[ 0] = (a_1_1*b_1_1) + (a_1_2*b_2_1) + (a_1_3*b_3_1) + (a_1_4*b_4_1);
     32 	values[ 1] = (a_1_1*b_1_2) + (a_1_2*b_2_2) + (a_1_3*b_3_2) + (a_1_4*b_4_2);
     33 	values[ 2] = (a_1_1*b_1_3) + (a_1_2*b_2_3) + (a_1_3*b_3_3) + (a_1_4*b_4_3);
     34 	values[ 3] = (a_1_1*b_1_4) + (a_1_2*b_2_4) + (a_1_3*b_3_4) + (a_1_4*b_4_4);
     35 	values[ 4] = (a_2_1*b_1_1) + (a_2_2*b_2_1) + (a_2_3*b_3_1) + (a_2_4*b_4_1);
     36 	values[ 5] = (a_2_1*b_1_2) + (a_2_2*b_2_2) + (a_2_3*b_3_2) + (a_2_4*b_4_2);
     37 	values[ 6] = (a_2_1*b_1_3) + (a_2_2*b_2_3) + (a_2_3*b_3_3) + (a_2_4*b_4_3);
     38 	values[ 7] = (a_2_1*b_1_4) + (a_2_2*b_2_4) + (a_2_3*b_3_4) + (a_2_4*b_4_4);
     39 	values[ 8] = (a_3_1*b_1_1) + (a_3_2*b_2_1) + (a_3_3*b_3_1) + (a_3_4*b_4_1);
     40 	values[ 9] = (a_3_1*b_1_2) + (a_3_2*b_2_2) + (a_3_3*b_3_2) + (a_3_4*b_4_2);
     41 	values[10] = (a_3_1*b_1_3) + (a_3_2*b_2_3) + (a_3_3*b_3_3) + (a_3_4*b_4_3);
     42 	values[11] = (a_3_1*b_1_4) + (a_3_2*b_2_4) + (a_3_3*b_3_4) + (a_3_4*b_4_4);
     43 	values[12] = (a_4_1*b_1_1) + (a_4_2*b_2_1) + (a_4_3*b_3_1) + (a_4_4*b_4_1);
     44 	values[13] = (a_4_1*b_1_2) + (a_4_2*b_2_2) + (a_4_3*b_3_2) + (a_4_4*b_4_2);
     45 	values[14] = (a_4_1*b_1_3) + (a_4_2*b_2_3) + (a_4_3*b_3_3) + (a_4_4*b_4_3);
     46 	values[15] = (a_4_1*b_1_4) + (a_4_2*b_2_4) + (a_4_3*b_3_4) + (a_4_4*b_4_4);
     47 }
     48