shithub: 3dee

Download patch

ref: e0ec1a4e2f1bd72e3191e4d8b1d3dc6aab643a26
parent: eae6ff156c4890c2f952fc5b13eff61698022779
author: rodri <rgl@antares-labs.eu>
date: Fri Mar 14 19:27:14 EDT 2025

vis: forget about Model.tex, use materials instead.

--- a/shaders.inc
+++ b/shaders.inc
@@ -54,9 +54,7 @@
 {
 	Color tc;
 
-	if(sp->su->entity->mdl->tex != nil && sp->v->uv.w != 0)
-		tc = sampletexture(sp->su->entity->mdl->tex, sp->v->uv, tsampler);
-	else if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0)
+	if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0)
 		tc = sampletexture(sp->v->mtl->diffusemap, sp->v->uv, tsampler);
 	else
 		tc = Pt3(1,1,1,1);
@@ -138,9 +136,7 @@
 		n = normvec3(invrframexform3(n, TBN));
 	}
 
-	if(sp->su->entity->mdl->tex != nil && sp->v->uv.w != 0)
-		m.diffuse = sampletexture(sp->su->entity->mdl->tex, sp->v->uv, tsampler);
-	else if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0)
+	if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0)
 		m.diffuse = sampletexture(sp->v->mtl->diffusemap, sp->v->uv, tsampler);
 
 	if(sp->v->mtl != nil && sp->v->mtl->specularmap != nil && sp->v->uv.w != 0)
@@ -221,9 +217,7 @@
 		n = normvec3(invrframexform3(n, TBN));
 	}
 
-	if(sp->su->entity->mdl->tex != nil && sp->v->uv.w != 0)
-		m.diffuse = sampletexture(sp->su->entity->mdl->tex, sp->v->uv, tsampler);
-	else if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0)
+	if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0)
 		m.diffuse = sampletexture(sp->v->mtl->diffusemap, sp->v->uv, tsampler);
 
 	if(sp->v->mtl != nil && sp->v->mtl->specularmap != nil && sp->v->uv.w != 0)
@@ -314,9 +308,7 @@
 {
 	Color tc;
 
-	if(sp->su->entity->mdl->tex != nil && sp->v->uv.w != 0)
-		tc = sampletexture(sp->su->entity->mdl->tex, sp->v->uv, tsampler);
-	else if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0)
+	if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0)
 		tc = sampletexture(sp->v->mtl->diffusemap, sp->v->uv, tsampler);
 	else
 		tc = Pt3(1,1,1,1);
--- a/vis.c
+++ b/vis.c
@@ -170,6 +170,8 @@
 void
 renderproc(void *)
 {
+	Material *mtl;
+	Primitive *prim;
 	uvlong t0, Δt;
 	int fd;
 	double time;
@@ -181,9 +183,15 @@
 		fd = open("/dev/screen", OREAD);
 		if(fd < 0)
 			sysfatal("open: %r");
-		model->tex = alloctexture(sRGBTexture, nil);
-		if((model->tex->image = readmemimage(fd)) == nil)
+		mtl = newmaterial("ιnception");
+		mtl->diffusemap = alloctexture(sRGBTexture, nil);
+		if((mtl->diffusemap->image = readmemimage(fd)) == nil)
 			sysfatal("readmemimage: %r");
+		model->addmaterial(model, *mtl);
+		free(mtl);
+		mtl = &model->materials[model->nmaterials-1];
+		for(prim = model->prims; prim < model->prims+model->nprims; prim++)
+			prim->mtl = mtl;
 	}
 
 	t0 = nanosec();
@@ -204,9 +212,9 @@
 			t0 += Δt;
 
 			if(inception){
-				freememimage(model->tex->image);
+				freememimage(mtl->diffusemap->image);
 				seek(fd, 0, 0);
-				if((model->tex->image = readmemimage(fd)) == nil)
+				if((mtl->diffusemap->image = readmemimage(fd)) == nil)
 					sysfatal("readmemimage: %r");
 			}
 		}
@@ -638,6 +646,8 @@
 	Viewport *v;
 	Channel *keyc;
 	Entity *subject;
+	Material *tmpmtl;
+	Primitive *prim;
 	char *texpath, *mdlpath, *s;
 	int i, fd, fbw, fbh, scale;
 	int blendtest = 0;
@@ -693,10 +703,16 @@
 			fd = open(texpath, OREAD);
 			if(fd < 0)
 				sysfatal("open: %r");
-			model->tex = alloctexture(sRGBTexture, nil);
-			if((model->tex->image = readmemimage(fd)) == nil)
+			tmpmtl = newmaterial("__tmp");
+			tmpmtl->diffusemap = alloctexture(sRGBTexture, nil);
+			if((tmpmtl->diffusemap->image = readmemimage(fd)) == nil)
 				sysfatal("readmemimage: %r");
 			close(fd);
+			model->addmaterial(model, *tmpmtl);
+			free(tmpmtl);
+			tmpmtl = &model->materials[model->nmaterials-1];
+			for(prim = model->prims; prim < model->prims+model->nprims; prim++)
+				prim->mtl = tmpmtl;
 		}
 	}
 	if(showskybox)
@@ -738,10 +754,10 @@
 	}
 	maincam = cams[3];
 	lights[0].p = Pt3(0,100,100,1);
-	lights[0].c = Pt3(0,1,0,1);
+	lights[0].c = Pt3(1,1,1,1);
 	lights[0].type = LightPoint;
 	lights[1].p = Pt3(0,100,-100,1);
-	lights[1].c = Pt3(1,0,0,1);
+	lights[1].c = Pt3(1,1,1,1);
 	lights[1].type = LightPoint;
 	/* to test spotlights */
 //	lights[0].dir = Vec3(0,-1,0);