2014/03/28(金) [n年前の日記]
#2 [ruby][dxruby] Rubyでレイトレーシングってできるのかな
気になってググってみたら、ソースを公開してくれてる方が。ありがたや。
_index ( Ray Tracing Project )
_Ray Tracing Project ( Ruby )
処理内容はさっぱり分からないけど、コピペして動かしてみたら、たしかにレイトレーシング画像が出力された。
試しに処理結果だけを DXRuby で表示するようにしてみたり。
しかし考えてみたら、GPUを使ってリアルタイムレイトレーシングもできたりするのだろうか。ググってみたら、そういうサンプルに遭遇。
_仮眠プログラマーのつぶやき : HLSLでリアルタイムレイトレーシング
_GLSLでレイトレーシング - tompngの日記
HSP + HLSL でレイトレーシングとか、GLSLでレイトレーシングとか…。今時は色々なソレでレイトレーシングができちゃうのだなとなんだか感動。どのハードも桁違いに速くなったのだなと…。
_index ( Ray Tracing Project )
_Ray Tracing Project ( Ruby )
処理内容はさっぱり分からないけど、コピペして動かしてみたら、たしかにレイトレーシング画像が出力された。
試しに処理結果だけを DXRuby で表示するようにしてみたり。
# Ray Tracing Project ( Ruby )
# http://www.not-enough.org/abe/manual/ray-project/ruby.html
require 'dxruby'
class Vector
def initialize(x, y, z)
@x = x
@y = y
@z = z
self
end
def + (v)
Vector.new(@x + v.x, @y + v.y, @z + v.z)
end
def - (v)
Vector.new(@x - v.x, @y - v.y, @z - v.z)
end
def * (t)
Vector.new(t * @x, t * @y, t * @z)
end
def normalize
d = Math.sqrt(@x * @x + @y * @y + @z * @z)
@x /= d
@y /= d
@z /= d
end
def innerproduct(v)
@x * v.x + @y * v.y + @z * v.z
end
attr_accessor :x, :y, :z
end
def make_eye(x, y)
Vector.new(-2.0 + x/50.0, 2.0 - y/50.0, 3.0)
end
def intersect(from, to, point, rad)
v = from - point
b = to.innerproduct(v)
c = v.innerproduct(v) - rad*rad
d = b*b - c
return -1 if d < 0
det = Math.sqrt(d)
t = -b + det
end
def shading(t, from, to, point, img, x, y)
l = Vector.new(0.5773, 0.5773, -0.5773)
v = from + to * t - point
v.normalize
d = l.innerproduct(v)
col = (255.0 * d).to_i + 50
col = 255 if col > 255
col = 0 if col < 0
# print col," 0 0 "
img[x, y] = [255, col, 0, 0]
end
def make_imag_ray(w, h)
img = Image.new(w, h, [0, 0, 0, 0])
rad = 1.0
to = Vector.new(0, 0, -1)
point = Vector.new(0, 0, 0)
(0...h).each do |y|
(0...w).each do |x|
from = make_eye(x, y)
t = intersect(from, to, point, rad)
if t >= 0
shading(t, from, to, point, img, x, y)
else
img[x, y] = [0, 0, 0, 0]
end
end
end
return img
end
img = make_imag_ray(200, 200)
Window.bgcolor = [64, 64, 64]
Window.loop do
break if Input.keyPush?(K_ESCAPE)
Window.draw(0, 0, img)
end
LLでも全然速いなあ…。一瞬で結果が表示される…。しかし考えてみたら、GPUを使ってリアルタイムレイトレーシングもできたりするのだろうか。ググってみたら、そういうサンプルに遭遇。
_仮眠プログラマーのつぶやき : HLSLでリアルタイムレイトレーシング
_GLSLでレイトレーシング - tompngの日記
HSP + HLSL でレイトレーシングとか、GLSLでレイトレーシングとか…。今時は色々なソレでレイトレーシングができちゃうのだなとなんだか感動。どのハードも桁違いに速くなったのだなと…。
[ ツッコむ ]
以上です。