作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用纯 Python 构建一个简单的 Python 光线追踪器(只是为了它),但我遇到了障碍。
目前我的场景设置是这样的:
0, -10, 0
,指向 y 轴。1
的球体位于 0, 0, 0
。1
,宽度和高度为 0.5
。我正在通过成像平面以均匀随机分布的方式发射光子,如果光子恰好与物体相交,我会在图像 Canvas 上绘制一个红点,对应于图像平面上光线穿过的点.
我的相交代码(我只有球体):
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)
它应该可以正常工作,但是当我渲染测试图像时,我没有得到任何看起来像球体轮廓的东西:
我期待这样的事情:
有人知道为什么我的代码不能正常运行吗?我调整和重写这一部分已经太久了......
最佳答案
您要对射线的方向矢量进行归一化吗?
关于Python 光线追踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5408669/
我正在尝试使用 Sfml 编写 2D 游戏。对于那个游戏,我需要一个 Lightengine 和一些代码,这些代码可以为我提供玩家可见的世界区域。由于这两个问题非常吻合(实际上是相同的),我想同时解决
我是一名优秀的程序员,十分优秀!