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

📄 ms3dloader.java

📁 Java 3D Game SDK.老外做的.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        model.iTotalFrames = byteBuffer.getInt();    }        // read number of joints    private void readNumberJoints() {        model.nNumJoints = byteBuffer.getShort();                model.joints = new MS3DJoint[model.nNumJoints];    }        // read joints    private void readJoints() {        MS3DJoint joint = null;                for(int i = 0; i < model.nNumJoints; i++) {            joint = new MS3DJoint();                        joint.flags = byteBuffer.get();                        byte buffer[] = new byte[32];            for(int k = 0; k < 32; k++)                buffer[k] = byteBuffer.get();            joint.name = makeString(buffer);                        buffer = new byte[32];            for(int k = 0; k < 32; k++)                buffer[k] = byteBuffer.get();            joint.parentName = makeString(buffer);                        joint.rotX = byteBuffer.getFloat();            joint.rotY = byteBuffer.getFloat();            joint.rotZ = byteBuffer.getFloat();                        joint.x = byteBuffer.getFloat();            joint.y = byteBuffer.getFloat();            joint.z = byteBuffer.getFloat();                        joint.numKeyFramesRot = byteBuffer.getShort();            joint.keyRotations = new MS3DKeyFrameRotation[joint.numKeyFramesRot];            joint.numKeyFramesTrans = byteBuffer.getShort();            joint.keyTranslations = new MS3DKeyFrameTranslation[joint.numKeyFramesTrans];                        MS3DKeyFrameRotation keyRotation = null;            for(int k = 0; k < joint.numKeyFramesRot; k++) {                keyRotation = new MS3DKeyFrameRotation();                                keyRotation.time = (long)(byteBuffer.getFloat() * 1000);                                keyRotation.rotX = byteBuffer.getFloat();                keyRotation.rotY = byteBuffer.getFloat();                keyRotation.rotZ = byteBuffer.getFloat();                                joint.keyRotations[k] = keyRotation;            }                        MS3DKeyFrameTranslation keyTranslation = null;            for(int k = 0; k < joint.numKeyFramesTrans; k++) {                keyTranslation = new MS3DKeyFrameTranslation();                                keyTranslation.time = (long)(byteBuffer.getFloat() * 1000);                                keyTranslation.x = byteBuffer.getFloat();                keyTranslation.y = byteBuffer.getFloat();                keyTranslation.z = byteBuffer.getFloat();                                joint.keyTranslations[k] = keyTranslation;            }                        model.joints[i] = joint;            // add to the animation list            model.addJoint(joint);        }    }        //    //    //        private String makeString(byte data[]) {        for(int i = 0; i < data.length; i++) {            if(data[i] == 0)                return new String(data, 0, i);        }                return new String(data);    }        //    //    //        private void buildModel() {                Shape3D shape3d = null;        TriangleArray triangleArray = null;                Appearance appearance = null;                MS3DGroup group = null;        MS3DMaterial groupMaterial = null;        int vertex = 0;        int texvertex = 0;        for(int i = 0; i < model.groups.length; i++) {            group = model.groups[i];            if(group.materialIndex != -1)                groupMaterial = model.materials[group.materialIndex];            vertex = 0;            texvertex = 0;                        shape3d = new Shape3D();                        appearance = new Appearance();                        group.coords = new float[group.numtriangles * 3 * 3];            group.coordsIndex = new int[group.numtriangles * 3];            group.normals = new float[group.numtriangles * 3 * 3];            group.texcoords = new float[group.numtriangles * 3 * 2];            triangleArray= new TriangleArray(group.numtriangles * 3, GeometryArray.COORDINATES | GeometryArray.NORMALS | GeometryArray.TEXTURE_COORDINATE_2 | GeometryArray.BY_REFERENCE);            MS3DTriangle triangle = null;            for(int k = 0; k < group.numtriangles; k++) {                triangle = model.triangles[group.triangleIndices[k]];                                for(int l = 0; l < 3; l++) {                                        if(group.materialIndex != -1) {                        if(groupMaterial.texture.length() > 0) {                            group.texcoords[texvertex++] = triangle.texCoord[l].x;                            group.texcoords[texvertex++] = triangle.texCoord[l].y;                        }                    }                                        group.coordsIndex[k*3+l] = triangle.vertexIndices[l];                                        group.coords[vertex] = model.vertices[triangle.vertexIndices[l]].x;                    group.normals[vertex++] = triangle.vertexNormals[l].x;                    group.coords[vertex] = model.vertices[triangle.vertexIndices[l]].y;                    group.normals[vertex++] = triangle.vertexNormals[l].y;                    group.coords[vertex] = model.vertices[triangle.vertexIndices[l]].z;                    group.normals[vertex++] = triangle.vertexNormals[l].z;                                    }            }            triangleArray.setCoordRefFloat(group.coords);            triangleArray.setCapability(GeometryArray.ALLOW_COORDINATE_READ);            triangleArray.setCapability(GeometryArray.ALLOW_COORDINATE_WRITE);                        triangleArray.setNormalRefFloat(group.normals);            triangleArray.setCapability(GeometryArray.ALLOW_NORMAL_READ);            triangleArray.setCapability(GeometryArray.ALLOW_NORMAL_WRITE);                        triangleArray.setTexCoordRefFloat(0, group.texcoords);            triangleArray.setCapability(GeometryArray.ALLOW_TEXCOORD_READ);            triangleArray.setCapability(GeometryArray.ALLOW_TEXCOORD_WRITE);                        triangleArray.setCapability(GeometryArray.ALLOW_REF_DATA_READ);            triangleArray.setCapability(GeometryArray.ALLOW_REF_DATA_READ);                        shape3d.setGeometry(triangleArray);            shape3d.setCapability(Shape3D.ALLOW_GEOMETRY_READ);            shape3d.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);                        // create the material            if(group.materialIndex != -1) {                                Material material = new Material(new Color3f(groupMaterial.ambient.x, groupMaterial.ambient.y, groupMaterial.ambient.z),                new Color3f(groupMaterial.emissive.x, groupMaterial.emissive.y, groupMaterial.emissive.z),                new Color3f(groupMaterial.diffuse.x, groupMaterial.diffuse.y, groupMaterial.diffuse.z),                new Color3f(groupMaterial.specular.x, groupMaterial.specular.y, groupMaterial.specular.z),                groupMaterial.shininess);                                appearance.setMaterial(material);                                // Texture                if(groupMaterial.texture.length() > 0) {                                        Texture texture = (Texture)textureCache.get(groupMaterial.texture);                                        if(texture == null) {                        // neede for the different operating systems                        String filename = groupMaterial.texture;                        filename = filename.replace('\\', File.separatorChar);                        //                        TextureLoader textureLoader = new TextureLoader(filename, component);                        texture = textureLoader.getTexture();                                                textureCache.put(groupMaterial.texture, texture);                    }                                        appearance.setTexture(texture);                                        TextureAttributes ta = new TextureAttributes();                    ta.setCapability(TextureAttributes.ALLOW_MODE_READ);                    ta.setCapability(TextureAttributes.ALLOW_MODE_WRITE);                    appearance.setTextureAttributes(ta);                }            }            shape3d.setAppearance(appearance);                        model.addShape3D(model.groups[i].name, shape3d);        }    }        private void makeBones() {        MS3DJoint joint = null;        MS3DVertex vertex = null;                // refer vertices to the joints        for(int i = 0; i < model.nNumVertices; i++) {            vertex = model.vertices[i];                        if(vertex.boneId != -1) {                joint = model.joints[vertex.boneId];            }        }                MS3DJoint father = null;                // make the bone hierarchy        for(int i = 0; i < model.nNumJoints; i++) {            joint = model.joints[i];                        joint.relativeMatrix = new Matrix4f();                        // Rotation            MS3DModel.setRzRyRxRotationTranspose(joint.relativeMatrix, joint.rotZ, joint.rotY, joint.rotX);                        // Translation            MS3DModel.setTranslationTranspose(joint.relativeMatrix, joint.x, joint.y, joint.z);                        if(!joint.parentName.equals("")) {                father = (MS3DJoint)model.getJoints().get(joint.parentName);                                joint.absoluteMatrix = new Matrix4f();                                MS3DModel.mulTransposeBothTanspose(joint.absoluteMatrix, father.absoluteMatrix, joint.relativeMatrix);                            } else {                                joint.absoluteMatrix = new Matrix4f(joint.relativeMatrix);                            }        }                // initialize the vertices        for (int i = 0; i < model.nNumVertices; i++ ) {            vertex = model.vertices[i];                        if ( vertex.boneId != -1 ) {                Matrix4f matrix = model.joints[vertex.boneId].absoluteMatrix;                                float x = vertex.x - matrix.m30;                float y = vertex.y - matrix.m31;                float z = vertex.z - matrix.m32;                                model.vertex[3*i] = x*matrix.m00 + y*matrix.m01 + z*matrix.m02;                model.vertex[3*i + 1] = x*matrix.m10 + y*matrix.m11 + z*matrix.m12;                model.vertex[3*i + 2] = x*matrix.m20 + y*matrix.m21 + z*matrix.m22;                            }        }            }        private void reset() {                model.reset();            }        public BoundingSphere getBoundingSphere() {        Point3d lower = new Point3d( minX, minY, minZ);        Point3d upper = new Point3d( maxX, maxY, maxZ);                Point3d center = new Point3d((maxX + minX)/2, (maxY + minY)/2, (maxZ + minZ)/2);                return new BoundingSphere(center, lower.distance(upper)/2);    }        public BoundingBox getBoundingBox() {                Point3d lower = new Point3d( minX, minY, minZ);        Point3d upper = new Point3d( maxX, maxY, maxZ);                return new BoundingBox(lower, upper);    }        }

⌨️ 快捷键说明

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