5
我正在用纯Python构建一个简单的Python光线追踪器(仅仅是为了它),但是我碰到了一个障碍。Python光线追踪
我的场景的设置目前是这样的:
- 摄像头位于
0, -10, 0
沿Ÿ轴指向。 - 半径球体
1
位于0, 0, 0
。 - 成像平面物距离摄像机的距离为
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)
它应该可以正常工作,但是当我渲染测试图像时,我看不到任何看起来像球体轮廓的东西:
我期待这样的事情:
有谁知道为什么我的代码不能正常工作?我一直在调整和重写这一部分的方式太长了......
哇,那很简单。谢谢! (我的输出图像看起来很像我用GIMP制作的图像)。 – Blender 2011-03-23 16:54:48
幸运猜测;-) – Alnitak 2011-03-23 17:42:39