summaryrefslogtreecommitdiff
path: root/scene.c
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2024-06-04 18:11:11 +0000
committerrodri <rgl@antares-labs.eu>2024-06-04 18:11:11 +0000
commitdc597a2c65278119b7d11f83218b860c0c5da051 (patch)
treea6baf268c4dc7f8ed15465e66268986e03415db7 /scene.c
parent5fa75a6d4b03c676112ca04cf5ff3f3ccd2f0fee (diff)
downloadlibgraphics-dc597a2c65278119b7d11f83218b860c0c5da051.tar.gz
libgraphics-dc597a2c65278119b7d11f83218b860c0c5da051.tar.bz2
libgraphics-dc597a2c65278119b7d11f83218b860c0c5da051.zip
add a tangent parameter for normal mapping, and a world2model xform.
Diffstat (limited to 'scene.c')
-rw-r--r--scene.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/scene.c b/scene.c
index c9c8d5c..7cec60d 100644
--- a/scene.c
+++ b/scene.c
@@ -277,6 +277,23 @@ loadobjmodel(Model *m, OBJ *obj)
p->v[idx].uv = Pt2(v->u, v->v, 1);
}
}
+ if(p->v[0].uv.w != 0){
+ Point3 e0, e1;
+ Point2 Δuv0, Δuv1;
+ double det;
+
+ e0 = subpt3(p->v[1].p, p->v[0].p);
+ e1 = subpt3(p->v[2].p, p->v[0].p);
+ Δuv0 = subpt2(p->v[1].uv, p->v[0].uv);
+ Δuv1 = subpt2(p->v[2].uv, p->v[0].uv);
+
+ det = Δuv0.x * Δuv1.y - Δuv1.x * Δuv0.y;
+ det = det == 0? 0: 1.0/det;
+ p->tangent.x = det*(Δuv1.y * e0.x - Δuv0.y * e1.x);
+ p->tangent.y = det*(Δuv1.y * e0.y - Δuv0.y * e1.y);
+ p->tangent.z = det*(Δuv1.y * e0.z - Δuv0.y * e1.z);
+ 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)));
@@ -316,11 +333,12 @@ delmodel(Model *m)
return;
if(m->tex != nil)
freememimage(m->tex);
- if(m->nor != nil)
- freememimage(m->nor);
if(m->nmaterials > 0){
- while(m->nmaterials--)
+ while(m->nmaterials--){
+ freememimage(m->materials[m->nmaterials].diffusemap);
+ freememimage(m->materials[m->nmaterials].normalmap);
free(m->materials[m->nmaterials].name);
+ }
free(m->materials);
}
if(m->nprims > 0)