next up previous contents index
Next: Chess Board Texture Up: Defining your own textures Previous: Defining your own textures

Generic Texture Example

 

  // This is the definition for a texture called ExampleTexture.
  class ExampleTexture : public Texture 
  {
  public:
    // Constructor with any parameters needed and a precedence.
    ExampleTexture(double var_1, double var_2, int prec);

    // This function determines how the texture affects the object. 
    // You do not need to call this function it is called within Mirage.
    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);

    // This function prints out the texture's parameters for debugging.
    virtual void print_tex(void) {
      printf("ExampleTexture: variable_1 %f, variable_2 %f, Precedence %d. \n",
             variable_1, variable_2, precedence);}
  
  private:
    // The parameters that the ExampleTexture stores about itself.
    double variable_1, variable_2;
  };

  // Constructor
  ExampleTexture::ExampleTexture(double var_1, double var_2, int prec) {
    variable_1 = var_1; 
    variable_2 = var_2;
    precedence = prec;
    // Texture objects must be deleted at the end of the frame.
    mark_for_deletion();
  }

  // This is the function that actually applies the ExampleTexture.
  void ExampleTexture::apply(vector ray_from, vector ray_dir, uint ray_flags,
                             Material *properties, vector hit_point,
                             vector hit_normal, RayTracer *engine,
                             transform tex_transform){

  // The ray comes from ray_from and is travelling in direction ray_dir.  
  // A definition of ray_flags is in constants.h. 
  // You might want to modify your texture depending on these properties.
  // properties is a pointer to the material.  
  // By modifying this you can change the material at the hit_point.
  // hit_point This is where in world space the ray hits the  object.  
  // If you modify this then rays reflected from or transmitted through 
  // this object could start somewhere else.
  // hit_normal  This is normal to the surface at hit_point.  
  // You can modify this to create bumb mapping. 
  // You can cast rays in your texture using an engine.
  // tex_transform is needed for changing from the hit_point, 
  // in world space, back to where the object was last bound 
  // before the texture was applied.
  // The apply code shows how to do this

    vector object_hit;
    Matrix::apply(tex_transform[AFT], hit_point, object_hit);
  //Usually you will want to modify the texture depending on 
  // where on the object it hits rather than where the object 
  // is in space.  The hit_point is a point in world space that 
  // it hits.  By defining object_hit as above, and defining the 
  // texture depending on object_hit rather than the hit_point 
  // then the texture is defined depending on the position where 
  // the object was bound before the texture was applied, not the 
  // final position of the object.  This means the texture will remain the 
  // same on the object no matter where the object is located.

  DSTmaterial *dstmat = (DSTmaterial *) properties;
  /*properties is a pointer to the Material class.  So that you 
  // can change the material properties you need to define a 
  // pointer that points to the appropriate subclass of Material.  
  // This creates a pointer dstmat that points to the DSTmaterial 
  // subclass.  This texture can only be applied to objects whose 
  // material is a DSTmaterial.


  //Then modify the texture properties however you want.

}

.
.
.

  // Give the texture to the object.
the_object->texture = new ExampleTexture(1.0, 0.5, 0);



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