Commit 0086b907
0086b90789bfMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix: ao_render works correctly by avoiding object-in-IntArray pattern
Replace Array-of-objects (spheres[], basis[]) with individual instance variables (@s0/@s1/@s2, Basis class). Objects stored in IntArray as mrb_int pointers were invisible to GC and got collected during render.
Both Spinel and CRuby now produce valid rendered images. Pixel values differ slightly due to float precision in Math.sqrt/cos/sin (6696 of 12288 bytes differ by 1-2 levels).
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 0086b90789bf
- Parent
- 3a83875d6d13
test/bm_ao_render.rbmodifiedView file
@@ -180,39 +180,52 @@
180180
i.to_i181181
end182182
183-
def orthoBasis(basis, n)184-
basis[2] = Vec.new(n.x, n.y, n.z)185-
basis[1] = Vec.new(0.0, 0.0, 0.0)186-
187-
if n.x < 0.6 and n.x > -0.6188-
basis[1].x = 1.0189-
elsif n.y < 0.6 and n.y > -0.6190-
basis[1].y = 1.0191-
elsif n.z < 0.6 and n.z > -0.6192-
basis[1].z = 1.0193-
else194-
basis[1].x = 1.0183+
class Basis184+
def initialize185+
@b0 = Vec.new(0.0, 0.0, 0.0)186+
@b1 = Vec.new(0.0, 0.0, 0.0)187+
@b2 = Vec.new(0.0, 0.0, 0.0)195188
end189+
def b0; @b0; end190+
def b0=(v); @b0 = v; end191+
def b1; @b1; end192+
def b1=(v); @b1 = v; end193+
def b2; @b2; end194+
def b2=(v); @b2 = v; end196195
197-
basis[0] = basis[1].vcross(basis[2])198-
basis[0] = basis[0].vnormalize196+
def compute(n)197+
@b2 = Vec.new(n.x, n.y, n.z)198+
@b1 = Vec.new(0.0, 0.0, 0.0)199199
200-
basis[1] = basis[2].vcross(basis[0])201-
basis[1] = basis[1].vnormalize200+
if n.x < 0.6 and n.x > -0.6201+
@b1.x = 1.0202+
elsif n.y < 0.6 and n.y > -0.6203+
@b1.y = 1.0204+
elsif n.z < 0.6 and n.z > -0.6205+
@b1.z = 1.0206+
else207+
@b1.x = 1.0208+
end209+
210+
@b0 = @b1.vcross(@b2)211+
@b0 = @b0.vnormalize212+
213+
@b1 = @b2.vcross(@b0)214+
@b1 = @b1.vnormalize215+
end202216
end203217
204218
class Scene205219
def initialize206-
@spheres = Array.new207-
@spheres[0] = Sphere.new(Vec.new(-2.0, 0.0, -3.5), 0.5)208-
@spheres[1] = Sphere.new(Vec.new(-0.5, 0.0, -3.0), 0.5)209-
@spheres[2] = Sphere.new(Vec.new(1.0, 0.0, -2.2), 0.5)220+
@s0 = Sphere.new(Vec.new(-2.0, 0.0, -3.5), 0.5)221+
@s1 = Sphere.new(Vec.new(-0.5, 0.0, -3.0), 0.5)222+
@s2 = Sphere.new(Vec.new(1.0, 0.0, -2.2), 0.5)210223
@plane = Plane.new(Vec.new(0.0, -0.5, 0.0), Vec.new(0.0, 1.0, 0.0))211224
end212225
213226
def ambient_occlusion(isect)214-
basis = Array.new(3)215-
orthoBasis(basis, isect.n)227+
basis = Basis.new228+
basis.compute(isect.n)216229
217230
ntheta = NAO_SAMPLES218231
nphi = NAO_SAMPLES@@ -230,17 +243,17 @@
230243
y = Math.sin(phi) * Math.sqrt(1.0 - r)231244
z = Math.sqrt(r)232245
233-
rx = x * basis[0].x + y * basis[1].x + z * basis[2].x234-
ry = x * basis[0].y + y * basis[1].y + z * basis[2].y235-
rz = x * basis[0].z + y * basis[1].z + z * basis[2].z246+
rx = x * basis.b0.x + y * basis.b1.x + z * basis.b2.x247+
ry = x * basis.b0.y + y * basis.b1.y + z * basis.b2.y248+
rz = x * basis.b0.z + y * basis.b1.z + z * basis.b2.z236249
237250
raydir = Vec.new(rx, ry, rz)238251
ray = Ray.new(p0, raydir)239252
240253
occisect = Isect.new241-
@spheres[0].intersect(ray, occisect)242-
@spheres[1].intersect(ray, occisect)243-
@spheres[2].intersect(ray, occisect)254+
@s0.intersect(ray, occisect)255+
@s1.intersect(ray, occisect)256+
@s2.intersect(ray, occisect)244257
@plane.intersect(ray, occisect)245258
if occisect.hit246259
occlusion = occlusion + 1.0@@ -277,9 +290,9 @@
277290
ray = Ray.new(Vec.new(0.0, 0.0, 0.0), eye)278291
279292
isect = Isect.new280-
@spheres[0].intersect(ray, isect)281-
@spheres[1].intersect(ray, isect)282-
@spheres[2].intersect(ray, isect)293+
@s0.intersect(ray, isect)294+
@s1.intersect(ray, isect)295+
@s2.intersect(ray, isect)283296
@plane.intersect(ray, isect)284297
if isect.hit285298
col = ambient_occlusion(isect)