From cdb4aca6643ebde7cfb7af332cb323ba2afacf15 Mon Sep 17 00:00:00 2001 From: rodri Date: Sat, 21 Sep 2024 11:44:08 +0000 Subject: put all the shaders in a single file. --- med.c | 201 +----------------------------------------------------------------- 1 file changed, 2 insertions(+), 199 deletions(-) (limited to 'med.c') diff --git a/med.c b/med.c index f4d66fd..96602ff 100644 --- a/med.c +++ b/med.c @@ -106,6 +106,8 @@ static int doprof; static int showhud; Color (*tsampler)(Texture*,Point2); +#include "shaders.inc" + static Point3 Vecquat(Quaternion q) { @@ -250,205 +252,6 @@ setupcompass(Compass *c, Rectangle r, Renderer *rctl) placecamera(c->cam, c->scn, camcfg.p, center, Vec3(0,1,0)); } -Point3 -gouraudvshader(Shaderparams *sp) -{ - static double Ka = 0.1; /* ambient factor */ - static double Ks = 0.5; /* specular factor */ - double Kd; /* diffuse factor */ - double spec; - Point3 pos, lightdir, lookdir; - Material m; - Color ambient, diffuse, specular, lightc; - - sp->v->n = model2world(sp->su->entity, sp->v->n); - sp->v->p = model2world(sp->su->entity, sp->v->p); - pos = sp->v->p; - - if(sp->v->mtl != nil) - m = *sp->v->mtl; - else{ - memset(&m, 0, sizeof m); - m.diffuse = sp->v->c; - m.specular = Pt3(1,1,1,1); - m.shininess = 1; - } - - lightdir = normvec3(subpt3(light.p, pos)); - lightc = getlightcolor(&light, lightdir); - - ambient = mulpt3(lightc, Ka); - ambient = modulapt3(ambient, m.diffuse); - - Kd = max(0, dotvec3(sp->v->n, lightdir)); - diffuse = mulpt3(lightc, Kd); - diffuse = modulapt3(diffuse, m.diffuse); - - lookdir = normvec3(subpt3(sp->su->camera->p, pos)); - lightdir = qrotate(lightdir, sp->v->n, PI); - spec = pow(max(0, dotvec3(lookdir, lightdir)), m.shininess); - specular = mulpt3(lightc, spec*Ks); - specular = modulapt3(specular, m.specular); - - sp->v->c = addpt3(ambient, addpt3(diffuse, specular)); - sp->v->c.a = m.diffuse.a; - return world2clip(sp->su->camera, pos); -} - -Color -gouraudshader(Shaderparams *sp) -{ - Color tc; - - if(sp->su->entity->mdl->tex != nil && sp->v->uv.w != 0) - tc = sampletexture(sp->su->entity->mdl->tex, sp->v->uv, tsampler); - else if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0) - tc = sampletexture(sp->v->mtl->diffusemap, sp->v->uv, tsampler); - else - tc = Pt3(1,1,1,1); - - return modulapt3(sp->v->c, tc); -} - -Point3 -phongvshader(Shaderparams *sp) -{ - Point3 pos; - Color a, d, s; - double ss; - - sp->v->n = model2world(sp->su->entity, sp->v->n); - sp->v->p = model2world(sp->su->entity, sp->v->p); - pos = sp->v->p; - sp->setattr(sp, "pos", VAPoint, &pos); - if(sp->v->mtl != nil && sp->v->mtl->normalmap != nil && sp->v->uv.w != 0){ - sp->v->tangent = model2world(sp->su->entity, sp->v->tangent); - sp->setattr(sp, "tangent", VAPoint, &sp->v->tangent); - } - if(sp->v->mtl != nil){ - a = sp->v->mtl->ambient; - d = sp->v->mtl->diffuse; - s = sp->v->mtl->specular; - ss = sp->v->mtl->shininess; - sp->setattr(sp, "ambient", VAPoint, &a); - sp->setattr(sp, "diffuse", VAPoint, &d); - sp->setattr(sp, "specular", VAPoint, &s); - sp->setattr(sp, "shininess", VANumber, &ss); - } - return world2clip(sp->su->camera, pos); -} - -Color -phongshader(Shaderparams *sp) -{ - static double Ka = 0.1; /* ambient factor */ - static double Ks = 0.5; /* specular factor */ - double Kd; /* diffuse factor */ - double spec; - Color ambient, diffuse, specular, lightc, c; - Point3 pos, n, lightdir, lookdir; - Material m; - RFrame3 TBN; - Vertexattr *va; - - va = sp->getattr(sp, "pos"); - pos = va->p; - - va = sp->getattr(sp, "ambient"); - m.ambient = va != nil? va->p: Pt3(1,1,1,1); - va = sp->getattr(sp, "diffuse"); - m.diffuse = va != nil? va->p: sp->v->c; - va = sp->getattr(sp, "specular"); - m.specular = va != nil? va->p: Pt3(1,1,1,1); - va = sp->getattr(sp, "shininess"); - m.shininess = va != nil? va->n: 1; - - lightdir = normvec3(subpt3(light.p, pos)); - lightc = getlightcolor(&light, lightdir); - - /* normal mapping */ - va = sp->getattr(sp, "tangent"); - if(va == nil) - n = sp->v->n; - else{ - /* TODO implement this on the VS instead and apply Gram-Schmidt here */ - n = sampletexture(sp->v->mtl->normalmap, sp->v->uv, neartexsampler); - n = normvec3(subpt3(mulpt3(n, 2), Vec3(1,1,1))); - - TBN.p = Pt3(0,0,0,1); - TBN.bx = va->p; /* T */ - TBN.bz = sp->v->n; /* N */ - TBN.by = crossvec3(TBN.bz, TBN.bx); /* B */ - - n = normvec3(invrframexform3(n, TBN)); - sp->v->n = n; - } - - if(sp->su->entity->mdl->tex != nil && sp->v->uv.w != 0) - m.diffuse = sampletexture(sp->su->entity->mdl->tex, sp->v->uv, tsampler); - else if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0) - m.diffuse = sampletexture(sp->v->mtl->diffusemap, sp->v->uv, tsampler); - - ambient = mulpt3(lightc, Ka); - ambient = modulapt3(ambient, m.diffuse); - - Kd = max(0, dotvec3(n, lightdir)); - diffuse = mulpt3(lightc, Kd); - diffuse = modulapt3(diffuse, m.diffuse); - - if(sp->v->mtl != nil && sp->v->mtl->specularmap != nil && sp->v->uv.w != 0) - m.specular = sampletexture(sp->v->mtl->specularmap, sp->v->uv, tsampler); - - lookdir = normvec3(subpt3(sp->su->camera->p, pos)); - lightdir = qrotate(lightdir, n, PI); - spec = pow(max(0, dotvec3(lookdir, lightdir)), m.shininess); - specular = mulpt3(lightc, spec*Ks); - specular = modulapt3(specular, m.specular); - - c = addpt3(ambient, addpt3(diffuse, specular)); - c.a = m.diffuse.a; - return c; -} - -Point3 -identvshader(Shaderparams *sp) -{ - if(sp->v->mtl != nil) - sp->v->c = sp->v->mtl->diffuse; - return world2clip(sp->su->camera, model2world(sp->su->entity, sp->v->p)); -} - -Color -identshader(Shaderparams *sp) -{ - Color tc; - - if(sp->su->entity->mdl->tex != nil && sp->v->uv.w != 0) - tc = sampletexture(sp->su->entity->mdl->tex, sp->v->uv, tsampler); - else if(sp->v->mtl != nil && sp->v->mtl->diffusemap != nil && sp->v->uv.w != 0) - tc = sampletexture(sp->v->mtl->diffusemap, sp->v->uv, tsampler); - else - tc = Pt3(1,1,1,1); - - return modulapt3(sp->v->c, tc); -} - -Shadertab shadertab[] = { - { "ident", identvshader, identshader }, - { "gouraud", gouraudvshader, gouraudshader }, - { "phong", phongvshader, phongshader }, -}; -Shadertab * -getshader(char *name) -{ - int i; - - for(i = 0; i < nelem(shadertab); i++) - if(strcmp(shadertab[i].name, name) == 0) - return &shadertab[i]; - return nil; -} - void zoomin(void) { -- cgit v1.2.3