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

📄 renderqueue.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        swap = opaqueBucket;
        opaqueBucket = opaqueBackBucket;
        opaqueBackBucket = swap;
    }

    /**
     * Renders the opaque, clone, transparent, and ortho buckets in that order.
     */
    public void renderBuckets() {
        renderOpaqueBucket();
        renderTransparentBucket();
        renderOrthoBucket();
    }

    /**
     * Renders the opaque buckets. Those closest to the camera are rendered
     * first.
     */
    private void renderOpaqueBucket() {
        opaqueBucket.sort();        
        for (int i = 0; i < opaqueBucket.listSize; i++) {
            opaqueBucket.list[i].draw(renderer);
        }
        opaqueBucket.clear();
    }

    /**
     * Renders the transparent buckets. Those farthest from the camera are
     * rendered first. Note that any items in the transparent bucket will
     * have their cullstate values overridden. Therefore, any settings assigned
     * to the cullstate of the rendered object will not be used.
     */
    private void renderTransparentBucket() {
        transparentBucket.sort();
            for (int i = 0; i < transparentBucket.listSize; i++) {
                Spatial obj = transparentBucket.list[i]; 

                if (twoPassTransparent
                    && obj instanceof Geometry
                    && (((Geometry) obj).states[RenderState.StateType.Cull.ordinal()] != null 
                    		&& !((Geometry) obj).states[RenderState.StateType.Cull.ordinal()].isEnabled())) {
                    Geometry geom = (Geometry)obj;
                    RenderState oldCullState = geom.states[RenderState.StateType.Cull.ordinal()];
                    geom.states[RenderState.StateType.Cull.ordinal()] = tranCull;
                    ZBufferState oldZState = (ZBufferState)geom.states[RenderState.StateType.ZBuffer.ordinal()];
                    geom.states[RenderState.StateType.ZBuffer.ordinal()] = tranZBuff;

                    // first render back-facing tris only
                    tranCull.setCullFace(CullState.Face.Front);
                    obj.draw(renderer);
                    
                    // then render front-facing tris only
                    geom.states[RenderState.StateType.ZBuffer.ordinal()] = oldZState;
                    tranCull.setCullFace(CullState.Face.Back);
                    obj.draw(renderer);
                    geom.states[RenderState.StateType.Cull.ordinal()] = oldCullState;
                } else {
                    // draw as usual
                    obj.draw(renderer);
                }
                obj.queueDistance = Float.NEGATIVE_INFINITY;
            }
        transparentBucket.clear();
    }

    /**
     * Renders the ortho buckets. Those will the highest ZOrder are rendered
     * first.
     */
    private void renderOrthoBucket() {
        orthoBucket.sort();
        if (orthoBucket.listSize > 0) {
            renderer.setOrtho();
            for (int i = 0; i < orthoBucket.listSize; i++) {
                orthoBucket.list[i].draw(renderer);
            }
            renderer.unsetOrtho();
        }
        orthoBucket.clear();
    }

    /**
     * This class is a special function list of Spatial objects for render
     * queueing.
     * 
     * @author Jack Lindamood
     * @author Three Rings - better sorting alg.
     */
    private class SpatialList {

        Spatial[] list, tlist;

        int listSize;

        private static final int DEFAULT_SIZE = 32;

        private Comparator<Spatial> c;

        SpatialList(Comparator<Spatial> c) {
            listSize = 0;
            list = new Spatial[DEFAULT_SIZE];
            this.c = c;
        }

        /**
         * Adds a spatial to the list. List size is doubled if there is no room.
         * 
         * @param s
         *            The spatial to add.
         */
        void add(Spatial s) {
            if (listSize == list.length) {
                Spatial[] temp = new Spatial[listSize * 2];
                System.arraycopy(list, 0, temp, 0, listSize);
                list = temp;
            }
            list[listSize++] = s;
        }

        /**
         * Resets list size to 0.
         */
        void clear() {
            for (int i = 0; i < listSize; i++)
                list[i] = null;
            if (tlist != null)
                Arrays.fill(tlist, null);
            listSize = 0;
        }

        /**
         * Sorts the elements in the list acording to their Comparator.
         */
        void sort() {
            if (listSize > 1) {
                // resize or populate our temporary array as necessary
                if (tlist == null || tlist.length != list.length) {
                    tlist = list.clone();
                } else {
                    System.arraycopy(list, 0, tlist, 0, list.length);
                }
                // now merge sort tlist into list
                SortUtil.msort(tlist, list, 0, listSize, c);
            }
        }
    }

    private class OpaqueComp implements Comparator<Spatial> {

        public int compare(Spatial o1, Spatial o2) {
            if (o1 instanceof Geometry && o2 instanceof Geometry) {
                return compareByStates((Geometry) o1, (Geometry) o2);
            }
            
            float d1 = distanceToCam(o1);
            float d2 = distanceToCam(o2);
            if (d1 == d2)
                return 0;
            else if (d1 < d2)
                return -1;
            else
                return 1;           
        }

        /**
         * Compare opaque items by their texture states - generally the most
         * expensive switch. Later this might expand to comparisons by other
         * states as well, such as lighting or material.
         */
        private int compareByStates(Geometry g1, Geometry g2) {
            TextureState ts1 = (TextureState)g1.states[RenderState.StateType.Texture.ordinal()];
            TextureState ts2 = (TextureState)g2.states[RenderState.StateType.Texture.ordinal()];
            if (ts1 == ts2) return 0;
            else if (ts1 == null && ts2 != null) return -1;
            else if (ts2 == null && ts1 != null) return  1;
                        
            for (int x = 0, nots = Math.min(ts1.getNumberOfSetTextures(), ts2.getNumberOfSetTextures()); x < nots; x++) {
                
                int tid1 = ts1.getTextureID(x);
                int tid2 = ts2.getTextureID(x);                
                if (tid1 == tid2)
                    continue;
                else if (tid1 < tid2)
                    return -1;
                else
                    return 1;
            }

            if (ts1.getNumberOfSetTextures() != ts2.getNumberOfSetTextures()) {
                return ts2.getNumberOfSetTextures() - ts1.getNumberOfSetTextures();
            }

            return 0;
        }
    }

    private class TransparentComp implements Comparator<Spatial> {

        public int compare(Spatial o1, Spatial o2) {
            float d1 = distanceToCam(o1);
            float d2 = distanceToCam(o2);
            if (d1 == d2)
                return 0;
            else if (d1 < d2)
                return 1;
            else
                return -1;
        }
    }

    private class OrthoComp implements Comparator<Spatial> {
        public int compare(Spatial o1, Spatial o2) {
            if (o2.getZOrder() == o1.getZOrder()) {
                return 0;
            } else if (o2.getZOrder() < o1.getZOrder()) {
                return -1;
            } else {
                return 1;
            }
        }
    }
}

⌨️ 快捷键说明

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