ref: b7c3f36539569f5861b259b136ba2f9f14ae9e78
parent: 27724b010694549f17cad50158cf73ca035e9cad
author: rodri <rgl@antares-labs.eu>
date: Fri Jul 5 14:21:18 EDT 2024
adapt to the new texture interface and fix the identvshader. the identvshader was used for the toon shader as well, and that forced it to do a light computation when its fragment shader won't ever use it. split it in two vshaders, each for their own purpose, so we don't get slow identity rendering.
--- a/med.c
+++ b/med.c
@@ -92,7 +92,7 @@
static int doprof;
static int showhud;
-Color (*tsampler)(Memimage*,Point2);
+Color (*tsampler)(Texture*,Point2);
static int
min(int a, int b)
@@ -358,7 +358,6 @@
else{
/* TODO implement this on the VS instead and apply Gram-Schmidt here */
n = texture(sp->v.mtl->normalmap, sp->v.uv, neartexsampler);
- n = linear2srgb(n); /* TODO not all textures require color space conversion */
n = normvec3(subpt3(mulpt3(n, 2), Vec3(1,1,1)));
TBN.p = Pt3(0,0,0,1);
@@ -396,11 +395,9 @@
Point3
identvshader(VSparams *sp)
{
- sp->v->n = model2world(sp->su->entity, sp->v->n);
- sp->v->p = model2world(sp->su->entity, sp->v->p);
if(sp->v->mtl != nil)
sp->v->c = sp->v->mtl->diffuse;
- return world2clip(sp->su->camera, sp->v->p);
+ return world2clip(sp->su->camera, model2world(sp->su->entity, sp->v->p));
}
Color
--- a/vis.c
+++ b/vis.c
@@ -91,7 +91,7 @@
static int inception;
static int showhud;
static int shownormals;
-Color (*tsampler)(Memimage*,Point2);
+Color (*tsampler)(Texture*,Point2);
static int
min(int a, int b)
@@ -240,7 +240,6 @@
else{
/* TODO implement this on the VS instead and apply Gram-Schmidt here */
n = texture(sp->v.mtl->normalmap, sp->v.uv, neartexsampler);
- n = linear2srgb(n); /* TODO not all textures require color space conversion */
n = normvec3(subpt3(mulpt3(n, 2), Vec3(1,1,1)));
TBN.p = Pt3(0,0,0,1);
@@ -276,14 +275,13 @@
}
Point3
-identvshader(VSparams *sp)
+toonvshader(VSparams *sp)
{
Point3 pos, lightdir;
double intens;
sp->v->n = model2world(sp->su->entity, sp->v->n);
- sp->v->p = model2world(sp->su->entity, sp->v->p);
- pos = sp->v->p;
+ pos = model2world(sp->su->entity, sp->v->p);
lightdir = normvec3(subpt3(light.p, pos));
intens = fmax(0, dotvec3(sp->v->n, lightdir));
addvattr(sp->v, "intensity", VANumber, &intens);
@@ -305,6 +303,14 @@
return Pt3(intens, 0.6*intens, 0, 1);
}
+Point3
+identvshader(VSparams *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(FSparams *sp)
{
@@ -424,7 +430,7 @@
{ "circle", ivshader, circleshader },
{ "box", ivshader, boxshader },
{ "sf", ivshader, sfshader },
- { "toon", identvshader, toonshader },
+ { "toon", toonvshader, toonshader },
{ "ident", identvshader, identshader },
{ "gouraud", gouraudvshader, gouraudshader },
{ "phong", phongvshader, phongshader },
@@ -506,8 +512,8 @@
fd = open("/dev/screen", OREAD);
if(fd < 0)
sysfatal("open: %r");
- freememimage(model->tex);
- if((model->tex = readmemimage(fd)) == nil)
+ model->tex = alloctexture(sRGBTexture, nil);
+ if((model->tex->image = readmemimage(fd)) == nil)
sysfatal("readmemimage: %r");
}
@@ -525,9 +531,9 @@
nbsend(drawc, nil);
t0 += Δt;
if(inception){
- freememimage(model->tex);
+ freememimage(model->tex->image);
seek(fd, 0, 0);
- if((model->tex = readmemimage(fd)) == nil)
+ if((model->tex->image = readmemimage(fd)) == nil)
sysfatal("readmemimage: %r");
}
}
@@ -868,7 +874,8 @@
fd = open(texpath, OREAD);
if(fd < 0)
sysfatal("open: %r");
- if((model->tex = readmemimage(fd)) == nil)
+ model->tex = alloctexture(sRGBTexture, nil);
+ if((model->tex->image = readmemimage(fd)) == nil)
sysfatal("readmemimage: %r");
close(fd);
}