200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > python渲染光线_Python光线追踪

python渲染光线_Python光线追踪

时间:2018-06-27 04:53:18

相关推荐

python渲染光线_Python光线追踪

我正在用纯Python构建一个简单的Python光线追踪器(仅仅是为了它),但我遇到了障碍。

我的场景的设置目前是这样的:

相机位于沿Y轴指向的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)它应该正常工作,但是当我渲染测试图像时,我没有看到任何看起来像球体轮廓的东西:

我期待着这样的事情:

有人知道我的代码为什么不能正常工作吗?我一直在调整和重写这一部分的时间太长了......

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。