Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 光线跟踪器光反射错误_C#_C++_Raytracing - Fatal编程技术网

C# 光线跟踪器光反射错误

C# 光线跟踪器光反射错误,c#,c++,raytracing,C#,C++,Raytracing,我正试图把它移植到C#上,当光线反射到漫反射表面时,我发现了一个奇怪的错误。有人知道问题可能来自哪里吗 这是我的漫反射曲面代码: if(sphere.Reflection == Sphere.ReflectionType.DIFFUSE) { double angleRand = random.NextDouble(seed) *2f*Math.PI; double distanceRand = random.NextDoubl

我正试图把它移植到C#上,当光线反射到漫反射表面时,我发现了一个奇怪的错误。有人知道问题可能来自哪里吗

这是我的漫反射曲面代码:

if(sphere.Reflection == Sphere.ReflectionType.DIFFUSE)
        {
            double angleRand = random.NextDouble(seed) *2f*Math.PI;
            double distanceRand = random.NextDouble(seed);
            double distanceRandSqtr = Math.Sqrt(distanceRand);

            Vector3 w = surfaceNormal;
            Vector3 u = Vector3.Normalize(Vector3.Cross(Math.Abs(w.X) > .1 ? new Vector3(0f, 1f, 0f) : new Vector3(1f, 0f, 0f), w));
            Vector3 v = Vector3.Cross(w, u);

            Vector3 ref1 = Vector3.Multiply(u, (float)Math.Cos(angleRand));
            ref1 = Vector3.Multiply(ref1, (float)distanceRandSqtr);
            Vector3 ref2 = Vector3.Multiply(v, (float)Math.Sin(angleRand));
            ref2 = Vector3.Multiply(ref2, (float)distanceRandSqtr);
            Vector3 ref3 = Vector3.Multiply(w, (float)Math.Sqrt(1 - distanceRand));
            Vector3 ref4 = Vector3.Add(ref1, ref2);
            ref4 = Vector3.Add(ref4, ref3);

            Vector3 reflectionRayRand = Vector3.Normalize(ref4);

            Vector3 nextRadiance = ComputeRadiance(new Ray(intersectionPoint, reflectionRayRand), depth, seed);

            Vector3 result = Vector3.Multiply(color, nextRadiance);
            result = Vector3.Add(sphere.Emission, result);

            if (float.IsNaN(result.X) || float.IsNaN(result.Y) || float.IsNaN(result.Z))
            {
                throw new Exception();
            } 

            return result;
        }
这是原件:

if (obj.refl == DIFF){                  // Ideal DIFFUSE reflection
double r1=2*M_PI*erand48(Xi), r2=erand48(Xi), r2s=sqrt(r2);
Vec w=nl, u=((fabs(w.x)>.1?Vec(0,1):Vec(1))%w).norm(), v=w%u;
Vec d = (u*cos(r1)*r2s + v*sin(r1)*r2s + w*sqrt(1-r2)).norm();
return obj.e + f.mult(radiance(Ray(x,d),depth,Xi));}

你得到的效果让我想起了一点暗影粉刺。通常这是一种更为圆形的图案,这就是我不确定的原因。 暗影痤疮的发生是由于浮标不准确。散射或反射时,从曲面上的某个原点沿某个方向生成新光线。根据浮动精度,原点有时会在曲面下方/上方移动。这就是为什么经常在法线方向上用一个小的ε偏移光线。 因此,新原点为:交点+交点.normal*EPSILON。你应该测试不同的epsilon值,但通常是0.01到0.02左右。 在您的代码中,您仍然使用intersectionpoint,我假设它没有偏移量。
我不确定这是否有效,因为您的结果看起来与我以前使用的阴影痤疮有点不同,但值得一试,对吗?

我放弃了Vector3,使用了一个自定义类,该类可以使用双精度而不是浮点数。这就解决了问题。谢谢@Amberleferink的帮助

class Vec
{
    public double X { get; set; }
    public double Y { get; set; }

    public double Z { get; set; }

    public Vec(double x=0, double y=0, double z=0)
    {
        X = x;
        Y = y;
        Z = z;
    }

    public static Vec Normalize(Vec vec) { return vec.GetNormal(); }

    public static Vec Cross(Vec right, Vec left) { return right.CrossWith(left); }

    public static double Dot(Vec right, Vec left) { return right.DotWith(left); }

    public static Vec Multiply(Vec right, Vec left) { return right * left; }

    public static Vec Multiply(Vec right, double left) { return right * left; }

    public static Vec Add(Vec right, Vec left) { return right + left; }

    public static Vec Subtract(Vec right, Vec left) { return right - left; }

    public static Vec operator+(Vec right, Vec left) { return new Vec(right.X + left.X, right.Y + left.Y, right.Z + left.Z); }
    public static Vec operator-(Vec right, Vec left) { return new Vec(right.X - left.X, right.Y - left.Y, right.Z - left.Z); }

    public static Vec operator *(Vec right, Vec left) { return new Vec(right.X * left.X, right.Y * left.Y, right.Z * left.Z); }

    public static Vec operator *(Vec right, double left) { return new Vec(right.X * left, right.Y * left, right.Z * left); }

    public Vec GetNormal() { return this * (1 / Math.Sqrt(X * X + Y * Y + Z * Z)); }

    public double DotWith(Vec b) { return X * b.X + Y * b.Y + Z * b.Z; }

    public Vec CrossWith(Vec b) { return new Vec(Y * b.Z - Z * b.Y, Z * b.X - X * b.Z, X * b.Y - Y * b.X); }
}

这就是我如何计算交点‘Vector3 intersectionPoint=ray.Origin+Vector3.Multiply(ray.Direction,(float)distance to intersection);intersectionPoint=intersectionPoint+Vector3.乘法(Vector3.规格化(intersectionPoint),0.00000000 1F);'我刚刚添加了第二部分,结果是same@PavelPavlov,0.00000001f太小了。尝试使用大约0.01的值进行测试。也许更大一点。浮点误差发生在大约3位小数后。所以这太精确了。我试过用0.1:0.01:0.001:很高兴听到你在@Pavel修复了它。请注意,光线追踪者不使用双打的原因是双打的性能更差,更糟糕的是,尤其是因为光线跟踪速度通常是内存绑定的(你的缓存一直是满的,这会导致从内存中检索东西的延迟时间)。使用双打可能会降低性能,但只要速度明显变慢对您来说无关紧要,您就可以:)