私は純粋な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)
正しく機能するはずですが、テストイメージをレンダリングすると、球の輪郭のように見えるものは何も得られません。
私はこのようなものを期待していました:
私のコードが正しく機能しない理由を誰かが知っていますか?私はこの部分をあまりにも長い間微調整して書き直してきました...