Archive for 12月 6th, 2012
-
Hello, Java 3D(Clojure) World!
Posted on 12月 6th, 2012 by cx20
Java 3D
Java 3D は Java による 3D グラフィックライブラリである。
以下は Clojure による Java 3D の使用例となっている。ソースコード
(import (java.awt BorderLayout) (java.awt GraphicsConfiguration) (java.awt Font) (javax.swing JFrame) (javax.swing JPanel) (javax.media.j3d BranchGroup) (javax.media.j3d Canvas3D) (javax.media.j3d Transform3D) (javax.media.j3d Appearance) (javax.media.j3d Material) (javax.media.j3d Font3D) (javax.media.j3d FontExtrusion) (javax.media.j3d Text3D) (javax.media.j3d Shape3D) (javax.media.j3d DirectionalLight) (javax.media.j3d TransformGroup) (javax.media.j3d RotationInterpolator) (javax.media.j3d Alpha) (javax.media.j3d BoundingSphere) (javax.vecmath Vector3f) (javax.vecmath Color3f) (com.sun.j3d.utils.universe SimpleUniverse) (com.sun.j3d.utils.universe ViewingPlatform) (com.sun.j3d.utils.geometry ColorCube)) (def frame (JFrame.)) (defn create-text-shape [] (let [ textAppear (Appearance.) font (Font. "MS Pゴシック" Font/PLAIN 1 ) fontEx (FontExtrusion.) font3D (Font3D. font fontEx) textGeom (Text3D. font3D "Hello, Java 3D World!") textShape (Shape3D.)] (.setMaterial textAppear (Material.)) (.setAlignment textGeom Text3D/ALIGN_CENTER) (.setGeometry textShape textGeom) (.setAppearance textShape textAppear) textShape)) (defn move-text-back [] (let [ transform3D (Transform3D.) vector3f (Vector3f. 0.0 0.0 -10.0)] (.setTranslation transform3D vector3f) (TransformGroup. transform3D))) (defn create-spinner [] (let [ spinner (TransformGroup.)] (.setCapability spinner (TransformGroup/ALLOW_TRANSFORM_WRITE)) spinner)) (defn set-lighting [objMove] (let [ light (DirectionalLight.)] (.setInfluencingBounds light (BoundingSphere.)) (.setDirection light (Vector3f. 0.0 0.0 -1.0)) (.setColor light (Color3f. 1.0 1.0 1.0)) (.addChild objMove light))) (defn create-scene-graph [] (let [ objRoot (BranchGroup.) mover (move-text-back) spinner (create-spinner) rotator (RotationInterpolator. (Alpha. -1 4000), spinner, (Transform3D.) 0.0 (* (Math/PI) 2))] (.addChild spinner (create-text-shape)) (.addChild mover spinner) (.addChild objRoot mover) (.setSchedulingBounds rotator (BoundingSphere.)) (.addChild spinner rotator) (set-lighting mover) objRoot)) (def canvas (Canvas3D. (SimpleUniverse/getPreferredConfiguration))) (def universe (SimpleUniverse. canvas)) (doto universe (.addBranchGraph (create-scene-graph))) (.setNominalViewingTransform (.getViewingPlatform universe)) (.add (.getContentPane frame) canvas) (doto frame (.setDefaultCloseOperation javax.swing.WindowConstants/EXIT_ON_CLOSE) (.setSize 640 480) (.setTitle "Hello, World") (.setVisible true))
実行方法
C:¥> clj Hello.clj
実行結果
+------------------------------------------+ |Hello, World! [_][~][X]| +------------------------------------------+ |Hello, Java 3D World! | | | | | | | | | | | | | | | | | | | +------------------------------------------+