This is shape.c in view mode; [Download] [Up]
#include <math.h> #include "ray.h" void initsphere(); struct intersect sphere(); initshape() { nshapetab=1; shapetab=nalloc(struct shapetab,nshapetab); shapetab[0].shapeinitfunc=initsphere; shapetab[0].shapefunc=sphere; shapetab[0].nparams=4; } void initsphere(o) struct object *o; { o->center.x=o->spparams[0]; o->center.y=o->spparams[1]; o->center.z=o->spparams[2]; o->radius=o->spparams[3]; } struct intersect sphere(r,obj) struct ray r; register int obj; {struct vector v,n; register double *p,b,c,d,t,t0,t1; struct intersect i; i.obj=0; p=objects[obj].spparams; v.x=r.a.x-p[0]; v.y=r.a.y-p[1]; v.z=r.a.z-p[2]; b=r.l.x*v.x+r.l.y*v.y+r.l.z*v.z; c=v.x*v.x+v.y*v.y+v.z*v.z-p[3]*p[3]; d=b*b-c; if(d<0) return i; d=sqrt(d); if(b>=0) { t0= -b-d; t1=c/(-b-d); } else { t0=c/(-b+d); t1= -b+d; } if(t0>MINT) t=t0; else if(t1>MINT) t=t1; else return i; i.obj=obj; i.t=t; n.x=r.a.x+r.l.x*t-p[0]; n.y=r.a.y+r.l.y*t-p[1]; n.z=r.a.z+r.l.z*t-p[2]; t=sqrt(n.x*n.x+n.y*n.y+n.z*n.z); i.n.x=n.x/t; i.n.y=n.y/t; i.n.z=n.z/t; return i; }
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.