📄 md2tojme.java
字号:
indices.add(i1);
break;
}
}
}
}
int[] indexArray = new int[indices.size()];
for (int x = 0; x < indexArray.length; x++) {
indexArray[x] = indices.get(x).intValue();
}
triMesh[0].setIndexBuffer(BufferUtils
.createIntBuffer(indexArray));
triMesh[0]
.setTextureCoords(TexCoords.makeNew(extractTexCoords(
vectorTexcoords, texVerts, indices
.size())));
controller.setMorphingMesh(triMesh[0]);
} // End if (i==0)
final Vector3f[] allVerts = extractVertices(vectorTexcoords,
uniqueVerts);
triMesh[i].setVertexBuffer(BufferUtils
.createFloatBuffer(allVerts));
final Vector3f[] allNorms = extractNormals(vectorTexcoords,
uniqueNorms);
triMesh[i].setNormalBuffer(BufferUtils
.createFloatBuffer(allNorms));
controller.setKeyframe(i, triMesh[i]);
}
mesh.addController(controller);
}
private Vector3f[] extractVertices(
final List<VectorTex> vectorTexcoords,
final Vector3f[] uniqueVerts) {
final List<Vector3f> ret = new ArrayList<Vector3f>();
for (int i = 0; i < vectorTexcoords.size(); i++) {
VectorTex vectorTex = vectorTexcoords.get(i);
final Vector3f vert = uniqueVerts[vectorTex.v];
ret.add(vert);
}
return ret.toArray(new Vector3f[ret.size()]);
}
private Vector3f[] extractNormals(
final List<VectorTex> vectorTexcoords,
final Vector3f[] uniqueNormals) {
final List<Vector3f> ret = new ArrayList<Vector3f>();
for (int i = 0; i < vectorTexcoords.size(); i++) {
VectorTex vectorTex = vectorTexcoords.get(i);
final Vector3f norm = uniqueNormals[vectorTex.v];
ret.add(norm);
}
return ret.toArray(new Vector3f[ret.size()]);
}
private Vector2f[] extractTexCoords(
final List<VectorTex> vectorTexcoords,
final Vector2f[] texVerts, final int coordCount) {
final Vector2f[] ret = new Vector2f[coordCount];
int count = 0;
for (int i = 0; i < vectorTexcoords.size(); i++) {
VectorTex vectorTex = vectorTexcoords.get(i);
final Vector2f vert = texVerts[vectorTex.t];
ret[count++] = vert;
}
return ret;
}
// This holds the header information that is read in at the beginning of
// the file
private class Header {
int magic; // This is used to identify the file
int version; // The version number of the file (Must be 8)
int skinWidth; // The skin width in pixels
int skinHeight; // The skin height in pixels
int frameSize; // The size in bytes the frames are
int numSkins; // The number of skins associated with the model
int numVertices; // The number of vertices (constant for each
// frame)
int numTexCoords; // The number of texture coordinates
int numTriangles; // The number of faces (polygons)
int numGlCommands; // The number of gl commands
int numFrames; // The number of animation frames
int offsetSkins; // The offset in the file for the skin data
int offsetTexCoords; // The offset in the file for the texture
// data
int offsetTriangles; // The offset in the file for the face data
int offsetFrames; // The offset in the file for the frames data
int offsetGlCommands;
// The offset in the file for the gl commands data
int offsetEnd; // The end of the file offset
Header() {
magic = bis.readInt();
version = bis.readInt();
skinWidth = bis.readInt();
skinHeight = bis.readInt();
frameSize = bis.readInt();
numSkins = bis.readInt();
numVertices = bis.readInt();
numTexCoords = bis.readInt();
numTriangles = bis.readInt();
numGlCommands = bis.readInt();
numFrames = bis.readInt();
offsetSkins = bis.readInt();
offsetTexCoords = bis.readInt();
offsetTriangles = bis.readInt();
offsetFrames = bis.readInt();
offsetGlCommands = bis.readInt();
offsetEnd = bis.readInt();
}
};
// This stores the normals and vertices for the frames
private class Triangle {
Vector3f vertex = new Vector3f();
Vector3f normal = new Vector3f();
};
// This stores the indices into the vertex and texture coordinate arrays
private class Md2Face {
int[] vertexIndices = new int[3]; // short
int[] textureIndices = new int[3]; // short
Md2Face() {
vertexIndices = new int[] { bis.readShort(), bis.readShort(),
bis.readShort() };
textureIndices = new int[] { bis.readShort(), bis.readShort(),
bis.readShort() };
}
};
// This stores the animation scale, translation and name information for
// a
// frame, plus verts
private class VectorKeyframe {
private Vector3f scale = new Vector3f();
private Vector3f translate = new Vector3f();
private String name;
VectorKeyframe() {
scale.x = bis.readFloat();
scale.y = bis.readFloat();
scale.z = bis.readFloat();
translate.x = bis.readFloat();
translate.y = bis.readFloat();
translate.z = bis.readFloat();
name = bis.readString(16);
}
};
// This stores the frames vertices after they have been transformed
private class Md2Frame {
String name; // char [16]
Triangle[] vertices;
Md2Frame() {
}
};
}
/**
* This function returns the KeyframeController that animates an MD2
* converted mesh. Null is returned if a KeyframeController cannot be found.
*
* @param model
* The MD2 mesh.
* @return This mesh's controller.
*/
public static KeyframeController findController(Node model) {
if (model.getQuantity() == 0
|| model.getChild(0).getControllers().size() == 0
|| !(model.getChild(0).getController(0) instanceof KeyframeController))
return null;
return (KeyframeController) model.getChild(0).getController(0);
}
private static final float[][] norms = {
{ -0.525731f, 0.000000f, 0.850651f },
{ -0.442863f, 0.238856f, 0.864188f },
{ -0.295242f, 0.000000f, 0.955423f },
{ -0.309017f, 0.500000f, 0.809017f },
{ -0.162460f, 0.262866f, 0.951056f },
{ 0.000000f, 0.000000f, 1.000000f },
{ 0.000000f, 0.850651f, 0.525731f },
{ -0.147621f, 0.716567f, 0.681718f },
{ 0.147621f, 0.716567f, 0.681718f },
{ 0.000000f, 0.525731f, 0.850651f },
{ 0.309017f, 0.500000f, 0.809017f },
{ 0.525731f, 0.000000f, 0.850651f },
{ 0.295242f, 0.000000f, 0.955423f },
{ 0.442863f, 0.238856f, 0.864188f },
{ 0.162460f, 0.262866f, 0.951056f },
{ -0.681718f, 0.147621f, 0.716567f },
{ -0.809017f, 0.309017f, 0.500000f },
{ -0.587785f, 0.425325f, 0.688191f },
{ -0.850651f, 0.525731f, 0.000000f },
{ -0.864188f, 0.442863f, 0.238856f },
{ -0.716567f, 0.681718f, 0.147621f },
{ -0.688191f, 0.587785f, 0.425325f },
{ -0.500000f, 0.809017f, 0.309017f },
{ -0.238856f, 0.864188f, 0.442863f },
{ -0.425325f, 0.688191f, 0.587785f },
{ -0.716567f, 0.681718f, -0.147621f },
{ -0.500000f, 0.809017f, -0.309017f },
{ -0.525731f, 0.850651f, 0.000000f },
{ 0.000000f, 0.850651f, -0.525731f },
{ -0.238856f, 0.864188f, -0.442863f },
{ 0.000000f, 0.955423f, -0.295242f },
{ -0.262866f, 0.951056f, -0.162460f },
{ 0.000000f, 1.000000f, 0.000000f },
{ 0.000000f, 0.955423f, 0.295242f },
{ -0.262866f, 0.951056f, 0.162460f },
{ 0.238856f, 0.864188f, 0.442863f },
{ 0.262866f, 0.951056f, 0.162460f },
{ 0.500000f, 0.809017f, 0.309017f },
{ 0.238856f, 0.864188f, -0.442863f },
{ 0.262866f, 0.951056f, -0.162460f },
{ 0.500000f, 0.809017f, -0.309017f },
{ 0.850651f, 0.525731f, 0.000000f },
{ 0.716567f, 0.681718f, 0.147621f },
{ 0.716567f, 0.681718f, -0.147621f },
{ 0.525731f, 0.850651f, 0.000000f },
{ 0.425325f, 0.688191f, 0.587785f },
{ 0.864188f, 0.442863f, 0.238856f },
{ 0.688191f, 0.587785f, 0.425325f },
{ 0.809017f, 0.309017f, 0.500000f },
{ 0.681718f, 0.147621f, 0.716567f },
{ 0.587785f, 0.425325f, 0.688191f },
{ 0.955423f, 0.295242f, 0.000000f },
{ 1.000000f, 0.000000f, 0.000000f },
{ 0.951056f, 0.162460f, 0.262866f },
{ 0.850651f, -0.525731f, 0.000000f },
{ 0.955423f, -0.295242f, 0.000000f },
{ 0.864188f, -0.442863f, 0.238856f },
{ 0.951056f, -0.162460f, 0.262866f },
{ 0.809017f, -0.309017f, 0.500000f },
{ 0.681718f, -0.147621f, 0.716567f },
{ 0.850651f, 0.000000f, 0.525731f },
{ 0.864188f, 0.442863f, -0.238856f },
{ 0.809017f, 0.309017f, -0.500000f },
{ 0.951056f, 0.162460f, -0.262866f },
{ 0.525731f, 0.000000f, -0.850651f },
{ 0.681718f, 0.147621f, -0.716567f },
{ 0.681718f, -0.147621f, -0.716567f },
{ 0.850651f, 0.000000f, -0.525731f },
{ 0.809017f, -0.309017f, -0.500000f },
{ 0.864188f, -0.442863f, -0.238856f },
{ 0.951056f, -0.162460f, -0.262866f },
{ 0.147621f, 0.716567f, -0.681718f },
{ 0.309017f, 0.500000f, -0.809017f },
{ 0.425325f, 0.688191f, -0.587785f },
{ 0.442863f, 0.238856f, -0.864188f },
{ 0.587785f, 0.425325f, -0.688191f },
{ 0.688191f, 0.587785f, -0.425325f },
{ -0.147621f, 0.716567f, -0.681718f },
{ -0.309017f, 0.500000f, -0.809017f },
{ 0.000000f, 0.525731f, -0.850651f },
{ -0.525731f, 0.000000f, -0.850651f },
{ -0.442863f, 0.238856f, -0.864188f },
{ -0.295242f, 0.000000f, -0.955423f },
{ -0.162460f, 0.262866f, -0.951056f },
{ 0.000000f, 0.000000f, -1.000000f },
{ 0.295242f, 0.000000f, -0.955423f },
{ 0.162460f, 0.262866f, -0.951056f },
{ -0.442863f, -0.238856f, -0.864188f },
{ -0.309017f, -0.500000f, -0.809017f },
{ -0.162460f, -0.262866f, -0.951056f },
{ 0.000000f, -0.850651f, -0.525731f },
{ -0.147621f, -0.716567f, -0.681718f },
{ 0.147621f, -0.716567f, -0.681718f },
{ 0.000000f, -0.525731f, -0.850651f },
{ 0.309017f, -0.500000f, -0.809017f },
{ 0.442863f, -0.238856f, -0.864188f },
{ 0.162460f, -0.262866f, -0.951056f },
{ 0.238856f, -0.864188f, -0.442863f },
{ 0.500000f, -0.809017f, -0.309017f },
{ 0.425325f, -0.688191f, -0.587785f },
{ 0.716567f, -0.681718f, -0.147621f },
{ 0.688191f, -0.587785f, -0.425325f },
{ 0.587785f, -0.425325f, -0.688191f },
{ 0.000000f, -0.955423f, -0.295242f },
{ 0.000000f, -1.000000f, 0.000000f },
{ 0.262866f, -0.951056f, -0.162460f },
{ 0.000000f, -0.850651f, 0.525731f },
{ 0.000000f, -0.955423f, 0.295242f },
{ 0.238856f, -0.864188f, 0.442863f },
{ 0.262866f, -0.951056f, 0.162460f },
{ 0.500000f, -0.809017f, 0.309017f },
{ 0.716567f, -0.681718f, 0.147621f },
{ 0.525731f, -0.850651f, 0.000000f },
{ -0.238856f, -0.864188f, -0.442863f },
{ -0.500000f, -0.809017f, -0.309017f },
{ -0.262866f, -0.951056f, -0.162460f },
{ -0.850651f, -0.525731f, 0.000000f },
{ -0.716567f, -0.681718f, -0.147621f },
{ -0.716567f, -0.681718f, 0.147621f },
{ -0.525731f, -0.850651f, 0.000000f },
{ -0.500000f, -0.809017f, 0.309017f },
{ -0.238856f, -0.864188f, 0.442863f },
{ -0.262866f, -0.951056f, 0.162460f },
{ -0.864188f, -0.442863f, 0.238856f },
{ -0.809017f, -0.309017f, 0.500000f },
{ -0.688191f, -0.587785f, 0.425325f },
{ -0.681718f, -0.147621f, 0.716567f },
{ -0.442863f, -0.238856f, 0.864188f },
{ -0.587785f, -0.425325f, 0.688191f },
{ -0.309017f, -0.500000f, 0.809017f },
{ -0.147621f, -0.716567f, 0.681718f },
{ -0.425325f, -0.688191f, 0.587785f },
{ -0.162460f, -0.262866f, 0.951056f },
{ 0.442863f, -0.238856f, 0.864188f },
{ 0.162460f, -0.262866f, 0.951056f },
{ 0.309017f, -0.500000f, 0.809017f },
{ 0.147621f, -0.716567f, 0.681718f },
{ 0.000000f, -0.525731f, 0.850651f },
{ 0.425325f, -0.688191f, 0.587785f },
{ 0.587785f, -0.425325f, 0.688191f },
{ 0.688191f, -0.587785f, 0.425325f },
{ -0.955423f, 0.295242f, 0.000000f },
{ -0.951056f, 0.162460f, 0.262866f },
{ -1.000000f, 0.000000f, 0.000000f },
{ -0.850651f, 0.000000f, 0.525731f },
{ -0.955423f, -0.295242f, 0.000000f },
{ -0.951056f, -0.162460f, 0.262866f },
{ -0.864188f, 0.442863f, -0.238856f },
{ -0.951056f, 0.162460f, -0.262866f },
{ -0.809017f, 0.309017f, -0.500000f },
{ -0.864188f, -0.442863f, -0.238856f },
{ -0.951056f, -0.162460f, -0.262866f },
{ -0.809017f, -0.309017f, -0.500000f },
{ -0.681718f, 0.147621f, -0.716567f },
{ -0.681718f, -0.147621f, -0.716567f },
{ -0.850651f, 0.000000f, -0.525731f },
{ -0.688191f, 0.587785f, -0.425325f },
{ -0.587785f, 0.425325f, -0.688191f },
{ -0.425325f, 0.688191f, -0.587785f },
{ -0.425325f, -0.688191f, -0.587785f },
{ -0.587785f, -0.425325f, -0.688191f },
{ -0.688191f, -0.587785f, -0.425325f } };
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -