1
// sphere flake bvh raytracer (c) 2005, thierry berger-perrin <tbptbp@gmail.com>
2
// this code is released under the GNU Public License.
3
// Emscripten changes: stdlib.h, some printf stuff, SIZE, SDL
4
// XXX - new in this file
6
#include <cmath> // see http://ompf.org/ray/sphereflake/
7
#include <iostream> // compile with ie g++ -O2 -ffast-math sphereflake.cc
11
#include "emscripten.h"
13
#define GIMME_SHADOWS // usage: ./sphereflake [lvl=6] >pix.ppm
15
SDL_Surface* screen = NULL;
17
enum { childs = 9, ss= 2, ss_sqr = ss*ss }; /* not really tweakable anymore */
18
static const double infinity = 1./0, epsilon = 1e-12;
20
struct v_t{ double x,y,z;v_t(){}
21
v_t(const double a,const double b,const double c):x(a),y(b),z(c){}
22
v_t operator+(const v_t&v)const{return v_t(x+v.x,y+v.y,z+v.z);}
23
v_t operator-(const v_t&v)const{return v_t(x-v.x,y-v.y,z-v.z);}
24
v_t operator-()const{return v_t(-x,-y,-z);}
25
v_t operator*(const double d)const{return v_t(x*d,y*d,z*d);}
26
v_t cross(const v_t&v)const{return v_t(y*v.z-z*v.y,z*v.x-x*v.z,x*v.y-y*v.x);}
27
v_t norm()const{return*this*(1./sqrt(magsqr()));}
28
double dot(const v_t&v)const{return x*v.x+y*v.y+z*v.z;}
29
double magsqr()const{return dot(*this);}
32
//static const v_t light(v_t(0.5,-.95,1.775).norm()); /*pick one*/
33
static const v_t light(v_t(-0.5,-.65,.9).norm()); /*fiat lux*/
37
ray_t(const v_t&v):o(v){}
38
ray_t(const v_t&v,const v_t&w):o(v),d(w){}
43
hit_t():n(v_t(0,0,0)),t(infinity){}
50
sphere_t(const v_t&v,double d):o(v),r(d){}
51
v_t get_normal(const v_t&v)const{return(v-o)*(1./r);}
52
double intersect(const ray_t&ray)const{
53
const v_t v(o-ray.o); const double b=ray.d.dot(v),disc=b*b-v.magsqr()+r*r;
55
return infinity; /*branch away from the square root*/
56
const double d=sqrt(disc), t2=b+d, t1=b-d; /*cond. move*/
60
return(t1 > 0.? t1 : t2);
65
static node_t *pool=0, *end=0;
67
struct node_t { /*a bvh in array form+skip for navigation.*/
69
long diff;/*far from optimal*/
70
node_t(){} node_t(const sphere_t&b,const sphere_t&l,const long jump) :bound(b),leaf(l),diff(jump){}
71
template<bool shadow> static void intersect(const ray_t &ray,hit_t &hit){
74
if(p->bound.intersect(ray)>=hit.t) /*missed bound*/
75
p+=p->diff; /*skip subtree*/
77
const double t=p->leaf.intersect(ray);
78
if(t < hit.t) { /*if hit, update, then break for shadows*/
81
hit.n=p->leaf.get_normal(ray.o+ray.d*t);
89
static double ray_trace(const node_t*const scene,const ray_t&ray) {
91
scene->intersect<false>(ray,hit);// trace primary
92
const double diffuse = hit.t==infinity ? 0. : -hit.n.dot(light);
96
const ray_t sray(ray.o+(ray.d*hit.t)+(hit.n*epsilon),-light);
98
scene->intersect<true>(sray,shit);// trace shadow
99
return shit.t==infinity ? diffuse : 0.;
101
return diffuse > 0. ? diffuse : 0.;
105
static const double grid[ss_sqr][2]={ /*our rotated grid*/
106
{-3/3.,-1/3.},{+1/3.,-3/3.},
107
{-1/3.,+3/3.},{+3/3.,+1/3.}
109
static void trace_line(const int width,const int height, const int y) {
110
const double w=width,h=height,rcp=1/double(ss),scale=256./double(ss_sqr);
111
ray_t ray(v_t(0,0,-4.5)); /* eye, looking into Z */
113
for(int i=0;i<ss_sqr;++i) /*precomp.*/ {
114
rgss[i]=v_t(grid[i][0]*rcp-w/2.,grid[i][1]*rcp-h/2.,0);
116
v_t scan(0,y,std::max(w,h)); /*scan line*/
117
SDL_LockSurface(screen);
119
for(int j=width;j;--j) {
121
for(int idx=0;idx < ss_sqr;++idx){ /*AA*/
122
ray.d=(scan+rgss[idx]).norm();
123
g+=ray_trace(pool,ray); /*trace*/
126
for (int k = 0; k < 3; k++)
127
*((char*)screen->pixels + int((width - scan.y)*width*4 + scan.x*4) + k) = int(scale*g);
129
scan.x+=1; /*next pixel*/
132
SDL_UnlockSurface(screen);
136
struct basis_t{ /* bogus and compact, exactly what we need */
138
basis_t(const v_t&v){ const v_t n(v.norm());
139
if ((n.x*n.x !=1.)&(n.y*n.y !=1.)&(n.z*n.z !=1.)) {/*cough*/
141
if(n.y*n.y>n.x*n.x) {
146
else if(n.z*n.z > n.x*n.x)
151
b1=v_t(n.z,n.x,n.y);/*leaves some cases out,dodge them*/
159
static node_t *create(node_t*n,const int lvl,int dist,v_t c,v_t d,double r) {
160
n = 1 + new (n) node_t(sphere_t(c,2.*r),sphere_t(c,r), lvl > 1 ? dist : 1);
162
return n; /*if not at the bottom, recurse a bit more*/
163
dist=std::max((dist-childs)/childs,1); const basis_t b(d);
164
const double nr=r*1/3.,daL=2.*M_PI/6.,daU=2.*M_PI/3.; double a=0;
165
for(int i=0;i<6;++i){ /*lower ring*/
166
const v_t ndir((d*-.2+b.b1*sin(a)+b.b2*cos(a)).norm()); /*transcendentals?!*/
167
n=create(n,lvl-1,dist,c+ndir*(r+nr),ndir,nr);
171
for(int i=0;i<3;++i){ /*upper ring*/
172
const v_t ndir((d*+.6+b.b1*sin(a)+b.b2*cos(a)).norm());
173
n=create(n,lvl-1,dist,c+ndir*(r+nr),ndir,nr); a+=daU;
179
int main(int argc,char*argv[]){
180
enum{ w = SIZE, h = w }; /* resolution */
181
const int lvl=(argc==2?std::max(atoi(argv[1]),2):6);
182
int count=childs, dec=lvl;
183
while(--dec > 1) count=(count*childs)+childs;
185
pool=new node_t[count]; /* raw */
187
create(pool,lvl,count,v_t(0,0,0),v_t(+.25,+1,-.5).norm(),1.); /* cooked */
189
SDL_Init(SDL_INIT_VIDEO);
190
screen = SDL_SetVideoMode(SIZE, SIZE, 32, SDL_SWSURFACE);
191
for (int y = h-1; y >= 0; y--)
192
trace_line(w, h, y); /* served */