记得之前已经发布过,没找到,于是就发一个。
不同平台需要对应的本地编译库。按照六石风格,已经贴心的提供了下载:
https://download.csdn.net/download/quantum7/86952154
package taishan; import java.awt.GraphicsConfiguration; import java.awt.image.BufferedImage; import java.util.Enumeration; import javax.media.j3d.Alpha; import javax.media.j3d.Appearance; import javax.media.j3d.BoundingSphere; import javax.media.j3d.BranchGroup; import javax.media.j3d.Canvas3D; import javax.media.j3d.Interpolator; import javax.media.j3d.PolygonAttributes; import javax.media.j3d.QuadArray; import javax.media.j3d.Shape3D; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.media.j3d.WakeupCriterion; import javax.media.j3d.WakeupOnElapsedFrames; import javax.swing.JFrame; import javax.swing.JPanel; import javax.vecmath.Point3d; import com.sun.j3d.utils.image.TextureLoader; import com.sun.j3d.utils.universe.SimpleUniverse; @SuppressWarnings("serial") public class Java3DTest extends TsFrame { Java3DTest() { setTitle("gl"); setBounds(0, 0, 500, 500); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); add(panel); } private void action3d() { try { GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas3d = new Canvas3D(config); canvas3d.setBounds(0, 0, 500, 500); add(canvas3d); SimpleUniverse u = new SimpleUniverse(canvas3d); u.addBranchGraph(createSence()); u.getViewingPlatform().setNominalViewingTransform(); } catch (Throwable e) { e.printStackTrace(); } } private BranchGroup createSence() { BranchGroup sence = new BranchGroup(); final TransformGroup tg = new TransformGroup(); tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); final Alpha alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0); final float[] verts = { -1, 1, 0, -1, -1, 0, 1, -1, 0, 1, 1, 0 }; final float[] src = { -1, 1, 0, -1, -1, 0, 1, -1, 0, 1, 1, 0 }; final float[] cs = new float[3]; Interpolator interpolator = new Interpolator() { private WakeupCriterion passiveWakeupCriterion = new WakeupOnElapsedFrames(0, true); private Transform3D transform3d = new Transform3D(); @Override public void processStimulus(@SuppressWarnings("rawtypes") Enumeration criteria) { float alphaValue = alpha.value(); double val = (1.0 - alphaValue) * 0 + alphaValue * Math.PI*2; for(int i=0;i<4;i++) { System.arraycopy(src, i*3, cs, 0, 3); float[] ps = MatrixKit.roteY(cs , val); System.arraycopy(ps, 0, verts, i*3, 3); } wakeupOn(passiveWakeupCriterion); tg.setTransform(transform3d); } }; interpolator.setSchedulingBounds(new BoundingSphere(new Point3d(0, 0, 0), 100f)); tg.addChild(interpolator); sence.addChild(tg); QuadArray arr = new QuadArray(4, QuadArray.COORDINATES|QuadArray.BY_REFERENCE|QuadArray.TEXTURE_COORDINATE_2); arr.setCapability(QuadArray.ALLOW_COORDINATE_WRITE); arr.setTexCoordRefFloat(0, new float[] {0, 0, 1, 0, 1, 1, 1, 0}); arr.setCoordRefFloat(verts); Shape3D shape = new Shape3D(arr); Appearance app = createTextureApp(); shape.setAppearance(app); tg.addChild(shape); return sence; } private Appearance createTextureApp() { Appearance app = new Appearance(); app.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE); BufferedImage image = getImage(); app.setTexture(new TextureLoader(image, TextureLoader.ALLOW_NON_POWER_OF_TWO|TextureLoader.BY_REFERENCE).getTexture()); //for loop. PolygonAttributes polygonAttributes = new PolygonAttributes(); polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE); polygonAttributes.setCapability(PolygonAttributes.ALLOW_CULL_FACE_WRITE); app.setPolygonAttributes(polygonAttributes); return app; } public static void main(String[] args) { Java3DTest test = new Java3DTest(); test.action3d(); test.setVisible(true); } }