KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > examples > c3d > C3DRenderingEngine


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.examples.c3d;
32
33 import org.objectweb.proactive.examples.c3d.geom.Ray;
34 import org.objectweb.proactive.examples.c3d.geom.Vec;
35 import org.objectweb.proactive.examples.c3d.prim.Isect;
36 import org.objectweb.proactive.examples.c3d.prim.Light;
37 import org.objectweb.proactive.examples.c3d.prim.Primitive;
38 import org.objectweb.proactive.examples.c3d.prim.Surface;
39
40
41 import java.awt.image.ColorModel JavaDoc;
42
43 /**
44  * Rendering Engine used by Remote 3D
45  * @version: 1.1 step 3
46  * @author: Florian DOYON
47  * @author: Wilfried KLAUSER
48  */

49 public class C3DRenderingEngine implements java.io.Serializable JavaDoc {
50
51   /**
52    * Lights for the rendering scene
53    */

54   private Light lights[];
55   /**
56    * Objects (spheres) for the rendering scene
57    */

58   private Primitive prim[];
59   /**
60    * Default RGB ColorModel for the newPixels(...)
61    */

62   private transient ColorModel JavaDoc model;
63   /**
64    * The view for the rendering scene
65    */

66   private View view;
67   /**
68    * Interval c3ddispatcher
69    */

70   private C3DDispatcher c3ddispatcher;
71   /**
72    * Alpha channel
73    */

74   private static final int alpha = 255 << 24;
75   /**
76    * Null vector (for speedup, instead of <code>new Vec(0,0,0)</code>
77    */

78   private static final Vec voidVec = new Vec();
79   /**
80    * Current intersection instance (only one is needed!)
81    */

82   private Isect inter = new Isect();
83   /**
84    * Temporary ray
85    */

86   private Ray tRay = new Ray();
87   /**
88    * Temporary vect
89    */

90   private Vec L = new Vec();
91
92
93   /**
94    * Feature the migration property
95    */

96
97   public void migrateTo(String JavaDoc nodeTarget) {
98     try {
99       org.objectweb.proactive.ProActive.migrateTo(nodeTarget);
100     } catch (Exception JavaDoc e) {
101       e.printStackTrace();
102     }
103   }
104
105
106   /**
107    * Default constructor
108    */

109   public C3DRenderingEngine() {
110   }
111
112
113   /**
114    * Constructor refernecing the current dispatcher
115    */

116   public C3DRenderingEngine(C3DDispatcher c3ddispatcher) {
117     model = ColorModel.getRGBdefault();
118     this.c3ddispatcher = c3ddispatcher;
119     //System.out.println("Rendering id "+org.objectweb.proactive.ProActive.getBodyOnThis().getID());
120
}
121
122
123   /**
124    * Find closest Ray, return initialized Isect
125    * with intersection information.
126    */

127   boolean intersect(Ray r, double maxt) {
128     Isect tp;
129     int i, nhits;
130
131     nhits = 0;
132     inter.t = 1e9;
133     for (i = 0; i < prim.length; i++) {
134       // uses global temporary Prim (tp) as temp.object for speedup
135
tp = prim[i].intersect(r);
136       if (tp != null && tp.t < inter.t) {
137         inter.t = tp.t;
138         inter.prim = tp.prim;
139         inter.surf = tp.surf;
140         inter.enter = tp.enter;
141         nhits++;
142       }
143     }
144     return nhits > 0 ? true : false;
145   }
146
147
148   /**
149    * Checks if there is a shadow
150    */

151   int Shadow(Ray r, double tmax) {
152     if (intersect(r, tmax))
153       return 0;
154     return 1;
155   }
156
157
158   /**
159    * Return the Vector's reflection direction
160    */

161   Vec SpecularDirection(Vec I, Vec N) {
162     Vec r;
163     r = Vec.comb(1.0 / Math.abs(Vec.dot(I, N)), I, 2.0, N);
164     r.normalize();
165     return r;
166   }
167
168
169   /**
170    * Return the Vector's transmission direction
171    */

172   Vec TransDir(Surface m1, Surface m2, Vec I, Vec N) {
173     double n1, n2, eta, c1, cs2;
174     Vec r;
175     n1 = m1 == null ? 1.0 : m1.ior;
176     n2 = m2 == null ? 1.0 : m2.ior;
177     eta = n1 / n2;
178     c1 = -Vec.dot(I, N);
179     cs2 = 1.0 - eta * eta * (1.0 - c1 * c1);
180     if (cs2 < 0.0)
181       return null;
182     r = Vec.comb(eta, I, eta * c1 - Math.sqrt(cs2), N);
183     r.normalize();
184     return r;
185   }
186
187
188   /**
189    * Returns the shaded color
190    */

191   Vec shade(int level, double weight, Vec P, Vec N, Vec I, Isect hit) {
192     Vec tcol;
193     Vec R;
194     double t, diff, spec;
195     Surface surf;
196     Vec col;
197     int l;
198
199     col = new Vec();
200     surf = hit.surf;
201     R = new Vec();
202     if (surf.shine > 1e-6) {
203       R = SpecularDirection(I, N);
204     }
205
206     // Computes the effectof each light
207
for (l = 0; l < lights.length; l++) {
208       L.sub2(lights[l].pos, P);
209       if (Vec.dot(N, L) >= 0.0) {
210         t = L.normalize();
211
212         tRay.P = P;
213         tRay.D = L;
214
215         // Checks if there is a shadow
216
if (Shadow(tRay, t) > 0) {
217           diff = Vec.dot(N, L) * surf.kd * lights[l].brightness;
218
219           col.adds(diff, surf.color);
220           if (surf.shine > 1e-6) {
221             spec = Vec.dot(R, L);
222             if (spec > 1e-6) {
223               spec = Math.pow(spec, surf.shine);
224               col.x += spec;
225               col.y += spec;
226               col.z += spec;
227             }
228           }
229         }
230       } // if
231
} // for
232

233     tRay.P = P;
234     if (surf.ks * weight > 1e-3) {
235       tRay.D = SpecularDirection(I, N);
236       tcol = trace(level + 1, surf.ks * weight, tRay);
237       col.adds(surf.ks, tcol);
238     }
239     if (surf.kt * weight > 1e-3) {
240       if (hit.enter > 0)
241         tRay.D = TransDir(null, surf, I, N);
242       else
243         tRay.D = TransDir(surf, null, I, N);
244       tcol = trace(level + 1, surf.kt * weight, tRay);
245       col.adds(surf.kt, tcol);
246     }
247
248     // garbaging...
249
tcol = null;
250     surf = null;
251
252     return col;
253   }
254
255
256   /**
257    * Launches a ray
258    */

259   Vec trace(int level, double weight, Ray r) {
260     Vec P, N;
261     boolean hit;
262     
263     // Checks the recursion level
264
if (level > 6) {
265       return new Vec();
266     }
267
268     hit = intersect(r, 1e6);
269     if (hit) {
270       P = r.point(inter.t);
271       N = inter.prim.normal(P);
272       if (Vec.dot(r.D, N) >= 0.0) {
273         N.negate();
274       }
275       return shade(level, weight, P, N, r.D, inter);
276     }
277     // no intersection --> col = 0,0,0
278
return voidVec;
279   }
280
281
282   /**
283    * Scan all pixels in the image intervals, have them traced
284    * and set the result with newPixels(...) on the MemoryImagesource
285    * <i>heavily optimized!!!</i>
286    */

287   public void render(int engine_number, Interval interval) {
288
289     // Screen variables
290
int row[] = new int[interval.width * (interval.yto - interval.yfrom)];
291     int pixCounter = 0; //iterator
292

293     // Renderding variables
294
int x, y, red, green, blue;
295     double xlen, ylen;
296
297     Vec viewVec = Vec.sub(view.at, view.from);
298     viewVec.normalize();
299
300     Vec tmpVec = new Vec(viewVec);
301     tmpVec.scale(Vec.dot(view.up, viewVec));
302
303     Vec upVec = Vec.sub(view.up, tmpVec);
304     upVec.normalize();
305
306     Vec leftVec = Vec.cross(view.up, viewVec);
307     leftVec.normalize();
308
309     double frustrumwidth = view.dist * Math.tan(view.angle);
310
311     upVec.scale(-frustrumwidth);
312     leftVec.scale(view.aspect * frustrumwidth);
313
314     Ray r = new Ray(view.from, voidVec);
315     Vec col = new Vec();
316
317     // All loops are reversed for 'speedup' (cf. thinking in java p331)
318

319     // For each line
320
for (y = interval.yfrom; y < interval.yto; y++) {
321       ylen = (double)(2.0 * y) / (double)interval.width - 1.0;
322
323       // For each pixel of the line
324
for (x = 0; x < interval.width; x++) {
325         xlen = (double)(2.0 * x) / (double)interval.width - 1.0;
326         r.D = Vec.comb(xlen, leftVec, ylen, upVec);
327         r.D.add(viewVec);
328         r.D.normalize();
329         col = trace(0, 1.0, r);
330     
331         // computes the color of the ray
332
red = (int)(col.x * 255.0);
333         if (red > 255)
334           red = 255;
335         green = (int)(col.y * 255.0);
336         if (green > 255)
337           green = 255;
338         blue = (int)(col.z * 255.0);
339         if (blue > 255)
340           blue = 255;
341
342         // Sets the pixels
343
row[pixCounter++] = alpha | (red << 16) | (green << 8) | (blue);
344       } // end for (x)
345
} // end for (y)
346
// sends the results to the dispatcher
347
c3ddispatcher.setPixels(row, interval, engine_number);
348   }
349
350
351   /**
352    * Creates the local objects used in the rendering
353    */

354   public void setScene(Scene scene) {
355     int nLights = scene.getLights();
356     int nObjects = scene.getObjects();
357
358     lights = new Light[nLights];
359     prim = new Primitive[nObjects];
360
361     for (int l = 0; l < nLights; l++) {
362       lights[l] = scene.getLight(l);
363     }
364
365     for (int o = 0; o < nObjects; o++) {
366       prim[o] = scene.getObject(o);
367     }
368     this.view = scene.getView();
369   }
370
371
372   /**
373    * Destructor
374    * @exception Throwable exception requested by RMI
375    */

376   protected void finalize() throws Throwable JavaDoc {
377     //System.out.println("Engine halted and released");
378
super.finalize();
379   }
380
381
382   /**
383    * The pinging function called by <code>C3DDispatcher</code>
384    * to get the avg. pinging time
385    */

386   public int ping() {
387     return 0;
388   }
389 }
390
Popular Tags