shithub: 3dee

Download patch

ref: 301fb01fb87df313c0dbd1afba983743b981d8d5
parent: 996690494d739fbea436b89fb72a9da0ce31d8b7
author: rodri <rgl@antares-labs.eu>
date: Sat Jun 14 07:57:32 EDT 2025

plot3: keep a color per every point

--- a/plot3.c
+++ b/plot3.c
@@ -11,6 +11,10 @@
 #include "fns.h"
 
 typedef struct AABB AABB;
+typedef struct PColor PColor;
+typedef struct Plotpt Plotpt;
+typedef struct Plot Plot;
+
 struct AABB
 {
 	Point3 min;
@@ -20,16 +24,6 @@
 	double r;
 };
 
-typedef struct Plot Plot;
-struct Plot
-{
-	Point3 *pts;
-	ulong npts;
-	AABB bbox;
-	Scene *scn;
-};
-
-typedef struct PColor PColor;
 struct PColor
 {
 	char *k;
@@ -37,6 +31,20 @@
 	Color c;
 };
 
+struct Plotpt
+{
+	Point3 p;
+	PColor *c;
+};
+
+struct Plot
+{
+	Plotpt *pts;
+	ulong npts;
+	AABB bbox;
+	Scene *scn;
+};
+
 Mousectl *mctl;
 Keyboardctl *kctl;
 Mouse om;
@@ -98,7 +106,7 @@
 	static int inited;
 	Point3 *lastpt;
 
-	lastpt = &theplot.pts[theplot.npts-1];
+	lastpt = &theplot.pts[theplot.npts-1].p;
 
 	if(!inited){
 		theplot.bbox.min = theplot.bbox.max = *lastpt;
@@ -116,8 +124,8 @@
 addpttotheplot(Point3 p)
 {
 	if(theplot.npts % 4 == 0)
-		theplot.pts = erealloc(theplot.pts, (theplot.npts + 4)*sizeof(Point3));
-	theplot.pts[theplot.npts++] = p;
+		theplot.pts = erealloc(theplot.pts, (theplot.npts + 4)*sizeof(Plotpt));
+	theplot.pts[theplot.npts++] = (Plotpt){p, brush};
 	updatebboxfromtheplot();
 }
 
@@ -195,7 +203,7 @@
 	Entity *ent;
 	Scene *scn;
 	Primitive prim;
-	Point3 *p;
+	Plotpt *p;
 
 	mdl = newmodel();
 	ent = newentity(nil, mdl);
@@ -205,12 +213,15 @@
 
 	memset(&prim, 0, sizeof prim);
 	prim.type = PPoint;
-	prim.v[0].c = brush->c;
 
 	for(p = theplot.pts; p < theplot.pts + theplot.npts; p++){
-		prim.v[0].p = *p;
+		prim.v[0].p = p->p;
+		prim.v[0].c = p->c->c;
 		mdl->addprim(mdl, prim);
 	}
+	free(theplot.pts);
+	theplot.pts = nil;
+	theplot.npts = 0;
 	frametheplot();
 }
 
--