shithub: 3dee

Download patch

ref: 4aca8f7a843e66454ba1f62e63bd91d7da6b315c
parent: 2f6efcecc3fb03a4c1a2ea6ed009bb286a2244d9
author: rodri <rgl@antares-labs.eu>
date: Wed Mar 6 17:20:53 EST 2024

use the diffuse color map texture from the OBJMaterial for shading.

--- a/main.c
+++ b/main.c
@@ -128,7 +128,9 @@
 	Color c;
 
 	va = getvattr(&sp->v, "intensity");
-	if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
+	if(sp->v.mtl != nil && sp->v.mtl->map_Kd != nil && sp->v.uv.w != 0)
+		c = texture(sp->v.mtl->map_Kd, sp->v.uv, tsampler);
+	else if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
 		c = texture(sp->su->entity->mdl->tex, sp->v.uv, tsampler);
 	else
 		c = Pt3(1,1,1,1);
@@ -186,20 +188,31 @@
 	m.shininess = va != nil? va->n: 1;
 
 	ambient = mulpt3(light.c, Ka);
-	ambient.r *= m.ambient.r; ambient.g *= m.ambient.g; ambient.b *= m.ambient.b; ambient.a *= m.ambient.a;
+	ambient.r *= m.ambient.r;
+	ambient.g *= m.ambient.g;
+	ambient.b *= m.ambient.b;
+	ambient.a *= m.ambient.a;
 
 	lightdir = normvec3(subpt3(light.p, pos));
 	Kd = fmax(0, dotvec3(sp->v.n, lightdir));
 	diffuse = mulpt3(light.c, Kd);
-	diffuse.r *= m.diffuse.r; diffuse.g *= m.diffuse.g; diffuse.b *= m.diffuse.b; diffuse.a *= m.diffuse.a;
+	diffuse.r *= m.diffuse.r;
+	diffuse.g *= m.diffuse.g;
+	diffuse.b *= m.diffuse.b;
+	diffuse.a *= m.diffuse.a;
 
 	lookdir = normvec3(subpt3(maincam->p, pos));
 	lightdir = qrotate(lightdir, sp->v.n, PI);
 	spec = pow(fmax(0, dotvec3(lookdir, lightdir)), m.shininess);
 	specular = mulpt3(light.c, spec*Ks);
-	specular.r *= m.specular.r; specular.g *= m.specular.g; specular.b *= m.specular.b; specular.a *= m.specular.a;
+	specular.r *= m.specular.r;
+	specular.g *= m.specular.g;
+	specular.b *= m.specular.b;
+	specular.a *= m.specular.a;
 
-	if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
+	if(sp->v.mtl != nil && sp->v.mtl->map_Kd != nil && sp->v.uv.w != 0)
+		tc = texture(sp->v.mtl->map_Kd, sp->v.uv, tsampler);
+	else if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
 		tc = texture(sp->su->entity->mdl->tex, sp->v.uv, tsampler);
 	else
 		tc = Pt3(1,1,1,1);
@@ -325,7 +338,9 @@
 {
 	Color c;
 
-	if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
+	if(sp->v.mtl != nil && sp->v.mtl->map_Kd != nil && sp->v.uv.w != 0)
+		c = texture(sp->v.mtl->map_Kd, sp->v.uv, tsampler);
+	else if(sp->su->entity->mdl->tex != nil && sp->v.uv.w != 0)
 		c = texture(sp->su->entity->mdl->tex, sp->v.uv, tsampler);
 	else
 		c = Pt3(1,1,1,1);