2015-09-26 69 views
0

我正在写光线跟踪器,我的灯光给我带来了问题。我的球体在0,0,0(x,y,z),我的相机在0,0,30和我的将它点亮0,0,40。问题在于光线之上的所有东西都在阴影中,我无法弄清楚为什么。光线之下的所有东西都很好。光线跟踪照明不工作

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.So它说,这是打的东西,但我不知道是什么。

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

回答

0

固定!我的平面交叉点计算不正确,所以我删除了飞机,它的工作!