shithub: 3dee

Download patch

ref: a7dae9d379e0e6d7609c0c3181f9bcf777395075
parent: 928376d9c31af304430276eb88fad7fc10d0be9d
author: rodri <rgl@antares-labs.eu>
date: Tue Feb 13 11:43:20 EST 2024

set up a scene to hold the world and its entities.

--- a/main.c
+++ b/main.c
@@ -43,9 +43,10 @@
 Mousectl *mctl;
 Channel *drawc;
 int kdown;
-OBJ *model;
-Memimage *modeltex;
 Shader *shader;
+Model *model;
+Entity *subject;
+Scene *scene;
 double θ, ω = 0;
 
 Camera cams[4], *maincam;
@@ -105,6 +106,12 @@
 //	string3(maincam, pz, display->black, font, "z");
 //}
 
+static Point3
+model2world(Entity *e, Point3 p)
+{
+	return invrframexform3(p, *e);
+}
+
 Point3
 vertshader(VSparams *sp)
 {
@@ -111,7 +118,7 @@
 	sp->v->n = qrotate(sp->v->n, Vec3(0,1,0), θ+fmod(ω*sp->su->uni_time/1e9, 2*PI));
 	sp->v->intensity = fmax(0, dotvec3(sp->v->n, light));
 	sp->v->p = qrotate(sp->v->p, Vec3(0,1,0), θ+fmod(ω*sp->su->uni_time/1e9, 2*PI));
-	return world2clip(maincam, sp->v->p);
+	return world2clip(maincam, model2world(subject, sp->v->p));
 }
 
 Memimage *
@@ -335,22 +342,22 @@
 		fd = open("/dev/screen", OREAD);
 		if(fd < 0)
 			sysfatal("open: %r");
-		freememimage(modeltex);
-		if((modeltex = readmemimage(fd)) == nil)
+		freememimage(model->tex);
+		if((model->tex = readmemimage(fd)) == nil)
 			sysfatal("readmemimage: %r");
 	}
 
 	t0 = nsec();
 	for(;;){
-		shootcamera(maincam, model, modeltex, shader);
+		shootcamera(maincam, shader);
 		Δt = nsec() - t0;
 		if(Δt > HZ2MS(60)*1000000ULL){
 			nbsend(drawc, nil);
 			t0 += Δt;
 			if(inception){
-				freememimage(modeltex);
+				freememimage(model->tex);
 				seek(fd, 0, 0);
-				if((modeltex = readmemimage(fd)) == nil)
+				if((model->tex = readmemimage(fd)) == nil)
 					sysfatal("readmemimage: %r");
 			}
 		}
@@ -535,16 +542,22 @@
 	if((shader = getshader(sname)) == nil)
 		sysfatal("couldn't find %s shader", sname);
 
-	if((model = objparse(mdlpath)) == nil)
+	scene = newscene(nil);
+	model = newmodel();
+	subject = newentity(model);
+	scene->addent(scene, subject);
+
+	if((model->obj = objparse(mdlpath)) == nil)
 		sysfatal("objparse: %r");
 	if(texpath != nil){
 		fd = open(texpath, OREAD);
 		if(fd < 0)
 			sysfatal("open: %r");
-		if((modeltex = readmemimage(fd)) == nil)
+		if((model->tex = readmemimage(fd)) == nil)
 			sysfatal("readmemimage: %r");
 		close(fd);
 	}
+	refreshmodel(model);
 
 	if(initdraw(nil, nil, "3d") < 0)
 		sysfatal("initdraw: %r");
@@ -558,6 +571,7 @@
 		v = mkviewport(screenfb->r);
 		placecamera(&cams[i], camcfgs[i].p, camcfgs[i].lookat, camcfgs[i].up);
 		configcamera(&cams[i], v, camcfgs[i].fov, camcfgs[i].clipn, camcfgs[i].clipf, camcfgs[i].ptype);
+		cams[i].s = scene;
 	}
 	maincam = &cams[0];
 	light = normvec3(subpt3(light, center));