shithub: tinyrend

Download patch

ref: dc91bf04e06898d84513edc8b2a5352355295427
parent: 04bca24d0d4f02f7e3b4442ef484df1178c7872b
author: rodri <[email protected]>
date: Sun Dec 17 06:07:10 EST 2023

added a table of shaders to choose from.

--- a/main.c
+++ b/main.c
@@ -48,7 +48,16 @@
 	Memimage *(*fshader)(FSparams*);
 };
 
+typedef struct Shader Shader;
 
+struct Shader
+{
+	char *name;
+	Point3 (*vshader)(VSparams*);
+	Memimage *(*fshader)(FSparams*);
+};
+
+
 Memimage *screenfb, *fb, *zfb, *nfb, *curfb;
 double *zbuf;
 Lock zbuflk;
@@ -583,7 +592,7 @@
 }
 
 void
-shade(Memimage *dst, Point3 (*vs)(VSparams*), Memimage *(*fs)(FSparams*))
+shade(Memimage *dst, Shader *s)
 {
 	int i, nelems, nparts, nworkers;
 	OBJObject *o;
@@ -621,8 +630,8 @@
 		params->e = params->b + nparts;
 		params->id = i;
 		params->donec = donec;
-		params->vshader = vs;
-		params->fshader = fs;
+		params->vshader = s->vshader;
+		params->fshader = s->fshader;
 		proccreate(shaderunit, params, mainstacksize);
 		fprint(2, "spawned su %d for elems [%d, %d)\n", params->id, i*nparts, i*nparts+nparts);
 	}
@@ -744,6 +753,24 @@
 	return sp->frag;
 }
 
+Shader shadertab[] = {
+	{ "triangle", vertshader, triangleshader },
+	{ "circle", vertshader, circleshader },
+	{ "box", vertshader, boxshader },
+	{ "gouraud", vertshader, gouraudshader },
+	{ "toon", vertshader, toonshader },
+};
+Shader *
+getshader(char *name)
+{
+	int i;
+
+	for(i = 0; i < nelem(shadertab); i++)
+		if(strcmp(shadertab[i].name, name) == 0)
+			return &shadertab[i];
+	return nil;
+}
+
 void
 redraw(void)
 {
@@ -758,22 +785,26 @@
 }
 
 void
-render(void)
+render(Shader *s)
 {
 	uvlong t0, t1;
 
 	t0 = nanosec();
-	shade(fb, vertshader, gouraudshader);
+	shade(fb, s);
 	t1 = nanosec();
 	fprint(2, "shader took %lludns\n", t1-t0);
 }
 
 void
-renderer(void *)
+renderer(void *arg)
 {
+	Shader *s;
+
 	threadsetname("renderer");
 
-	render();
+	s = arg;
+
+	render(s);
 	rendering = 0;
 	nbsendp(drawc, nil);
 	threadexits(nil);
@@ -863,7 +894,7 @@
 void
 usage(void)
 {
-	fprint(2, "usage: %s [-n nprocs] [-m objfile] [-t texfile] [-a yrotangle] [-z camzpos]\n", argv0);
+	fprint(2, "usage: %s [-n nprocs] [-m objfile] [-t texfile] [-a yrotangle] [-z camzpos] [-s shader]\n", argv0);
 	exits("usage");
 }
 
@@ -873,12 +904,15 @@
 	Mousectl *mc;
 	Keyboardctl *kc;
 	Rune r;
+	Shader *s;
 	char *mdlpath, *texpath;
+	char *sname;
 	double θ;
 
 	GEOMfmtinstall();
-	mdlpath = nil;
+	mdlpath = "mdl/def.obj";
 	texpath = nil;
+	sname = "gouraud";
 	θ = 0;
 	ARGBEGIN{
 	case 'n':
@@ -896,6 +930,9 @@
 	case 'z':
 		camera.z = strtod(EARGF(usage()), nil);
 		break;
+	case 's':
+		sname = EARGF(usage());
+		break;
 	default: usage();
 	}ARGEND;
 	if(argc != 0)
@@ -904,6 +941,14 @@
 	if(nprocs < 1)
 		nprocs = strtoul(getenv("NPROC"), nil, 10);
 
+	if((s = getshader(sname)) == nil)
+		sysfatal("couldn't find %s shader", sname);
+
+	if((model = objparse(mdlpath)) == nil)
+		sysfatal("objparse: %r");
+	if(texpath != nil && (modeltex = readtga(texpath)) == nil)
+		sysfatal("readtga: %r");
+
 	snprint(winspec, sizeof winspec, "-dx %d -dy %d", 800, 800);
 	if(newwindow(winspec) < 0)
 		sysfatal("newwindow: %r");
@@ -927,13 +972,6 @@
 	green = rgb(DGreen);
 	blue = rgb(DBlue);
 
-	if(mdlpath == nil)
-		mdlpath = "mdl/def.obj";
-	if((model = objparse(mdlpath)) == nil)
-		sysfatal("objparse: %r");
-	if(texpath != nil && (modeltex = readtga(texpath)) == nil)
-		sysfatal("readtga: %r");
-
 	{
 		int i, nv[OBJNVERT], nf;
 		OBJObject *o;
@@ -965,7 +1003,7 @@
 	mulm3(view, proj);
 
 	rendering = 1;
-	proccreate(renderer, nil, mainstacksize);
+	proccreate(renderer, s, mainstacksize);
 	threadcreate(scrsync, nil, mainstacksize);
 
 	for(;;){