2013-03-27 5 views
0

저는 컴퓨터 그래픽 클래스를위한 광선 추적 프로그램을 작성하고 있습니다. 지금까지는 구체와 그림자 광선 만 구현했습니다. 현재 문제는 내가 중심에서 내 구체를 움직일 때 뻗는다는 것입니다.중앙에서 벗어날 때 광선 추적 객체가 늘어납니다.

for(int i = -cam.width/2; i &lt cam.width/2; i++) 
    { 
     for(int j = -cam.height/2; j &lt cam.height/2; j++) 
     { 
      float normi = (float)i; 
      float normj = (float)j; 
      Vector pixlePos = cam.right*normi + cam.up*normj + cam.forward*cam.dist + cam.pos*1; 
      Vector direction = pixlePos + cam.pos*-1; 
      direction.normalize(); 
      Vector colour = recursiveRayTrace(Ray(pixlePos, direction), 30, 1, 0); 
      float red = colour.getX()/255; 
      float green = colour.getY()/255; 
      float blue = colour.getZ()/255; 
      fwrite (&red, sizeof(float), 1, myFile); 
      fwrite (&green, sizeof(float), 1, myFile); 
      fwrite (&blue, sizeof(float), 1, myFile); 
     } 
    } 

recursiveRayTrace :

Vector Scene::recursiveRayTrace(Ray r, float maxDist, int maxBounces, int bounces) 
{ 
    if(maxBounces &lt bounces) 
     return Vector(0,0,0); 
    int count = 0; 
    for(int i = 0; i &lt spheres.size(); i++) 
    { 
     if(spheres.at(i).onSphere(r)) 
     { 
      Vector colour(ambiant.colour); 
      for(int j = 0; j &lt lights.size(); j++) 
      { 
       Vector intersection(r.pos + r.dir*spheres.at(i).getT(r)); 
       Ray nRay(intersection, lights.at(i).centre + intersection*-1); 
       colour = colour + lights.at(i).colour; 
     } 
     return colour; 
     } 
    } 
    return Vector(0,0,0); 
} 
여기
bool Sphere::onSphere(Ray r) 
{ 
    float b = (r.dir*2).innerProduct(r.pos + centre*-1); 
    float c = (r.pos + centre*-1).innerProduct(r.pos + centre*-1) - radius*radius; 
    return b*b - 4*c >= 0; 
} 

내가 각 레이를 생성하는 데 사용하는 코드입니다 : 여기에 레이 구를 교차하면 내가 계산하는 데 사용하는 코드는

내가 얻는 것은 중심에서 원의 중심까지 벡터의 방향으로 뻗어있는 구입니다. 나는 숙제를 할 사람을 찾고 있지 않습니다. 이 문제를 디버깅하는 데 정말로 힘든 시간을 보내고 있습니다. 모든 힌트를 주셔서 감사합니다 :) 감사합니다!

편집 : cam.dist은 실제로 관점보기의 자연스러운 결과이다 스트레칭 뷰 평면

답변

1

카메라의 거리이며보기의 매우 넓은 필드가있는 경우는 과장이다. 즉, 카메라를 이미지면에서 뒤로 움직이면 더 자연스러워 보일 것입니다.