debugger.java

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

JAVA
681
字号
                    lineInds = BufferUtils.createIntBuffer(geom
                            .getVertexCount() * 2);
                    normalLines.setIndexBuffer(lineInds);
                } else {
                    lineInds.clear();
                    lineInds.limit(normalLines.getVertexCount());
                }

                verts.rewind();
                norms.rewind();
                lineVerts.rewind();
                lineInds.rewind();

                for (int x = 0; x < geom.getVertexCount(); x++) {
                    _normalVect.set(verts.get(), verts.get(), verts.get());
                    _normalVect.multLocal(geom.getWorldScale());
                    lineVerts.put(_normalVect.x);
                    lineVerts.put(_normalVect.y);
                    lineVerts.put(_normalVect.z);

                    lineColors.put(NORMAL_COLOR_BASE.r);
                    lineColors.put(NORMAL_COLOR_BASE.g);
                    lineColors.put(NORMAL_COLOR_BASE.b);
                    lineColors.put(NORMAL_COLOR_BASE.a);

                    lineInds.put(x * 2);

                    _normalVect.addLocal(norms.get() * rSize, norms.get()
                            * rSize, norms.get() * rSize);
                    lineVerts.put(_normalVect.x);
                    lineVerts.put(_normalVect.y);
                    lineVerts.put(_normalVect.z);

                    lineColors.put(NORMAL_COLOR_TIP.r);
                    lineColors.put(NORMAL_COLOR_TIP.g);
                    lineColors.put(NORMAL_COLOR_TIP.b);
                    lineColors.put(NORMAL_COLOR_TIP.a);

                    lineInds.put((x * 2) + 1);
                }

                normalLines.setLocalTranslation(geom.getWorldTranslation());
                normalLines.setLocalRotation(geom.getWorldRotation());
                normalLines.onDraw(r);
            }

        }

        if (doChildren && element instanceof Node) {
            Node n = (Node) element;
            if (n.getChildren() != null) {
                for (int i = n.getChildren().size(); --i >= 0;)
                    drawNormals(n.getChild(i), r, size, true);
            }
        }
    }

    public static void drawTangents(Spatial element, Renderer r,
            float size, boolean doChildren) {
        if (element == null)
            return;

        if (normZState == null) {
            normZState = r.createZBufferState();
            normalLines.setRenderState(normZState);
            normalLines.updateRenderState();
        }

        int state = r.getCamera().getPlaneState();
        if (element.getWorldBound() != null
                && r.getCamera().contains(element.getWorldBound()) == Camera.FrustumIntersect.Outside) {
            r.getCamera().setPlaneState(state);
            return;
        }
        r.getCamera().setPlaneState(state);
        if (element instanceof Geometry
                && element.getCullHint() != Spatial.CullHint.Always) {
            Geometry geom = (Geometry) element;

            float rSize = size;
            if (rSize == -1) {
                BoundingVolume vol = element.getWorldBound();
                if (vol != null) {
                    measureBox.setCenter(vol.getCenter());
                    measureBox.xExtent = 0;
                    measureBox.yExtent = 0;
                    measureBox.zExtent = 0;
                    measureBox.mergeLocal(vol);
                    rSize = AUTO_NORMAL_RATIO
                            * ((measureBox.xExtent + measureBox.yExtent + measureBox.zExtent) / 3f);
                } else
                    rSize = 1.0f;
            }

            FloatBuffer norms = geom.getTangentBuffer();
            FloatBuffer verts = geom.getVertexBuffer();
            if (norms != null && verts != null
                    && norms.limit() == verts.limit()) {
                FloatBuffer lineVerts = normalLines.getVertexBuffer();
                if (lineVerts.capacity() < (3 * (2 * geom.getVertexCount()))) {
                    normalLines.setVertexBuffer(null);
                    System.gc();
                    lineVerts = BufferUtils.createVector3Buffer(geom
                            .getVertexCount() * 2);
                    normalLines.setVertexBuffer(lineVerts);
                } else {
                    normalLines.setVertexCount(2 * geom.getVertexCount());
                    lineVerts.clear();
                }

                FloatBuffer lineColors = normalLines.getColorBuffer();
                if (lineColors.capacity() < (4 * (2 * geom.getVertexCount()))) {
                    normalLines.setColorBuffer(null);
                    System.gc();
                    lineColors = BufferUtils.createColorBuffer(geom
                            .getVertexCount() * 2);
                    normalLines.setColorBuffer(lineColors);
                } else {
                    lineColors.clear();
                }

                IntBuffer lineInds = normalLines.getIndexBuffer();
                if (lineInds == null
                        || lineInds.capacity() < (normalLines.getVertexCount())) {
                    normalLines.setIndexBuffer(null);
                    System.gc();
                    lineInds = BufferUtils.createIntBuffer(geom
                            .getVertexCount() * 2);
                    normalLines.setIndexBuffer(lineInds);
                } else {
                    lineInds.clear();
                    lineInds.limit(normalLines.getVertexCount());
                }

                verts.rewind();
                norms.rewind();
                lineVerts.rewind();
                lineInds.rewind();

                for (int x = 0; x < geom.getVertexCount(); x++) {
                    _normalVect.set(verts.get(), verts.get(), verts.get());
                    _normalVect.multLocal(geom.getWorldScale());
                    lineVerts.put(_normalVect.x);
                    lineVerts.put(_normalVect.y);
                    lineVerts.put(_normalVect.z);

                    lineColors.put(TANGENT_COLOR_BASE.r);
                    lineColors.put(TANGENT_COLOR_BASE.g);
                    lineColors.put(TANGENT_COLOR_BASE.b);
                    lineColors.put(TANGENT_COLOR_BASE.a);

                    lineInds.put(x * 2);

                    _normalVect.addLocal(norms.get() * rSize, norms.get()
                            * rSize, norms.get() * rSize);
                    lineVerts.put(_normalVect.x);
                    lineVerts.put(_normalVect.y);
                    lineVerts.put(_normalVect.z);

                    lineColors.put(TANGENT_COLOR_BASE.r);
                    lineColors.put(TANGENT_COLOR_BASE.g);
                    lineColors.put(TANGENT_COLOR_BASE.b);
                    lineColors.put(TANGENT_COLOR_BASE.a);

                    lineInds.put((x * 2) + 1);
                }

                if (geom != null) {
                    normalLines.setLocalTranslation(geom.getWorldTranslation());
                    normalLines.setLocalRotation(geom.getWorldRotation());
                    normalLines.onDraw(r);
                }
            }

        }

        if (doChildren && element instanceof Node) {
            Node n = (Node) element;
            if (n.getChildren() != null) {
                for (int i = n.getChildren().size(); --i >= 0;)
                    drawTangents(n.getChild(i), r, size, true);
            }
        }
    }

    // -- **** METHODS FOR DRAWING AXIS **** -- //

    private static final AxisRods rods = new AxisRods("debug_rods", true, 1);
    static {
        rods.setRenderQueueMode(Renderer.QUEUE_SKIP);
    }
    private static boolean axisInited = false;

    public static void drawAxis(Spatial spat, Renderer r) {
        drawAxis(spat, r, true, false);
    }

    public static void drawAxis(Spatial spat, Renderer r, boolean drawChildren, boolean drawAll) {
        if (!axisInited) {
            BlendState blendState = r.createBlendState();
            blendState.setBlendEnabled(true);
            blendState.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
            blendState.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
            rods.setRenderState(blendState);
            rods.updateRenderState();
            rods.updateGeometricState(0, false);
            axisInited = true;
        }

        if (drawAll
                || (spat instanceof Geometry && !(spat.getParent() instanceof SkinNode))
                || (spat instanceof SkinNode)) {
            if (spat.getWorldBound() != null) {
                float rSize;
                BoundingVolume vol = spat.getWorldBound(); 
                if (vol != null) {
                    measureBox.setCenter(vol.getCenter());
                    measureBox.xExtent = 0;
                    measureBox.yExtent = 0;
                    measureBox.zExtent = 0;
                    measureBox.mergeLocal(vol);
                    rSize = 1f * ((measureBox.xExtent + measureBox.yExtent + measureBox.zExtent) / 3f);
                } else
                    rSize = 1.0f;

                rods.getLocalTranslation().set(spat.getWorldBound().getCenter());
                rods.getLocalScale().set(rSize, rSize, rSize);
            } else {
                rods.getLocalTranslation().set(spat.getWorldTranslation());
                rods.getLocalScale().set(spat.getWorldScale());
            }
            rods.getLocalRotation().set(spat.getWorldRotation());
            rods.updateGeometricState(0, false);
    
            rods.draw(r);
        }

        if ((spat instanceof Node) && drawChildren) {
            Node n = (Node) spat;
            if (n.getChildren() == null) return;
            for (int x = 0, count = n.getChildren().size(); x < count; x++) {
                drawAxis(n.getChild(x), r, drawChildren, drawAll);
            }
        }
    }


    // -- **** METHODS FOR DISPLAYING BUFFERS **** -- //
    public static final int NORTHWEST = 0;
    public static final int NORTHEAST = 1;
    public static final int SOUTHEAST = 2;
    public static final int SOUTHWEST = 3;

    private static final Quad bQuad = new Quad("", 128, 128);
    private static Texture2D bufTexture;
    private static TextureRenderer bufTexRend;

    static {
        bQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);
        bQuad.setCullHint(Spatial.CullHint.Never);
    }

    public static void drawBuffer(Texture.RenderToTextureType rttSource, int location, Renderer r) {
        drawBuffer(rttSource, location, r, r.getWidth() / 6.25f);
    }

    public static void drawBuffer(Texture.RenderToTextureType rttSource, int location, Renderer r,
            float size) {
        r.flush();
        float locationX = r.getWidth(), locationY = r.getHeight();
        bQuad.resize(size, (r.getHeight() / (float) r.getWidth()) * size);
        if (bQuad.getRenderState(RenderState.StateType.Texture) == null) {
            TextureState ts = r.createTextureState();
            bufTexture = new Texture2D();
            ts.setTexture(bufTexture);
            bQuad.setRenderState(ts);
            bQuad.updateRenderState();
        }

        bufTexture.setRenderToTextureType(rttSource);

        if (bufTexRend == null) {
            bufTexRend = DisplaySystem.getDisplaySystem()
                    .createTextureRenderer(256, 256,
                            TextureRenderer.Target.Texture2D);
            bufTexRend.setupTexture(bufTexture);
        }
        int width = r.getWidth();
        if (!FastMath.isPowerOfTwo(width)) {
            int newWidth = 2;
            do {
                newWidth <<= 1;

            } while (newWidth < width);
            bQuad.getTextureCoords(0).coords.put(4, width / (float) newWidth);
            bQuad.getTextureCoords(0).coords.put(6, width / (float) newWidth);
            width = newWidth;
        }

        int height = r.getHeight();
        if (!FastMath.isPowerOfTwo(height)) {
            int newHeight = 2;
            do {
                newHeight <<= 1;

            } while (newHeight < height);
            bQuad.getTextureCoords(0).coords.put(1, height / (float) newHeight);
            bQuad.getTextureCoords(0).coords.put(7, height / (float) newHeight);
            height = newHeight;
        }

        bufTexRend.copyToTexture(bufTexture, width, height);

        float loc = size * .75f;
        switch (location) {
            case NORTHWEST:
                locationX = loc;
                locationY -= loc;
                break;
            case NORTHEAST:
                locationX -= loc;
                locationY -= loc;
                break;
            case SOUTHEAST:
                locationX -= loc;
                locationY = loc;
                break;
            case SOUTHWEST:
            default:
                locationX = loc;
                locationY = loc;
                break;
        }

        bQuad.getWorldTranslation().set(locationX, locationY, 0);

        bQuad.onDraw(r);
        r.flush();
    }
}

⌨️ 快捷键说明

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