rblock in set vars get tainted on rblock enter and wiped at rblock exit, works nicely
[IRC.git] / Robust / src / Tests / disjoint / raytracer_new / RayTracer.java
1
2
3 /**************************************************************************
4  *                                                                         *
5  *             Java Grande Forum Benchmark Suite - Version 2.0             *
6  *                                                                         *
7  *                            produced by                                  *
8  *                                                                         *
9  *                  Java Grande Benchmarking Project                       *
10  *                                                                         *
11  *                                at                                       *
12  *                                                                         *
13  *                Edinburgh Parallel Computing Centre                      *
14  *                                                                         *
15  *                email: epcc-javagrande@epcc.ed.ac.uk                     *
16  *                                                                         *
17  *                 Original version of this code by                        *
18  *            Florian Doyon (Florian.Doyon@sophia.inria.fr)                *
19  *              and  Wilfried Klauser (wklauser@acm.org)                   *
20  *                                                                         *
21  *      This version copyright (c) The University of Edinburgh, 1999.      *
22  *                         All rights reserved.                            *
23  *                                                                         *
24  **************************************************************************/
25
26
27 public class RayTracer {
28
29         Scene scene;
30         /**
31          * Lights for the rendering scene
32          */
33         Light lights[];
34
35         /**
36          * Objects (spheres) for the rendering scene
37          */
38         Primitive prim[];
39
40         /**
41          * The view for the rendering scene
42          */
43         View view;
44
45         /**
46          * Temporary ray
47          */
48 //      Ray tRay= new Ray();
49 //      Ray tRay;
50
51         /**
52          * Alpha channel
53          */
54 //      static final int alpha = 255 << 24;
55         static final int alpha ;
56
57         /**
58          * Null vector (for speedup, instead of <code>new Vec(0,0,0)</code>
59          */
60 //      static final Vec voidVec = new Vec();
61 //      static final Vec voidVec;
62
63         /**
64          * Temporary vect
65          */
66 //      Vec L = new Vec();
67 //      Vec L;
68
69         /**
70          * Current intersection instance (only one is needed!)
71          */
72 //      Isect inter = new Isect();
73 //      Isect inter;
74
75         /**
76          * Height of the <code>Image</code> to be rendered
77          */
78         int height;
79
80         /**
81          * Width of the <code>Image</code> to be rendered
82          */
83         int width;
84
85 //      int datasizes[] = { 150, 500 };
86         int datasizes[];
87
88         long checksum;
89
90         
91         int size;
92
93         int numobjects;
94         
95         public RayTracer() {
96 //              tRay = new Ray();
97                 alpha = 255 << 24;
98 //              voidVec = new Vec();
99 //              L = new Vec();
100 //              inter = new Isect();
101                 checksum=0;
102                 datasizes = new int[2];
103                 datasizes[0] = 150;
104                 datasizes[1] = 500;
105                 numobjects=0;
106                 width=0;
107                 height=0;
108                 size=0;
109         }
110
111         /**
112          * Create and initialize the scene for the rendering picture.
113          * 
114          * @return The scene just created
115          */
116
117         Scene createScene() {
118                 int x = 0;
119                 int y = 0;
120
121                 Scene scene = new Scene();
122
123                 /* create spheres */
124
125                 Primitive p;
126                 int nx = 4;
127                 int ny = 4;
128                 int nz = 4;
129                 for (int i = 0; i < nx; i++) {
130                         for (int j = 0; j < ny; j++) {
131                                 for (int k = 0; k < nz; k++) {
132                                         double xx = 20.0 / (nx - 1) * i - 10.0;
133                                         double yy = 20.0 / (ny - 1) * j - 10.0;
134                                         double zz = 20.0 / (nz - 1) * k - 10.0;
135
136                                         p = new Sphere(new Vec(xx, yy, zz), 3);
137                                         // p.setColor(i/(double) (nx-1), j/(double)(ny-1),
138                                         // k/(double) (nz-1));
139                                         p.setColor(0, 0, (i + j) / (double) (nx + ny - 2));
140                                         p.surf.shine = 15.0;
141                                         p.surf.ks = 1.5 - 1.0;
142                                         p.surf.kt = 1.5 - 1.0;
143                                         scene.addObject(p);
144                                 }
145                         }
146                 }
147
148                 /* Creates five lights for the scene */
149                 scene.addLight(new Light(100, 100, -50, 1.0));
150                 scene.addLight(new Light(-100, 100, -50, 1.0));
151                 scene.addLight(new Light(100, -100, -50, 1.0));
152                 scene.addLight(new Light(-100, -100, -50, 1.0));
153                 scene.addLight(new Light(200, 200, 0, 1.0));
154
155                 /* Creates a View (viewing point) for the rendering scene */
156                 View v = new View(new Vec(x, 20, -30), new Vec(x, y, 0), new Vec(0, 1,
157                                 0), 1.0, 35.0 * 3.14159265 / 180.0, 1.0);
158                 /*
159                  * v.from = new Vec(x, y, -30); v.at = new Vec(x, y, -15); v.up = new
160                  * Vec(0, 1, 0); v.angle = 35.0 * 3.14159265 / 180.0; v.aspect = 1.0;
161                  * v.dist = 1.0;
162                  */
163                 scene.setView(v);
164
165                 return scene;
166         }
167
168         public void setScene(Scene scene) {
169                 // Get the objects count
170                 int nLights = scene.getLights();
171                 int nObjects = scene.getObjects();
172
173                 lights = new Light[nLights];
174                 prim = new Primitive[nObjects];
175
176                 // Get the lights
177                 for (int l = 0; l < nLights; l++) {
178                         lights[l] = scene.getLight(l);
179                 }
180
181                 // Get the primitives
182                 for (int o = 0; o < nObjects; o++) {
183                         prim[o] = scene.getObject(o);
184                 }
185
186                 // Set the view
187                 view = scene.getView();
188         }
189
190         public void render(Interval interval) {
191                 
192 //              long checksum;
193 //              checksum=0;
194
195                 // Screen variables
196                 int row[] = disjoint ROW new int[interval.width * (interval.yto - interval.yfrom)];
197                 int pixCounter = 0; // iterator
198
199                 // Rendering variables
200         //      int x, y, red, green, blue;
201 //              double xlen, ylen;
202                 
203                 Vec viewVec;
204                 viewVec = Vec.sub(view.at, view.from);
205                 viewVec.normalize();
206                 Vec tmpVec = new Vec(viewVec);
207                 tmpVec.scale(Vec.dot(view.up, viewVec));
208                 Vec upVec = Vec.sub(view.up, tmpVec);
209                 upVec.normalize();
210                 Vec leftVec = Vec.cross(view.up, viewVec);
211                 leftVec.normalize();
212                 double frustrumwidth = view.dist * Math.tan(view.angle);
213                 upVec.scale(-frustrumwidth);
214                 leftVec.scale(view.aspect * frustrumwidth);
215                 
216 //              Ray r = new Ray(view.from, new Vec(0,0,0));
217 //              Vec col = new Vec();
218
219                 // Header for .ppm file
220                 // System.out.println("P3");
221                 // System.out.println(width + " " + height);
222                 // System.out.println("255");
223
224                 // All loops are reversed for 'speedup' (cf. thinking in java p331)
225
226                 // For each line
227                 for (int y = interval.yfrom; y < interval.yto; y++) {
228                          double ylen = (double) (2.0 * y) / (double) interval.width - 1.0;
229 //                       System.out.println("Doing line " + y);
230                         // For each pixel of the line, launch parallel sese
231                         sese parallel{
232                                 int tempArray[]= disjoint TEMPARRAY new int[interval.width];
233                                 int line_checksum=0;
234                                 Ray tRay = new Ray();
235                                 Ray r = new Ray(view.from, new Vec(0,0,0));
236                                 
237                                 for (int x = 0; x < interval.width; x++) {
238                                         Vec col = new Vec();
239                                 
240                                         double xlen = (double) (2.0 * x) / (double) interval.width - 1.0;                                       
241                                         int pixCounter_t=y*(interval.width)+x;
242
243                                         r.D = Vec.comb(xlen, leftVec, ylen, upVec);
244                                         r.D.add(viewVec);
245                                         r.D.normalize();
246                                         
247                                         col = trace(0, 1.0, r,new Isect(),new Ray(),new Vec());
248                         
249                                         // computes the color of the ray
250                                         
251                                         int red = (int) (col.x * 255.0);
252                                         if (red > 255)
253                                                 red = 255;
254                                         int green = (int) (col.y * 255.0);
255                                         if (green > 255)
256                                                 green = 255;
257                                         int blue = (int) (col.z * 255.0);
258                                         if (blue > 255)
259                                                 blue = 255;
260                                         
261                                         line_checksum += red;
262                                         line_checksum += green;
263                                         line_checksum += blue;
264                                         
265
266                                         // RGB values for .ppm file
267                                         // System.out.println(red + " " + green + " " + blue);
268                                         // Sets the pixels
269 //                                      row[pixCounter_t] = alpha | (red << 16) | (green << 8) | (blue);
270                                         tempArray[x]= alpha | (red << 16) | (green << 8) | (blue);
271                                 } // end for (x)
272                         }               // end of sese line             
273                         
274                         sese serial{
275                                 for (int x = 0; x < interval.width; x++) {
276                                     int pixCounter_t=y*(interval.width)+x;
277                                         row[pixCounter_t] = tempArray[x];
278                             }
279                                 checksum+=line_checksum;
280                                 if(y== (interval.yto-1)){
281                                         System.out.println("CHECKSUM="+checksum);
282                                 }
283                         }
284                         
285                 } // end for (y)
286
287                 System.out.println("END OF WORK");
288                 
289         }
290         
291
292         boolean intersect(Ray r, double maxt,Isect inter) {
293                 Isect tp;
294                 int i, nhits;
295
296                 nhits = 0;
297                 inter.t = 1e9;
298                 for (i = 0; i < prim.length; i++) {
299                         // uses global temporary Prim (tp) as temp.object for speedup
300                         tp = prim[i].intersect(r);
301                         if (tp != null && tp.t < inter.t) {
302                                 inter.t = tp.t;
303                                 inter.prim = tp.prim;
304                                 inter.surf = tp.surf;
305                                 inter.enter = tp.enter;
306                                 nhits++;
307                         }
308                 }
309                 return nhits > 0 ? true : false;
310         }
311
312         /**
313          * Checks if there is a shadow
314          * 
315          * @param r
316          *            The ray
317          * @return Returns 1 if there is a shadow, 0 if there isn't
318          */
319         int Shadow(Ray r, double tmax,Isect inter) {
320                 if (intersect(r, tmax,inter))
321                         return 0;
322                 return 1;
323         }
324
325         /**
326          * Return the Vector's reflection direction
327          * 
328          * @return The specular direction
329          */
330         Vec SpecularDirection(Vec I, Vec N) {
331                 Vec r;
332                 r = Vec.comb(1.0 / Math.abs(Vec.dot(I, N)), I, 2.0, N);
333                 r.normalize();
334                 return r;
335         }
336
337         /**
338          * Return the Vector's transmission direction
339          */
340         Vec TransDir(Surface m1, Surface m2, Vec I, Vec N) {
341                 double n1, n2, eta, c1, cs2;
342                 Vec r;
343                 n1 = m1 == null ? 1.0 : m1.ior;
344                 n2 = m2 == null ? 1.0 : m2.ior;
345                 eta = n1 / n2;
346                 c1 = -Vec.dot(I, N);
347                 cs2 = 1.0 - eta * eta * (1.0 - c1 * c1);
348                 if (cs2 < 0.0)
349                         return null;
350                 r = Vec.comb(eta, I, eta * c1 - Math.sqrt(cs2), N);
351                 r.normalize();
352                 return r;
353         }
354
355         /**
356          * Returns the shaded color
357          * 
358          * @return The color in Vec form (rgb)
359          */
360         Vec shade(int level, double weight, Vec P, Vec N, Vec I, Isect hit,Ray tRay,Vec L) {
361                 double n1, n2, eta, c1, cs2;
362                 Vec r;
363                 Vec tcol;
364                 Vec R;
365                 double t, diff, spec;
366                 Surface surf;
367                 Vec col;
368                 int l;
369
370                 col = new Vec();
371                 surf = hit.surf;
372                 R = new Vec();
373                 if (surf.shine > 1e-6) {
374                         R = SpecularDirection(I, N);
375                 }
376
377                 // Computes the effectof each light
378                 for (l = 0; l < lights.length; l++) {
379 //                      L.sub2(lights[l].pos, P);
380                         
381                         L.x=lights[l].pos.x-P.x;
382                         L.y=lights[l].pos.y-P.y;
383                         L.z=lights[l].pos.z-P.z;
384                         
385                         if (Vec.dot(N, L) >= 0.0) {
386                                 t = L.normalize();
387
388                                 tRay.P = P;
389                                 tRay.D = L;
390
391                                 // Checks if there is a shadow
392                                 if (Shadow(tRay, t,hit) > 0) {
393                                         diff = Vec.dot(N, L) * surf.kd * lights[l].brightness;
394
395                                         col.adds(diff, surf.color);
396                                         if (surf.shine > 1e-6) {
397                                                 spec = Vec.dot(R, L);
398                                                 if (spec > 1e-6) {
399                                                         spec = Math.pow(spec, surf.shine);
400                                                         col.x += spec;
401                                                         col.y += spec;
402                                                         col.z += spec;
403                                                 }
404                                         }
405                                 }
406                         } // if
407                 } // for
408
409                 tRay.P = P;
410                 if (surf.ks * weight > 1e-3) {
411                         tRay.D = SpecularDirection(I, N);
412                         tcol = trace(level + 1, surf.ks * weight, tRay,hit,tRay,L);
413                         col.adds(surf.ks, tcol);
414                 }
415                 if (surf.kt * weight > 1e-3) {
416                         if (hit.enter > 0)
417                                 tRay.D = TransDir(null, surf, I, N);
418                         else
419                                 tRay.D = TransDir(surf, null, I, N);
420                         tcol = trace(level + 1, surf.kt * weight, tRay,hit,tRay,L);
421                         col.adds(surf.kt, tcol);
422                 }
423
424                 // garbaging...
425                 tcol = null;
426                 surf = null;
427
428                 return col;
429         }
430
431         /**
432          * Launches a ray
433          */
434         Vec trace(int level, double weight, Ray r,Isect inter,Ray tRay,Vec L) {
435                 
436                 Vec P, N;
437                 boolean hit;
438
439                 // Checks the recursion level
440                 if (level > 6) {
441                         return new Vec();
442                 }
443                 hit = intersect(r, 1e6,inter);
444                 if (hit) {
445                         P = r.point(inter.t);
446                         N = inter.prim.normal(P);
447                         if (Vec.dot(r.D, N) >= 0.0) {
448                                 N.negate();
449                         }
450                         return shade(level, weight, P, N, r.D, inter,tRay,L);
451                 }
452                 
453                 // no intersection --> col = 0,0,0
454                 return new Vec(0,0,0);
455         }
456
457 }