📄 j3dlwoparser.java
字号:
/* * $RCSfile: J3dLwoParser.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:07 $ * $State: Exp $ */package com.sun.j3d.loaders.lw3d;import java.awt.Component;import java.awt.Image;import java.util.Enumeration;import java.util.Vector;import com.sun.j3d.utils.geometry.GeometryInfo;import com.sun.j3d.utils.geometry.NormalGenerator;import com.sun.j3d.utils.geometry.Stripifier;import com.sun.j3d.utils.image.TextureLoader;import com.sun.j3d.loaders.IncorrectFormatException;import java.io.FileNotFoundException;import javax.media.j3d.*;import javax.vecmath.*;import java.net.*;/** * This class is responsible for turning Lightwave geometry data into * Java3D geometry. It is a subclass of LwoObject and calls that * superclass when first instantiated to parse the binary file and turn it * into intermediate data structures. J3dLwoParser then goes through * those data structures and turns them into Java3D objects. For each * ShapeHolder object created by the parent class, this class retrieves * the geometry data and associated surface data, creates the * appropriate Geometry object (usually IndexedTriangleFanArray, * unless the object is points or lines), including calculating normals for * the polygons and sets up the Appearance according to the surface * parameters (including calculating texture coordinates if necessary). */class J3dLwoParser extends LwoParser { float normalCoordsArray[]; int normalIndicesArray[]; Shape3D objectShape; Color3f color, diffuseColor, specularColor, emissiveColor; float shininess; Vector objectShapeList = new Vector(); /** * Constructor: Calls LwoObject to parse file and create data structures */ J3dLwoParser(String fileName, int debugVals) throws FileNotFoundException { super(fileName, debugVals); } J3dLwoParser(URL url, int debugVals) throws FileNotFoundException { super(url, debugVals); } void getSurf(int length) throws FileNotFoundException { super.getSurf(length); } /** * Turns LwoObject's data structures (created from the binary geometry * file) into Java3d objects */ void createJava3dGeometry() throws IncorrectFormatException { GeometryArray object; LwoTexture texture; for (Enumeration e = shapeList.elements(); e.hasMoreElements() ;) { int vertexFormat = javax.media.j3d.GeometryArray.COORDINATES; ShapeHolder shape = (ShapeHolder)e.nextElement(); debugOutputLn(LINE_TRACE, "about to create Arrays for Shape"); debugOutputLn(VALUES, "shape = " + shape); shape.createArrays(true); int vertexCount = shape.coordsArray.length/3; int indexCount = 0; if (shape.facetIndices != null) indexCount = shape.facetIndices.length; debugOutputLn(VALUES, "numSurf = " + shape.numSurf); // Find the right surface. Note: surfaces are indexed by // name. So take this surf number, look up the name of that // surface in surfaceNameList, then take that name and // find the matching LwoSurface String surfName = (String)surfNameList.elementAt(shape.numSurf - 1); LwoSurface surf = null; for (int surfNum = 0; surfNum < surfaceList.size(); ++surfNum) { LwoSurface tempSurf = (LwoSurface)surfaceList.elementAt(surfNum); String tempSurfName = tempSurf.surfName; if (surfName.equals(tempSurfName)) { surf = tempSurf; break; } } if (surf == null) { throw new IncorrectFormatException( "bad surf for surfnum/name = " + shape.numSurf + ", " + surfName); } debugOutputLn(VALUES, "surf = " + surf); // Get the LwoTexture object (if any) for the surface texture = surf.getTexture(); Appearance appearance = new Appearance(); if (shape.facetSizes[0] == 1) { // This case happens if the objects are points // Note that points are colored, not lit object = new javax.media.j3d.PointArray(vertexCount, vertexFormat); object.setCoordinates(0, shape.coordsArray); ColoringAttributes colorAtt = new ColoringAttributes(surf.getColor(), ColoringAttributes.FASTEST); PointAttributes pointStyle = new PointAttributes(); pointStyle.setPointSize(1); appearance.setColoringAttributes(colorAtt); appearance.setPointAttributes(pointStyle); } else if (shape.facetSizes[0] == 2) { // This case happens if the objects are lines // Note that lines are colored, not lit debugOutputLn(LINE_TRACE, "Creating IndexedLineArray"); object = new javax.media.j3d.LineArray(vertexCount, vertexFormat); object.setCoordinates(0, shape.coordsArray); ColoringAttributes colorAtt = new ColoringAttributes(surf.getColor(), ColoringAttributes.FASTEST); appearance.setColoringAttributes(colorAtt); } else { // This is the case for any polygonal objects debugOutputLn(LINE_TRACE, "Creating IndexedTriFanArray"); // create triFanArray vertexFormat |= javax.media.j3d.GeometryArray.NORMALS; debugOutputLn(LINE_TRACE, "about to process vertices/indices, facetIndices = " + shape.facetIndices); if (shape.facetIndices != null) { float[] textureCoords = null; int[] textureIndices = null; debugOutputLn(LINE_TRACE, "setting vertexCount, normind = " + shape.normalIndices); // If these are null we're going direct (non-indexed) debugOutputLn(LINE_TRACE, "vtxcount, format, indcount = " + vertexCount + ", " + vertexFormat + ", " + indexCount); if (texture != null) { // There's a texture here - need to create the appropriate arrays // and calculate texture coordinates for the object vertexFormat |= GeometryArray.TEXTURE_COORDINATE_2; textureCoords = new float[vertexCount * 2]; textureIndices = new int[shape.facetIndices.length]; calculateTextureCoords(texture, shape.coordsArray, shape.facetIndices, textureCoords, textureIndices); debugOutputLn(LINE_TRACE, "textureCoords:"); debugOutputLn(LINE_TRACE, "texture Coords, Indices.length = " + textureCoords.length + ", " + textureIndices.length); } debugOutputLn(LINE_TRACE, "about to create GeometryInfo"); // Use the GeometryInfo utility to calculate smooth normals GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_FAN_ARRAY); gi.setCoordinates(shape.coordsArray); gi.setCoordinateIndices(shape.facetIndices); gi.setStripCounts(shape.facetSizes); if (texture != null) { gi.setTextureCoordinateParams(1, 2); gi.setTextureCoordinates(0, textureCoords); gi.setTextureCoordinateIndices(0, textureIndices); } gi.recomputeIndices(); NormalGenerator ng = new NormalGenerator(surf.getCreaseAngle()); ng.generateNormals(gi); Stripifier st = new Stripifier(); st.stripify(gi); object = gi.getGeometryArray(true, true, false); debugOutputLn(LINE_TRACE, "done."); } else { // This case is called if LwoObject did not create facet // indices. This code is not currently used because facet // indices are always created for polygonal objects debugOutputLn(LINE_TRACE, "about to create trifanarray with " + "vertexCount, facetSizes.len = " + vertexCount + ", " + shape.facetSizes.length); object = new javax.media.j3d.TriangleFanArray(vertexCount, vertexFormat, shape.facetSizes); object.setCoordinates(0, shape.coordsArray); object.setNormals(0, shape.normalCoords); debugOutputLn(VALUES, "passed in normalCoords, length = " + shape.normalCoords.length); } debugOutputLn(LINE_TRACE, "created fan array"); // Setup Appearance given the surface parameters Material material = new Material(surf.getColor(), surf.getEmissiveColor(), surf.getDiffuseColor(), surf.getSpecularColor(), surf.getShininess()); material.setLightingEnable(true); appearance.setMaterial(material); if (surf.getTransparency() != 0f) { TransparencyAttributes ta = new TransparencyAttributes(); ta.setTransparency(surf.getTransparency()); ta.setTransparencyMode(ta.BLENDED); appearance.setTransparencyAttributes(ta); } if (texture != null) { debugOutputLn(LINE_TRACE, "texture != null, enable texturing"); Texture tex = texture.getTexture(); tex.setEnable(true); appearance.setTexture(tex); TextureAttributes ta = new TextureAttributes(); if (texture.getType().equals("DTEX")) ta.setTextureMode(TextureAttributes.MODULATE); else if (texture.getType().equals("CTEX")) ta.setTextureMode(TextureAttributes.DECAL); appearance.setTextureAttributes(ta); } else { debugOutputLn(LINE_TRACE, "texture == null, no texture to use"); } } debugOutputLn(LINE_TRACE, "done creating object"); // This does gc shape.nullify(); objectShape = new Shape3D(object); // Combine the appearance and geometry objectShape.setAppearance(appearance); objectShapeList.addElement(objectShape); } } /** * Calculate texture coordinates for the geometry given the texture * map properties specified in the LwoTexture object */ void calculateTextureCoords(LwoTexture texture, float verts[], int indices[],
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -