~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to demos/raytrace.cpp

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
5
 
 
6
#include <cmath>       // see http://ompf.org/ray/sphereflake/
 
7
#include <iostream>    // compile with ie g++ -O2 -ffast-math sphereflake.cc
 
8
#include <stdlib.h>
 
9
 
 
10
#include "SDL/SDL.h"
 
11
#include "emscripten.h"
 
12
 
 
13
#define GIMME_SHADOWS  // usage: ./sphereflake [lvl=6] >pix.ppm
 
14
 
 
15
SDL_Surface* screen = NULL;
 
16
 
 
17
enum { childs = 9, ss= 2, ss_sqr = ss*ss }; /* not really tweakable anymore */
 
18
static const double infinity = 1./0, epsilon = 1e-12;
 
19
 
 
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);}
 
30
};
 
31
 
 
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*/
 
34
 
 
35
struct ray_t{
 
36
        v_t o,d;
 
37
        ray_t(const v_t&v):o(v){}
 
38
         ray_t(const v_t&v,const v_t&w):o(v),d(w){}
 
39
};
 
40
struct hit_t {
 
41
        v_t n;
 
42
        double t;
 
43
        hit_t():n(v_t(0,0,0)),t(infinity){}
 
44
};
 
45
 
 
46
struct sphere_t{ 
 
47
        v_t o;
 
48
        double r;
 
49
        sphere_t(){}
 
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;
 
54
                if(disc < 0.)
 
55
                        return infinity; /*branch away from the square root*/
 
56
                const double d=sqrt(disc), t2=b+d, t1=b-d; /*cond. move*/
 
57
                if(t2 < 0.)
 
58
                        return infinity;
 
59
                else
 
60
                        return(t1 > 0.? t1 : t2);
 
61
        }
 
62
};
 
63
 
 
64
struct node_t; 
 
65
static node_t *pool=0, *end=0;
 
66
 
 
67
struct node_t { /*a bvh in array form+skip for navigation.*/
 
68
        sphere_t bound,leaf;
 
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){
 
72
                const node_t*p=pool;
 
73
                while(p < end) {
 
74
                        if(p->bound.intersect(ray)>=hit.t) /*missed bound*/
 
75
                                p+=p->diff; /*skip subtree*/
 
76
                        else{
 
77
                                const double t=p->leaf.intersect(ray);
 
78
                                if(t < hit.t) { /*if hit, update, then break for shadows*/
 
79
                                         hit.t=t;
 
80
                                        if(shadow) break;
 
81
                                        hit.n=p->leaf.get_normal(ray.o+ray.d*t);
 
82
                                }
 
83
                                ++p; /*next!*/
 
84
                        }
 
85
                }
 
86
        }
 
87
};
 
88
 
 
89
static double ray_trace(const node_t*const scene,const ray_t&ray) {
 
90
        hit_t hit;
 
91
        scene->intersect<false>(ray,hit);// trace primary
 
92
        const double diffuse = hit.t==infinity ? 0. : -hit.n.dot(light);
 
93
        #ifdef GIMME_SHADOWS
 
94
                if (diffuse <= 0.)
 
95
                        return 0.;
 
96
                const ray_t sray(ray.o+(ray.d*hit.t)+(hit.n*epsilon),-light);
 
97
                hit_t shit;
 
98
                scene->intersect<true>(sray,shit);// trace shadow
 
99
                return shit.t==infinity ? diffuse : 0.;
 
100
        #else
 
101
                return diffuse > 0. ? diffuse : 0.;
 
102
        #endif
 
103
}
 
104
 
 
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.}
 
108
};
 
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 */
 
112
        v_t rgss[ss_sqr];
 
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);
 
115
        }
 
116
        v_t scan(0,y,std::max(w,h)); /*scan line*/
 
117
  SDL_LockSurface(screen);
 
118
 
 
119
        for(int j=width;j;--j) {
 
120
                double g=0;
 
121
                for(int idx=0;idx < ss_sqr;++idx){ /*AA*/
 
122
                        ray.d=(scan+rgss[idx]).norm();
 
123
                        g+=ray_trace(pool,ray); /*trace*/
 
124
                }
 
125
 
 
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);
 
128
 
 
129
                scan.x+=1; /*next pixel*/
 
130
        }
 
131
 
 
132
  SDL_UnlockSurface(screen);
 
133
  SDL_Flip(screen); 
 
134
}
 
135
        
 
136
struct basis_t{ /* bogus and compact, exactly what we need */
 
137
        v_t up,b1,b2;
 
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*/
 
140
                        b1=n;
 
141
                        if(n.y*n.y>n.x*n.x) {
 
142
                                if(n.y*n.y>n.z*n.z)
 
143
                                        b1.y=-b1.y;
 
144
                                else b1.z=-b1.z;
 
145
                        }
 
146
                        else if(n.z*n.z > n.x*n.x)
 
147
                                b1.z=-b1.z;
 
148
                        else b1.x=-b1.x;
 
149
                }
 
150
                else
 
151
                        b1=v_t(n.z,n.x,n.y);/*leaves some cases out,dodge them*/
 
152
                
 
153
                up=n;
 
154
                b2=up.cross(b1);
 
155
                b1=up.cross(b2);
 
156
        }
 
157
};
 
158
 
 
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);
 
161
        if (lvl <= 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);
 
168
                a+=daL;
 
169
        }
 
170
        a-=daL/3.;/*tweak*/
 
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;
 
174
        }
 
175
        return n;
 
176
}
 
177
 
 
178
#define SIZE 512
 
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;
 
184
        ++count;
 
185
        pool=new node_t[count];  /* raw */
 
186
        end=pool+count;
 
187
        create(pool,lvl,count,v_t(0,0,0),v_t(+.25,+1,-.5).norm(),1.); /* cooked */
 
188
 
 
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 */
 
193
 
 
194
  SDL_Delay( 20000 );
 
195
  SDL_Quit();
 
196
 
 
197
        return 0;
 
198
}
 
199