import com.ardor3d.image.Image; import com.ardor3d.image.ImageDataFormat; import com.ardor3d.image.PixelDataType; import com.ardor3d.renderer.Camera; import com.ardor3d.renderer.IndexMode; import com.ardor3d.renderer.state.MaterialState; import com.ardor3d.renderer.state.OffsetState; import com.ardor3d.renderer.state.RenderState; import com.ardor3d.renderer.state.TextureState; import com.ardor3d.scenegraph.Mesh; import com.ardor3d.scenegraph.MeshData; import com.ardor3d.scenegraph.Node; import com.ardor3d.scenegraph.Spatial; import com.ardor3d.util.TextureManager; import com.ardor3d.util.geom.BufferUtils; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Window; import java.nio.ByteBuffer; import javax.swing.JFrame; /** DisplayImplJ3D is the VisAD class for displays that use Java 3D. It is runnable.

DisplayImplJ3D is not Serializable and should not be copied between JVMs.

*/ public class DisplayA3D { /** distance behind for surfaces in 2-D mode */ // WLH 25 March 2003 (at BOM) // public static final float BACK2D = -2.0f; public static final float BACK2D = -0.01f; /** * Use a parallel projection view * @see GraphicsModeControlJ3D#setProjectionPolicy */ public static final int PARALLEL_PROJECTION = Camera.ProjectionMode.Parallel.ordinal(); /** * Use a perspective projection view. This is the default. * @see GraphicsModeControlJ3D#setProjectionPolicy */ public static final int PERSPECTIVE_PROJECTION = Camera.ProjectionMode.Parallel.ordinal(); /** Render polygonal primitives by filling the interior of the polygon @see GraphicsModeControlJ3D#setPolygonMode */ public static final int POLYGON_FILL = 0; /** * Render polygonal primitives as lines drawn between consecutive vertices * of the polygon. * @see GraphicsModeControlJ3D#setPolygonMode */ public static final int POLYGON_LINE = 1; /** * Render polygonal primitives as points drawn at the vertices of * the polygon. * @see GraphicsModeControlJ3D#setPolygonMode */ public static final int POLYGON_POINT = 2; /** * Transparency attributes. */ public static final int SCREEN_DOOR = 0; public static final int BLENDED = 1; public static final int NONE = 2; public static final int FASTEST = 3; public static final int NICEST = 4; /** Field for specifying unknown API type */ public static final int UNKNOWN = 0; /** Field for specifying that the DisplayImpl be created in a JPanel */ public static final int JPANEL = 1; /** Field for specifying that the DisplayImpl does not have a screen Component */ public static final int OFFSCREEN = 2; /** Field for specifying that the DisplayImpl transforms but does not render */ public static final int TRANSFORM_ONLY = 4; /** Ardor3D Canvas type */ public static final int JOGL_SWING = 5; public static final int JOGL_AWT = 6; /** AWT-SWING Tab */ public static boolean isTab = false; /** * Property name for setting whether to use geometry by reference. * @see #GEOMETRY_BY_REF */ public static final String PROP_GEOMETRY_BY_REF = "visad.java3d.geometryByRef"; /** * Indicates whether to use geometry by reference when creating geometry arrays. * @see javax.media.j3d.GeometryArray#BY_REFERENCE */ public static final boolean GEOMETRY_BY_REF; static { GEOMETRY_BY_REF = Boolean.parseBoolean(System.getProperty(PROP_GEOMETRY_BY_REF, "true")); } /** * Property name for enabling the use of non-power of two textures. * @see #TEXTURE_NPOT */ public static final String PROP_TEXTURE_NPOT = "visad.java3d.textureNpot"; /** * Indicates whether to allow non-power of two textures. This has been known * to cause some issues with Apple 32bit Macs eventhough the Canvas3D * properties indicate that NPOT is supported. * @see javax.media.j3d.Canvas3D#queryProperties() */ // FIXME: // This works with the Java3D 1.5.2 example TextureImageNPOT but does not work // with the VisAD library image rednering. On initial testing it behaves as if // there may be threading issues. This requires more investigation before we // can enable this based on the Canvas3D properties. public static final boolean TEXTURE_NPOT; static { TEXTURE_NPOT = Boolean.parseBoolean(System.getProperty(PROP_TEXTURE_NPOT, "true")); //System.err.println("TEXTURE_NPOT:"+TEXTURE_NPOT); } private int apiValue = UNKNOWN; private SceneA3D scene = null; public DisplayA3D(String name, Window window, Container comp, int api) throws Exception { this(name, null, window, comp, comp.getWidth(), comp.getHeight(), api); } public DisplayA3D(String name, DisplayRendererA3D dspRenderer, Window window, Container comp, int api) throws Exception { this(name, dspRenderer, window, comp, comp.getWidth(), comp.getHeight(), api); } public DisplayA3D(String name, Window window, Container comp, int width, int height, int api) throws Exception { this(name, null, window, comp, width, height, api); } public DisplayA3D(String name, DisplayRendererA3D renderer, Window window, Container comp, int width, int height, int api) throws Exception { /* TODO: make sure comp is an ancestor of window */ if (!window.isShowing()) { throw new Exception("Containing window must exist on screen. For example JFrame.setVisible(true)"); } initialize(comp, width, height, api); } private void initialize(Container comp, int width, int height, int api) { scene = new DefaultSceneA3D(); ManagerA3D manager = new ManagerA3D(comp, new Dimension(width, height), scene, api); } public void addImage(Object node, int width, int height, float z, byte[] rgb) { byte[][] imageColors = new byte[3][width*height]; java.util.Arrays.fill(imageColors[0], rgb[0]); java.util.Arrays.fill(imageColors[1], rgb[1]); java.util.Arrays.fill(imageColors[2], rgb[2]); Object img = createImage(width, height, width, height, imageColors); Spatial geom = new Mesh(); MeshData meshData = new MeshData(); meshData.setIndexMode(IndexMode.Quads); float[] coordinates = new float[12]; coordinates[0] = -1f; coordinates[1] = -1f; coordinates[2] = z; coordinates[3] = 1f; coordinates[4] = -1f; coordinates[5] = z; coordinates[6] = 1f; coordinates[7] = 1f; coordinates[8] = z; coordinates[9] = -1f; coordinates[10] = 1f; coordinates[11] = z; basicGeometry(coordinates, null, null, null, meshData, false); ((Mesh)geom).setMeshData(meshData); textureToGroup(node, img, geom, false); } public Object createImage(int data_width, int data_height, int textureWidth, int textureHeight, byte[][] color_values) { int clrDim = color_values.length; byte[] byteData = new byte[clrDim*textureWidth*textureHeight]; ImageDataFormat imgFrmt = null; if (clrDim == 4) { imgFrmt = ImageDataFormat.BGRA; int k = 0; int m = 0; byte r, g, b, a; for (int j=0; j