From 45c21bb2483adf0ad1dd2f9950681f6a07a14b24 Mon Sep 17 00:00:00 2001 From: rodri Date: Fri, 12 Jul 2024 15:10:29 +0000 Subject: small improvements. --- clip.c | 52 +++++++++++++++++++++++++++------------------------- graphics.h | 3 ++- render.c | 11 +++++------ scene.c | 4 +++- 4 files changed, 37 insertions(+), 33 deletions(-) diff --git a/clip.c b/clip.c index 4c5e245..db1f58e 100644 --- a/clip.c +++ b/clip.c @@ -34,9 +34,9 @@ addvert(Polygon *p, Vertex v) } static void -swappoly(Polygon *a, Polygon *b) +swappoly(Polygon **a, Polygon **b) { - Polygon tmp; + Polygon *tmp; tmp = *a; *a = *b; @@ -88,20 +88,22 @@ clipprimitive(Primitive *p) }; double sd0[6], sd1[6]; double d0, d1, perc; - Polygon Vin, Vout; + Polygon Vinp, Voutp, *Vin, *Vout; Vertex *v0, *v1, v; /* edge verts and new vertex (line-plane intersection) */ int i, j, np; np = 0; - memset(&Vin, 0, sizeof Vin); - memset(&Vout, 0, sizeof Vout); + Vin = &Vinp; + Vout = &Voutp; + memset(Vin, 0, sizeof Vinp); + memset(Vout, 0, sizeof Voutp); for(i = 0; i < p[0].type+1; i++) - addvert(&Vin, p[0].v[i]); + addvert(Vin, p[0].v[i]); - for(j = 0; j < 6 && Vin.n > 0; j++){ - for(i = 0; i < Vin.n; i++){ - v0 = &Vin.v[i]; - v1 = &Vin.v[(i+1) % Vin.n]; + for(j = 0; j < 6 && Vin->n > 0; j++){ + for(i = 0; i < Vin->n; i++){ + v0 = &Vin->v[i]; + v1 = &Vin->v[(i+1) % Vin->n]; mulsdm(sd0, sdm, v0->p); mulsdm(sd1, sdm, v1->p); @@ -117,44 +119,44 @@ clipprimitive(Primitive *p) perc = d0/(d0 - d1); lerpvertex(&v, v0, v1, perc); - addvert(&Vout, v); + addvert(Vout, v); if(sd1[j] >= 0){ allin: - addvert(&Vout, dupvertex(v1)); + addvert(Vout, dupvertex(v1)); } } - cleanpoly(&Vin); + cleanpoly(Vin); if(j < 6-1) swappoly(&Vin, &Vout); } - if(Vout.n < 2) - cleanpoly(&Vout); + if(Vout->n < 2) + cleanpoly(Vout); else switch(p[0].type){ case PLine: - p[0].v[0] = dupvertex(&Vout.v[0]); - p[0].v[1] = eqpt3(Vout.v[0].p, Vout.v[1].p)? dupvertex(&Vout.v[2]): dupvertex(&Vout.v[1]); - cleanpoly(&Vout); + p[0].v[0] = dupvertex(&Vout->v[0]); + p[0].v[1] = eqpt3(Vout->v[0].p, Vout->v[1].p)? dupvertex(&Vout->v[2]): dupvertex(&Vout->v[1]); + cleanpoly(Vout); np = 1; break; case PTriangle: /* triangulate */ - for(i = 0; i < Vout.n-2; i++, np++){ + for(i = 0; i < Vout->n-2; i++, np++){ /* * when performing fan triangulation, indices 0 and 2 * are referenced on every triangle, so duplicate them * to avoid complications during rasterization. */ - memmove(&p[np], &p[0], sizeof *p); - p[np].v[0] = i < Vout.n-2-1? dupvertex(&Vout.v[0]): Vout.v[0]; - p[np].v[1] = Vout.v[i+1]; - p[np].v[2] = i < Vout.n-2-1? dupvertex(&Vout.v[i+2]): Vout.v[i+2]; + p[np] = p[0]; + p[np].v[0] = i < Vout->n-2-1? dupvertex(&Vout->v[0]): Vout->v[0]; + p[np].v[1] = Vout->v[i+1]; + p[np].v[2] = i < Vout->n-2-1? dupvertex(&Vout->v[i+2]): Vout->v[i+2]; } break; } - free(Vout.v); - free(Vin.v); + free(Vout->v); + free(Vin->v); return np; } diff --git a/graphics.h b/graphics.h index 691e97f..fbeea2f 100644 --- a/graphics.h +++ b/graphics.h @@ -147,9 +147,10 @@ struct Model { Primitive *prims; ulong nprims; - Texture *tex; /* texture map (TODO get rid of it, use materials) */ Material *materials; ulong nmaterials; + + Texture *tex; /* texture map (TODO get rid of it, use materials) */ }; struct Entity diff --git a/render.c b/render.c index 48cf346..278c186 100644 --- a/render.c +++ b/render.c @@ -108,7 +108,6 @@ rasterize(Rastertask *task) params = task->params; prim = task->p; - memmove(prim.v, task->p.v, sizeof prim.v); fsp.su = params; memset(&fsp.v, 0, sizeof fsp.v); @@ -278,7 +277,7 @@ tilerdurden(void *arg) SUparams *params, *newparams; Rastertask *task; VSparams vsp; - Primitive *ep, *p; /* primitives to raster */ + Primitive *ep, *p; /* primitives to raster */ Rectangle *wr, bbox; Channel **taskchans; ulong Δy, nproc; @@ -324,7 +323,7 @@ tilerdurden(void *arg) for(ep = params->eb; ep != params->ee; ep++){ np = 1; /* start with one. after clipping it might change */ - memmove(p, ep, sizeof *p); + *p = *ep; switch(ep->type){ case PPoint: p[0].v[0].mtl = ep->mtl; @@ -353,7 +352,7 @@ tilerdurden(void *arg) task = emalloc(sizeof *task); task->params = newparams; task->wr = wr[i]; - memmove(&task->p, &p[0], sizeof task->p); + task->p = p[0]; task->p.v[0] = dupvertex(&p[0].v[0]); sendp(taskchans[i], task); } @@ -392,7 +391,7 @@ tilerdurden(void *arg) task = emalloc(sizeof *task); task->params = newparams; task->wr = wr[i]; - memmove(&task->p, &p[np], sizeof task->p); + task->p = p[np]; task->p.v[0] = dupvertex(&p[np].v[0]); task->p.v[1] = dupvertex(&p[np].v[1]); sendp(taskchans[i], task); @@ -441,7 +440,7 @@ tilerdurden(void *arg) task = emalloc(sizeof *task); task->params = newparams; task->wr = wr[i]; - memmove(&task->p, &p[np], sizeof task->p); + task->p = p[np]; task->p.v[0] = dupvertex(&p[np].v[0]); task->p.v[1] = dupvertex(&p[np].v[1]); task->p.v[2] = dupvertex(&p[np].v[2]); diff --git a/scene.c b/scene.c index c11588d..ba30de9 100644 --- a/scene.c +++ b/scene.c @@ -288,7 +288,6 @@ loadobjmodel(Model *m, OBJ *obj) p->tangent = normvec3(p->tangent); } if(neednormal){ - /* TODO build a list of per-vertex normals earlier */ n = normvec3(crossvec3(subpt3(p->v[1].p, p->v[0].p), subpt3(p->v[2].p, p->v[0].p))); p->v[0].n = p->v[1].n = p->v[2].n = n; } @@ -336,6 +335,7 @@ delmodel(Model *m) } if(m->nprims > 0) free(m->prims); + memset(m, 0, sizeof *m); free(m); } @@ -361,6 +361,7 @@ delentity(Entity *e) return; if(e->mdl != nil) delmodel(e->mdl); + memset(e, 0, sizeof *e); free(e); } @@ -405,6 +406,7 @@ delscene(Scene *s) return; clearscene(s); free(s->name); + memset(s, 0, sizeof *s); free(s); } -- cgit v1.2.3