⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asetojme.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            getData(currentObject, TEXTURE, desiredObject);

            // Load the U tile for this object
            getData(currentObject, UTILE, desiredObject);

            // Load the V tile for this object
            getData(currentObject, VTILE, desiredObject);
        }

        /**
         *
         * <code>getData</code> reads a specified bit of data out of a GEOMOBECT
         * entry in the ase file.
         * @param currentObject the object to save the data in.
         * @param desiredData the object type to read.
         * @param desiredObject the object to read.
         */
        private void getData(
            ASEObject currentObject,
            String desiredData,
            int desiredObject) {
            String word;

            moveToObject(desiredObject);

            // Go through the file until we reach the end
            while (tokenizer.hasMoreTokens()) {
                word = tokenizer.nextToken();

                // If we reached an object tag, stop read because we went to far
                if (word.equals(OBJECT)) {
                    // Stop reading because we are done with the current object
                    return;
                }
                // If we hit a vertex tag
                else if (word.equals(VERTEX)) {
                    // Make sure that is the data that we want to read in
                    if (desiredData.equals(VERTEX)) {
                        // Read in a vertex
                        readVertex(currentObject);
                    }
                }
                // If we hit a texture vertex
                else if (word.equals(TVERTEX)) {
                    // Make sure that is the data that we want to read in
                    if (desiredData.equals(TVERTEX)) {
                        // Read in a texture vertex
                        readTextureVertex(
                            currentObject,
                            materials.get(
                                currentObject.materialID));
                    }
                }
                // If we hit a vertice index to a face
                else if (word.equals(FACE)) {
                    // Make sure that is the data that we want to read in
                    if (desiredData.equals(FACE)) {
                        // Read in a face
                        readFace(currentObject);
                    }
                }
                // If we hit a texture index to a face
                else if (word.equals(TFACE)) {
                    // Make sure that is the data that we want to read in
                    if (desiredData.equals(TFACE)) {
                        // Read in a texture indice for a face
                        readTextureFace(currentObject);
                    }
                }
                // If we hit the material ID to the object
                else if (word.equals(MATERIAL_ID)) {
                    // Make sure that is the data that we want to read in
                    if (desiredData.equals(MATERIAL_ID)) {
                        // Read in the material ID assigned to this object
                        currentObject.materialID =
                            (int) Float.parseFloat(tokenizer.nextToken());
                        return;
                    }
                }
            }
        }

        /**
         *
         * <code>readVertex</code> reads the vertices information from a
         * GEOMOBJECT entry. Some converting is required to get the
         * coordinate axes into the default jme axes.
         * @param currentObject the object to start the vertex in.
         */
        private void readVertex(ASEObject currentObject) {
            int index = 0;

            // Read past the vertex index
            index = Integer.parseInt(tokenizer.nextToken());
            float x = Float.parseFloat(tokenizer.nextToken());
            float z =-Float.parseFloat(tokenizer.nextToken());
            float y = Float.parseFloat(tokenizer.nextToken());
            currentObject.tm.getVertexBuffer().position(index*3);
            currentObject.tm.getVertexBuffer().put(x).put(y).put(z);

        }

        /**
         *
         * <code>readTextureVertex</code> reads in a single texture coordinate
         * from the ase file.
         * @param currentObject the object that has the coordinate.
         * @param texture the object that defines the texture.
         */
        private void readTextureVertex(
            ASEObject currentObject,
            ASEMaterialInfo texture) {
            int index = 0;

            // Here we read past the index of the texture coordinate
            index = Integer.parseInt(tokenizer.nextToken());
            currentObject.tempTexVerts[index] = new Vector2f();

            // Next, we read in the (U, V) texture coordinates.
            currentObject.tempTexVerts[index].x =
                Float.parseFloat(tokenizer.nextToken());
            currentObject.tempTexVerts[index].y =
                Float.parseFloat(tokenizer.nextToken());

            currentObject.tempTexVerts[index].x *= texture.uTile;
            currentObject.tempTexVerts[index].y *= texture.vTile;

        }

        /**
         *
         * <code>readFace</code> reads the face of a triangle, that
         * is how vertices are put together to
         * form the mesh.
         * @param currentObject the object to store the information
         * in.
         */
        private void readFace(ASEObject currentObject) {
            int index = 0;

            // Read past the index of this Face
            String temp = tokenizer.nextToken();
            if (temp.indexOf(":") > 0) {
                temp = temp.substring(0, temp.length() - 1);
            }
            index = Integer.parseInt(temp);
            currentObject.faces[index] = new Face();

            tokenizer.nextToken(); // "A:"
            currentObject.faces[index].vertIndex[0] =
                Integer.parseInt(tokenizer.nextToken());
            tokenizer.nextToken(); // "B:"
            currentObject.faces[index].vertIndex[1] =
                Integer.parseInt(tokenizer.nextToken());
            tokenizer.nextToken(); // "C:"
            currentObject.faces[index].vertIndex[2] =
                Integer.parseInt(tokenizer.nextToken());
        }

        /**
         *
         * <code>readFace</code> reads the face of a triangle, that
         * is how texture vertices are put together to
         * form the mesh.
         * @param currentObject the object to store the information
         * in.
         */
        private void readTextureFace(ASEObject currentObject) {
            int index = 0;

            // Read past the index for this texture coordinate
            index = Integer.parseInt(tokenizer.nextToken());

            // Now we read in the UV coordinate index for the current face.
            // This will be an index into pTexCoords[] for each point in the face.
            currentObject.faces[index].coordIndex[0] =
                Integer.parseInt(tokenizer.nextToken());
            currentObject.faces[index].coordIndex[1] =
                Integer.parseInt(tokenizer.nextToken());
            currentObject.faces[index].coordIndex[2] =
                Integer.parseInt(tokenizer.nextToken());
        }

        /**
         *
         * <code>computeNormals</code> normals are not defined in the
         * ase file, so we calculate them manually. Each vertex has a
         * matching normal. This normal is the average of all the face
         * normals surrounding the vertex.
         *
         */
        private void computeNormals() {
            if (numOfObjects <= 0) {
                return;
            }

            Vector3f vector1 = new Vector3f();
            Vector3f vector2 = new Vector3f();
            Vector3f vector3 = new Vector3f();

            // Go through each of the objects to calculate their normals
            for (int index = 0; index < numOfObjects; index++) {
                // Get the current object
                ASEObject object = objectList.get(index);
                // Here we allocate all the memory we need to calculate the normals
                Vector3f[] tempNormals = new Vector3f[object.faces.length];
                Vector3f[] normals = new Vector3f[object.tm.getVertexCount()];

                // Go though all of the faces of this object
                for (int i = 0; i < object.faces.length; i++) {
                    BufferUtils.populateFromBuffer(vector1, object.tm.getVertexBuffer(), object.faces[i].vertIndex[0]);
                    BufferUtils.populateFromBuffer(vector2, object.tm.getVertexBuffer(), object.faces[i].vertIndex[1]);
                    BufferUtils.populateFromBuffer(vector3, object.tm.getVertexBuffer(), object.faces[i].vertIndex[2]);
                    
                    vector1.subtractLocal(vector3);
                    
                    tempNormals[i] = vector1.cross(vector3.subtract(vector2)).normalizeLocal();
                }

                Vector3f sum = new Vector3f();
                int shared = 0;

                for (int i = 0; i < object.tm.getVertexCount(); i++) {
                    for (int j = 0; j < object.faces.length; j++) {
                        if (object.faces[j].vertIndex[0] == i
                            || object.faces[j].vertIndex[1] == i
                            || object.faces[j].vertIndex[2] == i) {
                            sum.addLocal(tempNormals[j]);

                            shared++;
                        }
                    }

                    normals[i] = sum.divide((-shared)).normalizeLocal();

                    sum.zero(); // Reset the sum
                    shared = 0; // Reset the shared
                }

                object.tm.setNormalBuffer(BufferUtils.createFloatBuffer(normals));

            }
        }

        /**
         *
         * <code>ASEMaterialInfo</code> holds material and texture information.
         */
        private class ASEMaterialInfo {
            String name; // The texture name
            public String file;
            // The texture file name (If this is set it's a texture map)
            public float[] diffuse = new float[3];
            public float[] ambient = new float[3];
            public float[] specular = new float[3];
            public float shine;
            // The color of the object (R, G, B)
            float uTile; // u tiling of texture (Currently not used)
            float vTile; // v tiling of texture (Currently not used)
            float uOffset; // u offset of texture (Currently not used)
            float vOffset; // v offset of texture (Currently not used)
        };

        /**
         *
         * <code>ASEObject</code> holds the data for the mesh.
         */
        public class ASEObject {
            private static final long serialVersionUID = 1L;
			public int materialID;
            public Vector2f[] tempTexVerts; // The texture's UV coordinates
            public Face[] faces; // The faces information of the object

            public TriMesh tm;
            public ASEObject(String name) {
                tm =new TriMesh(name);
            }
        };
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -