shithub: 3dee

Download patch

ref: 6024b93609eef22c9cf9d87203e0f404851c018d
parent: 3a84c951f3921fc3204538fb9ffed9242f23aae7
author: rodri <rgl@antares-labs.eu>
date: Sat Apr 5 12:12:39 EDT 2025

use new LightSource mgmt routines

--- a/med.c
+++ b/med.c
@@ -99,7 +99,7 @@
 	40*DEG, 0.01, 10, PERSPECTIVE
 };
 Point3 center = {0,0,0,1};
-LightSource light;	/* global point light */
+LightSource *light;	/* global point light */
 Compass compass;	/* 3d compass */
 Userlog *usrlog;
 
@@ -805,11 +805,9 @@
 	screenb = eallocimage(display, rectsubpt(screen->r, screen->r.min), XRGB32, 0, DNofill);
 	cam = Cam(screenb->r, rctl, camcfg.ptype, camcfg.fov, camcfg.clipn, camcfg.clipf);
 	placecamera(cam, scene, camcfg.p, camcfg.lookat, camcfg.up);
-	light.p = Pt3(0,100,100,1);
-	light.c = Pt3(1,1,1,1);
-	light.type = LightPoint;
-	light.cutoff = 10000;
-	scene->addlight(scene, &light);
+	light = newpointlight(Pt3(0,100,100,1), Pt3(1,1,1,1));
+	light->cutoff = 10000;
+	scene->addlight(scene, light);
 	tsampler = neartexsampler;
 
 	setupcompass(&compass, rectaddpt(Rect(0,0,100,100), subpt(screenb->r.max, Pt(100,100))), rctl);
--- a/shaders.inc
+++ b/shaders.inc
@@ -24,7 +24,7 @@
 	}
 
 	lightc = getscenecolor(sp->su->camera->scene, sp->v->p, sp->v->n);
-	sp->v->c = Vec3(0,0,0);
+	sp->v->c = ZP3;
 
 	for(l = sp->su->camera->scene->lights.next; l != &sp->su->camera->scene->lights; l = l->next){
 		lightdir = normvec3(subpt3(l->p, sp->v->p));
@@ -132,7 +132,7 @@
 	}
 
 	lightc = getscenecolor(sp->su->camera->scene, pos, n);
-	sp->v->c = Vec3(0,0,0);
+	sp->v->c = ZP3;
 
 	for(l = sp->su->camera->scene->lights.next; l != &sp->su->camera->scene->lights; l = l->next){
 		lightdir = normvec3(subpt3(l->p, pos));
@@ -213,7 +213,7 @@
 	}
 
 	lightc = getscenecolor(sp->su->camera->scene, pos, n);
-	sp->v->c = Vec3(0,0,0);
+	sp->v->c = ZP3;
 
 	for(l = sp->su->camera->scene->lights.next; l != &sp->su->camera->scene->lights; l = l->next){
 		lightdir = normvec3(subpt3(l->p, pos));
@@ -332,11 +332,11 @@
 		max(max(t.p0.x, t.p1.x), t.p2.x), max(max(t.p0.y, t.p1.y), t.p2.y)
 	);
 	if(!ptinrect(sp->p, bbox))
-		return Vec3(0,0,0);
+		return ZP3;
 
 	bc = barycoords(t, Pt2(sp->p.x,sp->p.y,1));
 	if(bc.x < 0 || bc.y < 0 || bc.z < 0)
-		return Vec3(0,0,0);
+		return ZP3;
 
 	/* wireframe version */
 	enum { THICKNESS = 8 };
@@ -361,7 +361,7 @@
 
 	d = min(d01, min(d12, d20));
 	if(d > THICKNESS)
-		return Vec3(0,0,0);
+		return ZP3;
 
 	return Pt3(bc.x, bc.y, bc.z, 1);
 }
@@ -385,7 +385,7 @@
 	d = vec2len(subpt2(uv, Vec2(0.5,0.5)));
 
 	if(d > r + r*0.05 || d < r - r*0.05)
-		return Vec3(0,0,0);
+		return ZP3;
 
 	return Pt3(uv.x, uv.y, 0, 1);
 }
@@ -433,7 +433,7 @@
 	p.y = max(p.y, 0);
 
 	if(vec2len(p) > 0)
-		return Vec3(0,0,0);
+		return ZP3;
 
 	return Pt3(uv.x, uv.y, smoothstep(0,1,uv.x+uv.y), 1);
 }
--- a/vis.c
+++ b/vis.c
@@ -99,7 +99,7 @@
 	80*DEG, 0.01, 1000, PERSPECTIVE
 };
 Point3 center = {0,0,0,1};
-LightSource lights[2];
+LightSource *lights[2];
 
 static int showskybox;
 static int doprof;
@@ -311,7 +311,7 @@
 		zr = v->fetchraster(v, "z-buffer");
 		nr = v->fetchraster(v, "normals");
 		c = ul2col(cr->data[p.y*Dx(fb->r) + p.x]);
-		n = nr != nil? ul2col(nr->data[p.y*Dx(fb->r) + p.x]): Vec3(0,0,0);
+		n = nr != nil? ul2col(nr->data[p.y*Dx(fb->r) + p.x]): ZP3;
 		z = *(float*)&zr->data[p.y*Dx(fb->r) + p.x];
 //		abuf = &fb->abuf;
 //		if(abuf->stk != nil){
@@ -380,15 +380,15 @@
 	lockdisplay(display);
 	switch(menuhit(2, mctl, &menu, _screen)){
 	case MOVELIGHT:
-		snprint(buf, sizeof buf, "%g %g %g", lights[0].p.x, lights[0].p.y, lights[0].p.z);
+		snprint(buf, sizeof buf, "%g %g %g", lights[0]->p.x, lights[0]->p.y, lights[0]->p.z);
 		if(enter("light pos", buf, sizeof buf, mctl, kctl, nil) <= 0)
 			break;
 		nf = tokenize(buf, f, 3);
 		if(nf != 3)
 			break;
-		lights[0].p.x = strtod(f[0], nil);
-		lights[0].p.y = strtod(f[1], nil);
-		lights[0].p.z = strtod(f[2], nil);
+		lights[0]->p.x = strtod(f[0], nil);
+		lights[0]->p.y = strtod(f[1], nil);
+		lights[0]->p.z = strtod(f[2], nil);
 		break;
 	case TSNEAREST:
 		tsampler = neartexsampler;
@@ -793,21 +793,14 @@
 		cams[i]->p.w = 1;
 	}
 	maincam = cams[3];
-	lights[0].p = Pt3(0,100,100,1);
-	lights[0].c = Pt3(1,1,1,1);
-	lights[0].type = LightPoint;
-	lights[0].cutoff = 3000;
-	lights[1].p = Pt3(0,100,-100,1);
-	lights[1].c = Pt3(1,1,1,1);
-	lights[1].type = LightPoint;
-	lights[1].cutoff = 3000;
+	lights[0] = newpointlight(Pt3(0,100,100,1), Pt3(1,1,1,1));
+	lights[0]->cutoff = 3000;
+	lights[1] = newpointlight(Pt3(0,100,-100,1), Pt3(1,1,1,1));
+	lights[1]->cutoff = 3000;
 	/* to test spotlights */
-//	lights[0].dir = Vec3(0,-1,0);
-//	lights[0].type = LightSpot;
-//	lights[0].θu = 30*DEG;
-//	lights[0].θp = 5*DEG;
-	scene->addlight(scene, &lights[0]);
-	scene->addlight(scene, &lights[1]);
+//	lights[0] = newspotlight(Pt3(0,100,100,1), Vec3(0,-1,0), Pt3(1,1,1,1), 30*DEG, 5*DEG);
+	scene->addlight(scene, lights[0]);
+	scene->addlight(scene, lights[1]);
 	tsampler = neartexsampler;
 
 	kctl = emalloc(sizeof *kctl);
--