orientedbox.java

来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 357 行 · 第 1/2 页

JAVA
357
字号
    /** @deprecated Use {@link #getYAxis()} instead */
    public Vector3f getyAxis() {
        return getYAxis();
    }

    public Vector3f getYAxis() {
        return yAxis;
    }

    /** @deprecated Use {@link #getZAxis()} instead */
    public Vector3f getzAxis() {
        return getZAxis();
    }

    public Vector3f getZAxis() {
        return zAxis;
    }

    /**
     * Have the corners of the box been set correctly.
     * 
     * @return {@code true} if the vector store is correct,
     *      {@code false} otherwise.
     */
    public boolean isCorrectCorners() {
        return correctCorners;
    }

    public void read(JMEImporter e) throws IOException {
        super.read(e);
        InputCapsule capsule = e.getCapsule(this);

        center = (Vector3f) capsule.readSavable("center", Vector3f.ZERO.clone());
        xAxis = (Vector3f) capsule.readSavable("xAxis", Vector3f.UNIT_X.clone());
        yAxis = (Vector3f) capsule.readSavable("yAxis", Vector3f.UNIT_Y.clone());
        zAxis = (Vector3f) capsule.readSavable("zAxis", Vector3f.UNIT_Z.clone());
        extent = (Vector3f) capsule.readSavable("extent", Vector3f.ZERO.clone());
        texTopRight = (Vector2f) capsule.readSavable("texTopRight", new Vector2f(1, 1));
        texTopLeft = (Vector2f) capsule.readSavable("texTopLeft", new Vector2f(1, 0));
        texBotRight = (Vector2f) capsule.readSavable("texBotRight", new Vector2f(0, 1));
        texBotLeft = (Vector2f) capsule.readSavable("texBotLeft", new Vector2f(0, 0));

        Savable[] savs = capsule.readSavableArray("vectorStore", new Vector3f[8]);
        if (savs == null) {
            vectorStore = null;
        } else {
            vectorStore = new Vector3f[savs.length];
            for (int x = 0; x < savs.length; x++) {
                vectorStore[x] = (Vector3f) savs[x];
            }
        }

        correctCorners = capsule.readBoolean("correctCorners", false);
    }

    /** @deprecated use {@link #updateGeometry(Vector3f, Vector2f, Vector2f, Vector2f, Vector2f, boolean)} instead. */
    public void setCenter(Vector3f center) {
        this.center = center;
    }

    /** @deprecated use {@link #updateGeometry(Vector3f, Vector2f, Vector2f, Vector2f, Vector2f, boolean)} instead. */
    public void setExtent(Vector3f extent) {
        this.extent = extent;
    }

    /** @deprecated use {@link #updateGeometry(Vector3f, Vector2f, Vector2f, Vector2f, Vector2f, boolean)} instead. */
    public void setxAxis(Vector3f xAxis) {
        this.xAxis = xAxis;
    }

    /** @deprecated use {@link #updateGeometry(Vector3f, Vector2f, Vector2f, Vector2f, Vector2f, boolean)} instead. */
    public void setyAxis(Vector3f yAxis) {
        this.yAxis = yAxis;
    }

    /** @deprecated use {@link #updateGeometry(Vector3f, Vector2f, Vector2f, Vector2f, Vector2f, boolean)} instead. */
    public void setzAxis(Vector3f zAxis) {
        this.zAxis = zAxis;
    }

    /** Update the box’s geometry after a property has been altered directly. */
    public void updateGeometry() {
        updateGeometryVertices();
        updateGeometryNormals();
        updateGeometryTextures();
        updateGeometryIndices();
    }
    
    public void updateGeometry(Vector3f center, Vector2f topRight, Vector2f topLeft, Vector2f bottomRight, Vector2f bottomLeft) {
        this.center = center;
        this.texTopRight = topRight;
        this.texTopLeft = topLeft;
        this.texBotRight = bottomRight;
        this.texBotLeft = bottomLeft;
        this.correctCorners = false;
        vectorStore = new Vector3f[8];
        for (int i = 0; i < vectorStore.length; i++) {
            vectorStore[i] = new Vector3f();
        }
        updateGeometry();
   }

    /** Sets the correct indices array for the box. */
    private void updateGeometryIndices() {
        setIndexBuffer(BufferUtils.createIntBuffer(getIndexBuffer(), 36));
        setTriangleQuantity(12);
        int[] ints = new int[6];
        for (int i = 0; i < 6; i++) {
            ints[0] = i * 4 + 0;
            ints[1] = i * 4 + 1;
            ints[2] = i * 4 + 3;
            ints[3] = i * 4 + 1;
            ints[4] = i * 4 + 2;
            ints[5] = i * 4 + 3;
            getIndexBuffer().put(ints);
        }
    }

    /** Sets the correct normal array for the box. */
    private void updateGeometryNormals() {
        setNormalBuffer(BufferUtils.createVector3Buffer(getNormalBuffer(), 24));
        getNormalBuffer().put(new float[] {
                 yAxis.x,  yAxis.y,  yAxis.z,  yAxis.x,  yAxis.y,  yAxis.z,  yAxis.x,  yAxis.y,  yAxis.z,  yAxis.x,  yAxis.y,  yAxis.z, // top
                 xAxis.x,  xAxis.y,  xAxis.z,  xAxis.x,  xAxis.y,  xAxis.z,  xAxis.x,  xAxis.y,  xAxis.z,  xAxis.x,  xAxis.y,  xAxis.z, // right
                -xAxis.x, -xAxis.y, -xAxis.z, -xAxis.x, -xAxis.y, -xAxis.z, -xAxis.x, -xAxis.y, -xAxis.z, -xAxis.x, -xAxis.y, -xAxis.z, // left
                -yAxis.x, -yAxis.y, -yAxis.z, -yAxis.x, -yAxis.y, -yAxis.z, -yAxis.x, -yAxis.y, -yAxis.z, -yAxis.x, -yAxis.y, -yAxis.z, // bottom
                -zAxis.x, -zAxis.y, -zAxis.z, -zAxis.x, -zAxis.y, -zAxis.z, -zAxis.x, -zAxis.y, -zAxis.z, -zAxis.x, -zAxis.y, -zAxis.z, // back
                 zAxis.x,  zAxis.y,  zAxis.z,  zAxis.x,  zAxis.y,  zAxis.z,  zAxis.x,  zAxis.y,  zAxis.z,  zAxis.x,  zAxis.y,  zAxis.z, //front
        });
    }

    /** Sets the correct texture array for the box. */
    private void updateGeometryTextures() {
        if (getTextureCoords().get(0) == null) {
            getTextureCoords().set(0, new TexCoords(BufferUtils.createFloatBuffer(new float[] {
                    texTopRight.x, texTopRight.y, texTopLeft.x, texTopLeft.y, texBotLeft.x, texBotLeft.y, texBotRight.x, texBotRight.y,
                    texTopRight.x, texTopRight.y, texTopLeft.x, texTopLeft.y, texBotLeft.x, texBotLeft.y, texBotRight.x, texBotRight.y,
                    texTopRight.x, texTopRight.y, texTopLeft.x, texTopLeft.y, texBotLeft.x, texBotLeft.y, texBotRight.x, texBotRight.y,
                    texTopRight.x, texTopRight.y, texTopLeft.x, texTopLeft.y, texBotLeft.x, texBotLeft.y, texBotRight.x, texBotRight.y,
                    texTopRight.x, texTopRight.y, texTopLeft.x, texTopLeft.y, texBotLeft.x, texBotLeft.y, texBotRight.x, texBotRight.y,
                    texTopRight.x, texTopRight.y, texTopLeft.x, texTopLeft.y, texBotLeft.x, texBotLeft.y, texBotRight.x, texBotRight.y
            })));
        }
    }

    /** Sets the correct vertex information for the box. */
    private void updateGeometryVertices() {
        computeCorners();
        setVertexBuffer(BufferUtils.createVector3Buffer(getVertexBuffer(), 24));
        setVertexCount(24);
        Vector3f[] v = vectorStore; // use an alias to save typring ;-)
        getVertexBuffer().put(new float[] {
                v[0].x, v[0].y, v[0].z, v[1].x, v[1].y, v[1].z, v[5].x, v[5].y, v[5].z, v[3].x, v[3].y, v[3].z, // top
                v[0].x, v[0].y, v[0].z, v[3].x, v[3].y, v[3].z, v[6].x, v[6].y, v[6].z, v[2].x, v[2].y, v[2].z, // right
                v[5].x, v[5].y, v[5].z, v[1].x, v[1].y, v[1].z, v[4].x, v[4].y, v[4].z, v[7].x, v[7].y, v[7].z, // left
                v[6].x, v[6].y, v[6].z, v[7].x, v[7].y, v[7].z, v[4].x, v[4].y, v[4].z, v[2].x, v[2].y, v[2].z, // bottom
                v[3].x, v[3].y, v[3].z, v[5].x, v[5].y, v[5].z, v[7].x, v[7].y, v[7].z, v[6].x, v[6].y, v[6].z, // back
                v[1].x, v[1].y, v[1].z, v[4].x, v[4].y, v[4].z, v[2].x, v[2].y, v[2].z, v[0].x, v[0].y, v[0].z  // front
        });
    }
    
    public void write(JMEExporter e) throws IOException {
        super.write(e);

        OutputCapsule capsule = e.getCapsule(this);
        capsule.write(center, "center", Vector3f.ZERO);
        capsule.write(xAxis, "xAxis", Vector3f.UNIT_X);
        capsule.write(yAxis, "yAxis", Vector3f.UNIT_Y);
        capsule.write(zAxis, "zAxis", Vector3f.UNIT_Z);
        capsule.write(extent, "extent", Vector3f.ZERO);
        capsule.write(texTopRight, "texTopRight", new Vector2f(1, 1));
        capsule.write(texTopLeft, "texTopLeft", new Vector2f(1, 0));
        capsule.write(texBotRight, "texBotRight", new Vector2f(0, 1));
        capsule.write(texBotLeft, "texBotLeft", new Vector2f(0, 0));
        capsule.write(vectorStore, "vectorStore", new Vector3f[8]);
        capsule.write(correctCorners, "correctCorners", false);
    }
}

⌨️ 快捷键说明

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