shithub: 3dee

Download patch

ref: a146875003e42330bba95e1e277ce75d685ad795
parent: c33a4a068cd1a742c4b92f7f8d724149309b822a
author: rodri <[email protected]>
date: Fri May 3 09:34:40 EDT 2024

adapt to the recent changes in libgraphics. add new test files.

--- /dev/null
+++ b/mdl/basis.mtl
@@ -1,0 +1,8 @@
+newmtl x
+Kd 1 0 0
+
+newmtl y
+Kd 0 1 0
+
+newmtl z
+Kd 0 0 1
--- /dev/null
+++ b/mdl/basis.obj
@@ -1,0 +1,11 @@
+mtllib basis.mtl
+v 0 0 0
+v 1 0 0
+v 0 1 0
+v 0 0 1
+usemtl x
+l 1 2
+usemtl y
+l 1 3
+usemtl z
+l 1 4
--- /dev/null
+++ b/mdl/basispt.obj
@@ -1,0 +1,12 @@
+mtllib basis.mtl
+v 0 0 0
+v 1 0 0
+v 0 1 0
+v 0 0 1
+p 1
+usemtl x
+p 2
+usemtl y
+p 3
+usemtl z
+p 4
--- /dev/null
+++ b/mdl/line.obj
@@ -1,0 +1,3 @@
+v 0 0 0
+v 1 0 0
+l 1 2
--- a/vis.c
+++ b/vis.c
@@ -119,7 +119,6 @@
 	double spec;
 	Point3 pos, lightdir, lookdir;
 	Material m;
-	Color a, d, s;
 	Color ambient, diffuse, specular;
 
 	sp->v->n = qrotate(sp->v->n, Vec3(0,1,0), θ+fmod(ω*sp->su->uni_time/1e9, 2*PI));
@@ -126,13 +125,7 @@
 	sp->v->p = qrotate(sp->v->p, Vec3(0,1,0), θ+fmod(ω*sp->su->uni_time/1e9, 2*PI));
 	pos = model2world(sp->su->entity, sp->v->p);
 	if(sp->v->mtl != nil){
-		a.r = sp->v->mtl->Ka.r; a.g = sp->v->mtl->Ka.g; a.b = sp->v->mtl->Ka.b; a.a = 1;
-		d.r = sp->v->mtl->Kd.r; d.g = sp->v->mtl->Kd.g; d.b = sp->v->mtl->Kd.b; d.a = 1;
-		s.r = sp->v->mtl->Ks.r; s.g = sp->v->mtl->Ks.g; s.b = sp->v->mtl->Ks.b; s.a = 1;
-		m.ambient = a;
-		m.diffuse = d;
-		m.specular = s;
-		m.shininess = sp->v->mtl->Ns;
+		m = *sp->v->mtl;
 
 		ambient = mulpt3(light.c, Ka);
 		ambient.r *= m.ambient.r;
@@ -167,9 +160,9 @@
 {
 	Color tc, c;
 
-	if(sp->v.mtl != nil && sp->v.mtl->map_Kd != nil && sp->v.uv.w != 0)
-		tc = texture(sp->v.mtl->map_Kd, sp->v.uv, tsampler);
-	else if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
+	if(sp->v.mtl != nil && sp->v.mtl->diffusemap != nil && sp->v.uv.w != 0){
+		tc = texture(sp->v.mtl->diffusemap, sp->v.uv, tsampler);
+	}else if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
 		tc = texture(sp->su->entity->mdl->tex, sp->v.uv, tsampler);
 	else
 		tc = Pt3(1,1,1,1);
@@ -194,10 +187,10 @@
 	pos = model2world(sp->su->entity, sp->v->p);
 	addvattr(sp->v, "pos", VAPoint, &pos);
 	if(sp->v->mtl != nil){
-		a.r = sp->v->mtl->Ka.r; a.g = sp->v->mtl->Ka.g; a.b = sp->v->mtl->Ka.b; a.a = 1;
-		d.r = sp->v->mtl->Kd.r; d.g = sp->v->mtl->Kd.g; d.b = sp->v->mtl->Kd.b; d.a = 1;
-		s.r = sp->v->mtl->Ks.r; s.g = sp->v->mtl->Ks.g; s.b = sp->v->mtl->Ks.b; s.a = 1;
-		ss = sp->v->mtl->Ns;
+		a = sp->v->mtl->ambient;
+		d = sp->v->mtl->diffuse;
+		s = sp->v->mtl->specular;
+		ss = sp->v->mtl->shininess;
 		addvattr(sp->v, "ambient", VAPoint, &a);
 		addvattr(sp->v, "diffuse", VAPoint, &d);
 		addvattr(sp->v, "specular", VAPoint, &s);
@@ -253,8 +246,8 @@
 	specular.b *= m.specular.b;
 	specular.a *= m.specular.a;
 
-	if(sp->v.mtl != nil && sp->v.mtl->map_Kd != nil && sp->v.uv.w != 0)
-		tc = texture(sp->v.mtl->map_Kd, sp->v.uv, tsampler);
+	if(sp->v.mtl != nil && sp->v.mtl->diffusemap != nil && sp->v.uv.w != 0)
+		tc = texture(sp->v.mtl->diffusemap, sp->v.uv, tsampler);
 	else if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
 		tc = texture(sp->su->entity->mdl->tex, sp->v.uv, tsampler);
 	else
@@ -279,12 +272,8 @@
 	lightdir = normvec3(subpt3(light.p, pos));
 	intens = fmax(0, dotvec3(sp->v->n, lightdir));
 	addvattr(sp->v, "intensity", VANumber, &intens);
-	if(sp->v->mtl != nil){
-		sp->v->c.r = sp->v->mtl->Kd.r;
-		sp->v->c.g = sp->v->mtl->Kd.g;
-		sp->v->c.b = sp->v->mtl->Kd.b;
-		sp->v->c.a = 1;
-	}
+	if(sp->v->mtl != nil)
+		sp->v->c = sp->v->mtl->diffuse;
 	return world2clip(maincam, pos);
 }
 
@@ -306,8 +295,8 @@
 {
 	Color tc, c;
 
-	if(sp->v.mtl != nil && sp->v.mtl->map_Kd != nil && sp->v.uv.w != 0)
-		tc = texture(sp->v.mtl->map_Kd, sp->v.uv, tsampler);
+	if(sp->v.mtl != nil && sp->v.mtl->diffusemap != nil && sp->v.uv.w != 0)
+		tc = texture(sp->v.mtl->diffusemap, sp->v.uv, tsampler);
 	else if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
 		tc = texture(sp->su->entity->mdl->tex, sp->v.uv, tsampler);
 	else
@@ -747,6 +736,7 @@
 	Viewport *v;
 	Renderer *rctl;
 	Channel *keyc;
+	OBJ *obj;
 	char *texpath, *norpath, *sname, *mdlpath;
 	int i, fd;
 
@@ -779,8 +769,11 @@
 		subject->p.x = argc*4;
 		scene->addent(scene, subject);
 
-		if((model->obj = objparse(mdlpath)) == nil)
+		if((obj = objparse(mdlpath)) == nil)
 			sysfatal("objparse: %r");
+		loadobjmodel(model, obj);
+		objfree(obj);
+
 		if(argc == 0 && texpath != nil){
 			fd = open(texpath, OREAD);
 			if(fd < 0)
@@ -797,7 +790,6 @@
 				sysfatal("readmemimage: %r");
 			close(fd);
 		}
-		refreshmodel(model);
 	}
 
 	if(memimageinit() != 0)