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_i
181181 end
182182
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.6
188- basis[1].x = 1.0
189- elsif n.y < 0.6 and n.y > -0.6
190- basis[1].y = 1.0
191- elsif n.z < 0.6 and n.z > -0.6
192- basis[1].z = 1.0
193- else
194- basis[1].x = 1.0
183+class Basis
184+ def initialize
185+ @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 end
189+ def b0; @b0; end
190+ def b0=(v); @b0 = v; end
191+ def b1; @b1; end
192+ def b1=(v); @b1 = v; end
193+ def b2; @b2; end
194+ def b2=(v); @b2 = v; end
196195
197- basis[0] = basis[1].vcross(basis[2])
198- basis[0] = basis[0].vnormalize
196+ 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].vnormalize
200+ if n.x < 0.6 and n.x > -0.6
201+ @b1.x = 1.0
202+ elsif n.y < 0.6 and n.y > -0.6
203+ @b1.y = 1.0
204+ elsif n.z < 0.6 and n.z > -0.6
205+ @b1.z = 1.0
206+ else
207+ @b1.x = 1.0
208+ end
209+
210+ @b0 = @b1.vcross(@b2)
211+ @b0 = @b0.vnormalize
212+
213+ @b1 = @b2.vcross(@b0)
214+ @b1 = @b1.vnormalize
215+ end
202216 end
203217
204218 class Scene
205219 def initialize
206- @spheres = Array.new
207- @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 end
212225
213226 def ambient_occlusion(isect)
214- basis = Array.new(3)
215- orthoBasis(basis, isect.n)
227+ basis = Basis.new
228+ basis.compute(isect.n)
216229
217230 ntheta = NAO_SAMPLES
218231 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].x
234- ry = x * basis[0].y + y * basis[1].y + z * basis[2].y
235- rz = x * basis[0].z + y * basis[1].z + z * basis[2].z
246+ rx = x * basis.b0.x + y * basis.b1.x + z * basis.b2.x
247+ ry = x * basis.b0.y + y * basis.b1.y + z * basis.b2.y
248+ rz = x * basis.b0.z + y * basis.b1.z + z * basis.b2.z
236249
237250 raydir = Vec.new(rx, ry, rz)
238251 ray = Ray.new(p0, raydir)
239252
240253 occisect = Isect.new
241- @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.hit
246259 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.new
280- @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.hit
285298 col = ambient_occlusion(isect)