shithub: libgraphics

Download patch

ref: 45c21bb2483adf0ad1dd2f9950681f6a07a14b24
parent: 45bdbbcb16c1c95b8c7450fd6700c0a32418c98d
author: rodri <rgl@antares-labs.eu>
date: Fri Jul 12 11:10:29 EDT 2024

small improvements.

--- a/clip.c
+++ b/clip.c
@@ -34,9 +34,9 @@
 }
 
 static void
-swappoly(Polygon *a, Polygon *b)
+swappoly(Polygon **a, Polygon **b)
 {
-	Polygon tmp;
+	Polygon *tmp;
 
 	tmp = *a;
 	*a = *b;
@@ -88,20 +88,22 @@
 	};
 	double sd0[6], sd1[6];
 	double d0, d1, perc;
-	Polygon Vin, Vout;
+	Polygon Vinp, Voutp, *Vin, *Vout;
 	Vertex *v0, *v1, v;	/* edge verts and new vertex (line-plane intersection) */
 	int i, j, np;
 
 	np = 0;
-	memset(&Vin, 0, sizeof Vin);
-	memset(&Vout, 0, sizeof Vout);
+	Vin = &Vinp;
+	Vout = &Voutp;
+	memset(Vin, 0, sizeof Vinp);
+	memset(Vout, 0, sizeof Voutp);
 	for(i = 0; i < p[0].type+1; i++)
-		addvert(&Vin, p[0].v[i]);
+		addvert(Vin, p[0].v[i]);
 
