shithub: libgraphics

Download patch

ref: aad0e63683c0b66cdd8d30c0253aafc5e4da0ef0
parent: 1ec784ed5ae593f3d382c37c6f34fabe1497a861
author: rodri <rgl@antares-labs.eu>
date: Wed Aug 21 19:14:29 EDT 2024

fix the A-buffer so that it handles multiple render passes.

--- a/graphics.h
+++ b/graphics.h
@@ -144,6 +144,7 @@
 	Color specular;
 	double shininess;
 	Texture *diffusemap;
+	Texture *specularmap;
 	Texture *normalmap;
 };
 
--- a/render.c
+++ b/render.c
@@ -93,7 +93,7 @@
 	memset(&stk->items[stk->size-1], 0, sizeof(*stk->items));
 
 	for(i = 0; i < stk->size; i++)
-		if(z < stk->items[i].z)
+		if(z > stk->items[i].z)
 			break;
 
 	if(i < stk->size){
@@ -117,13 +117,14 @@
 {
 	Abuf *buf;
 	Astk *stk;
-	int i;
+	int i, j;
 
 	buf = &fb->abuf;
 	for(i = 0; i < buf->nact; i++){
 		stk = buf->act[i];
-		while(stk->size--)
-			pixel(fb, stk->p, stk->items[stk->size].c, blend);
+		j = stk->size;
+		while(j--)
+			pixel(fb, stk->p, stk->items[j].c, blend);
 		/* write to the depth buffer as well */
 		fb->zb[stk->p.x + stk->p.y*Dx(fb->r)] = stk->items[0].z;
 	}
@@ -156,7 +157,7 @@
 	Point3 bc;
 	Color c;
 	double dplen, perc;
-	float z;
+	float z, pcz;
 	int steep = 0, Δe, e, Δy;
 
 	params = task->params;
@@ -225,11 +226,11 @@
 			}
 
 			/* interpolate z⁻¹ and get actual z */
-			z = flerp(prim.v[0].p.w, prim.v[1].p.w, perc);
-			z = 1.0/(z < 1e-5? 1e-5: z);
+			pcz = flerp(prim.v[0].p.w, prim.v[1].p.w, perc);
+			pcz = 1.0/(pcz < 1e-5? 1e-5: pcz);
 
 			/* perspective-correct attribute interpolation  */
-			perc *= prim.v[0].p.w * z;
+			perc *= prim.v[0].p.w * pcz;
 			lerpvertex(&fsp.v, &prim.v[0], &prim.v[1], perc);
 
 			fsp.p = p;
@@ -277,11 +278,13 @@
 				}
 
 				/* interpolate z⁻¹ and get actual z */
-				z = fberp(prim.v[0].p.w, prim.v[1].p.w, prim.v[2].p.w, bc);
-				z = 1.0/(z < 1e-5? 1e-5: z);
+				pcz = fberp(prim.v[0].p.w, prim.v[1].p.w, prim.v[2].p.w, bc);
+				pcz = 1.0/(pcz < 1e-5? 1e-5: pcz);
 
 				/* perspective-correct attribute interpolation  */
-				bc = modulapt3(bc, Vec3(prim.v[0].p.w*z,prim.v[1].p.w*z,prim.v[2].p.w*z));
+				bc = modulapt3(bc, Vec3(prim.v[0].p.w*pcz,
+							prim.v[1].p.w*pcz,
+							prim.v[2].p.w*pcz));
 				berpvertex(&fsp.v, &prim.v[0], &prim.v[1], &prim.v[2], bc);
 
 				fsp.p = p;
@@ -643,7 +646,7 @@
 		}
 
 		/* initialize the A-buffer */
-		if(job->camera->enableAbuff){
+		if(job->camera->enableAbuff && job->fb->abuf.stk == nil){
 			job->fb->abuf.stk = emalloc(Dx(job->fb->r)*Dy(job->fb->r)*sizeof(Astk));
 			memset(job->fb->abuf.stk, 0, Dx(job->fb->r)*Dy(job->fb->r)*sizeof(Astk));
 		}