spatial.java

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

JAVA
1,773
字号
    /**
     * Flags this spatial and those below it to allow for transform updating
     * (the default).
     * 
     * @see #lockTransforms()
     */
    public void unlockTransforms() {
        lockedMode &= ~LOCKED_TRANSFORMS;
    }

    /**
     * Flags this spatial and those below it to allow for mesh updating (the
     * default). Generally this means that any display lists setup will be
     * erased and released. Calls unlockMeshes(Renderer) with the current
     * display system's renderer.
     * 
     * @see #unlockMeshes(Renderer)
     */
    public void unlockMeshes() {
        unlockMeshes(DisplaySystem.getDisplaySystem().getRenderer());
    }

    /**
     * Flags this spatial and those below it to allow for mesh updating (the
     * default). Generally this means that any display lists setup will be
     * erased and released.
     * 
     * @param r
     *            The renderer used to lock against.
     * @see #lockMeshes(Renderer)
     */
    public void unlockMeshes(Renderer r) {
        lockedMode &= ~LOCKED_MESH_DATA;
    }

    /**
     * Convienence function for unlocking all aspects of a Spatial.
     * 
     * @see #unlockBounds()
     * @see #unlockTransforms()
     * @see #unlockMeshes(Renderer)
     * @see #unlockShadows()
     * @see #unlockBranch()
     */
    public void unlock(Renderer r) {
        unlockBounds();
        unlockBranch();
        unlockTransforms();
        unlockMeshes(r);
        unlockShadows();
        unlockBranch();
    }

    /**
     * Convienence function for unlocking all aspects of a Spatial. For
     * unlockMeshes it calls:
     * <code>unlockMeshes(DisplaySystem.getDisplaySystem().getRenderer());</code>
     * 
     * @see #unlockBounds()
     * @see #unlockTransforms()
     * @see #unlockMeshes()
     * @see #unlockShadows()
     * @see #unlockBranch()
     */
    public void unlock() {
        unlockBounds();
        unlockBranch();
        unlockTransforms();
        unlockMeshes();
        unlockShadows();
        unlockBranch();
    }

    /**
     * @return a bitwise combination of the current locks established on this
     *         Spatial.
     */
    public int getLocks() {
        return lockedMode;
    }

    /**
     * Note: Uses the currently set Renderer to generate a display list if
     * LOCKED_MESH_DATA is set.
     * 
     * @param locks
     *            a bitwise combination of the locks to establish on this
     *            Spatial.
     */
    public void setLocks(int lockedMode) {
        if ((lockedMode & Spatial.LOCKED_BOUNDS) != 0) {
            lockBounds();
        } else {
            unlockBounds();
        }
        if ((lockedMode & Spatial.LOCKED_BRANCH) != 0) {
            lockBranch();
        } else {
            unlockBranch();
        }
        if ((lockedMode & Spatial.LOCKED_MESH_DATA) != 0) {
            lockMeshes();
        } else {
            unlockMeshes();
        }
        if ((lockedMode & Spatial.LOCKED_SHADOWS) != 0) {
            lockShadows();
        } else {
            unlockShadows();
        }
        if ((lockedMode & Spatial.LOCKED_TRANSFORMS) != 0) {
            lockTransforms();
        } else {
            unlockTransforms();
        }
    }

    /**
     * @param locks
     *            a bitwise combination of the locks to establish on this
     *            Spatial.
     * @param r
     *            the renderer to create display lists with if LOCKED_MESH_DATA
     *            is set.
     */
    public void setLocks(int locks, Renderer r) {
        if ((lockedMode & Spatial.LOCKED_BOUNDS) != 0)
            lockBounds();
        if ((lockedMode & Spatial.LOCKED_MESH_DATA) != 0)
            lockMeshes(r);
        if ((lockedMode & Spatial.LOCKED_SHADOWS) != 0)
            lockShadows();
        if ((lockedMode & Spatial.LOCKED_TRANSFORMS) != 0)
            lockTransforms();
    }

    /**
     * <code>updateWorldBound</code> updates the bounding volume of the world.
     * Abstract, geometry transforms the bound while node merges the children's
     * bound. In most cases, users will want to call updateModelBound() and let
     * this function be called automatically during updateGeometricState().
     */
    public abstract void updateWorldBound();

    /**
     * Updates the render state values of this Spatial and and children it has.
     * Should be called whenever render states change.
     */
    public void updateRenderState() {
        updateRenderState(null);
    }

    /**
     * Called internally. Updates the render states of this Spatial. The stack
     * contains parent render states.
     * 
     * @param parentStates
     *            The list of parent renderstates.
     */
    @SuppressWarnings("unchecked")
    protected void updateRenderState(Stack[] parentStates) {
        boolean initiator = (parentStates == null);

        // first we need to get all the states from parent to us.
        if (initiator) {
            // grab all states from root to here.
            parentStates = new Stack[RenderState.StateType.values().length];
            for (int x = 0; x < parentStates.length; x++)
                parentStates[x] = new Stack<RenderState>();
            propagateStatesFromRoot(parentStates);
        } else {
            for (RenderState.StateType type : RenderState.StateType.values()) {
                if (getRenderState(type) != null)
                    parentStates[type.ordinal()].push(getRenderState(type));
            }
        }

        applyRenderState(parentStates);

        // restore previous if we are not the initiator
        if (!initiator) {
            for (RenderState.StateType type : RenderState.StateType.values()) {
                if (getRenderState(type) != null)
                	parentStates[type.ordinal()].pop();
            }
        }

    }

    /**
     * Called during updateRenderState(Stack[]), this function determines how
     * the render states are actually applied to the spatial and any children it
     * may have. By default, this function does nothing.
     * 
     * @param states
     *            An array of stacks for each state.
     */
    protected void applyRenderState(Stack<? extends RenderState>[] states) {
    }

    public void sortLights() {        
    }
    
    /**
     * <code>setRenderState</code> sets a render state for this node. Note,
     * there can only be one render state per type per node. That is, there can
     * only be a single BlendState a single TextureState, etc. If there is
     * already a render state for a type set the old render state will be
     * returned. Otherwise, null is returned.
     * 
     * @param rs
     *            the render state to add.
     * @return the old render state.
     */
    public RenderState setRenderState(RenderState rs) {
        if (rs == null) {
            return null;
        }

        if (renderStateList == null) {
            renderStateList = new RenderState[RenderState.StateType.values().length];
        }

        RenderState oldState = renderStateList[rs.getStateType().ordinal()];
        renderStateList[rs.getStateType().ordinal()] = rs;
        
        return oldState;
    }

    /**
     * Returns the requested RenderState that this Spatial currently has set or
     * null if none is set.
     * 
     * @param type
     *            the renderstate type to retrieve
     * @return a renderstate at the given position or null
     * @deprecated As of 2.0, use {@link #getRenderState(com.jme.scene.state.RenderState.StateType)} instead.
     */
    public RenderState getRenderState(int type) {
        return renderStateList != null ? renderStateList[type] : null;
    }

    /**
     * Returns the requested RenderState that this Spatial currently has set or
     * null if none is set.
     * 
     * @param type
     *            the {@link RenderState.StateType} to return
     * @return a {@link RenderState} that matches the given {@link RenderState.StateType} or null
     */
    public RenderState getRenderState(RenderState.StateType type) {

        return renderStateList != null ? renderStateList[type.ordinal()] : null;
    }

    /**
     * Clears a given render state index by setting it to null.
     * 
     * @param renderStateType
     *            The index of a RenderState to clear
     * @see com.jme.scene.state.RenderState#getType()
     * @deprecated As of 2.0, use {@link #clearRenderState(com.jme.scene.state.RenderState.StateType)} instead.
     */
    public void clearRenderState(int renderStateType) {
        if (renderStateList != null) {
            renderStateList[renderStateType] = null;
        }
    }

    /**
     * Clears a given render state index by setting it to null.
     * 
     * @param renderStateType
     *            The index of a RenderState to clear
     * @see com.jme.scene.state.RenderState#getType()
     */
    public void clearRenderState(RenderState.StateType type) {
        if (renderStateList != null) {
            renderStateList[type.ordinal()] = null;
        }
    }

    /**
     * <code>setRenderQueueMode</code> determines at what phase of the
     * rendering proces this Spatial will rendered. There are 4 different
     * phases: QUEUE_SKIP - The spatial will be drawn as soon as possible,
     * before the other phases of rendering. QUEUE_OPAQUE - The renderer will
     * try to find the optimal order for rendering all objects using this mode.
     * You should use this mode for most normal objects, except transparant
     * ones, as it could give a nice performance boost to your application.
     * QUEUE_TRANSPARENT - This is the mode you should use for object with
     * transparancy in them. It will ensure the objects furthest away are
     * rendered first. That ensures when another transparent object is drawn on
     * top of previously drawn objects, you can see those (and the object drawn
     * using SKIP and OPAQUE) through the tranparant parts of the newly drawn
     * object. QUEUE_ORTHO - This is a special mode, for drawing 2D object
     * without prespective (such as GUI or HUD parts) Lastly, there is a special
     * mode, QUEUE_INHERIT, that will ensure that this spatial uses the same
     * mode as the parent Node does.
     * 
     * @param renderQueueMode
     *            The mode to use for this Spatial.
     */
    public void setRenderQueueMode(int renderQueueMode) {
        this.renderQueueMode = renderQueueMode;
    }

    /**
     * @return
     */
    public int getLocalRenderQueueMode() {
        return renderQueueMode;
    }

    /**
     * @param zOrder
     */
    public void setZOrder(int zOrder) {
        this.zOrder = zOrder;
    }

    /**
     * @return
     */
    public int getZOrder() {
        return zOrder;
    }

    /**
     * @return
     */
    public NormalsMode getLocalNormalsMode() {
        return normalsMode;
    }

    /**
     * @param mode
     */
    public void setNormalsMode(NormalsMode mode) {
        this.normalsMode = mode;
    }

    /**
     * Sets how lights from parents should be combined for this spatial.
     * 
     * @param mode
     *            The light combine mode for this spatial
     * @throws IllegalArgumentException
     *             if mode is null
     */
    public void setLightCombineMode(LightCombineMode mode) {
        if (mode == null) {
            throw new IllegalArgumentException("mode can not be null.");
        }
        this.lightCombineMode = mode;
    }

    /**
     * @return the lightCombineMode set on this Spatial
     */
    public LightCombineMode getLocalLightCombineMode() {
        return lightCombineMode;
    }

    /**
     * Sets how textures from parents should be combined for this Spatial.
     * 
     * @param mode
     *            The new texture combine mode for this spatial.
     * @throws IllegalArgumentException
     *             if mode is null
     */
    public void setTextureCombineMode(TextureCombineMode mode) {
        if (mode == null) {
            throw new IllegalArgumentException("mode can not be null.");
        }
        this.textureCombineMode = mode;
    }

    /**
     * @return the textureCombineMode set on this Spatial
     */
    public TextureCombineMode getLocalTextureCombineMode() {
        return textureCombineMode;
    }

    /**
     * Returns this spatial's last frustum intersection result. This int is set
     * when a check is made to determine if the bounds of the object fall inside
     * a camera's frustum. If a parent is found to fall outside the frustum, the
     * value for this spatial will not be updated.
     * 
     * @return The spatial's last frustum intersection result.
     */
    public Camera.FrustumIntersect getLastFrustumIntersection() {
        return frustrumIntersects;
    }

    /**
     * Overrides the last intersection result. This is useful for operations
     * that want to start rendering at the middle of a scene tree and don't want
     * the parent of that node to influence culling. (See texture renderer code
     * for example.)
     * 
     * @param intersects
     *            the new value
     */
    public void setLastFrustumIntersection(Camera.FrustumIntersect intersects) {
        frustrumIntersects = intersects;
    }

    /**
     * Returns the Spatial's name followed by the class of the spatial <br>
     * Example: "MyNode (com.jme.scene.Spatial)
     * 
     * @return Spatial's name followed by the class of the Spatial
     */
    public String toString() {
        return name + " (" + this.getClass().getName() + ')';
    }

    public Matrix4f getLocalToWorldMatrix(Matrix4f store) {
        if (store == null) {
            store = new Matrix4f();
        } else {
            store.loadIdentity();
        }
        // multiply with scale first, then rotate, finally translate (cf.
        // Eberly)
        store.scale(getWorldScale());
        store.multLocal(getWorldRotation());
        store.setTranslation(getWorldTranslation());
        return store;
    }

    public Class<? extends Spatial> getClassTag() {
        return this.getClass();
    }
}

⌨️ 快捷键说明

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