geometry.java
来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 943 行 · 第 1/3 页
JAVA
943 行
/*
* Copyright (c) 2003-2009 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions 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 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme.scene;
import java.io.IOException;
import java.io.Serializable;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.Stack;
import java.util.logging.Logger;
import com.jme.bounding.BoundingVolume;
import com.jme.intersection.PickResults;
import com.jme.math.FastMath;
import com.jme.math.Quaternion;
import com.jme.math.Ray;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.state.LightState;
import com.jme.scene.state.LightUtil;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.TextureState;
import com.jme.util.export.InputCapsule;
import com.jme.util.export.JMEExporter;
import com.jme.util.export.JMEImporter;
import com.jme.util.export.OutputCapsule;
import com.jme.util.export.Savable;
import com.jme.util.geom.BufferUtils;
/**
* <code>Geometry</code> defines a leaf node of the scene graph. The leaf node
* contains the geometric data for rendering objects. It manages all rendering
* information such as a collection of states and the data for a model.
* Subclasses define what the model data is.
*
* @author Mark Powell
* @author Joshua Slack
*/
public abstract class Geometry extends Spatial implements Serializable, Savable {
private static final Logger logger = Logger.getLogger(Geometry.class.getName());
private static final long serialVersionUID = 1;
/** The local bounds of this Geometry object. */
protected BoundingVolume bound;
/** The number of vertexes in this geometry. */
protected int vertQuantity = 0;
/** The geometry's per vertex color information. */
protected transient FloatBuffer colorBuf;
/** The geometry's per vertex normal information. */
protected transient FloatBuffer normBuf;
/** The geometry's vertex information. */
protected transient FloatBuffer vertBuf;
/** The geometry's per Texture per vertex texture coordinate information. */
protected transient ArrayList<TexCoords> texBuf;
/** The geometry's per vertex color information. */
protected transient FloatBuffer tangentBuf;
/** The geometry's per vertex normal information. */
protected transient FloatBuffer binormalBuf;
/** The geometry's per vertex fog buffer depth values */
protected transient FloatBuffer fogBuf;
/** The geometry's VBO information. */
protected transient VBOInfo vboInfo;
protected boolean enabled = true;
protected boolean castsShadows = true;
protected boolean hasDirtyVertices = false;
/**
* The compiled list of renderstates for this geometry, taking into account
* ancestors' states - updated with updateRenderStates()
*/
public RenderState[] states = new RenderState[RenderState.StateType.values().length];
private LightState lightState;
protected ColorRGBA defaultColor = new ColorRGBA(ColorRGBA.white);
/**
* Non -1 values signal that drawing this scene should use the provided
* display list instead of drawing from the buffers.
*/
protected int displayListID = -1;
/** Static computation field */
protected static final Vector3f compVect = new Vector3f();
/**
* Empty Constructor to be used internally only.
*/
public Geometry() {
super();
texBuf = new ArrayList<TexCoords>(1);
texBuf.add(null);
}
/**
* Constructor instantiates a new <code>Geometry</code> object. This is
* the default object which has an empty vertex array. All other data is
* null.
*
* @param name
* the name of the scene element. This is required for
* identification and comparision purposes.
*/
public Geometry(String name) {
super(name);
texBuf = new ArrayList<TexCoords>(1);
texBuf.add(null);
if (!(this instanceof SharedMesh))
reconstruct(null, null, null, null);
}
/**
* Constructor creates a new <code>Geometry</code> object. During
* instantiation the geometry is set including vertex, normal, color and
* texture information.
*
* @param name
* the name of the scene element. This is required for
* identification and comparision purposes.
* @param vertex
* the points that make up the geometry.
* @param normal
* the normals of the geometry.
* @param color
* the color of each point of the geometry.
* @param coords
* the texture coordinates of the geometry (position 0.)
*/
public Geometry(String name, FloatBuffer vertex, FloatBuffer normal,
FloatBuffer color, TexCoords coords) {
super(name);
texBuf = new ArrayList<TexCoords>(1);
texBuf.add(null);
reconstruct(vertex, normal, color, coords);
}
/**
* returns the number of vertices contained in this geometry.
*/
@Override
public int getVertexCount() {
return vertQuantity;
}
public void setVertexCount(int vertQuantity) {
this.vertQuantity = vertQuantity;
}
@Override
public int getTriangleCount() {
return 0;
}
/**
* <code>reconstruct</code> reinitializes the geometry with new data. This
* will reuse the geometry object.
*
* @param vertices
* the new vertices to use.
* @param normals
* the new normals to use.
* @param colors
* the new colors to use.
* @param coords
* the new texture coordinates to use (position 0).
*/
public void reconstruct(FloatBuffer vertices, FloatBuffer normals,
FloatBuffer colors, TexCoords coords) {
if (vertices == null)
setVertexCount(0);
else
setVertexCount(vertices.limit() / 3);
setVertexBuffer(vertices);
setNormalBuffer(normals);
setColorBuffer(colors);
if (getTextureCoords() == null) {
setTextureCoords(new ArrayList<TexCoords>(1));
}
clearTextureBuffers();
addTextureCoordinates(coords);
if (getVBOInfo() != null)
resizeTextureIds(1);
}
/**
* Sets VBO info on this Geometry.
*
* @param info
* the VBO info to set
* @see VBOInfo
*/
public void setVBOInfo(VBOInfo info) {
vboInfo = info;
if (vboInfo != null) {
vboInfo.resizeTextureIds(texBuf.size());
}
}
/**
* @return VBO info object
* @see VBOInfo
*/
public VBOInfo getVBOInfo() {
return vboInfo;
}
/**
* <code>setSolidColor</code> sets the color array of this geometry to a
* single color. For greater efficiency, try setting the the ColorBuffer to
* null and using DefaultColor instead.
*
* @param color
* the color to set.
*/
public void setSolidColor(ColorRGBA color) {
if (colorBuf == null)
colorBuf = BufferUtils.createColorBuffer(vertQuantity);
colorBuf.rewind();
for (int x = 0, cLength = colorBuf.remaining(); x < cLength; x += 4) {
colorBuf.put(color.r);
colorBuf.put(color.g);
colorBuf.put(color.b);
colorBuf.put(color.a);
}
colorBuf.flip();
}
/**
* Sets every color of this geometry's color array to a random color.
*/
public void setRandomColors() {
if (colorBuf == null)
colorBuf = BufferUtils.createColorBuffer(vertQuantity);
for (int x = 0, cLength = colorBuf.limit(); x < cLength; x += 4) {
colorBuf.put(FastMath.nextRandomFloat());
colorBuf.put(FastMath.nextRandomFloat());
colorBuf.put(FastMath.nextRandomFloat());
colorBuf.put(1);
}
colorBuf.flip();
}
/**
* <code>getVertexBuffer</code> returns the float buffer that contains
* this geometry's vertex information.
*
* @return the float buffer that contains this geometry's vertex
* information.
*/
public FloatBuffer getVertexBuffer() {
return vertBuf;
}
/**
* <code>setVertexBuffer</code> sets this geometry's vertices via a float
* buffer consisting of groups of three floats: x,y and z.
*
* @param vertBuf
* the new vertex buffer.
*/
public void setVertexBuffer(FloatBuffer vertBuf) {
this.vertBuf = vertBuf;
if (vertBuf != null)
vertQuantity = vertBuf.limit() / 3;
else
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?