光线追踪三角形法线。奇怪的效果

时间:2019-06-22 21:44:27

标签: c++ raytracing

我正在尝试使用内部球体跟踪房间。 我有搅拌机的模型。几何外观看起来应该是应该的。

enter image description here

每个平面由2个矩形组成。我不知道为什么会这样。

enter image description here

我检查了法线,并且平面中的每个三角形都具有相同的法线。 是什么导致此问题?

// COLOR FUNCTION
V3 color(const Ray &r, Hitable *world, int depth)
{
  hit_record rec;
  if (world->hit(r, 0.001, MAXFLOAT, rec))
  {
    Ray scattered;
    V3 attenuation;
    if (depth < 50 && rec.mat_ptr->scatter(r, rec, attenuation, scattered))
    {
      return attenuation * color(scattered, world, depth + 1);
    }
    else
    {
      return V3(0, 0, 0);
    }
  }
  V3 unit_dierection = unit_vector(r.direction());
  float t = 0.5 * (unit_dierection.y() + 1.0);
  return (1.0 - t) * V3(1.0, 1.0, 1.0) + t * V3(0.5, 0.7, 1.0);
}

/// DEFINITION OF LAMBERTIAN SCATTER
 virtual bool scatter(const Ray &ray_in, const hit_record &rec, V3& attentation, Ray &scattered) const {
    V3 target = rec.p + rec.normal + random_in_unit_sphere();
    scattered = Ray(rec.p, target - rec.p);
    attentation = albedo;
    return true;
  }
// RENDER LOOP
for (int y = height - 1; y >= 0; y--)
  {
    for (int x = 0; x < width; x++)
    {
      V3 col(0, 0, 0);
      for (int i = 0; i < p_scene.camera.max_bounces; i++)
      {
        float u = float(x + drand48()) / float(width);
        float v = float(y + drand48()) / float(height);
        Ray ray = camera.get_ray(u, v);
        col += color(ray, world, 0);
      }

      col /= float(p_scene.camera.max_bounces);
      col = V3(sqrt(col[0]), sqrt(col[1]), sqrt(col[2]));
      int Cr = int(255.99 * col.r());
      int Cg = int(255.99 * col.g());
      int Cb = int(255.99 * col.b());

      img << Cr << " " << Cg << " " << Cb << "\n";
    }
  }

我正在尝试关注tutorial

0 个答案:

没有答案