Esempio n. 1
0
 public void computeImage(double time) {
   initFrame(time);
   // initial each pixar pz depth
   for (int i = 0; i < W * H; i++) {
     pz_pix[i] = 0;
   }
   // initialize each pixar color to background color
   for (int i = 0; i < H * W; i++)
     pix[i] =
         pack(
             (int) (background[0] * 255),
             (int) (background[1] * 255),
             (int) (background[2] * 255));
   // for each shapes
   for (int i = 0; i < geoI; i++) {
     double vertex[][] = t[i].vertices;
     double normal[][] = t[i].normals;
     int face[][] = t[i].faces;
     // calculate rgb color for each vertice
     for (int j = 0; j < vertex.length; j++) {
       normalize(normal[j]);
       double I[] = {vertex[i][0] - viewX, vertex[i][1] - viewY, vertex[i][2] - viewZ};
       normalize(I);
       double dotP = dot(normal[j], I);
       double R[] = {
         I[0] - 2.0 * dotP * normal[j][0],
         I[1] - 2.0 * dotP * normal[j][1],
         I[2] - 2.0 * dotP * normal[j][2]
       };
       normalize(R);
       double p = materials[i].p;
       t[i].colors[j][0] = materials[i].Argb[0];
       t[i].colors[j][1] = materials[i].Argb[1];
       t[i].colors[j][2] = materials[i].Argb[2];
       // for each light!
       for (int iL = 0; iL < lights.length; iL++) {
         double dotLN = Math.max(0, dot(lights[iL].Lxyz, normal[j]));
         double dotLV = Math.pow(Math.max(0, dot(lights[iL].Lxyz, R)), p);
         t[i].colors[j][0] +=
             lights[iL].Irgb[0] * (materials[i].Drgb[0] * dotLN + materials[i].Srgb[0] * dotLV);
         t[i].colors[j][1] +=
             lights[iL].Irgb[1] * (materials[i].Drgb[1] * dotLN + materials[i].Srgb[1] * dotLV);
         t[i].colors[j][2] +=
             lights[iL].Irgb[2] * (materials[i].Drgb[2] * dotLN + materials[i].Srgb[2] * dotLV);
       }
     }
     for (int j = 0; j < face.length; j++) {
       // first half => triangle
       draw.set1vertex(vertex[face[j][0]][0], vertex[face[j][0]][1], vertex[face[j][0]][2]);
       draw.set2vertex(vertex[face[j][1]][0], vertex[face[j][1]][1], vertex[face[j][1]][2]);
       draw.set3vertex(vertex[face[j][2]][0], vertex[face[j][2]][1], vertex[face[j][2]][2]);
       draw.set1rgb(
           t[i].colors[face[j][0]][0], t[i].colors[face[j][0]][1], t[i].colors[face[j][0]][2]);
       draw.set2rgb(
           t[i].colors[face[j][1]][0], t[i].colors[face[j][1]][1], t[i].colors[face[j][1]][2]);
       draw.set3rgb(
           t[i].colors[face[j][2]][0], t[i].colors[face[j][2]][1], t[i].colors[face[j][2]][2]);
       perspective(draw);
       draw.toTrapezoids();
       interpTri(draw);
       // second half => triangle
       draw.set1vertex(vertex[face[j][0]][0], vertex[face[j][0]][1], vertex[face[j][0]][2]);
       draw.set2vertex(vertex[face[j][2]][0], vertex[face[j][2]][1], vertex[face[j][2]][2]);
       draw.set3vertex(vertex[face[j][3]][0], vertex[face[j][3]][1], vertex[face[j][3]][2]);
       draw.set1rgb(
           t[i].colors[face[j][0]][0], t[i].colors[face[j][0]][1], t[i].colors[face[j][0]][2]);
       draw.set2rgb(
           t[i].colors[face[j][2]][0], t[i].colors[face[j][2]][1], t[i].colors[face[j][2]][2]);
       draw.set3rgb(
           t[i].colors[face[j][3]][0], t[i].colors[face[j][3]][1], t[i].colors[face[j][3]][2]);
       perspective(draw);
       draw.toTrapezoids();
       interpTri(draw);
     }
   }
 }