📄 geometryinfo.java
字号:
/* * $RCSfile: GeometryInfo.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.4 $ * $Date: 2007/02/09 17:20:19 $ * $State: Exp $ */package com.sun.j3d.utils.geometry;import com.sun.j3d.utils.geometry.Triangulator;import java.io.*;import javax.media.j3d.*;import javax.vecmath.*;import com.sun.j3d.internal.J3dUtilsI18N;import java.util.HashMap;import com.sun.j3d.utils.geometry.GeometryInfoGenerator;import com.sun.j3d.internal.BufferWrapper;import com.sun.j3d.internal.ByteBufferWrapper;import com.sun.j3d.internal.FloatBufferWrapper;import com.sun.j3d.internal.DoubleBufferWrapper;import com.sun.j3d.internal.ByteOrderWrapper;import javax.media.j3d.J3DBuffer;/** * The GeometryInfo object holds data for processing by the Java3D geometry * utility tools.<p><blockquote> * * The NormalGenerator adds normals to geometry without normals.<p> * * The Stripifier combines adjacent triangles into triangle strips for * more efficent rendering.<p></blockquote> * * Also, the GeometryCompressor can take a set of GeometryInfo objects in a * CompressionSteam and generate a CompressedGeometry object from the * geometry.<p> * Geometry is loaded into a GeometryInfo in a manner similar to the * <a href="../../../../../javax/media/j3d/GeometryArray.html"> * GeometryArray</a> methods. The constructor for the GeometryInfo takes a flag * that specifies the kind of data being loaded. The vertex data is * specified using methods that are similar to the GeometryArray methods, but * with fewer variations.<p> * The major difference between GeometryInfo and GeometryArray is * that the number of vertices, vertex format, and other data are specified * implictly, rather than as part of the constructor. The number of verticies * comes from the number of coordinates passed to the setCoordinates() * method. The format comes from the set of data components that are * specified. For example, calling the setCoordinates(), setColors3() and * setTextureCoordinatesParames(1, 2) methods implies a * format of COORDINATES | COLOR_3 * | TEXTURE_COORDINATE_2. Indexed representation is specified by calling * the methods that specify the indices, for example * setCoordinateIndices().<p> * Stripped primitives are loaded using the TRIANGLE_FAN_ARRAY or * TRIANGLE_STRIP_ARRAY flags to the constructor. The setStripCounts() * method specifies the length of each strip.<p> * A set of complex polygons is loaded using the POLYGON_ARRAY * flag to the constructor. The setStripCounts() method specifies the length * of each contour of the polygons. The setContourCounts() method specifies * the number of countours in each polygon. For example, a triangle with a * triangular hole would have strip counts [3, 3] (indicating two contours of * three points) and contour counts [2] (indicating a single polygon with two * contours).<p> * GeometryInfo itelf contains some simple utilities, such as * calculating indices for non-indexed data ("indexifying") and getting rid * of unused data in your indexed geometry ("compacting").<p> * The geometry utility tools modify the contents of the * GeometryInfo. After processing, the resulting geometry can be extracted * from the GeometryInfo by calling getGeometryArray(). If multiple tools * are used, the order of processing should be: generate normals, then * stripify. For example, to convert a general mesh of polygons without * normals into an optimized mesh call: * <pre><blockquote> * GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY); * // initialize the geometry info here * // generate normals * NormalGenerator ng = new NormalGenerator(); * ng.generateNormals(gi); * // stripify * Stripifier st = new Stripifier(); * st.stripify(gi); * GeometryArray result = gi.getGeometryArray(); * </blockquote></pre> * * @see NormalGenerator * @see Stripifier * @see com.sun.j3d.utils.compression.CompressionStream * @see com.sun.j3d.utils.compression.GeometryCompressor * @see javax.media.j3d.GeometryArray */public class GeometryInfo { /** * Send to the constructor to inform that the data will be arranged so * that each set of three vertices form an independent triangle */ public static final int TRIANGLE_ARRAY = 1; /** * Send to the constructor to inform that the data will be arranged so * that each set of four vertices form an independent quad */ public static final int QUAD_ARRAY = 2; /** * Send to the constructor to inform that the data will be arranged so * that the stripCounts array indicates how many vertices to use * for each triangle fan. */ public static final int TRIANGLE_FAN_ARRAY = 3; /** * Send to the constructor to inform that the data will be arranged so * that the stripCounts array indicates how many vertices to use * for each triangle strip. */ public static final int TRIANGLE_STRIP_ARRAY = 4; /** * Send to the constructor to inform that the data is arranged as * possibly multi-contour, possible non-planar polygons. * The stripCounts array indicates how many vertices to use * for each contour, and the contourCounts array indicates how many * stripCounts entries to use for each polygon. The first * contour is the bounding polygon, and subsequent contours are * "holes." If contourCounts is left null, the default is * one contour per polygon. */ public static final int POLYGON_ARRAY = 5; private int prim; // 1 Show indexification details private static final int DEBUG = 0; private Point3f coordinates[] = null; private Color3f colors3[] = null; private Color4f colors4[] = null; private Vector3f normals[] = null; private Object texCoordSets[][] = null; private int coordinateIndices[] = null; private int colorIndices[] = null; private int normalIndices[] = null; private int texCoordIndexSets[][] = null; private int[] texCoordSetMap = null; private int texCoordSetCount = 0; private int texCoordDim = 0; private int stripCounts[] = null; private int contourCounts[] = null; private Triangulator tr = null; private NormalGenerator ng = null; private int oldPrim = 0; private int oldStripCounts[] = null; private boolean coordOnly = false; /** * Constructor. * Creates an empty GeometryInfo object. * @param primitive Tells the GeometryInfo object the type of * primitive data to be stored * in it, so it will know the format of the data. It can be one of * TRIANGLE_ARRAY, * QUAD_ARRAY, TRIANGLE_FAN_ARRAY, TRIANGLE_STRIP_ARRAY, or POLYGON_ARRAY. */ public GeometryInfo(int primitive) { if ((primitive >= TRIANGLE_ARRAY) && (primitive <= POLYGON_ARRAY)) { prim = primitive; } else { throw new IllegalArgumentException( J3dUtilsI18N.getString("GeometryInfo0")); } } // End of GeometryInfo(int) /** * Contructor. Populates the GeometryInfo with the geometry from * the GeometryArray.<p> * If the GeometryArray uses the <code>Initial</code> and * <code>Valid</code> GeometryArray methods (<code> * setInitialVertexIndex()</code> and <code>setValidVertexCount() * </code> and their cousins) then only the needed geometry * is copied into the GeometryInfo. */ public GeometryInfo(GeometryArray ga) { GeometryInfoGenerator.create(this, ga); } // End of GeometryInfo(GeometryArray) /** * Removes all data from the GeometryInfo and resets the primitive. * After a call to reset(), the GeometryInfo object will be just like * it was when it was newly constructed. * @param primitive Either TRIANGLE_ARRAY, QUAD_ARRAY, * TRIANGLE_FAN_ARRAY, TRIANGLE_STRIP_ARRAY, or POLYGON_ARRAY. * Tells the GeometryInfo object the type of primitive data to be stored * in it, so it will know the format of the data. */ public void reset(int primitive) { if ((primitive >= TRIANGLE_ARRAY) && (primitive <= POLYGON_ARRAY)) { prim = primitive; } else { throw new IllegalArgumentException( J3dUtilsI18N.getString("GeometryInfo0")); } coordinates = null; colors3 = null; colors4 = null; normals = null; coordinateIndices = null; colorIndices = null; normalIndices = null; stripCounts = null; contourCounts = null; oldPrim = 0; oldStripCounts = null; texCoordDim = 0; texCoordSetCount = 0; texCoordSets = null; texCoordIndexSets = null; texCoordSetMap = null; coordOnly = false; } // End of reset(int) /** * Removes all data from this GeometryInfo and populates it with * the geometry from the GeometryArray. */ public void reset(GeometryArray ga) { GeometryInfoGenerator.create(this, ga); } // End of reset(GeometryArray) // This method takes an indexed quad array and expands it to // a list of indexed triangles. It is used for the Coordinate // indices as well as the color and texture indices. private int[] expandQuad(int indices[]) { int triangles[] = new int[indices.length / 4 * 6]; for (int i = 0 ; i < indices.length / 4 ; i++ ) { triangles[i * 6 + 0] = indices[i * 4]; triangles[i * 6 + 1] = indices[i * 4 + 1]; triangles[i * 6 + 2] = indices[i * 4 + 2]; triangles[i * 6 + 3] = indices[i * 4]; triangles[i * 6 + 4] = indices[i * 4 + 2]; triangles[i * 6 + 5] = indices[i * 4 + 3]; } return triangles; } // End of expandQuad // This method takes an indexed triangle fan and expands it to // a list of indexed triangles. It is used for the Coordinate // indices as well as the color and texture indices. private int[] expandTriFan(int numTris, int indices[]) { int triangles[] = new int[numTris * 3]; int p = 0; int base = 0; for (int f = 0 ; f < stripCounts.length ; f++) { for (int t = 0 ; t < stripCounts[f] - 2 ; t++) { triangles[p++] = indices[base]; triangles[p++] = indices[base + t + 1]; triangles[p++] = indices[base + t + 2]; } base += stripCounts[f]; } return triangles; } // End of expandTriFan // This method takes an indexed triangle strip and expands it to // a list of indexed triangles. It is used for the Coordinate // indices as well as the color and texture indices. private int[] expandTriStrip(int numTris, int indices[]) { int triangles[] = new int[numTris * 3]; int p = 0; int base = 0; for (int s = 0 ; s < stripCounts.length ; s++) { for (int t = 0 ; t < stripCounts[s] - 2 ; t++) { // Use a ping-ponging algorithm to reverse order on every other // triangle to preserve winding if (t % 2 == 0) { triangles[p++] = indices[base + t + 0]; triangles[p++] = indices[base + t + 1]; triangles[p++] = indices[base + t + 2]; } else { triangles[p++] = indices[base + t + 0]; triangles[p++] = indices[base + t + 2]; triangles[p++] = indices[base + t + 1]; } } base += stripCounts[s]; } return triangles; } // End of expandTriStrip // Used by the NormalGenerator utility. Informs the GeometryInfo object // to remember its current primitive and stripCounts arrays so that // they can be used to convert the object back to its original // primitive void rememberOldPrim() { oldPrim = prim; oldStripCounts = stripCounts; } // End of rememberOldPrim // The NormalGenerator needs to know the original primitive for // facet normal generation for quads int getOldPrim() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -