POV-Ray : Newsgroups : povray.unofficial.patches : Importance sampling of phong brdf : Re: Importance sampling of phong brdf Server Time
3 Jul 2024 01:08:48 EDT (-0400)
  Re: Importance sampling of phong brdf  
From: Mael
Date: 15 Apr 2003 14:37:07
Message: <3e9c5153@news.povray.org>
> Is this based on actual measures?  I would assume the calculation of the
> sampling directions is negligible on terms of computation time in
> comparison to the actual shooting of rays.  Isn't it possible to calculate
> the sampling directions and their weights in advance anyway?

Yes it would be possible (you need a set of samples for each different
phong_size). And you're right, calls to trace() are certainly what make it
slow

as i'm not planning to release more mlpov versions here is the code for
people interested (it still needs some work)
it uses a 2D halton sequence for random variables to compute the sampling
directions
the function have the finish metallic as i think it's possible to use it but
this not done

void do_phong_rad(VECTOR pt, VECTOR dir, VECTOR normal, VECTOR raw_normal,
                  DBL phong, DBL phong_size, SNGL metallic, COLOUR col, DBL
weight)
{
  unsigned long Save_Quality_Flags, Save_Options;
  int save_Max_Trace_Level;
  int nb_samples, i;
  static DBL cache_phong_size=0;
  static int cache_nb_samples=0;
  DBL ang;
  DBL depth;
  COLOUR tmp_col, sum_col;
  VECTOR in, in_s;
  DBL cosalpha, sinalpha, phi;
  DBL costheta;
  VECTOR ru,rv;
  RAY ray_s;

  // FIXME this could be computed at parse time and saved in the finish
  if (phong_size != cache_phong_size)
  {
    // Compute a number of samples with the rad_phong value and phong_size
    costheta=pow(.01,1/phong_size);
    cache_nb_samples=opts.Radiosity_Phong * (1-costheta);
  }
  nb_samples = cache_nb_samples;

  // Adjust the weight with phong
  weight *= phong;

  // Save and set tracing options
  save_Max_Trace_Level = Max_Trace_Level;
  Max_Trace_Level = Trace_Level + 2;
  if (Max_Trace_Level>save_Max_Trace_Level) Max_Trace_Level =
save_Max_Trace_Level;
  Save_Quality_Flags = opts.Quality_Flags;
  Save_Options = opts.Options;
  opts.Radiosity_Quality = 6;
  opts.Options &= ~USE_LIGHT_BUFFER;
  opts.Quality_Flags &= ~Q_AREA_LIGHT;
  if(!opts.Radiosity_Use_Media)
    opts.Quality_Flags &= ~Q_VOLUME;
  // FIXME do we consider it radiosity ??
  Radiosity_Trace_Level++;
  Trace_Level++;

  // Mirror the incoming ray, and build a local coord system
  // FIXME change for in ~= x
  VDot(ang, dir, normal);
  ang *= -2.0;
  VAddScaled(in, dir, ang, normal);
  VNormalizeEq(in);
  ru[X]=1-in[X]*in[X];
  ru[Y]=0-in[X]*in[Y];
  ru[Z]=0-in[X]*in[Z];
  VNormalizeEq(ru);
  VCross(rv, ru, in);

  // Samples the modified phong brdf, and save the sum in sum_col
  Make_Colour(sum_col, 0., 0., 0.);
  for (i=0;i<nb_samples;i++)
  {
    // Build a sampling direction in_s
    cosalpha = pow(halton_samples[i][X], 1. / (phong_size+1));
    sinalpha = sqrt(1 - cosalpha*cosalpha);
    phi = halton_samples[i][Y] * 2. * M_PI;
    in_s[X] = in[X] * cosalpha + sinalpha * (sin(phi) * ru[X] + cos(phi) *
rv[X]);
    in_s[Y] = in[Y] * cosalpha + sinalpha * (sin(phi) * ru[Y] + cos(phi) *
rv[Y]);
    in_s[Z] = in[Z] * cosalpha + sinalpha * (sin(phi) * ru[Z] + cos(phi) *
rv[Z]);

    // Test if not in the object
    VDot(ang, in_s, raw_normal)
    if ( ang > 0.)
    {
      // Trace a ray
      Initialize_Ray_Containers(&ray_s);
      Assign_Vector(ray_s.Direction, in_s);
      Assign_Vector(ray_s.Initial, pt);
      depth = Trace(&ray_s, tmp_col, weight);
      // for the modified phong divided by the pdf the integral only have
constant term (cf below), and costheta
      VDot(costheta, in_s, normal);
      VAddScaledEq(sum_col, costheta, tmp_col);
    }
  }
  // Scale and add in the returned col
  VScaleEq(sum_col, phong*(phong_size+2)/(nb_samples*(phong_size+1)));
  VAddEq(col, sum_col);

  // Back to previous values
  Radiosity_Trace_Level--;
  Trace_Level--;
  opts.Quality_Flags = Save_Quality_Flags;
  opts.Options = Save_Options;
  Max_Trace_Level = save_Max_Trace_Level;
}


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.