shithub: tinygl

Download patch

ref: c175fd8cdf3b905bb27b2bbc7d09db825d7c1516
parent: b257bd831900953f00ac4540a4538210851f9d25
author: MHS <gek@katherine>
date: Tue Mar 16 11:18:51 EDT 2021

Automatic commit.

--- a/SDL_Examples/model.c
+++ b/SDL_Examples/model.c
@@ -621,7 +621,12 @@
 				// glDisable(GL_COLOR_MATERIAL);
 				for (unsigned int i = 0; i < count; i++) {
 					glPushMatrix();
-					glTranslatef((float)(i % 10) * 8.0, (float)(i / 10) * 8.0, -10);
+					mat4 horiz_translation = translate((vec3){{8.0 * (i%10),0.0,0.0}});
+					mat4 vert_translation = translate((vec3){{0.0,8.0 * (i/10),0.0}});
+					const mat4 ztranslation = translate((vec3){{0,0,-10}});
+					mat4 total_translation = multm4(multm4(horiz_translation, vert_translation),ztranslation);
+					glMultMatrixf(total_translation.d);
+					//glTranslatef((float)(i % 10) * 8.0, (float)(i / 10) * 8.0, -10);
 					glBegin(GL_TRIANGLES);
 					for(uint j = 0; j < ModelArray.npoints; j++)
 						glArrayElement(j);
@@ -639,7 +644,12 @@
 			// glDisable(GL_COLOR_MATERIAL);
 			for (unsigned int i = 0; i < count; i++) {
 				glPushMatrix();
-				glTranslatef((float)(i % 10) * 8.0, (float)(i / 10) * 8.0, -10);
+				mat4 horiz_translation = translate((vec3){{8.0 * (i%10),0.0,0.0}});
+				mat4 vert_translation = translate((vec3){{0.0,8.0 * (i/10),0.0}});
+				const mat4 ztranslation = translate((vec3){{0,0,-10}});
+				mat4 total_translation = multm4(multm4(horiz_translation, vert_translation),ztranslation);
+				glMultMatrixf(total_translation.d);
+				//glTranslatef((float)(i % 10) * 8.0, (float)(i / 10) * 8.0, -10);
 				glCallList(modelDisplayList);
 				// drawModel(
 				// m.d, m.npoints,
--- a/include-demo/3dMath.h
+++ b/include-demo/3dMath.h
@@ -36,37 +36,7 @@
 
 
 
-static inline vec4 getrow( mat4 a,  uint index){
-	return (vec4){
-		.d[0]=a.d[0*4+index],
-		.d[1]=a.d[1*4+index],
-		.d[2]=a.d[2*4+index],
-		.d[3]=a.d[3*4+index]
-	};
-}
-static inline mat4 swapRowColumnMajor( mat4 in){
-	mat4 result;
-	vec4 t;
-	int i = 0;
-	t = getrow(in,i);
-	memcpy(result.d+i*4, t.d, 4*4);i++;
-	t = getrow(in,i);
-	memcpy(result.d+i*4, t.d, 4*4);i++;
-	t = getrow(in,i);
-	memcpy(result.d+i*4, t.d, 4*4);i++;
-	t = getrow(in,i);
-	memcpy(result.d+i*4, t.d, 4*4);
-	return result;
-}
 
-static inline vec4 getcol( mat4 a,  uint index){
-	return (vec4){
-		.d[0]=a.d[index*4+0],
-		.d[1]=a.d[index*4+1],
-		.d[2]=a.d[index*4+2],
-		.d[3]=a.d[index*4+3]
-	};
-}
 static inline mat4 scalemat4( vec4 s){
 	mat4 ret;
 	for(int i = 1; i < 16; i++)
@@ -305,38 +275,69 @@
 static inline f_ dotv4( vec4 a,  vec4 b){
 	return a.d[0] * b.d[0] + a.d[1] * b.d[1] + a.d[2] * b.d[2] + a.d[3] * b.d[3]; 
 }
+static inline vec4 getrow( mat4 a,  uint index){
+	return (vec4){
+		.d[0]=a.d[index],
+		.d[1]=a.d[4+index],
+		.d[2]=a.d[8+index],
+		.d[3]=a.d[12+index]
+	};
+}
+static inline mat4 swapRowColumnMajor( mat4 in){
+	mat4 result;
+	vec4 t;
+	int i = 0;
+	t = getrow(in,i);
+	memcpy(result.d+i*4, t.d, 4*4);i++;
+	t = getrow(in,i);
+	memcpy(result.d+i*4, t.d, 4*4);i++;
+	t = getrow(in,i);
+	memcpy(result.d+i*4, t.d, 4*4);i++;
+	t = getrow(in,i);
+	memcpy(result.d+i*4, t.d, 4*4);
+	return result;
+}
+
+static inline vec4 getcol( mat4 a,  uint index){
+	return (vec4){
+		.d[0]=a.d[index*4],
+		.d[1]=a.d[index*4+1],
+		.d[2]=a.d[index*4+2],
+		.d[3]=a.d[index*4+3]
+	};
+}
 static inline mat4 multm4( mat4 a,  mat4 b){
 	mat4 ret;
+#pragma omp simd
 	for(int i = 0; i < 4; i++)
 	for(int j = 0; j < 4; j++)
-		ret.d[i*4 + j] = dotv4(
-			getrow(a, j),
-			getcol(b, i)
+		ret.d[i*4 + j] = dotv4( //j is the ROW of the target, i is the COLUMN.
+			getrow(a, j), //we retrieve the same ROW as our ROW INDEX.
+			getcol(b, i) //we retrieve the same COLUMN as our COLUMN INDEX.
 		);
 	return ret;
 }
 static inline vec4 mat4xvec4( mat4 t,  vec4 v){
-	uint i = 0;
 	vec4 vr;
-	vr.d[0] = 	t.d[0*4+i] * v.d[0] + 
-				t.d[1*4+i] * v.d[1] +
-				t.d[2*4+i] * v.d[2] +
-				t.d[3*4+i] * v.d[3];
-	i++;
-	vr.d[1] = 	t.d[0*4+i] * v.d[0] +
-				t.d[1*4+i] * v.d[1] + 
-				t.d[2*4+i] * v.d[2] + 
-				t.d[3*4+i] * v.d[3];
-	i++;
-	vr.d[2] = 	t.d[0*4+i] * v.d[0] + 
-				t.d[1*4+i] * v.d[1] + 
-				t.d[2*4+i] * v.d[2] + 
-				t.d[3*4+i] * v.d[3];
-	i++;
-	vr.d[3] = 	t.d[0*4+i] * v.d[0] + 
-				t.d[1*4+i] * v.d[1] + 
-				t.d[2*4+i] * v.d[2] + 
-				t.d[3*4+i] * v.d[3];
+	//Getting a ROW of the matrix and dotting it with the COLUMN VECTOR to get
+	// ONE ROW of the output COLUMN VECTOR- one float.
+	vr.d[0] = 	t.d[0*4] * v.d[0] + 
+				t.d[1*4] * v.d[1] +
+				t.d[2*4] * v.d[2] +
+				t.d[3*4] * v.d[3];
+	//The next one.
+	vr.d[1] = 	t.d[0*4+1] * v.d[0] +
+				t.d[1*4+1] * v.d[1] + 
+				t.d[2*4+1] * v.d[2] + 
+				t.d[3*4+1] * v.d[3];
+	vr.d[2] = 	t.d[0*4+2] * v.d[0] + 
+				t.d[1*4+2] * v.d[1] + 
+				t.d[2*4+2] * v.d[2] + 
+				t.d[3*4+2] * v.d[3];
+	vr.d[3] = 	t.d[0*4+3] * v.d[0] + 
+				t.d[1*4+3] * v.d[1] + 
+				t.d[2*4+3] * v.d[2] + 
+				t.d[3*4+3] * v.d[3];
 	return vr;
 }
 static inline vec3 crossv3( vec3 a,  vec3 b){
--- a/include-demo/chadphys.h
+++ b/include-demo/chadphys.h
@@ -1,9 +1,6 @@
 #ifndef CHAD_PHYS_H
 #define CHAD_PHYS_H
 
-#ifdef CHAD_PHYS_IMPL
-#define CHAD_MATH_IMPL
-#endif
 #include "3dMath.h"
 typedef struct {
 	aabb shape; //c.d[3] is sphere radius. 
@@ -12,7 +9,7 @@
 	f_ bounciness; //default 0, put portion of displacement into velocity.
 	f_ airfriction; //default 1, multiplied by velocity every time timestep.
 	f_ friction; //default 0.1
-	vec3 r; //Rotation, Used for rendering only
+	mat4 localt; //Local Transform.
 	vec3 v; //velocity
 	vec3 a; //Body specific acceleration, combined with gravity
 	void* d; //User defined pointer.
@@ -36,8 +33,14 @@
 	body->friction = 0.1;
 	body->airfriction = 1.0;
 	body->a = (vec3){.d[0] = 0,.d[1] = 0,.d[2] = 0};
-	body->r = (vec3){.d[0] = 0,.d[1] = 0,.d[2] = 0};
+	body->localt = identitymat4();
 	body->d = NULL;
+}
+static inline mat4 getPhysBodyRenderTransform(phys_body* body){
+	return multm4(
+		translate(downv4(body->shape.c)),
+		body->localt
+	);
 }
 
 //Check for and, if necessary, resolve colliding bodies.