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

📄 lwjglrenderer.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                    ARBBufferObject.glBindBufferARB(
                            ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vbo
                                    .getVBOVertexID());
                    ARBBufferObject.glBufferDataARB(
                            ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, g
                                    .getVertexBuffer(),
                            ARBBufferObject.GL_STATIC_DRAW_ARB);
                }
            }
        }

        if (g instanceof TriMesh) {

            if (vbo.isVBOIndexEnabled() && vbo.getVBOIndexID() <= 0) {
                TriMesh tb = (TriMesh) g;
                if (tb.getIndexBuffer() != null) {
                    Object vboid;
                    if ((vboid = vboMap.get(tb.getIndexBuffer())) != null) {
                        vbo.setVBOIndexID(((Integer) vboid).intValue());
                    } else {
                        tb.getIndexBuffer().rewind();
                        int vboID = rendRecord.makeVBOId();
                        vbo.setVBOIndexID(vboID);
                        vboMap.put(tb.getIndexBuffer(), vboID);

                        rendRecord.invalidateVBO(); // make sure we set it...
                        rendRecord.setBoundElementVBO(vbo.getVBOIndexID());
                        ARBBufferObject
                                .glBufferDataARB(
                                        ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB,
                                        tb.getIndexBuffer(),
                                        ARBBufferObject.GL_STATIC_DRAW_ARB);

                    }
                }
            }
        }

        if (vbo.isVBONormalEnabled() && vbo.getVBONormalID() <= 0) {
            if (g.getNormalBuffer() != null) {

                Object vboid;
                if ((vboid = vboMap.get(g.getNormalBuffer())) != null) {
                    vbo.setVBONormalID(((Integer) vboid).intValue());
                } else {
                    g.getNormalBuffer().rewind();
                    int vboID = rendRecord.makeVBOId();
                    vbo.setVBONormalID(vboID);
                    vboMap.put(g.getNormalBuffer(), vboID);

                    rendRecord.invalidateVBO(); // make sure we set it...
                    rendRecord.setBoundVBO(vbo.getVBONormalID());
                    ARBBufferObject.glBufferDataARB(
                            ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, g
                                    .getNormalBuffer(),
                            ARBBufferObject.GL_STATIC_DRAW_ARB);
                }
            }
        }
        if (vbo.isVBOColorEnabled() && vbo.getVBOColorID() <= 0) {
            if (g.getColorBuffer() != null) {
                Object vboid;
                if ((vboid = vboMap.get(g.getColorBuffer())) != null) {
                    vbo.setVBOColorID(((Integer) vboid).intValue());
                } else {
                    g.getColorBuffer().rewind();
                    int vboID = rendRecord.makeVBOId();
                    vbo.setVBOColorID(vboID);
                    vboMap.put(g.getColorBuffer(), vboID);

                    rendRecord.invalidateVBO(); // make sure we set it...
                    rendRecord.setBoundVBO(vbo.getVBOColorID());
                    ARBBufferObject.glBufferDataARB(
                            ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, g
                                    .getColorBuffer(),
                            ARBBufferObject.GL_STATIC_DRAW_ARB);
                }
            }
        }
        if (supportsFogCoords && vbo.isVBOFogCoordsEnabled() && vbo.getVBOFogCoordsID() <= 0) {
            if (g.getFogBuffer() != null) {
                Object vboid;
                if ((vboid = vboMap.get(g.getFogBuffer())) != null) {
                    vbo.setVBOFogCoordsID(((Integer) vboid).intValue());
                } else {
                    g.getFogBuffer().rewind();
                    int vboID = rendRecord.makeVBOId();
                    vbo.setVBOFogCoordsID(vboID);
                    vboMap.put(g.getFogBuffer(), vboID);

                    rendRecord.invalidateVBO(); // make sure we set it...
                    rendRecord.setBoundVBO(vbo.getVBOFogCoordsID());
                    ARBBufferObject.glBufferDataARB(
                            ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, g
                                    .getFogBuffer(),
                            ARBBufferObject.GL_STATIC_DRAW_ARB);
                }
            }
        }
        if (vbo.isVBOTextureEnabled()) {
            for (int i = 0; i < g.getNumberOfUnits(); i++) {

                if (vbo.getVBOTextureID(i) <= 0
                        && g.getTextureCoords(i) != null) {
                    Object vboid;
                    TexCoords texC = g.getTextureCoords(i);
                    if ((vboid = vboMap.get(texC.coords)) != null) {
                        vbo.setVBOTextureID(i, ((Integer) vboid).intValue());
                    } else {
                        texC.coords.rewind();
                        int vboID = rendRecord.makeVBOId();
                        vbo.setVBOTextureID(i, vboID);
                        vboMap.put(texC.coords, vboID);

                        rendRecord.invalidateVBO(); // make sure we set it...
                        rendRecord.setBoundVBO(vbo.getVBOTextureID(i));
                        ARBBufferObject.glBufferDataARB(
                                ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, texC.coords,
                                ARBBufferObject.GL_STATIC_DRAW_ARB);
                    }
                }
            }
        }
    }

    /**
     * <code>draw</code> renders a scene by calling the nodes
     * <code>onDraw</code> method.
     * 
     * @see com.jme.renderer.Renderer#draw(com.jme.scene.Spatial)
     */
    public void draw(final Spatial s) {
        if (camera != null)
            camera.apply();

        if (s != null) {
            s.onDraw(this);
        }
    }

    /**
     * <code>draw</code> renders a text object using a predefined font.
     * 
     * @see com.jme.renderer.Renderer#draw(com.jme.scene.Text)
     */
    public void draw(Text t) {
        if (font == null) {
            font = new LWJGLFont();
        }
        font.setColor(t.getTextColor());
        applyStates(t.states, null);
        if (Debug.stats) {
            StatCollector.startStat(StatType.STAT_RENDER_TIMER);
        }
        
        font.print(this, t.getWorldTranslation().x, t
                .getWorldTranslation().y, t.getWorldScale(), t.getText(), 0);
        
        if (Debug.stats) {
            StatCollector.endStat(StatType.STAT_RENDER_TIMER);
        }
    }

    /**
     * checkAndAdd is used to process the Spatial for the render queue.
     * It's queue mode is checked, and it is added to the proper queue. If the
     * queue mode is QUEUE_SKIP, false is returned.
     * 
     * @return true if the Spatial was added to a queue, false otherwise.
     */
    public boolean checkAndAdd(Spatial s) {
        int rqMode = s.getRenderQueueMode();
        if (rqMode != Renderer.QUEUE_SKIP) {
            getQueue().addToQueue(s, rqMode);
            return true;
        }
        return false;
    }

    /**
     * Return true if the system running this supports VBO
     * 
     * @return boolean true if VBO supported
     */
    public boolean supportsVBO() {
        return supportsVBO;
    }

    /**
     * re-initializes the GL context for rendering of another piece of geometry.
     */
    protected void postdrawGeometry(Geometry g) {
        // Nothing to do here
    }

    /**
     * <code>flush</code> tells opengl to send through all currently waiting
     * commands in the buffer.
     */
    public void flush() {
        GL11.glFlush();
    }

    /**
     * <code>finish</code> is similar to flush, however it blocks until all
     * waiting OpenGL commands have been finished.
     */
    public void finish() {
        GL11.glFinish();
    }

    /**
     * Prepares the GL Context for rendering this geometry. This involves
     * initializing the VBO and obtaining the buffer data.
     * 
     * @param g
     *            the geometry to process.
     * @return true if VBO is used for indicis, false if not
     */
    protected boolean predrawGeometry(Geometry g) {
        RenderContext<?> context = DisplaySystem.getDisplaySystem()
                .getCurrentContext();
        RendererRecord rendRecord = (RendererRecord) context
                .getRendererRecord();

        VBOInfo vbo = !generatingDisplayList ? g.getVBOInfo() : null;
        if (vbo != null && supportsVBO()) {
            prepVBO(g);
        }

        indicesVBO = false;

        // set up data to be sent to card
        // first to go is vertices
        int oldLimit = -1;
        FloatBuffer vertices = g.getVertexBuffer();
        if (vertices != null) {
            oldLimit = vertices.limit();
            // make sure only the necessary verts are sent through on old cards.
            vertices.limit(g.getVertexCount() * 3);
        }
        if ((supportsVBO && vbo != null && vbo.getVBOVertexID() > 0)) { // use
            // VBO
            GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
            rendRecord.setBoundVBO(vbo.getVBOVertexID());
            GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
        } else if (vertices == null) {
            GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
        } else if (prevVerts != vertices) {
            // verts have changed
            GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
            // ensure no VBO is bound
            if (supportsVBO)
                rendRecord.setBoundVBO(0);
            vertices.rewind();
            GL11.glVertexPointer(3, 0, vertices);
        }

        if (oldLimit != -1)
            vertices.limit(oldLimit);
        prevVerts = vertices;

        // apply fogging coordinate if support and the buffer is set for this
        // tri mesh
        if (supportsFogCoords) {
            oldLimit = -1;
            FloatBuffer fogCoords = g.getFogBuffer();
            if (fogCoords != null) {
                oldLimit = fogCoords.limit();
                // make sure only the necessary verts are sent through on old cards.
                fogCoords.limit(g.getVertexCount());
            }
            if ((supportsVBO && vbo != null && vbo.getVBOVertexID() > 0)) { // use
                // VBO
                GL11.glEnableClientState(EXTFogCoord.GL_FOG_COORDINATE_ARRAY_EXT);
                rendRecord.setBoundVBO(vbo.getVBOVertexID());
                EXTFogCoord.glFogCoordPointerEXT(GL11.GL_FLOAT, 0, 0);
            } else if (fogCoords == null) {
                GL11.glDisableClientState(EXTFogCoord.GL_FOG_COORDINATE_ARRAY_EXT);
            } else if (prevFogCoords != fogCoords) {
                // fog coords have changed
                GL11.glEnableClientState(EXTFogCoord.GL_FOG_COORDINATE_ARRAY_EXT);
                // ensure no VBO is bound
                if (supportsVBO)
                    rendRecord.setBoundVBO(0);
                fogCoords.rewind();
                EXTFogCoord.glFogCoordPointerEXT(0, g.getFogBuffer());
            }
            if (oldLimit != -1)
                fogCoords.limit(oldLimit);
            prevFogCoords = fogCoords;
        }

        if (g instanceof TriMesh) {
            if ((supportsVBO && vbo != null && vbo.getVBOIndexID() > 0)) { // use VBO
                indicesVBO = true;
                rendRecord.setBoundElementVBO(vbo.getVBOIndexID());
            } else if (supportsVBO) {
                rendRecord.setBoundElementVBO(0);
            }
        }

        Spatial.NormalsMode normMode = g.getNormalsMode();
        if (normMode != Spatial.NormalsMode.Off) {
            applyNormalMode(normMode, g);
            FloatBuffer normals = g.getNormalBuffer();
            oldLimit = -1;
            if (normals != null) {
                // make sure only the necessary normals are sent through on old
                // cards.
                oldLimit = normals.limit();
                normals.limit(g.getVertexCount() * 3);
            }
            if ((supportsVBO && vbo != null && vbo.getVBONormalID() > 0)) { // use
                // VBO
                GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
                rendRecord.setBoundVBO(vbo.getVBONormalID());
                GL11.glNormalPointer(GL11.GL_FLOAT, 0, 0);
            } else if (normals == null) {
                GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
            } else if (prevNorms != normals) {
                // textures have changed
                GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
                // ensure no VBO is bound
                if (supportsVBO)
                    rendRecord.setBoundVBO(0);
                normals.rewind();
                GL11.glNormalPointer(0, normals);
            }
            if (oldLimit != -1)
                normals.limit(oldLimit);
            prevNorms = normals;
        } else {
            if (prevNormMode == GL12.GL_RESCALE_NORMAL) {
                GL11.glDisable(GL12.GL_RESCALE_NORMAL);
                prevNormMode = GL11.GL_ZERO;
            } else if (prevNormMode == GL11.GL_NORMALIZE) {
                GL11.glDisable(GL11.GL_NORMALIZE);
                prevNormMode = GL11.GL_ZERO;
            }
            oldLimit = -1;
            GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
            prevNorms = null;
        }

        FloatBuffer colors = g.getColorBuffer();
        oldLimit = -1;
        if (colors != null) {
            // make sure only the necessary colors are sent through on old
            // cards.
            oldLimit = colors.limit();
            colors.limit(g.getVertexCount() * 4);
        }
        if ((supportsVBO && vbo != null && vbo.getVBOColorID() > 0)) { // use
            // VBO

⌨️ 快捷键说明

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