📄 compressionstream.java
字号:
/* * $RCSfile: CompressionStream.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.3 $ * $Date: 2007/02/09 17:20:22 $ * $State: Exp $ */package com.sun.j3d.utils.geometry.compression;import com.sun.j3d.internal.BufferWrapper;import com.sun.j3d.internal.ByteBufferWrapper;import com.sun.j3d.internal.DoubleBufferWrapper;import com.sun.j3d.internal.FloatBufferWrapper;import com.sun.j3d.utils.geometry.GeometryInfo;import java.util.Collection;import java.util.Iterator;import java.util.LinkedList;import javax.media.j3d.Appearance;import javax.media.j3d.Geometry;import javax.media.j3d.GeometryArray;import javax.media.j3d.GeometryStripArray;import javax.media.j3d.IndexedGeometryArray;import javax.media.j3d.IndexedGeometryStripArray;import javax.media.j3d.IndexedLineArray;import javax.media.j3d.IndexedLineStripArray;import javax.media.j3d.IndexedQuadArray;import javax.media.j3d.IndexedTriangleArray;import javax.media.j3d.IndexedTriangleFanArray;import javax.media.j3d.IndexedTriangleStripArray;import javax.media.j3d.J3DBuffer;import javax.media.j3d.LineArray;import javax.media.j3d.LineStripArray;import javax.media.j3d.Material;import javax.media.j3d.QuadArray;import javax.media.j3d.Shape3D;import javax.media.j3d.TriangleArray;import javax.media.j3d.TriangleFanArray;import javax.media.j3d.TriangleStripArray;import javax.vecmath.Color3f;import javax.vecmath.Color4f;import javax.vecmath.Point3d;import javax.vecmath.Point3f;import javax.vecmath.Point3i;import javax.vecmath.Vector3f;/** * This class is used as input to a geometry compressor. It collects elements * such as vertices, normals, colors, mesh references, and quantization * parameters in an ordered stream. This stream is then traversed during * the compression process and used to build the compressed output buffer. * * @see GeometryCompressor * * @since Java 3D 1.5 */public class CompressionStream { // // NOTE: For now, copies are made of all GeometryArray vertex components // even when by-reference access is available. // // TODO: Retrofit all CompressionStreamElements and MeshBuffer to handle // offsets to vertex data array references so that vertex components don't // have to be copied. New CompressionStreamElements could be defined to // set the current array reference during the quantization pass, or the // reference could be included in every CompressionStreamElement along // with the data offsets. // // TODO: Quantize on-the-fly when adding GeometryArray vertex data so that // CompressionStreamElements don't need references to the original float, // double, or byte data. Quantization is currently a separate pass since // the 1st pass adds vertex data and gets the total object bounds, but // this can be computed by merging the bounds of each GeometryArray // compressed into a single object. The 2nd pass quantization is still // needed for vertex data which isn't retrieved from a GeometryArray; for // example, apps that might use the addVertex() methods directly instead // of addGeometryArray(). // // TODO: To further optimize memory, create new subclasses of // CompressionStream{Color, Normal} for bundled attributes and add them as // explicit stream elements. Then CompressionStreamVertex won't need to // carry references to them. This memory savings might be negated by the // extra overhead of adding more elements to the stream, however. // // TODO: Keep the absolute quantized values in the mesh buffer mirror so // that unmeshed CompressionStreamElements don't need to carry them. // // TODO: Support texture coordinate compression even though Level II is // not supported by any hardware decompressor on any graphics card. // Software decompression is still useful for applications interested in // minimizing file space, transmission time, and object loading time. // private static final boolean debug = false ; private static final boolean benchmark = false ; // Mesh buffer normal substitution is unavailable in Level I. private static final boolean noMeshNormalSubstitution = true ; /** * This flag indicates that a vertex starts a new triangle or line strip. */ static final int RESTART = 1 ; /** * This flag indicates that the next triangle in the strip is defined by * replacing the middle vertex of the previous triangle in the strip. * Equivalent to REPLACE_OLDEST for line strips. */ static final int REPLACE_MIDDLE = 2 ; /** * This flag indicates that the next triangle in the strip is defined by * replacing the oldest vertex of the previous triangle in the strip. * Equivalent to REPLACE_MIDDLE for line strips. */ static final int REPLACE_OLDEST = 3 ; /** * This flag indicates that a vertex is to be pushed into the mesh buffer. */ static final int MESH_PUSH = 1 ; /** * This flag indicates that a vertex does not use the mesh buffer. */ static final int NO_MESH_PUSH = 0 ; /** * Byte to float scale factor for scaling byte color components. */ static final float ByteToFloatScale = 1.0f/255.0f; /** * Type of this stream, either CompressedGeometryData.Header.POINT_BUFFER, * CompressedGeometryData.Header.LINE_BUFFER, or * CompressedGeometryData.Header.TRIANGLE_BUFFER */ int streamType ; /** * A mask indicating which components are present in each vertex, as * defined by GeometryArray. */ int vertexComponents ; /** * Boolean indicating colors are bundled with the vertices. */ boolean vertexColors ; /** * Boolean indicating RGB colors are bundled with the vertices. */ boolean vertexColor3 ; /** * Boolean indicating RGBA colors are bundled with the vertices. */ boolean vertexColor4 ; /** * Boolean indicating normals are bundled with the vertices. */ boolean vertexNormals ; /** * Boolean indicating texture coordinates are present. */ boolean vertexTextures ; /** * Boolean indicating that 2D texture coordinates are used. * Currently only used to skip over textures in interleaved data. */ boolean vertexTexture2 ; /** * Boolean indicating that 3D texture coordinates are used. * Currently only used to skip over textures in interleaved data. */ boolean vertexTexture3 ; /** * Boolean indicating that 4D texture coordinates are used. * Currently only used to skip over textures in interleaved data. */ boolean vertexTexture4 ; /** * Axes-aligned box enclosing all vertices in model coordinates. */ Point3d mcBounds[] = new Point3d[2] ; /** * Axes-aligned box enclosing all vertices in normalized coordinates. */ Point3d ncBounds[] = new Point3d[2] ; /** * Axes-aligned box enclosing all vertices in quantized coordinates. */ Point3i qcBounds[] = new Point3i[2] ; /** * Center for normalizing positions to the unit cube. */ double center[] = new double[3] ; /** * Maximum position range along the 3 axes. */ double positionRangeMaximum ; /** * Scale for normalizing positions to the unit cube. */ double scale ; /** * Current position component (X, Y, and Z) quantization value. This can * range from 1 to 16 bits and has a default of 16.<p> * * At 1 bit of quantization it is not possible to express positive * absolute or delta positions. */ int positionQuant ; /** * Current color component (R, G, B, A) quantization value. This can * range from 2 to 16 bits and has a default of 9.<p> * * A color component is represented with a signed fixed-point value in * order to be able express negative deltas; the default of 9 bits * corresponds to the 8-bit color component range of the graphics hardware * commonly available. Colors must be non-negative, so the lower limit of * quantization is 2 bits. */ int colorQuant ; /** * Current normal component (U and V) quantization value. This can range * from 0 to 6 bits and has a default of 6.<p> * * At 0 bits of quantization normals are represented only as 6 bit * sextant/octant pairs and 14 specially encoded normals (the 6 axis * normals and the 8 octant midpoint normals); since U and V can only be 0 * at the minimum quantization, the totally number of unique normals is * 12 + 14 = 26. */ int normalQuant ; /** * Flag indicating position quantization change. */ boolean positionQuantChanged ; /** * Flag indicating color quantization change. */ boolean colorQuantChanged ; /** * Flag indicating normal quantization change. */ boolean normalQuantChanged ; /** * Last quantized position. */ int lastPosition[] = new int[3] ; /** * Last quantized color. */ int lastColor[] = new int[4] ; /** * Last quantized normal's sextant. */ int lastSextant ; /** * Last quantized normal's octant. */ int lastOctant ; /** * Last quantized normal's U encoding parameter. */ int lastU ; /** * Last quantized normal's V encoding parameter. */ int lastV ; /** * Flag indicating last normal used a special encoding. */ boolean lastSpecialNormal ; /** * Flag indicating the first position in this stream. */ boolean firstPosition ; /** * Flag indicating the first color in this stream. */ boolean firstColor ; /** * Flag indicating the first normal in this stream. */ boolean firstNormal ; /** * The total number of bytes used to create the uncompressed geometric * elements in this stream, useful for performance analysis. This * excludes mesh buffer references. */ int byteCount ; /** * The number of vertices created for this stream, excluding mesh buffer * references. */ int vertexCount ; /** * The number of mesh buffer references created for this stream. */ int meshReferenceCount ; /** * Mesh buffer mirror used for computing deltas during quantization pass * and a limited meshing algorithm for unstripped data. */ MeshBuffer meshBuffer = new MeshBuffer() ; // Collection which holds the elements of this stream. private Collection stream ; // True if preceding stream elements were colors or normals. Used to flag // color and normal mesh buffer substitution when computing deltas during // quantization pass. private boolean lastElementColor = false ; private boolean lastLastElementColor = false ; private boolean lastElementNormal = false ; private boolean lastLastElementNormal = false ; // Some convenient temporary holding variables. private Point3f p3f = new Point3f() ; private Color3f c3f = new Color3f() ; private Color4f c4f = new Color4f() ; private Vector3f n3f = new Vector3f() ; // Private constructor for common initializations. private CompressionStream() { this.stream = new LinkedList() ; byteCount = 0 ; vertexCount = 0 ; meshReferenceCount = 0 ; mcBounds[0] = new Point3d(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY) ; mcBounds[1] = new Point3d(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY) ; qcBounds[0] = new Point3i(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE) ; qcBounds[1] = new Point3i(Integer.MIN_VALUE, Integer.MIN_VALUE,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -