shithub: tinygl

Download patch

ref: 380de7a2b7a5c960d04422d8179bc31e2c0aebf6
parent: 48ed15451f05d717c6eb1cbdcac4be26857cb42f
author: MHS <gek@katherine>
date: Fri Apr 23 12:32:02 EDT 2021

Automatic commit.

--- /dev/null
+++ b/include-demo/chade.h
@@ -1,0 +1,138 @@
+
+#include "3dMath.h"
+
+#include "chadphys.h"
+#include "../include/GL/gl.h"
+#include "../include/zbuffer.h"
+#include "api_audio.h"
+#include "tobjparse.h"
+
+static inline GLuint loadRGBTexture(unsigned char* buf, unsigned int w, unsigned int h) {
+	GLuint t = 0;
+	glGenTextures(1, &t);
+	glBindTexture(GL_TEXTURE_2D, t);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+	glTexImage2D(GL_TEXTURE_2D, 0, 3, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, buf);
+	return t;
+}
+
+static inline GLuint createModelDisplayList(
+	vec3* points, GLuint npoints, vec3* colors, vec3* normals, vec3* texcoords) {
+	GLuint ret = 0;
+	if (!points)
+		return 0;
+	ret = glGenLists(1);
+	glNewList(ret, GL_COMPILE);
+	glBegin(GL_TRIANGLES);
+	for (GLuint i = 0; i < npoints; i++) {
+		if (colors) {
+			glColor3f(colors[i].d[0], colors[i].d[1], colors[i].d[2]);
+		}
+		if (texcoords)
+			glTexCoord2f(texcoords[i].d[0], texcoords[i].d[1]);
+		if (normals)
+			glNormal3f(normals[i].d[0], normals[i].d[1], normals[i].d[2]);
+		glVertex3f(points[i].d[0], points[i].d[1], points[i].d[2]);
+	}
+	glEnd();
+	glEndList();
+	return ret;
+}
+
+typedef struct{
+	GLuint dlframedata[256];
+	GLuint tx; /*if this is zero, texture will be unbound.*/
+} ChadAsset;
+
+typedef struct {
+	phys_body body; /*body*/
+	void* pimpl; /*pointer to implementation*/
+	GLuint asset; /*Rendering details*/
+	unsigned char frame; /*the frame of animation to be used from the asset.*/
+} ChadEntity;
+
+typedef struct{
+	phys_world world;
+	ChadEntity* ents;
+	ChadEntity* ents_phys; /*Where you place your objects.*/
+	ChadAsset* assets;
+	unsigned long n_ents; /*ents and ents_phys must contain the same number of elements.*/
+	unsigned long n_assets;
+} ChadWorld;
+
+void initChadWorld(ChadWorld* w){
+	*w = (ChadWorld){0};
+}
+
+void stepChadWorld(ChadWorld* world){
+	stepPhysWorld(&world->world, 1);
+}
+/*Called at the beginning of every frame before the split.*/
+void syncChadWorld(ChadWorld* world){
+	memcpy(world->ents, world->ents_phys, sizeof(ChadEntity) * world->n_ents);
+}
+/*invoked every single time that an object is added or removed*/
+void prepChadWorld(ChadWorld* world){
+GLuint i;
+	if(world->world.bodies) free(world->world.bodies);
+	world->world.bodies = malloc(sizeof(phys_body*) * world->n_ents);
+	/*Bodies is an array of pointers.*/
+	for(i = 0; i < world->n_ents; i++){
+		world->world.bodies[i] = &world->ents_phys[i].body;
+	}
+	syncChadWorld(world);
+}
+
+void ChadWorld_AddEntity(ChadWorld* world, ChadEntity ent){
+	world->ents_phys = realloc(world->ents_phys, sizeof(ChadEntity) * (world->n_ents++));
+	world->ents_phys[world->n_ents-1] = ent;
+	prepChadWorld(world);
+}
+
+void ChadWorld_RemoveEntity(ChadWorld* world, GLuint index){
+	ChadEntity* old_ents_phys = world->ents_phys;
+	if(world->n_ents <= index) return; /*Bad index*/
+	world->ents_phys = malloc((--world->n_ents) * sizeof(ChadEntity));
+	for(unsigned int i = 0; i < world->n_ents+1; i++) /**/
+	{
+		
+		if(i < index)
+			world->ents_phys[i] = old_ents_phys[i];
+		else if(i==index) continue; /*Skip.*/
+		else if(i > index) /*Gets moved back a position.*/
+			world->ents_phys[i-1] = old_ents_phys[i];
+			
+	}
+	free(old_ents_phys);
+	prepChadWorld(world);
+}
+
+
+void renderChadWorld(ChadWorld* world){
+	GLuint i; 
+	/*The user will already have set up the viewport, the camera, and the lights.*/
+	for(i = 0; i < world->n_ents; i++){
+		if(world->ents[i] == NULL) continue; /*Empty spot.*/
+		glPushMatrix();
+			/*build the transformation matrix*/
+			glTranslatef(world->ents[i].body.v.d[0],
+						world->ents[i].body.v.d[1],
+						world->ents[i].body.v.d[2]);
+			glMultMatrixf(world->ents[i].body.localt.d);
+			if(world->assets[world->ents[i].asset].tx){
+				glEnable(GL_TEXTURE_2D);
+				glBindTexture(GL_TEXTURE_2D, world->assets[world->ents[i].asset].tx);
+			} else {
+				glDisable(GL_TEXTURE_2D);
+			}
+			/*Render that shizzle!*/
+			glCallList(
+				world->assets[world->ents[i].asset].dlframedata[world->ents[i].frame]
+			);
+		glPopMatrix();
+	}
+}
+
--- a/include-demo/chadphys.h
+++ b/include-demo/chadphys.h
@@ -7,7 +7,6 @@
 	mat4 localt; //Local Transform.
 	vec3 v; //velocity
 	vec3 a; //Body specific acceleration, combined with gravity
-	void* d; //User defined pointer.
 	f_ mass; //0 means kinematic, or static. Defaults to zero.
 	f_ bounciness; //default 0, put portion of displacement into velocity.
 	f_ airfriction; //default 1, multiplied by velocity every time timestep.
@@ -33,7 +32,6 @@
 	body->airfriction = 1.0;
 	body->a = (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(
@@ -47,7 +45,7 @@
 	if(a->mass > 0 || b->mass > 0){ //Perform a preliminary check. Do we even have to do anything?
 		/*We must do shit*/
 	} else {return;}
-	//Optimized for branch prediction.
+	/*Optimized for branch prediction.*/
 	vec4 penvec = (vec4){
 		.d[0]=0,
 		.d[1]=0,
@@ -54,7 +52,7 @@
 		.d[2]=0,
 		.d[3]=0
 	};
-	//Check if the two bodies are colliding.
+	/*Check if the two bodies are colliding.*/
 	if(a->shape.c.d[3] > 0 && b->shape.c.d[3] > 0) //Both Spheres!
 	{
 		penvec = spherevsphere(a->shape.c, b->shape.c);
@@ -85,12 +83,11 @@
 	f_ adisplacefactor = b->mass / (a->mass + b->mass);
 	vec3 comvel;
 	if(!(a->mass > 0)) {
-		adisplacefactor = 0; bdisplacefactor = 1;comvel = (vec3){{0,0,0}};
+		adisplacefactor = 0; bdisplacefactor = 1;
 	}else if(!(b->mass > 0)) {
-		bdisplacefactor = 0; adisplacefactor = 1;comvel = (vec3){{0,0,0}};
-	}else{
-		comvel = addv3( scalev3(bdisplacefactor, a->v), scalev3(adisplacefactor, b->v));
+		bdisplacefactor = 0; adisplacefactor = 1;
 	}
+	comvel = addv3( scalev3(bdisplacefactor, a->v), scalev3(adisplacefactor, b->v));
 	if(a->mass > 0){
 		vec4 displacea = scalev4(-adisplacefactor, penvec);
 		vec3 a_relvel = subv3(a->v, comvel);
--- a/include-demo/tobjparse.h
+++ b/include-demo/tobjparse.h
@@ -30,13 +30,13 @@
 	facedef* faces;
 }objraw;
 typedef struct{
-	int npoints; //Number of points.
+	long npoints; //Number of points.
 	vec3* d; //Triangles (Same winding as in the file)
 	vec3* n; //Normals
 	vec3* t; //Texture Cordinates
 	vec3* c; //colors
 }model;
-objraw initobjraw(){
+static inline objraw initobjraw(){
 	return (objraw){
 		.npos=0,
 		.nnorm=0,
@@ -49,7 +49,7 @@
 		.faces=NULL
 	};
 }
-model initmodel(){
+static inline model initmodel(){
 	return (model){
 		.npoints=0,
 		.d=NULL,
@@ -58,7 +58,7 @@
 		.c=NULL
 	};
 }
-void freeobjraw(objraw* o){
+static inline void freeobjraw(objraw* o){
 	free(o->positions);
 	free(o->texcoords);
 	free(o->normals);
@@ -65,13 +65,13 @@
 	free(o->colors);
 	free(o->faces);
 }
-void freemodel(model* o){
+static inline void freemodel(model* o){
 	free(o->d);
 	free(o->t);
 	free(o->n);
 	free(o->c);
 }
-model tobj_tomodel(objraw* raw){
+static inline model tobj_tomodel(objraw* raw){
 	if(!raw || raw->faces == NULL)
 	{
 		puts("\nAttempted to convert empty model... Aborting...\n");
@@ -83,7 +83,7 @@
 	if(raw->normals)ret.n=malloc(sizeof(vec3) * raw->nfaces);
 	if(raw->texcoords)ret.t=malloc(sizeof(vec3) * raw->nfaces);
 	if(raw->colors)ret.c=malloc(sizeof(vec3) * raw->nfaces);
-	long long unsigned int piter = 0;
+	long piter = 0;
 	long long unsigned int niter = 0;
 	long long unsigned int titer = 0;
 	long long unsigned int citer = 0;
@@ -127,7 +127,6 @@
 			}
 		}
 	}
-	//printf("\ntobj_tomodel completed.\n");
 	if(ret.npoints != piter){
 		printf("\nBAD DATA!!! ABORTING...\n");
 		exit(1);
@@ -135,7 +134,7 @@
 	return ret;
 }
 //Only loads 
-objraw tobj_load(const char* fn){
+static inline objraw tobj_load(const char* fn){
 	FILE* f;
 	f = fopen(fn, "r");
 	objraw retval = initobjraw();