double rayvolume(Prim *p, double stepsize){ Ray rayo; Vector3 vo; Vector3 vd; // Rays parallel to Y axis: vd = v3_make(0,1,0); double t0, t1; Inode *intersect; double i, j; double Vol = 0.0; for (i=-30; i<=30; i+=stepsize){ for (j=-20; j<=20; j+=stepsize){ // Origin of the rays XZ parallel plane: vo = v3_make(j*1.0,-100,-i*1.0); rayo = ray_make(vo,vd); if ( prim_intersect(p,rayo) != NULL ){ t0 = prim_intersect(p,rayo)[0].t; if (prim_intersect(p,rayo)[0].enter){ t1 = prim_intersect(p,rayo)[0].next->t; Vol += abs(t1 - t0); } else { } } else { } } }; return Vol*SQR(stepsize); }
Val view_parse(int pass, Pval *pl) { Val v = {V_NULL, 0}; if (pass == T_EXEC) { View *view = initview(); Vector3 ref = pvl_get_v3(pl, "from", v3_make(0,-5,0)); Vector3 at = pvl_get_v3(pl, "at", v3_make(0,0,0)); Vector3 up = pvl_get_v3(pl, "up", v3_make(0,0,1)); double fov = pvl_get_num(pl, "fov", 90); double w = pvl_get_num(pl, "imgw", 320); double h = pvl_get_num(pl, "imgh", 240); lookat(view, ref.x, ref.y, ref.z, at.x, at.y, at.z, up.x, up.y, up.z); setviewpoint(ref.x, ref.y, ref.z); setviewnormal(at.x - ref.x ,at.y - ref.y, at.z - ref.z); makeviewV(); perspective(view, fov * DTOR, w/h, 1.0, 100000.0); viewport(view, 0.,0., w, h); v.type = V_CAMERA; v.u.v = view; } return v; }
int main(int argc, char **argv) { int i; Real x0, x1, y0, y1, z0, z1; Vector3 v1, v2, v3, v4, v5, v6, v7, v8; Hpoly *quad[2], *hplist = NULL; x0 = -5; x1 = 5; y0 = -2; y1 = 2; z0 = -5; z1 = 5; v1 = v3_make(x0, y0, z0); v2 = v3_make(x1, y0, z0); v3 = v3_make(x1, y0, z1); v4 = v3_make(x0, y0, z1); quad[0] = poly3_make(v2, v1, v3); quad[1] = poly3_make(v3, v1, v4); for (i = 0; i < 2; i++) hplist = poly_insert(hplist, quad[i]); hplist = hplist_refine(hplist, 0.0, 1); // trilist_write(plist, stdout); exit(1); }
iMesh *cylin_uv_decomp(Prim *p, Real level) { int i, j, nu = 20 * level, nv = 20 * level; Real u, v, iu = ULEN/(Real)nu, iv = VLEN/(Real)nv; Vector3 *pts = NEWARRAY((nu+3)*(nv+1), Vector3); Vector3 *pvals = NEWARRAY((nu+3)*(nv+1), Vector3); iTri *tl = NULL; iMesh *m = NULL; for (u = UMIN - iu, i = 0; i <= nu+2; u += iu, i++) { for (v = VMIN, j = 0; j <= nv; v += iv, j++) { pts[IDX2(i,j,nu+3)] = v3_make(u,v,0); pvals[IDX2(i,j,nu+3)] = prim_point(p,u,v); } } m = imesh_alloc((nu+3) * (nv+1), NULL, NULL); m->m = pts; m->p = pvals; for (i = 0; i < nu+2; i++) { for (j = 0; j < nv; j++) { tl = itri_insert(tl, itri_make(IDX2(i,j,nu+3),IDX2(i,j+1,nu+3),IDX2(i+1,j,nu+3), m)); tl = itri_insert(tl, itri_make(IDX2(i+1,j+1,nu+3),IDX2(i+1,j,nu+3),IDX2(i,j+1,nu+3), m)); } } m->t = tl; return m; }
Box3d cylin_bbox(Prim *p) { Box3d b = {{0,0,0},{0,0,0}}; Vector3 v; double x, y, z; for (x = -1; x <= 1; x +=2) { for (y = -1; y <= 1; y +=2) { for (z = 0; z <= 1; z +=1) { v = v3_m4mult(v3_make(x, y, z), p->td); if (x == -1 && y == -1 && z == 0) { b.ll = b.ur = v; } else { if (v.x < b.ll.x) b.ll.x = v.x; if (v.y < b.ll.y) b.ll.y = v.y; if (v.z < b.ll.z) b.ll.z = v.z; if (v.x > b.ur.x) b.ur.x = v.x; if (v.y > b.ur.y) b.ur.y = v.y; if (v.z > b.ur.z) b.ur.z = v.z; } } } } return b; }
Color bump_map(void *info, Vector3 t, Vector3 n, Vector3 ds, Vector3 dt) { TextureSrc *src = info; Real h = 0.0005; Real fo = texture_c1((*src->texfunc)(src->texdata, t)); Real fu = texture_c1((*src->texfunc)(src->texdata, v3_add(t,v3_make(h,0,0)))); Real fv = texture_c1((*src->texfunc)(src->texdata, v3_add(t,v3_make(0,h,0)))); Real du = fderiv(fo, fu, h); Real dv = fderiv(fo, fv, h); Vector3 u = v3_scale(du, v3_cross(n, dt)); Vector3 v = v3_scale(-dv, v3_cross(n, ds)); return v3_add(u, v); }
Vector3 sph_coord(Vector3 r) { Real len = v3_norm(r); Real theta = atan2(r.y/len, r.x/len); Real phi = asin(r.z/len); return v3_make(theta, phi, len); }
void m_revrotate(const matrix m, vector3 &v, float &phi) { float c,s; v3_make(m[2][1] - m[1][2], m[0][2] - m[2][0], m[1][0] - m[0][1], v); s = v3_len(v); if (s == 0) { v3_make(0,0,0,v); phi = 0; return; } v=vect_skalarszorzas(v,1/s); c = (m[0][0] + m[1][1] + m[2][2] - 1); if (c == 0) phi = pi/2.0f; else { phi = (float)atan(s / c); if (c < 0) phi += pi; } }
Val cylin_parse(int pass, Pval *pl) { Val v = {V_NULL, {0}}; if (pass == T_EXEC) { Vector3 bc = pvl_get_v3(pl, "center", v3_make(0,0,0)); double h = pvl_get_num(pl, "height", 1); double r = pvl_get_num(pl, "radius", 1); v.type = V_PRIM; cylin_set(v.u.v = cylin_instance(&cylin_funcs), bc, h, r); }; return v; }
Prim *cylin_instance(PrimFuncs *f) { Vector3 ll = {-1,-1,-1}, ur = {1,1,1}; Prim *p = NEWSTRUCT(Prim); Cylin *s = NEWSTRUCT(Cylin); p->f = f; p->b.ll = ll; p->b.ur = ur; p->ti = p->td = m4_ident(); s->bc = v3_make(0,0,0); s->h = 1; s->r = 1; p->d = s; return p; }
Prim *torus_instance(PrimFuncs *f) { Vector3 ll = {-3,-3,-1}, ur = {3,3,1}; Prim *p = NEWSTRUCT(Prim); Torus *t = NEWSTRUCT(Torus); p->f = f; p->b.ll = ll; p->b.ur = ur; p->ti = p->td = m4_ident(); t->c = v3_make(0,0,0); t->r = 1; t->R = 2; p->d = t; return p; }
Val torus_parse(int pass, Pval *pl) { Val v = {V_NULL, {0}}; if (pass == T_EXEC) { Vector3 c = pvl_get_v3(pl, "center", v3_make(0,0,0)); double r = pvl_get_num(pl, "radius", 1); double R = pvl_get_num(pl, "Radius", 2); v.type = V_PRIM; torus_set(v.u.v = torus_instance(&torus_funcs), c, r, R); } return v; }
Vector3 refract_dir(Vector3 d, Vector3 n, Real eta) { Real c1, c2; if ((c1 = v3_dot(d, n)) < 0) c1 = -c1; else n = v3_scale(-1.0, n); if ((c2 = 1 - SQR(eta) * (1 - SQR(c1))) < 0) return v3_make(0,0,0); else return v3_add(v3_scale(eta, d), v3_scale(eta*c1 - sqrt(c2), n)); }
Color rgb_to_hsv(Real r, Real g, Real b) { Real v, x, f; int i; x = MIN(r, MIN(g, b)); v = MAX(r, MAX(g, b)); if (v == x) return v3_make(UNDEFINED, 0, v); f = (r == x) ? g - b : ((g == x) ? b - r : r - g); i = (r == x) ? 3 : ((g == x) ? 5 : 1); return c_make(i - f /(v - x), (v - x)/v, v); }
void addvertex(object *o, float x, float y, float z) { if (o->vertexcapacity==o->vertexnum) { vertex *v=new vertex[(o->vertexnum+10)*2]; memset(v,0,sizeof(vertex)*(o->vertexnum+10)*2); memcpy(v,o->vertices,sizeof(vertex)*o->vertexnum); o->vertices=v; o->vertexcapacity=(o->vertexnum+10)*2; } o->vertices[o->vertexnum].d=v3_make(x,y,z); o->vertices[o->vertexnum].base=o->vertices[o->vertexnum].d; o->vertices[o->vertexnum].position=o->vertices[o->vertexnum].d; o->vertexnum++; }
void m_rotate(float ax,float ay,float az,float phi,matrix &m) { matrix m1; vector a; if (ax==0 && ay==0 && az==0) {m_identity(m); return;} v3_make(ax, ay, az, a); v3_normalize(a, a); m_identity(m); m_mults(m, (float)cos(phi), m); m_diadic3(a, a, m1); m_mults(m1, (float)(1-cos(phi)), m1); m_add(m, m1, m); m_cross(a, m1); m_mults(m1, (float)sin(phi), m1); m_add(m, m1, m); m[3][3] = 1.0; }
Vector3 cylin_dv(Prim *p, Real u, Real v) { return v3_make(- cos(u) * sin(v), - sin(u) * sin(v), cos(v)); }
Vector3 cylin_du(Prim *p, Real u, Real v) { return v3_make(- sin(u) * cos(v), cos(u) * cos(v), 0); }
Vector3 cylin_texc(Prim *p, Real u, Real v) { return v3_make((u - UMIN)/ULEN, (v - VMIN)/VLEN, 0); }
Ray ray_view(int u, int v) { Vector4 w = v4_m4mult(v4_make(u, v,s->view->sc.ur.z, 1), mdpy); return ray_make(v3_v4conv(v4_m4mult(v4_make(0, 0, 1, 0), mdpy)), v3_make(w.x, w.y, w.z)); }