next up previous contents index
Next: Noise Texture Up: Defining your own textures Previous: Generic Texture Example

Chess Board Texture

   

Figure 14 shows an object textured with the ChessBoardTexture.

  figure402
Figure 14: Chess Board Texture: Varying Colour at hit point.

class ChessBoardTexture : public Texture {
public:
  ChessBoardTexture(int prec); 

  virtual void apply(vector ray_from, vector ray_dir, 
                     uint ray_flags,
		     Material *properties, vector hit_point,
		     vector hit_normal, RayTracer *engine,
		     transform tex_transform);

  int even(double x)
  {
    if (fmod(x,2) == 0)
      return 1;
    else
      return 0;
  }

  // Print the texture's parameters for debugging
  virtual void print_tex(void) 
  {
    printf("ChessBoardTexture: precedence = %d\n",precedence);
  }
};

 ChessBoardTexture::ChessBoardTexture(int prec) {
    precedence = prec;
    // Texture objects must be deleted at the end of the frame.
    mark_for_deletion();
  }


void ChessBoardTexture::apply(vector ray_from, vector ray_dir, uint ray_flags,
			  Material *properties, vector hit_point,
			  vector hit_normal, RayTracer *engine,
			  transform tex_transform)
{
  vector object_hit;
  // so that if you move the object the texture stays in the 
  // same place on the object.
  Matrix::apply(tex_transform[AFT], hit_point, object_hit);

  DSTmaterial *dstmat = (DSTmaterial *) properties;

  double sum = floor(object_hit[0]) + floor(object_hit[1]) 
             + floor(object_hit[2]);
  if (even(sum)) 
    Vector::assign(dstmat->diffuse,0.0,0.0,0.0);
  else
    Vector::assign(dstmat->diffuse,1.0,1.0,1.0);  
}

.
.
.

  // Add a Chess Board Rectangle.
  the_object = bind(stretch(2.999,3.999,1.999,new_cube()));
  the_object->texture = new ChessBoardTexture(0);
  LINK(the_scene, PLUS, bind(the_object));



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