ref: 153df8095d25c82adb8e79f2334700b7c1c09044
dir: /tostl.c/
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <memdraw.h>
#include <geometry.h>
#include "libgraphics/graphics.h"
#include "fns.h"
#include "libstl/stl.h"
static int
loadstlmodel(Stl *stl, Model *m)
{
Primitive *p;
Stltri **tri, *t;
int i;
snprint((char*)stl->hdr, sizeof(stl->hdr), "Exported with libstl from ⑨");
/* XXX we assume all prims are triangles */
stl->ntris = m->nprims;
stl->tris = emalloc(stl->ntris*sizeof(Stltri*));
/* since we don't use attributes we can allocate tris in bulk */
t = emalloc(stl->ntris*sizeof(Stltri));
memset(t, 0, stl->ntris*sizeof(Stltri));
for(tri = stl->tris; tri < stl->tris+stl->ntris; tri++)
*tri = &t[tri - stl->tris];
tri = stl->tris;
for(p = m->prims; p < m->prims+m->nprims; p++){
if(p->type != PTriangle){
stl->ntris--;
continue;
}
(*tri)->n[0] = p->v[0].n.x;
(*tri)->n[1] = p->v[0].n.y;
(*tri)->n[2] = p->v[0].n.z;
for(i = 0; i < 3; i++){
(*tri)->v[i][0] = p->v[i].p.x;
(*tri)->v[i][1] = p->v[i].p.y;
(*tri)->v[i][2] = p->v[i].p.z;
}
tri++;
}
return stl->ntris;
}
static void
usage(void)
{
fprint(2, "usage: %s [-t] [mdlfile]\n", argv0);
exits("usage");
}
void
main(int argc, char *argv[])
{
Model *m;
Stl *stl;
char *infile;
int fd, ofmt;
ofmt = STLBINARY;
infile = "/fd/0";
ARGBEGIN{
case 't': ofmt = STLTEXT; break;
default: usage();
}ARGEND;
if(argc == 1)
infile = argv[0];
else if(argc > 1)
usage();
fd = open(infile, OREAD);
if(fd < 0)
sysfatal("open: %r");
m = readmodel(fd);
if(m == nil)
sysfatal("readmodel: %r");
stl = emalloc(sizeof(Stl));
loadstlmodel(stl, m);
if(writestl(1, stl, ofmt) == 0)
sysfatal("writestl: %r");
exits(nil);
}