-	for(j = 0; j < 6 && Vin.n > 0; j++){
-		for(i = 0; i < Vin.n; i++){
-			v0 = &Vin.v[i];
-			v1 = &Vin.v[(i+1) % Vin.n];
+	for(j = 0; j < 6 && Vin->n > 0; j++){
+		for(i = 0; i < Vin->n; i++){
+			v0 = &Vin->v[i];
+			v1 = &Vin->v[(i+1) % Vin->n];
 
 			mulsdm(sd0, sdm, v0->p);
 			mulsdm(sd1, sdm, v1->p);
@@ -117,44 +119,44 @@
 			perc = d0/(d0 - d1);
 
 			lerpvertex(&v, v0, v1, perc);
-			addvert(&Vout, v);
+			addvert(Vout, v);
 
 			if(sd1[j] >= 0){
 allin:
-				addvert(&Vout, dupvertex(v1));
+				addvert(Vout, dupvertex(v1));
 			}
 		}
-		cleanpoly(&Vin);
+		cleanpoly(Vin);
 		if(j < 6-1)
 			swappoly(&Vin, &Vout);
 	}
 
-	if(Vout.n < 2)
-		cleanpoly(&Vout);
+	if(Vout->n < 2)
+		cleanpoly(Vout);
 	else switch(p[0].type){
 	case PLine:
-		p[0].v[0] = dupvertex(&Vout.v[0]);
-		p[0].v[1] = eqpt3(Vout.v[0].p, Vout.v[1].p)? dupvertex(&Vout.v[2]): dupvertex(&Vout.v[1]);
-		cleanpoly(&Vout);
+		p[0].v[0] = dupvertex(&Vout->v[0]);
+		p[0].v[1] = eqpt3(Vout->v[0].p, Vout->v[1].p)? dupvertex(&Vout->v[2]): dupvertex(&Vout->v[1]);
+		cleanpoly(Vout);
 		np = 1;
 		break;
 	case PTriangle:
 		/* triangulate */
-		for(i = 0; i < Vout.n-2; i++, np++){
+		for(i = 0; i < Vout->n-2; i++, np++){
 			/*
 			 * when performing fan triangulation, indices 0 and 2
 			 * are referenced on every triangle, so duplicate them
 			 * to avoid complications during rasterization.
 			 */
-			memmove(&p[np], &p[0], sizeof *p);
-			p[np].v[0] = i < Vout.n-2-1? dupvertex(&Vout.v[0]): Vout.v[0];
-			p[np].v[1] = Vout.v[i+1];
-			p[np].v[2] = i < Vout.n-2-1? dupvertex(&Vout.v[i+2]): Vout.v[i+2];
+			p[np] = p[0];
+			p[np].v[0] = i < Vout->n-2-1? dupvertex(&Vout->v[0]): Vout->v[0];
+			p[np].v[1] = Vout->v[i+1];
+			p[np].v[2] = i < Vout->n-2-1? dupvertex(&Vout->v[i+2]): Vout->v[i+2];
 		}
 		break;
 	}
-	free(Vout.v);
-	free(Vin.v);
+	free(Vout->v);
+	free(Vin->v);
 
 	return np;
 }
--- a/graphics.h
+++ b/graphics.h
@@ -147,9 +147,10 @@
 {
 	Primitive *prims;
 	ulong nprims;
-	Texture *tex;		/* texture map (TODO get rid of it, use materials) */
 	Material *materials;
 	ulong nmaterials;
+
+	Texture *tex;		/* texture map (TODO get rid of it, use materials) */
 };
 
 struct Entity
--- a/render.c
+++ b/render.c
@@ -108,7 +108,6 @@
 
 	params = task->params;
 	prim = task->p;
-	memmove(prim.v, task->p.v, sizeof prim.v);
 	fsp.su = params;
 	memset(&fsp.v, 0, sizeof fsp.v);
 
@@ -278,7 +277,7 @@
 	SUparams *params, *newparams;
 	Rastertask *task;
 	VSparams vsp;
-	Primitive *ep, *p;			/* primitives to raster */
+	Primitive *ep, *p;	/* primitives to raster */
 	Rectangle *wr, bbox;
 	Channel **taskchans;
 	ulong Δy, nproc;
@@ -324,7 +323,7 @@
 		for(ep = params->eb; ep != params->ee; ep++){
 			np = 1;	/* start with one. after clipping it might change */
 
-			memmove(p, ep, sizeof *p);
+			*p = *ep;
 			switch(ep->type){
 			case PPoint:
 				p[0].v[0].mtl = ep->mtl;
@@ -353,7 +352,7 @@
 						task = emalloc(sizeof *task);
 						task->params = newparams;
 						task->wr = wr[i];
-						memmove(&task->p, &p[0], sizeof task->p);
+						task->p = p[0];
 						task->p.v[0] = dupvertex(&p[0].v[0]);
 						sendp(taskchans[i], task);
 					}
@@ -392,7 +391,7 @@
 							task = emalloc(sizeof *task);
 							task->params = newparams;
 							task->wr = wr[i];
-							memmove(&task->p, &p[np], sizeof task->p);
+							task->p = p[np];
 							task->p.v[0] = dupvertex(&p[np].v[0]);
 							task->p.v[1] = dupvertex(&p[np].v[1]);
 							sendp(taskchans[i], task);
@@ -441,7 +440,7 @@
 							task = emalloc(sizeof *task);
 							task->params = newparams;
 							task->wr = wr[i];
-							memmove(&task->p, &p[np], sizeof task->p);
+							task->p = p[np];
 							task->p.v[0] = dupvertex(&p[np].v[0]);
 							task->p.v[1] = dupvertex(&p[np].v[1]);
 							task->p.v[2] = dupvertex(&p[np].v[2]);
--- a/scene.c
+++ b/scene.c
@@ -288,7 +288,6 @@
 							p->tangent = normvec3(p->tangent);
 						}
 						if(neednormal){
-							/* TODO build a list of per-vertex normals earlier */
 							n = normvec3(crossvec3(subpt3(p->v[1].p, p->v[0].p), subpt3(p->v[2].p, p->v[0].p)));
 							p->v[0].n = p->v[1].n = p->v[2].n = n;
 						}
@@ -336,6 +335,7 @@
 	}
 	if(m->nprims > 0)
 		free(m->prims);
+	memset(m, 0, sizeof *m);
 	free(m);
 }
 
@@ -361,6 +361,7 @@
 		return;
 	if(e->mdl != nil)
 		delmodel(e->mdl);
+	memset(e, 0, sizeof *e);
 	free(e);
 }
 
@@ -405,6 +406,7 @@
 		return;
 	clearscene(s);
 	free(s->name);
+	memset(s, 0, sizeof *s);
 	free(s);
 }