JRubyでJava3D その1
一先ず、java3Dのチュートリアルの「HelloJava3Da.java」を
強引にjrubyで書き直してみたが、問題なく動いている。
うい、結構使えるかも^^
でも、もう少し綺麗に書かないと、なんか気持ち悪いコードになってしまった。
後、jruby特有の記述があるみたいだけどいまいちその意味がわかんないから
ソースとサンプルで動作確認中
HelloJava3Da.rb
require 'java'include_class 'java.awt.BorderLayout'
include_class 'com.sun.j3d.utils.universe.SimpleUniverse'
include_class 'javax.media.j3d.Canvas3D'
include_class 'javax.media.j3d.BranchGroup'
include_class 'com.sun.j3d.utils.geometry.ColorCube'
include_class 'java.applet.Applet'MainFram = com.sun.j3d.utils.applet.MainFrame
applet = Applet.new()
border = BorderLayout.new()applet.setLayout(border)
config = SimpleUniverse.getPreferredConfiguration()
canvas = Canvas3D.new(config)
applet.add("Center", canvas)simpleU = SimpleUniverse.new(canvas)
simpleU.getViewingPlatform().setNominalViewingTransform()branshG = BranchGroup.new()
branshG.addChild(ColorCube.new(0.4))simpleU.addBranchGraph(branshG)
frame = MainFram.new(applet, 256, 256)
HelloJava3Db.rb
createSceneGraph関数に処理を記述するよう変更
require 'java'
include_class 'java.awt.BorderLayout'
include_class 'com.sun.j3d.utils.universe.SimpleUniverse'
include_class 'javax.media.j3d.Canvas3D'
include_class 'javax.media.j3d.BranchGroup'
include_class 'com.sun.j3d.utils.geometry.ColorCube'
include_class 'java.applet.Applet'
include_class 'javax.media.j3d.Transform3D'
include_class 'javax.media.j3d.TransformGroup'MainFram = com.sun.j3d.utils.applet.MainFrame
def createSceneGraph()
objRoot = BranchGroup.new()
rotate = Transform3D.new()
tempRotate = Transform3D.new()
rotate.rotX(Math::PI/4.0)
tempRotate.rotY(Math::PI/5.0)
rotate.mul(tempRotate)
objRotate = TransformGroup.new(rotate)objRoot.addChild(objRotate)
objRotate.addChild(ColorCube.new(0.4))
objRoot.compile();
return objRoot
endapplet = Applet.new()
border = BorderLayout.new()applet.setLayout(border)
config = SimpleUniverse.getPreferredConfiguration()
canvas = Canvas3D.new(config)
applet.add("Center", canvas)simpleU = SimpleUniverse.new(canvas)
simpleU.getViewingPlatform().setNominalViewingTransform()branshG = createSceneGraph();
simpleU.addBranchGraph(branshG)
frame = MainFram.new(applet, 256, 256)
HelloJava3Dc.rb
一先ずrotator = RotationInterpolator.new( rotationA, objSpin )でコンパイルエラー、原因不明*A*
ArgumentError: wrong # of arguments(3 for 1)
jruby-1.0無印だと問題なく実行できました。バグだったようです。
require 'java'
module Hello3D
include_package 'javax.media.j3d'
include_class 'java.awt.BorderLayout'
include_class 'com.sun.j3d.utils.universe.SimpleUniverse'
include_class 'com.sun.j3d.utils.geometry.ColorCube'
include_package 'java.applet'
def initApplet()
applet = Applet.new()
border = BorderLayout.new()
applet.setLayout(border)
config = SimpleUniverse.getPreferredConfiguration()
canvas = Canvas3D.new(config)
applet.add("Center", canvas)
simpleU = SimpleUniverse.new(canvas)
simpleU.getViewingPlatform().setNominalViewingTransform()
branshG = createSceneGraph();
simpleU.addBranchGraph(branshG)
return applet
end
def createSceneGraph()
objRoot = BranchGroup.new()
objSpin = TransformGroup.new()
objSpin.setCapability(TransformGroup::ALLOW_TRANSFORM_WRITE)
objRoot.addChild(objSpin)
objSpin.addChild(ColorCube.new(0.4))
rotationA = Alpha.new(-1, 4000)
rotator = RotationInterpolator.new( rotationA, objSpin )
bounds = BoundingSphere.new()
rotator.setSchedulingBounds(bounds)
objSpin.addChild(rotator)
return objRoot
end
endinclude Hello3D
MainFram = com.sun.j3d.utils.applet.MainFrame
frame = MainFram.new(initApplet(), 256, 256)
HelloJava3Dd.rb
HelloJava3Dc.rbと同件のエラー。
どこが間違ってるんだろう?
/jruby-0_9_9/src/builtin/javasupport/proxy/concrete.rb:6:in `new': wrong # of arguments(6 for 1) (ArgumentError)
こちらも同様です。^^;
require 'java'module Hello3D
include_package 'javax.media.j3d'
include_class 'java.awt.BorderLayout'
include_class 'com.sun.j3d.utils.universe.SimpleUniverse'
include_class 'com.sun.j3d.utils.geometry.ColorCube'
include_package 'java.applet'
def initApplet()
applet = Applet.new()
applet.setLayout(BorderLayout.new())
config = SimpleUniverse.getPreferredConfiguration()
canvas = Canvas3D.new(config)
applet.add("Center", canvas)
scene = createSceneGraph()
scene.compile()
simpleU = SimpleUniverse.new(canvas)
simpleU.getViewingPlatform().setNominalViewingTransform()
simpleU.addBranchGraph(scene)
return applet
end
def createSceneGraph()
objRoot = BranchGroup.new()
rotate = Transform3D.new()
tempRotate = Transform3D.new()
rotate.rotX(Math::PI/4.0)
tempRotate.rotY(Math::PI/5.0)
rotate.mul(tempRotate)
objRotate = TransformGroup.new(rotate);
objSpin = TransformGroup.new();
objSpin.setCapability(TransformGroup::ALLOW_TRANSFORM_WRITE)
objRoot.addChild(objRotate)
objRotate.addChild(objSpin)
objSpin.addChild(ColorCube.new(0.4))
yAxis = Transform3D.new()
rotationAlpha = Alpha.new(-1, 4000)
rotator = RotationInterpolator.new(rotationAlpha, objSpin, yAxis, 0.0, Math::PI*2.0)
bounds = BoundingSphere.new()
rotator.setSchedulingBounds(bounds)
objSpin.addChild(rotator)
return objRoot
end
endinclude Hello3D
MainFram = com.sun.j3d.utils.applet.MainFrame
frame = MainFram.new(initApplet(), 256, 256)