光线跟踪照明不起作用

时间:2015-09-26 19:56:07

标签: java 3d raytracing lighting

我正在写一个光线追踪器,我的灯光给我带来了问题。我的球体位于0,0,0(x,y,z),我的相机位于0,0,30,我的光线为0,0 ,问题是光线上方的一切都在阴影中,我无法弄清楚为什么。在Light的位置下方的所有东西都很好。

Lighting Bug

这是normals.Normal direction.x * 255,direction.y * 255,direction.z * 255 rgb。

Lightng Bug Normals

以下是我的灯光计算的代码。

public Color Cal_Light2(Color color, Normal normal) {
        // Color Maths Stuff
        ArrayList<Color> PixelShade = new ArrayList<Color>();
        Color Final = new Color();

        // Calculate Lighting For Pixel
        for (int l = 0; l < LightObjects.size(); l++) {

            // Calculate Direction To Each Light(Shadow Ray)
            LightObject light = LightObjects.get(l);
            Vector3D r_Ori = normal.Origin.Add(normal.Direction.Mul(1E-10));
            Vector3D r_Dir = light.Pos.Sub(r_Ori);
            r_Dir.normalize();
            Ray raytolight = new Ray(r_Ori, r_Dir);
            int WAS_HIT = 0;

            // Checks If In Light Is Blocked(Shadows)
            for (int o = 0; o < GeoObjects.size(); o++) {
                GeometricObject NGO = GeoObjects.get(o);
                double hit = NGO.hit(raytolight);
                if (hit != 0.0) {
                    WAS_HIT = 1;
                }
            }

            // Light Shades Pixel With Given Info
            PixelShade.add(light.ShadePixel(WAS_HIT, normal, r_Dir, color, AmbientLight, DiffuseLight));
        }

        // Color Averaging
        for (int s = 0; s < PixelShade.size(); s++) {
            Final.Add(PixelShade.get(s));
        }
        Final.Divide(PixelShade.size());

        // Final
        return Final;
    }


  Here is the light shade pixel.

public Maths.Color ShadePixel(int WAS_HIT, Normal normal, Vector3D r_Dir,  Color color, double AmbientLight, double DiffuseLight) {
    if(WAS_HIT == 0){
        double Dot = normal.Direction.Dot(r_Dir);
         Color Color = new Color(color);
         double Shade = AmbientLight + DiffuseLight*Dot;
         Color.Mul(Shade);
         double lightIntesity = Dot*Intensity;
         Color lightColor = new Color(color);
         lightColor.Mul(lightIntesity);
         Color.Add(lightColor);
         Color.Divide(2);
        return Color;
    }else{
         Color Color = new Color(color);
         double Shade = AmbientLight;
         Color.Mul(Shade);
         Color.Divide(2);
         return Color;
    }
}

我在调试中运行它并且它说WAS_HIT = 1.所以它说它正在点什么,但我不知道是什么。

如果您需要我分享更多代码,请询问。

1 个答案:

答案 0 :(得分:0)

已修复!我的飞机交叉点计算不正确所以我删除了飞机并且它有效!