1 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 ; 42 43 49 public class C3DRenderingEngine implements java.io.Serializable { 50 51 54 private Light lights[]; 55 58 private Primitive prim[]; 59 62 private transient ColorModel model; 63 66 private View view; 67 70 private C3DDispatcher c3ddispatcher; 71 74 private static final int alpha = 255 << 24; 75 78 private static final Vec voidVec = new Vec(); 79 82 private Isect inter = new Isect(); 83 86 private Ray tRay = new Ray(); 87 90 private Vec L = new Vec(); 91 92 93 96 97 public void migrateTo(String nodeTarget) { 98 try { 99 org.objectweb.proactive.ProActive.migrateTo(nodeTarget); 100 } catch (Exception e) { 101 e.printStackTrace(); 102 } 103 } 104 105 106 109 public C3DRenderingEngine() { 110 } 111 112 113 116 public C3DRenderingEngine(C3DDispatcher c3ddispatcher) { 117 model = ColorModel.getRGBdefault(); 118 this.c3ddispatcher = c3ddispatcher; 119 } 121 122 123 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 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 151 int Shadow(Ray r, double tmax) { 152 if (intersect(r, tmax)) 153 return 0; 154 return 1; 155 } 156 157 158 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 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 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 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 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 } } 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 tcol = null; 250 surf = null; 251 252 return col; 253 } 254 255 256 259 Vec trace(int level, double weight, Ray r) { 260 Vec P, N; 261 boolean hit; 262 263 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 return voidVec; 279 } 280 281 282 287 public void render(int engine_number, Interval interval) { 288 289 int row[] = new int[interval.width * (interval.yto - interval.yfrom)]; 291 int pixCounter = 0; 293 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 319 for (y = interval.yfrom; y < interval.yto; y++) { 321 ylen = (double)(2.0 * y) / (double)interval.width - 1.0; 322 323 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 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 row[pixCounter++] = alpha | (red << 16) | (green << 8) | (blue); 344 } } c3ddispatcher.setPixels(row, interval, engine_number); 348 } 349 350 351 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 376 protected void finalize() throws Throwable { 377 super.finalize(); 379 } 380 381 382 386 public int ping() { 387 return 0; 388 } 389 } 390 | Popular Tags |