shithub: 3dee

Download patch

ref: d626030faba2ea45d1ed93cf5682e65095e5e1ec
parent: ca7f3196863e0012ec244f7e0354005275e69726
author: rodri <rgl@antares-labs.eu>
date: Thu Jun 6 13:40:01 EDT 2024

add skyboxes based on cubemaps.

diff: cannot open b/cubemap/skybox//null: file does not exist: 'b/cubemap/skybox//null' diff: cannot open b/cubemap/solar//null: file does not exist: 'b/cubemap/solar//null' diff: cannot open b/cubemap//null: file does not exist: 'b/cubemap//null'
binary files /dev/null b/cubemap/skybox/back.jpg differ
binary files /dev/null b/cubemap/skybox/bottom.jpg differ
--- /dev/null
+++ b/cubemap/skybox/conv
@@ -1,0 +1,3 @@
+#!/bin/rc
+for(f in *.jpg) <$f jpg -t9 >`{echo $f | sed 's/jpg/pic/g'}
+chmod +t *.pic
binary files /dev/null b/cubemap/skybox/front.jpg differ
binary files /dev/null b/cubemap/skybox/left.jpg differ
binary files /dev/null b/cubemap/skybox/right.jpg differ
binary files /dev/null b/cubemap/skybox/top.jpg differ
binary files /dev/null b/cubemap/solar/back.jpg differ
binary files /dev/null b/cubemap/solar/bottom.jpg differ
--- /dev/null
+++ b/cubemap/solar/conv
@@ -1,0 +1,3 @@
+#!/bin/rc
+for(f in *.jpg) <$f jpg -t9 >`{echo $f | sed 's/jpg/pic/g'}
+chmod +t *.pic
binary files /dev/null b/cubemap/solar/front.jpg differ
binary files /dev/null b/cubemap/solar/left.jpg differ
binary files /dev/null b/cubemap/solar/right.jpg differ
binary files /dev/null b/cubemap/solar/top.jpg differ
--- a/med.c
+++ b/med.c
@@ -243,7 +243,7 @@
 		diffuse.a *= m->diffuse.a;
 	}
 
-	lookdir = normvec3(subpt3(cam.p, pos));
+	lookdir = normvec3(subpt3(sp->su->camera->p, pos));
 	lightdir = qrotate(lightdir, sp->v->n, PI);
 	spec = pow(fmax(0, dotvec3(lookdir, lightdir)), m? m->shininess: 1);
 	specular = mulpt3(light.c, spec*Ks);
@@ -255,7 +255,7 @@
 	}
 
 	sp->v->c = addpt3(ambient, addpt3(diffuse, specular));
-	return world2clip(&cam, pos);
+	return world2clip(sp->su->camera, pos);
 }
  
 Color
@@ -299,7 +299,7 @@
 		addvattr(sp->v, "specular", VAPoint, &s);
 		addvattr(sp->v, "shininess", VANumber, &ss);
 	}
-	return world2clip(&cam, pos);
+	return world2clip(sp->su->camera, pos);
 }
 
 Color
@@ -340,7 +340,7 @@
 	diffuse.b *= m.diffuse.b;
 	diffuse.a *= m.diffuse.a;
 
-	lookdir = normvec3(subpt3(cam.p, pos));
+	lookdir = normvec3(subpt3(sp->su->camera->p, pos));
 	lightdir = qrotate(lightdir, sp->v.n, PI);
 	spec = pow(fmax(0, dotvec3(lookdir, lightdir)), m.shininess);
 	specular = mulpt3(light.c, spec*Ks);
@@ -372,7 +372,7 @@
 	sp->v->p = model2world(sp->su->entity, sp->v->p);
 	if(sp->v->mtl != nil)
 		sp->v->c = sp->v->mtl->diffuse;
-	return world2clip(&cam, sp->v->p);
+	return world2clip(sp->su->camera, sp->v->p);
 }
 
 Color
@@ -777,7 +777,7 @@
 	v = mkviewport(screenb->r);
 	placecamera(&cam, camcfg.p, camcfg.lookat, camcfg.up);
 	configcamera(&cam, v, camcfg.fov, camcfg.clipn, camcfg.clipf, camcfg.ptype);
-	cam.s = scene;
+	cam.scene = scene;
 	cam.rctl = rctl;
 	light.p = Pt3(0,100,100,1);
 	light.c = Pt3(1,1,1,1);
--- a/solar.c
+++ b/solar.c
@@ -109,6 +109,14 @@
  [Kzoomout]	= 'x',
  [Khud]		= 'h',
 };
+char *skyboxpaths[] = {
+	"cubemap/solar/left.pic",
+	"cubemap/solar/right.pic",
+	"cubemap/solar/bottom.pic",
+	"cubemap/solar/top.pic",
+	"cubemap/solar/front.pic",
+	"cubemap/solar/back.pic",
+};
 Planet planets[] = {
 	{ .id = 10,	.name = "Sol",		.scale = 695700 },
 	{ .id = 1,	.name = "Mercury",	.scale = 2439.4 },
@@ -149,6 +157,7 @@
 double speed = 10;
 
 static int museummode;
+static int showskybox;
 static int doprof;
 static int showhud;
 
@@ -334,7 +343,7 @@
 
 	sp->v->mtl = p->mtl;
 	sp->v->c = p->mtl->diffuse;
-	return world2clip(&camera, model2world(sp->su->entity, pos));
+	return world2clip(sp->su->camera, model2world(sp->su->entity, pos));
 }
 
 Color
@@ -730,7 +739,7 @@
 void
 usage(void)
 {
-	fprint(2, "usage: %s [-m]\n", argv0);
+	fprint(2, "usage: %s [-ms]\n", argv0);
 	exits("usage");
 }
 
@@ -750,6 +759,7 @@
 	GEOMfmtinstall();
 	ARGBEGIN{
 	case 'm': museummode++; break;
+	case 's': showskybox++; break;
 	case 'p': doprof++; break;
 	default: usage();
 	}ARGEND;
@@ -790,6 +800,8 @@
 	snprint(datestr, sizeof datestr, "%τ", tmfmt(&date, datefmt));
 	if(!museummode)
 		updateplanets();
+	if(showskybox)
+		scene->skybox = readcubemap(skyboxpaths);
 
 	if(memimageinit() != 0)
 		sysfatal("memimageinit: %r");
@@ -816,7 +828,7 @@
 	v = mkviewport(screenb->r);
 	placecamera(&camera, cameracfg.p, cameracfg.lookat, cameracfg.up);
 	configcamera(&camera, v, cameracfg.fov, cameracfg.clipn, cameracfg.clipf, cameracfg.ptype);
-	camera.s = scene;
+	camera.scene = scene;
 	camera.rctl = rctl;
 	gotoplanet(getplanet("Sol"));
 
--- a/vis.c
+++ b/vis.c
@@ -40,6 +40,14 @@
  [Kcam3]	= KF|4,
  [Khud]		= 'h',
 };
+char *skyboxpaths[] = {
+	"cubemap/skybox/left.pic",
+	"cubemap/skybox/right.pic",
+	"cubemap/skybox/bottom.pic",
+	"cubemap/skybox/top.pic",
+	"cubemap/skybox/front.pic",
+	"cubemap/skybox/back.pic",
+};
 char stats[Se][256];
 Image *screenb;
 Mousectl *mctl;
@@ -78,6 +86,7 @@
 Point3 center = {0,0,0,1};
 LightSource light;		/* global point light */
 
+static int showskybox;
 static int doprof;
 static int inception;
 static int showhud;
@@ -141,7 +150,7 @@
 		diffuse.a *= m->diffuse.a;
 	}
 
