James Bigler: CS6620 Homework 5




Use perlin noise to make a marble like texture.



Here are a couple.



This one has phong highlights.


// this is sample code
#define PLAIN_NOISE 0
#define MARBLE_NOISE 5

static int noise_type = MARBLE_NOISE;

static float plain_noise(float v0, float v1, float v2) {
  float vec[3];
  vec[0] = v0;
  vec[1] = v1;
  vec[2] = v2;
  return noise3(vec);
}

static float marble(float v0, float v1, float v2, float period, int iters) {
  float vec[3];
  float noise = 0;
  float multiplier = 1;
  for(int i = 0; i < iters; i++) {
    vec[0] = v0 * multiplier;
    vec[1] = v1 * multiplier;
    vec[2] = v2 * multiplier;
    noise += ((float)1/multiplier) * fabs(noise3(vec));
    multiplier *= 2;
  }
  return (sinf(period*2 + noise * 2) + 1)/2;
}

static ScalarTransform1D color_transform1;

NoiseTexture::NoiseTexture(float period_factor):
  period_factor(period_factor)
{
  vector *colors = new vector();
  colors->push_back(RGB(1,1,1));
  colors->push_back(RGB(0.5,0.05,0.05));
  colors->push_back(RGB(0,0,0));
  colors->push_back(RGB(1,1,1));
  colors->push_back(RGB(0.5,0.05,0.05));
  colors->push_back(RGB(0,0,0));
  color_transform1.set_results_ptr(colors);
  color_transform1.scale(0,1.5);
}

Spectral NoiseTexture::get_color(HitInfo *hit) {

  ////////////////////////////////////////////////////////////
  // Compute the noise at the hit location
  UVW uvw;
  hit->obj->get_UVW(uvw, hit);
  float noise;
  RGB color;
  switch (noise_type) {
  case MARBLE_NOISE:
    {
      noise = plain_noise(uvw.x, uvw.y, uvw.z);
      noise += marble(uvw.x, uvw.y, uvw.z, uvw.x * period_factor, 4);
      color = color_transform1.interpolate(noise);
    }
    break;
  case PLAIN_NOISE:
  default:
    noise = plain_noise(uvw.x, uvw.y, uvw.z);
    color = RGB(noise, noise, noise);
    break;
  }
  // override the member reflectance

  return color;
}