📄 geometrydecompressorshape3d.java
字号:
/* * $RCSfile: GeometryDecompressorShape3D.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.J3dUtilsI18N;import java.util.ArrayList;import javax.media.j3d.Appearance;import javax.media.j3d.GeometryArray;import javax.media.j3d.GeometryStripArray;import javax.media.j3d.LineStripArray;import javax.media.j3d.Material;import javax.media.j3d.PointArray;import javax.media.j3d.Shape3D;import javax.media.j3d.TriangleArray;import javax.media.j3d.TriangleStripArray;import javax.vecmath.Color4f;import javax.vecmath.Point3f;import javax.vecmath.Vector3f;/** * This class implements a Shape3D backend for the abstract * GeometryDecompressor. */class GeometryDecompressorShape3D extends GeometryDecompressor { private static final boolean debug = false ; private static final boolean benchmark = false ; private static final boolean statistics = false ; private static final boolean printInfo = debug || benchmark || statistics ; // Type of connections in the compressed data: // TYPE_POINT (1), TYPE_LINE (2), or TYPE_TRIANGLE (4). private int bufferDataType ; // Data bundled with each vertex: bitwise combination of // NORMAL_IN_BUFFER (1), COLOR_IN_BUFFER (2), ALPHA_IN_BUFFER (4). private int dataPresent ; // List for accumulating the output of the decompressor and converting to // GeometryArray representations. private GeneralizedVertexList vlist ; // Accumulates Shape3D objects constructed from decompressor output. private ArrayList shapes ; // Decompressor output state variables. private Color4f curColor ; private Vector3f curNormal ; // Variables for gathering statistics. private int origVertexCount ; private int stripCount ; private int vertexCount ; private int triangleCount ; private long startTime ; private long endTime ; // Triangle array type to construct. private int triOutputType ; // Types of triangle output available. private static final int TRI_SET = 0 ; private static final int TRI_STRIP_SET = 1 ; private static final int TRI_STRIP_AND_FAN_SET = 2 ; private static final int TRI_STRIP_AND_TRI_SET = 3 ; // Private convenience copies of various constants. private static final int TYPE_POINT = CompressedGeometryRetained.TYPE_POINT ; private static final int TYPE_LINE = CompressedGeometryRetained.TYPE_LINE ; private static final int TYPE_TRIANGLE = CompressedGeometryRetained.TYPE_TRIANGLE ; private static final int FRONTFACE_CCW = GeneralizedStripFlags.FRONTFACE_CCW ; /** * Decompress the given compressed geometry. * @param cgr CompressedGeometryRetained object with compressed geometry * @return an array of Shape3D with TriangleArray geometry if compressed * data contains triangles; otherwise, Shape3D array containing PointArray * or LineStripArray geometry * @see CompressedGeometry * @see GeometryDecompressor */ Shape3D[] toTriangleArrays(CompressedGeometryRetained cgr) { return decompress(cgr, TRI_SET) ; } /** * Decompress the given compressed geometry. * @param cgr CompressedGeometryRetained object with compressed geometry * @return an array of Shape3D with TriangleStripArray geometry if * compressed data contains triangles; otherwise, Shape3D array containing * PointArray or LineStripArray geometry * @see CompressedGeometry * @see GeometryDecompressor */ Shape3D[] toTriangleStripArrays(CompressedGeometryRetained cgr) { return decompress(cgr, TRI_STRIP_SET) ; } /** * Decompress the given compressed geometry. * @param cgr CompressedGeometryRetained object with compressed geometry * @return an array of Shape3D with TriangleStripArray and * TriangleFanArray geometry if compressed data contains triangles; * otherwise, Shape3D array containing PointArray or LineStripArray * geometry * @see CompressedGeometry * @see GeometryDecompressor */ Shape3D[] toStripAndFanArrays(CompressedGeometryRetained cgr) { return decompress(cgr, TRI_STRIP_AND_FAN_SET) ; } /** * Decompress the given compressed geometry. * @param cgr CompressedGeometryRetained object with compressed geometry * @return an array of Shape3D with TriangleStripArray and * TriangleArray geometry if compressed data contains triangles; * otherwise, Shape3D array containing PointArray or LineStripArray * geometry * @see CompressedGeometry * @see GeometryDecompressor */ Shape3D[] toStripAndTriangleArrays(CompressedGeometryRetained cgr) { return decompress(cgr, TRI_STRIP_AND_TRI_SET) ; } /** * Decompress the data contained in a CompressedGeometryRetained and * return an array of Shape3D objects using the specified triangle output * type. The triangle output type is ignored if the compressed data * contains points or lines. */ private Shape3D[] decompress(CompressedGeometryRetained cgr, int triOutputType) { if (! checkVersion(cgr.majorVersionNumber, cgr.minorVersionNumber)) { return null ; } vlist = null ; curColor = null ; curNormal = null ; // Get descriptors for compressed data. bufferDataType = cgr.bufferType ; dataPresent = cgr.bufferContents ; if (printInfo) beginPrint() ; // Initialize the decompressor backend. this.triOutputType = triOutputType ; shapes = new ArrayList() ; // Call the superclass decompress() method which calls the output // methods of this subclass. The results are stored in vlist. super.decompress(cgr.offset, cgr.size, cgr.compressedGeometry) ; // Convert the decompressor output to Shape3D objects. addShape3D() ; if (printInfo) endPrint() ; // Return the fixed-length output array. Shape3D shapeArray[] = new Shape3D[shapes.size()] ; return (Shape3D[])shapes.toArray(shapeArray) ; } /** * Initialize the vertex output list based on the vertex format provided * by the SetState decompression command. */ void outputVertexFormat(boolean bundlingNorm, boolean bundlingColor, boolean doingAlpha) { if (vlist != null) // Construct shapes using the current vertex format. addShape3D() ; int vertexFormat = GeometryArray.COORDINATES ; if (bundlingNorm) { vertexFormat |= GeometryArray.NORMALS ; } if (bundlingColor) { if (doingAlpha) { vertexFormat |= GeometryArray.COLOR_4; } else { vertexFormat |= GeometryArray.COLOR_3; } } vlist = new GeneralizedVertexList(vertexFormat, FRONTFACE_CCW) ; } /** * Add a new decompressed vertex to the current list. */ void outputVertex(Point3f position, Vector3f normal, Color4f color, int vertexReplaceCode) { if (curNormal != null) normal = curNormal ; vlist.addVertex(position, normal, color, vertexReplaceCode) ; if (debug) { System.out.println(" outputVertex: flag " + vertexReplaceCode) ; System.out.println(" position " + position.toString()) ; if (normal != null) System.out.println(" normal " + normal.toString()) ; if (color != null) System.out.println(" color " + color.toString()) ; } } /** * Create a Shape3D using the current color for both the ambient and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -