2011-03-23 4 views
5

파이썬 파이썬과 함께 간단한 파이썬 광선 추적기를 구축하고 있습니다. (단지 지옥을위한 것입니다.)하지만 나는로드 블록을 쳤습니다. Y시킴으로써 행한다 함께 가리키는 0, -10, 0에있는파이썬 레이 트레이싱

  1. 카메라 :

    내 장면의 설정은 현재이입니다.

  2. 반원형 1의 구체는 0, 0, 0에 있습니다.
  3. 이미징 비행기 물건은 카메라에서 멀리 떨어진 1의 거리이고 너비와 높이가 0.5입니다.

나는 영상면을 통해 균일하게 랜덤하게 광자를 쏘고 있으며, 광자가 물체와 교차하는 경우 이미지면의 점에 해당하는 이미지 캔버스에 빨간 점을 그립니다. 광선이지나 갔다.

내 교차로 코드 (난 단지 구체가) :

def intersection(self, ray): 
    cp = self.pos - ray.origin 
    v = cp.dot(ray.direction) 
    discriminant = self.radius**2 - cp.dot(cp) + v * v 

    if discriminant < 0: 
    return False 
    else: 
    return ray.position(v - sqrt(discriminant)) # Position of ray at time t 

그리고 내 렌더링 코드를 (이것은 광자의 특정 번호를 렌더링하지 픽셀 단위) :

def bake(self, rays): 
    self.image = Image.new('RGB', [int(self.camera.focalplane.width * 800), int(self.camera.focalplane.height * 800)]) 
    canvas = ImageDraw.Draw(self.image) 

    for i in xrange(rays): 
    x = random.uniform(-camera.focalplane.width/2.0, camera.focalplane.width/2.0) 
    z = random.uniform(-camera.focalplane.height/2.0, camera.focalplane.height/2.0) 

    ray = Ray(camera.pos, Vector(x, 1, z)) 

    for name in scene.objects.keys(): 
     result = scene.objects[name].intersection(ray) 

     if result: 
     n = Vector(0, 1, 0) 
     d = ((ray.origin - Point(self.camera.pos.x, self.camera.pos.y + self.camera.focalplane.offset, self.camera.pos.z)).dot(n))/(ray.direction.dot(n)) 
     pos = ray.position(d) 

     x = pos.x 
     y = pos.y 

     canvas.point([int(self.camera.focalplane.width * 800) * (self.camera.focalplane.width/2 + x)/self.camera.focalplane.width, 
         int(self.camera.focalplane.height * 800) * (self.camera.focalplane.height/2 + z)/self.camera.focalplane.height], 
         fill = 128) 

그것을 제대로 작동해야하지만 테스트 이미지를 렌더링 할 때 구체의 윤곽선처럼 보이지는 않습니다.

enter image description here

내 코드가 제대로 작동되지 않는 이유

enter image description here

아무도 알고 있나요 : 0

나는 이런 식으로 뭔가를 기다리고 있었다? 나는 너무 오랫동안이 부분을 조정하고 다시 작성 해왔다 ...

답변

7

광선의 방향 벡터를 정규화하고 있습니까?

+0

와우, 간단했습니다. 고맙습니다! (내 출력 이미지는 김프로 만든 이미지와 비슷합니다.) – Blender

+0

행운의 추측 ;-) – Alnitak

관련 문제