Dana Vrajitoru
B424 Parallel and Distributed Programming

Parallel Computer Graphics

Convex Hull

Graham's Scan
  • Start with the lowest, left-most point.
  • Order all the other points by angle with the first.
  • Traverse the points from the bottom in order of the angle. For each of them, if it's a left turn (1), accept it in the hull.
  • If it's a right turn (2), discard the last point and connect the new point with the one before the last (3). Then check backwards to make sure the hull is convex.
  • O(n log(n))

Jarvis's March

Parallel Version

Geometrical Transformations

Partitioning into Processes

Master() {
   for (i = 0, row = 0; i < 48; i++, row = row + 10)
      send(row, i); // send row no to each process
   for (i = 0; i < 480; i++) /* initialize temp */
      for (j = 0; j < 640; j++)
         temp_map[i][j] = 0;
   for (i = 0; i < (640 * 480); i++) { // for each pixel 
      recv(oldrow,oldcol,newrow,newcol, any_source); 
      if (!((newrow < 0)||(newrow >= 480)||
            (newcol < 0)||(newcol >= 640)))
         temp_map[newrow][newcol]=map[oldrow][oldcol];
   }
   for (i = 0; i < 480; i++) /* update bitmap */
      for (j = 0; j < 640; j++)
         map[i][j] = temp_map[i][j];
}
Slave() {
   recv(row, master); // receive row no. 
   for (oldrow = row; oldrow < (row + 10); 
        oldrow++)
      for (oldcol = 0; oldcol < 640; oldcol++)
      { // transform coords 
          newrow = oldrow + delta_x; // shift
          newcol = oldcol + delta_y; 
          send(oldrow,oldcol,newrow,newcol, 
               master); // coords to master
      }
}
// In reverse order
Slave() {
   recv(row, master); // receive row no. 
   for (newrow = row; newrow < (row + 10); 
        newrow++)
      for (newcol = 0; newcol < 640; newcol++)
      { // transform coords 
         oldrow = newrow / Fx; // reverse scale
         oldcol = newcol / Fy; 
         send(oldrow,oldcol,newrow,newcol, 
              master); // send coords to master
    }
}

Reversing the Rotation

Mandelbrot Set


Range of the image: [-2, 2]x[-2, 2].

// Calculates the number of iterations
// for a pixel with creal = x, cimag = y.

int cal_pixel(float creal, float cimag) {
   int count=0, max = 256;
   float temp, lengthsq, zreal=0, zimag=0;
   do {
      temp = zreal*zreal - zimag*zimag + creal;
      zimag = 2*zreal*zimag + cimag;
      zreal = temp;
      lengthsq = zreal*zreal + zimag*zimag;
      count++;
   } while ((lengthsq < 4.0) && (count < max));
   return count;
}

Generate_fractal(int width, int height, 
        float real_min, float real_max, 
        float imag_min, float img_max) {
  // scale the fractal to the image size 
   float scalex = (real_max - real_min)/width;
   float scaley = (imag_max - imag_min)/height;
   // generate the fractal:
   for (x = 0; x < width; x++)
      for (y = 0; y < height; y++) {
         creal = real_min + (float) x * scalex;
         cimag = imag_min + (float) y * scaley;
         color = cal_pixel(creal, cimag);
         display(x, y, color);
     }
}

Parallel Mandelbrot

Master(min_real, max_real, min_imag, max_imag) {
   float scalex = (real_max - real_min)/width;
   float scaley = (imag_max - imag_min)/height;

   bcast([width,height,scalex,scaley,min_real, 
         max_real,min_imag,max_imag]);

   for (i=0, row=0; i<48; i++, row += 10)
      send(row, i); // send row no. to process i
   for (i=0; i<(480 * 640); i++) {
      recv([x, y, color], any_source);
      display(x, y, color); 
  }
}
Slave () {
   bcast([width,height,scalex,scaley,min_real, 
         max_real,min_imag,max_imag]);
   recv(row, master);
   for (x = 0; x < width; x++) 
      for (y = row; y < (row + 10); y++) {
         creal = real_min + (float) x * scalex;
         cimag = imag_min + (float) y * scaley;
         color = cal_pixel(creal, cimag);
         send([x, y, color], 0); //send to master
      }
}

Flood Fill

FloodFill (x0, y0, fill_color, original_color) {
   int color, x, y;
   x = x0; y = y0;
   getPixel(x, y, color);
   if (color == original_color) {
      while (color == original_color) {
         setPixel(x, y, fill_color);
         FloodFill(x, y+1, fill_color, original_color);
         FloodFill(x, y-1, fill_color, original_color);
         x = x - 1; // go left
         getPixel(x, y, color);
      }
      x = x0 + 1; y = y0;
      getPixel(x, y, color);
      while (color == original_color) {
         setPixel(x, y, fill_color);
         FloodFill(x, y+1, fill_color, original_color);
         FloodFill(x, y-1, fill_color, original_color);
         x = x + 1; // go right
         getPixel(x, y, color);
      }
   }
}

Ray Tracing - the Method

Speed-up Techniques

Parallel Ray Tracing

Existing Techniques