-	lookdir = normvec3(subpt3(maincam->p, pos));
+	lookdir = normvec3(subpt3(sp->su->camera->p, pos));
 	lightdir = qrotate(lightdir, sp->v->n, PI);
 	spec = pow(fmax(0, dotvec3(lookdir, lightdir)), m? m->shininess: 1);
 	specular = mulpt3(light.c, spec*Ks);
@@ -153,7 +162,7 @@
 	}
 
 	sp->v->c = addpt3(ambient, addpt3(diffuse, specular));
-	return world2clip(maincam, pos);
+	return world2clip(sp->su->camera, pos);
 }
 
 Color
@@ -201,7 +210,7 @@
 		addvattr(sp->v, "specular", VAPoint, &s);
 		addvattr(sp->v, "shininess", VANumber, &ss);
 	}
-	return world2clip(maincam, pos);
+	return world2clip(sp->su->camera, pos);
 }
 
 Color
@@ -260,7 +269,7 @@
 	diffuse.b *= m.diffuse.b;
 	diffuse.a *= m.diffuse.a;
 
-	lookdir = normvec3(subpt3(maincam->p, pos));
+	lookdir = normvec3(subpt3(sp->su->camera->p, pos));
 	lightdir = qrotate(lightdir, n, PI);
 	spec = pow(fmax(0, dotvec3(lookdir, lightdir)), m.shininess);
 	specular = mulpt3(light.c, spec*Ks);
@@ -299,7 +308,7 @@
 	addvattr(sp->v, "intensity", VANumber, &intens);
 	if(sp->v->mtl != nil)
 		sp->v->c = sp->v->mtl->diffuse;
-	return world2clip(maincam, pos);
+	return world2clip(sp->su->camera, pos);
 }
 
 Color
@@ -340,7 +349,7 @@
 {
 	sp->v->n = model2world(sp->su->entity, sp->v->n);
 	sp->v->p = model2world(sp->su->entity, sp->v->p);
-	return world2clip(maincam, sp->v->p);
+	return world2clip(sp->su->camera, sp->v->p);
 }
 
 Color
@@ -796,7 +805,7 @@
 void
 usage(void)
 {
-	fprint(2, "usage: %s [-t texture] [-s shader] model...\n", argv0);
+	fprint(2, "usage: %s [-s] [-t texture] model...\n", argv0);
 	exits("usage");
 }
 
@@ -808,15 +817,14 @@
 	Channel *keyc;
 	Entity *subject;
 	OBJ *obj;
-	char *texpath, *sname, *mdlpath;
+	char *texpath, *mdlpath;
 	int i, fd;
 
 	GEOMfmtinstall();
 	texpath = nil;
-	sname = "gouraud";
 	ARGBEGIN{
+	case 's': showskybox++; break;
 	case 't': texpath = EARGF(usage()); break;
-	case 's': sname = EARGF(usage()); break;
 	case L'ι': inception++; break;
 	case 'p': doprof++; break;
 	default: usage();
@@ -826,8 +834,8 @@
 
 	confproc();
 
-	if((shader = getshader(sname)) == nil)
-		sysfatal("couldn't find %s shader", sname);
+	if((shader = getshader("gouraud")) == nil)
+		sysfatal("couldn't find gouraud shader");
 
 	scene = newscene(nil);
 	while(argc--){
@@ -834,7 +842,7 @@
 		mdlpath = argv[argc];
 		model = newmodel();
 		subject = newentity(model);
-		subject->p.x = argc*4;
+//		subject->p.x = argc*4;
 		scene->addent(scene, subject);
 
 		if((obj = objparse(mdlpath)) == nil)
@@ -851,6 +859,8 @@
 			close(fd);
 		}
 	}
+	if(showskybox)
+		scene->skybox = readcubemap(skyboxpaths);
 
 	if(memimageinit() != 0)
 		sysfatal("memimageinit: %r");
@@ -866,7 +876,7 @@
 		v = mkviewport(screenb->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;
+		cams[i].scene = scene;
 		cams[i].rctl = rctl;
 	}
 	maincam = &cams[3];