next up previous contents index
Next: Extrusion Up: Volume Previous: Volume

Soft Objects

 

Soft objects are a type of volume whose field is defined by its distance from a number of control points in the field. Each control point has a location and a radius of influence. The field strength contribution from that point is a cubic interpolation from MAX_SHORT at the control point location to 0 at the radius of influence. The strength of the field at any point is the sum of the field strength contributions from each point. The example gives a soft object defined with two control points.

 figure617
Figure 29: Soft Object Created with the Volume Primitive.

  //Create a simple soft object
  double contribute(double strength, vector center, vector p)
  //Returns the contribution to the field strength at p of a control 
  // point at center with radius of influence strength.
  {
    vector result;
    Vector::sub(center,p,result);
    double dist = Vector::magnitude(result);  
    dist = dist/strength;//0 if p=center and 1 if distance(p,center)=strength.
    if (dist>1)
      dist = 1;
    dist = 1-dist;  // 1 at p = center and 0 at distance(p,center) = strength
    return(cubicInterpolate(0, 65534, dist));
    //cubicInterpolate is defined in useful.h & .c
  }

  .
  .
  .

  vector point1 = {0.25,0.5,0.5};
  double strength1 = 0.5;
  vector point2 = {0.75,0.5,0.5};
  double strength2 = 0.3;

  isosurface_val *volume_data;
  int x_index, y_index, z_index;

  // Allocate an array of field data for the volume.
  volume_data = (isosurface_val *) calloc(CUBE(40), sizeof(isosurface_val));
  if (volume_data == NULL) {
    fatal_error("make_isosurf", "can't allocate volume data",
                "must be low memory.");
  }

  // Fill the field data.
  printf("Creating soft object data ...");
  fflush(stdout);
  for(x_index = 0;x_index < 40; x_index ++) {
    for(y_index = 0; y_index < 40; y_index ++) {
      for(z_index = 0; z_index < 40; z_index ++) {
        vector point = {1.0/40 * x_index, 1.0/40 *y_index, 1.0/40 *z_index};	
        double point1contribution = contribute(strength1, point, point1);
        double point2contribution = contribute(strength2, point, point2);
        volume_data[x_index + 40 * y_index + 40 * 40 * z_index]=
          (unsigned short) (point1contribution + point2contribution);
      }
    }
  }
  printf(" done.\n");

  // Create a soft object from the field data.
  object soft_obj = new_volume(FALSE, FALSE, 40, 40, 40, volume_data, 32767);
  soft_obj->material = purple;
  soft_obj = new_octree_cluster(soft_obj,0,0,0,1,1,1,4,2,TRUE);
  LINK(the_scene, PLUS,bind(soft_obj));



Sophie Day
Fri Feb 20 15:47:19 NZDT 1998