capsule.java

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

JAVA
379
字号

    private void doUpdateIndexData() {
        // start with top of top dome.
        for (int samples = 1; samples <= radialSamples; samples++) {
            getIndexBuffer().put(samples + 1);
            getIndexBuffer().put(samples);
            getIndexBuffer().put(0);
        }

        for (int plane = 1; plane < sphereSamples; plane++) {
            int topPlaneStart = plane * (radialSamples + 1);
            int bottomPlaneStart = (plane - 1) * (radialSamples + 1);
            for (int sample = 1; sample <= radialSamples; sample++) {
                getIndexBuffer().put(bottomPlaneStart + sample);
                getIndexBuffer().put(bottomPlaneStart + sample + 1);
                getIndexBuffer().put(topPlaneStart + sample);
                getIndexBuffer().put(bottomPlaneStart + sample + 1);
                getIndexBuffer().put(topPlaneStart + sample + 1);
                getIndexBuffer().put(topPlaneStart + sample);
            }
        }

        int start = sphereSamples * (radialSamples + 1);

        // add cylinder
        for (int plane = 0; plane < (axisSamples); plane++) {
            int topPlaneStart = start + plane * (radialSamples + 1);
            int bottomPlaneStart = start + (plane - 1) * (radialSamples + 1);
            for (int sample = 1; sample <= radialSamples; sample++) {
                getIndexBuffer().put(bottomPlaneStart + sample);
                getIndexBuffer().put(bottomPlaneStart + sample + 1);
                getIndexBuffer().put(topPlaneStart + sample);
                getIndexBuffer().put(bottomPlaneStart + sample + 1);
                getIndexBuffer().put(topPlaneStart + sample + 1);
                getIndexBuffer().put(topPlaneStart + sample);
            }
        }

        start += ((axisSamples - 1) * (radialSamples + 1));

        // Add most of the bottom dome triangles.
        for (int plane = 1; plane < (sphereSamples); plane++) {
            int topPlaneStart = start + plane * (radialSamples + 1);
            int bottomPlaneStart = start + (plane - 1) * (radialSamples + 1);
            for (int sample = 1; sample <= radialSamples; sample++) {
                getIndexBuffer().put(bottomPlaneStart + sample);
                getIndexBuffer().put(bottomPlaneStart + sample + 1);
                getIndexBuffer().put(topPlaneStart + sample);
                getIndexBuffer().put(bottomPlaneStart + sample + 1);
                getIndexBuffer().put(topPlaneStart + sample + 1);
                getIndexBuffer().put(topPlaneStart + sample);
            }
        }

        start += ((sphereSamples - 1) * (radialSamples + 1));
        // Finally the bottom of bottom dome.
        for (int samples = 1; samples <= radialSamples; samples++) {
            getIndexBuffer().put(start + samples);
            getIndexBuffer().put(start + samples + 1);
            getIndexBuffer().put(start + radialSamples + 2);
        }
    }

    /** Get the sampling frequency lengthwise along the capsules main axis. */
    public int getAxisSamples() {
        return axisSamples;
    }

    /**
     * @return Returns the height.
     */
    public float getHeight() {
        return height;
    }

    /** Get the sampling frequency radially around the capsules main axis. */
    public int getRadialSamples() {
        return radialSamples;
    }

    /**
     * @return Returns the radius.
     */
    public float getRadius() {
        return radius;
    }

    /** Get the sampling frequency used for the domes at either end of the capsule. */
    public int getSphereSamples() {
        return sphereSamples;
    }

    public void read(JMEImporter e) throws IOException {
        super.read(e);
        InputCapsule capsule = e.getCapsule(this);
        axisSamples = capsule.readInt("circleSamples", 0);
        radialSamples = capsule.readInt("radialSamples", 0);
        sphereSamples = capsule.readInt("sphereSamples", 0);
        radius = capsule.readFloat("radius", 0);
        height = capsule.readFloat("height", 0);
    }

    /**
     * @deprecated use @{link {@link #updateGeometry(Vector3f, Vector3f, float)}.
     */
    public void reconstruct(Vector3f top, Vector3f bottom, float radius) {
        updateGeometry(top, bottom, radius);
    }

    /**
     * Rebuilds this capsule based on a new set of parameters.
     * 
     * @param axisSamples the number of samples along the axis.
     * @param radialSamples the number of samples along the radial.
     * @param sphereSamples the number of samples for the dom end caps.
     * @param radius the radius of the cylinder.
     * @param height the cylinder's height.
     */
    public void updateGeometry(int axisSamples, int radialSamples, int sphereSamples, float radius, float height) {
        this.axisSamples = axisSamples;
        this.sphereSamples = sphereSamples;
        this.radialSamples = radialSamples;
        this.radius = radius;
        this.height = height;
        // determine vert quantity - first the sphere caps
        int sampleLines = (2 * sphereSamples - 1 + axisSamples);
        int verts = (radialSamples + 1) * sampleLines + 2;
        
        setVertexCount(verts);
        setVertexBuffer(BufferUtils.createVector3Buffer(getVertexBuffer(),
                getVertexCount()));
        
        // allocate normals
        setNormalBuffer(BufferUtils.createVector3Buffer(getNormalBuffer(),
                getVertexCount()));
        
        // allocate texture coordinates
        getTextureCoords().set(0,
                new TexCoords(BufferUtils.createVector2Buffer(getVertexCount())));
        
        // determine tri quantity
        int tris = 2 * radialSamples * sampleLines;
        
        setTriangleQuantity(tris);
        setIndexBuffer(BufferUtils.createIntBuffer(getIndexBuffer(),
                3 * getTriangleCount()));
        
        doUpdateGeometry();
        doUpdateIndexData();
    }

    /**
     * Rebuilds this capsule based on a new set of parameters.
     * 
     * @param top the top of the casule.
     * @param bottom the bottom of the capsule.
     * @param radius the radius of the cylinder.
     */
    public void updateGeometry(Vector3f top, Vector3f bottom, float radius) {
        // first make the capsule the right shape
        this.height = top.distance(bottom);
        this.radius = radius;
        doUpdateGeometry();

        // now orient it in space.
        top.add(bottom, localTranslation).multLocal(.5f); // ok we got
                                                            // translation.

        // we need the rotation that takes us from 0,1,0 to the unit vector
        // described by top/center.
        Vector3f capsuleUp = top.subtract(localTranslation).normalizeLocal();
        Matrix3f rotation = new Matrix3f();
        rotation.fromStartEndVectors(Vector3f.UNIT_Y, capsuleUp);
        localRotation.fromRotationMatrix(rotation);
        updateWorldVectors();
    }

    public void write(JMEExporter e) throws IOException {
        super.write(e);
        OutputCapsule capsule = e.getCapsule(this);
        capsule.write(axisSamples, "axisSamples", 0);
        capsule.write(radialSamples, "radialSamples", 0);
        capsule.write(sphereSamples, "sphereSamples", 0);
        capsule.write(radius, "radius", 0);
        capsule.write(height, "height", 0);
    }

}

⌨️ 快捷键说明

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