geometry.java

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

JAVA
943
字号

    /**
     * <code>getWorldCoords</code> translates/rotates and scales the
     * coordinates of this Geometry to world coordinates based on its world
     * settings. The results are stored in the given FloatBuffer. If given
     * FloatBuffer is null, one is created.
     * 
     * @param store
     *            the FloatBuffer to store the results in, or null if you want
     *            one created.
     * @return store or new FloatBuffer if store == null.
     */
    public FloatBuffer getWorldCoords(FloatBuffer store) {
        final FloatBuffer vertBuf = getVertexBuffer();
        if (store == null || store.capacity() != vertBuf.limit()) {
            store = BufferUtils.createFloatBuffer(vertBuf.limit());
            if (store == null) {
                return null;
            }
        }

        for (int v = 0, vSize = store.capacity() / 3; v < vSize; v++) {
            BufferUtils.populateFromBuffer(compVect, vertBuf, v);
            localToWorld(compVect, compVect);
            BufferUtils.setInBuffer(compVect, store, v);
        }
        return store;
    }

    /**
     * <code>getWorldNormals</code> rotates the normals of this Geometry to
     * world normals based on its world settings. The results are stored in the
     * given FloatBuffer. If given FloatBuffer is null, one is created.
     * 
     * @param store
     *            the FloatBuffer to store the results in, or null if you want
     *            one created.
     * @return store or new FloatBuffer if store == null.
     */
    public FloatBuffer getWorldNormals(FloatBuffer store) {
        final FloatBuffer normBuf = getNormalBuffer();
        if (store == null || store.capacity() != normBuf.limit()) {
            store = BufferUtils.createFloatBuffer(normBuf.limit());
            if (store == null) {
                return null;
            }
        }

        for (int v = 0, vSize = store.capacity() / 3; v < vSize; v++) {
            BufferUtils.populateFromBuffer(compVect, normBuf, v);
            getWorldRotation().multLocal(compVect);
            BufferUtils.setInBuffer(compVect, store, v);
        }
        return store;
    }

    public int getDisplayListID() {
        return displayListID;
    }

    public void setDisplayListID(int displayListID) {
        this.displayListID = displayListID;
    }

    public void setTextureCoords(ArrayList<TexCoords> texBuf) {
        this.texBuf = texBuf;
        checkTextureCoordinates();
    }

    public void clearTextureBuffers() {
        if (texBuf != null) {
            texBuf.clear();
        }
    }

    public void addTextureCoordinates(TexCoords textureCoords) {
        addTextureCoordinates(textureCoords, 2);
    }

    public void addTextureCoordinates(TexCoords textureCoords, int coordSize) {
        if (texBuf != null) {
            texBuf.add(textureCoords);
        }
        checkTextureCoordinates();
    }

    public void resizeTextureIds(int i) {
        vboInfo.resizeTextureIds(i);
    }

    protected void checkTextureCoordinates() {
        int max = TextureState.getNumberOfFragmentTexCoordUnits();
        if (max == -1)
            return; // No texture state created yet.
        if (texBuf != null && texBuf.size() > max) {
            for (int i = max; i < texBuf.size(); i++) {
                if (texBuf.get(i) != null) {
                    logger.warning("Texture coordinates set for unit " + i
                            + ". Only " + max + " units are available.");
                }
            }
        }
    }


    public void scaleTextureCoordinates(int index, float factor) {
        scaleTextureCoordinates(index, new Vector2f(factor, factor));
    }

    public void scaleTextureCoordinates(int index, Vector2f factor) {
        if (texBuf == null)
            return;

        if (index < 0 || index >= texBuf.size() || texBuf.get(index) == null) {
            return;
        }

        TexCoords tc = texBuf.get(index);

        for (int i = 0, len = tc.coords.limit() / 2; i < len; i++) {
            BufferUtils.multInBuffer(factor, tc.coords, i);
        }

        if (vboInfo != null) {
            vboInfo.resizeTextureIds(this.texBuf.size());
        }
    }

    public boolean isCastsShadows() {
        return castsShadows;
    }

    public void setCastsShadows(boolean castsShadows) {
        this.castsShadows = castsShadows;
    }

    /**
     * <code>getNumberOfUnits</code> returns the number of texture units this
     * geometry is currently using.
     * 
     * @return the number of texture units in use.
     */
    public int getNumberOfUnits() {
        if (texBuf == null)
            return 0;
        return texBuf.size();
    }

    @Override
    public void lockMeshes(Renderer r) {
        if (getDisplayListID() != -1) {
            logger.warning("This Geometry already has locked meshes."
                    + "(Use unlockMeshes to clear)");
            return;
        }

        updateRenderState();
        lockedMode |= LOCKED_MESH_DATA;

        setDisplayListID(r.createDisplayList(this));
    }

    @Override
    public void unlockMeshes(Renderer r) {
        lockedMode &= ~LOCKED_MESH_DATA;

        if (getDisplayListID() != -1) {
            r.releaseDisplayList(getDisplayListID());
            setDisplayListID(-1);
        }
    }

    /**
     * Called just before renderer starts drawing this geometry. If it returns
     * false, we'll skip rendering.
     */
    public boolean predraw(Renderer r) {
        return true;
    }

    /**
     * Called after renderer finishes drawing this geometry.
     */
    public void postdraw(Renderer r) {
    }

    public void translatePoints(float x, float y, float z) {
        translatePoints(new Vector3f(x, y, z));
    }

    public void translatePoints(Vector3f amount) {
        for (int x = 0; x < vertQuantity; x++) {
            BufferUtils.addInBuffer(amount, vertBuf, x);
        }
    }

    public void rotatePoints(Quaternion rotate) {
        Vector3f store = new Vector3f();
        for (int x = 0; x < vertQuantity; x++) {
            BufferUtils.populateFromBuffer(store, vertBuf, x);
            rotate.mult(store, store);
            BufferUtils.setInBuffer(store, vertBuf, x);
        }
    }

    public void rotateNormals(Quaternion rotate) {
        Vector3f store = new Vector3f();
        for (int x = 0; x < vertQuantity; x++) {
            BufferUtils.populateFromBuffer(store, normBuf, x);
            rotate.mult(store, store);
            BufferUtils.setInBuffer(store, normBuf, x);
        }
    }

    /**
     * <code>getDefaultColor</code> returns the color used if no per vertex
     * colors are specified.
     * 
     * @return default color
     */
    public ColorRGBA getDefaultColor() {
        return defaultColor;
    }

    public void write(JMEExporter e) throws IOException {
        super.write(e);
        OutputCapsule capsule = e.getCapsule(this);
        capsule.write(colorBuf, "colorBuf", null);
        capsule.write(normBuf, "normBuf", null);
        capsule.write(vertBuf, "vertBuf", null);
        capsule.writeSavableArrayList(texBuf, "texBuf",
                new ArrayList<TexCoords>(1));
        capsule.write(tangentBuf, "tangentBuf", null);
        capsule.write(binormalBuf, "binormalBuf", null);
        capsule.write(enabled, "enabled", true);
        capsule.write(castsShadows, "castsShadows", true);
        capsule.write(bound, "bound", null);
        capsule.write(defaultColor, "defaultColor", ColorRGBA.white);
        capsule.write(vboInfo, "vboInfo", null);
    }

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

        colorBuf = capsule.readFloatBuffer("colorBuf", null);
        normBuf = capsule.readFloatBuffer("normBuf", null);
        vertBuf = capsule.readFloatBuffer("vertBuf", null);
        if (vertBuf != null)
            vertQuantity = vertBuf.limit() / 3;
        else
            vertQuantity = 0;
        tangentBuf = capsule.readFloatBuffer("tangentBuf", null);
        binormalBuf = capsule.readFloatBuffer("binormalBuf", null);
        texBuf = capsule.readSavableArrayList("texBuf",
                new ArrayList<TexCoords>(1));
        checkTextureCoordinates();

        enabled = capsule.readBoolean("enabled", true);
        castsShadows = capsule.readBoolean("castsShadows", true);
        bound = (BoundingVolume) capsule.readSavable("bound", null);
        if (bound != null)
            worldBound = bound.clone(null);
        defaultColor = (ColorRGBA) capsule.readSavable("defaultColor",
                ColorRGBA.white.clone());
        vboInfo = (VBOInfo) capsule.readSavable("vboInfo", null);
    }

    /**
     * <code>getModelBound</code> retrieves the bounding object that contains
     * the geometry's vertices.
     * 
     * @return the bounding object for this geometry.
     */
    public BoundingVolume getModelBound() {
        return bound;
    }

    public boolean hasDirtyVertices() {
        return hasDirtyVertices;
    }

    public void setHasDirtyVertices(boolean flag) {
        hasDirtyVertices = flag;
    }

    public void setTangentBuffer(FloatBuffer tangentBuf) {
        this.tangentBuf = tangentBuf;
    }

    public FloatBuffer getTangentBuffer() {
        return this.tangentBuf;
    }

    public void setBinormalBuffer(FloatBuffer binormalBuf) {
        this.binormalBuf = binormalBuf;
    }

    public FloatBuffer getBinormalBuffer() {
        return binormalBuf;
    }
    
    public void setLightState(LightState lightState) {
        this.lightState = lightState;
    }

    public LightState getLightState() {
        return lightState;
    }
}

⌨️ 快捷键说明

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