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

📄 lwjgltexturerenderer.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	    components = GL11.GL_INTENSITY12;
	    break;
	case Intensity16:
	    format = GL11.GL_INTENSITY;
	    components = GL11.GL_INTENSITY4;
	    break;
	case R3_G3_B2:
	    format = GL11.GL_RGB;
	    components = GL11.GL_R3_G3_B2;
	    break;
	case RGB4:
	    format = GL11.GL_RGB;
	    components = GL11.GL_RGB4;
	    break;
	case RGB5:
	    format = GL11.GL_RGB;
	    components = GL11.GL_RGB5;
	    break;
	case RGB10:
	    format = GL11.GL_RGB;
	    components = GL11.GL_RGB10;
	    break;
	case RGB12:
	    format = GL11.GL_RGB;
	    components = GL11.GL_RGB12;
	    break;
	case RGB16:
	    format = GL11.GL_RGB;
	    components = GL11.GL_RGB16;
	    break;
	case RGBA2:
	    format = GL11.GL_RGBA;
	    components = GL11.GL_RGBA2;
	    break;
	case RGBA4:
	    format = GL11.GL_RGBA;
	    components = GL11.GL_RGBA4;
	    break;
	case RGB5_A1:
	    format = GL11.GL_RGBA;
	    components = GL11.GL_RGB5_A1;
	    break;
	case RGB10_A2:
	    format = GL11.GL_RGBA;
	    components = GL11.GL_RGB10_A2;
	    break;
	case RGBA12:
	    format = GL11.GL_RGBA;
	    components = GL11.GL_RGBA12;
	    break;
	case RGBA16:
	    format = GL11.GL_RGBA;
	    components = GL11.GL_RGBA16;
	    break;
	case RGBA32F:
	    format = GL11.GL_RGBA;
	    components = ARBTextureFloat.GL_RGBA32F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case RGB32F:
	    format = GL11.GL_RGB;
	    components = ARBTextureFloat.GL_RGB32F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case Alpha32F:
	    format = GL11.GL_ALPHA;
	    components = ARBTextureFloat.GL_ALPHA32F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case Intensity32F:
	    format = GL11.GL_INTENSITY;
	    components = ARBTextureFloat.GL_INTENSITY32F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case Luminance32F:
	    format = GL11.GL_LUMINANCE;
	    components = ARBTextureFloat.GL_LUMINANCE32F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case LuminanceAlpha32F:
	    format = GL11.GL_LUMINANCE_ALPHA;
	    components = ARBTextureFloat.GL_LUMINANCE_ALPHA32F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case RGBA16F:
	    format = GL11.GL_RGBA;
	    components = ARBTextureFloat.GL_RGBA16F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case RGB16F:
	    format = GL11.GL_RGB;
	    components = ARBTextureFloat.GL_RGB16F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case Alpha16F:
	    format = GL11.GL_ALPHA;
	    components = ARBTextureFloat.GL_ALPHA16F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case Intensity16F:
	    format = GL11.GL_INTENSITY;
	    components = ARBTextureFloat.GL_INTENSITY16F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case Luminance16F:
	    format = GL11.GL_LUMINANCE;
	    components = ARBTextureFloat.GL_LUMINANCE16F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	case LuminanceAlpha16F:
	    format = GL11.GL_LUMINANCE_ALPHA;
	    components = ARBTextureFloat.GL_LUMINANCE_ALPHA16F_ARB;
	    dataType = GL11.GL_FLOAT;
	    break;
	}

	// Initialize our texture with some default data.
	if (dataType == GL11.GL_UNSIGNED_BYTE) {
	    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, components, width, height,
		    0, format, dataType, (ByteBuffer) null);
	} else {
	    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, components, width, height,
		    0, format, dataType, (FloatBuffer) null);
	}

        // Initialize mipmapping for this texture, if requested
        if (tex.getMinificationFilter().usesMipMapLevels()) {
            EXTFramebufferObject.glGenerateMipmapEXT(GL11.GL_TEXTURE_2D);
        }

        // Setup filtering and wrap
        RenderContext<?> context = display.getCurrentContext();
        TextureStateRecord record = (TextureStateRecord) context.getStateRecord(RenderState.StateType.Texture);
        TextureRecord texRecord = record.getTextureRecord(tex.getTextureId(), tex.getType());

        LWJGLTextureState.applyFilter(tex, texRecord, 0, record);
        LWJGLTextureState.applyWrap(tex, texRecord, 0, record);

        logger.info("setup fbo tex with id " + tex.getTextureId() + ": " + width
                + "," + height);
    }

    /**
     * <code>render</code> renders a scene. As it recieves a base class of
     * <code>Spatial</code> the renderer hands off management of the scene to
     * spatial for it to determine when a <code>Geometry</code> leaf is
     * reached. The result of the rendering is then copied into the given
     * texture(s). What is copied is based on the Texture object's rttSource
     * field.
     * 
     * @param toDraw
     *            the scene to render.
     * @param tex
     *            the Texture(s) to render it to.
     */
    public void render(Spatial toDraw, Texture tex) {
        render(toDraw, tex, true);
    }

    /**
     * <code>render</code> renders a scene. As it recieves a base class of
     * <code>Spatial</code> the renderer hands off management of the scene to
     * spatial for it to determine when a <code>Geometry</code> leaf is
     * reached. The result of the rendering is then copied into the given
     * texture(s). What is copied is based on the Texture object's rttSource
     * field.
     * 
     * @param toDraw
     *            the scene to render.
     * @param tex
     *            the Texture(s) to render it to.
     */
    public void render(Spatial toDraw, Texture tex, boolean doClear) {
        if (!isSupported) {
            return;
        }

        try {
            activate();
            
            setupForSingleTexDraw(tex, doClear);

            doDraw(toDraw);

            takedownForSingleTexDraw(tex);

            deactivate();
        } catch (Exception e) {
            logger.logp(Level.SEVERE, this.getClass().toString(),
                    "render(Spatial, Texture, boolean)", "Exception", e);
        }
    }

    public void render(ArrayList<? extends Spatial> toDraw,
            ArrayList<Texture> texs) {
        render(toDraw, texs, true);
    }

    public void render(ArrayList<? extends Spatial> toDraw, ArrayList<Texture> texs, boolean doClear) {
        if (!isSupported) {
            return;
        }

        // if we only support 1 draw buffer at a time anyway, we'll have to render to each texture individually...
        if (maxDrawBuffers == 1 || texs.size() == 1) {
            try {
                activate();
                for (int i = 0; i < texs.size(); i++) {
                    Texture tex = texs.get(i);
                    
                    setupForSingleTexDraw(tex, doClear);

                    doDraw(toDraw);

                    takedownForSingleTexDraw(tex);
                }
            } catch (Exception e) {
                logger.logp(Level.SEVERE, this.getClass().toString(),
                        "render(Spatial, Texture, boolean)", "Exception", e);
            } finally {
                deactivate();
            }
            return;
        }
        try {
            activate();

            // Otherwise, we can streamline this by rendering to multiple textures at once.
            // first determine how many groups we need
            LinkedList<Texture> depths = new LinkedList<Texture>();
            LinkedList<Texture> colors = new LinkedList<Texture>();
            for (int i = 0; i < texs.size(); i++) {
                Texture tex = texs.get(i);
                if (tex.getRTTSource() == Texture.RenderToTextureType.Depth) {
                    depths.add(tex);
                } else {
                    colors.add(tex);
                }
            }
            // we can only render to 1 depth texture at a time, so # groups is at minimum == numDepth
            int groups = Math.max(depths.size(), (int)(0.999f + (colors.size() / (float)maxDrawBuffers)));
            for (int i = 0; i < groups; i++) {
                // First handle colors
                int colorsAdded = 0;
                while (colorsAdded < maxDrawBuffers && !colors.isEmpty()) {
                    Texture tex = colors.removeFirst();
                    EXTFramebufferObject.glFramebufferTexture2DEXT(
                            EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                            EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT + colorsAdded,
                            GL11.GL_TEXTURE_2D, tex.getTextureId(), 0);
                    colorsAdded++;
                }

                // Now take care of depth.
                if (!depths.isEmpty()) {
                    Texture tex = depths.removeFirst();
                    // Set up our depth texture
                    EXTFramebufferObject.glFramebufferTexture2DEXT(
                            EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                            EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT,
                            GL11.GL_TEXTURE_2D, tex.getTextureId(), 0);
                    usingDepthRB = false;
                } else if (!usingDepthRB) {
                    // setup our default depth render buffer if not already set
                    EXTFramebufferObject.glFramebufferRenderbufferEXT(
                            EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                            EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT,
                            EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthRBID);
                    usingDepthRB = true;
                }
                
                setDrawBuffers(colorsAdded);
                setReadBuffer(colorsAdded != 0 ? EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT : GL11.GL_NONE);
                
                // Check FBO complete
                checkFBOComplete();
                
                switchCameraIn(doClear);
                
                doDraw(toDraw);
                
                switchCameraOut();
            }
        
            // automatically generate mipmaps for our textures.
            for (int x = 0, max = texs.size(); x < max; x++) {
                if (texs.get(x).getMinificationFilter().usesMipMapLevels()) {
                    LWJGLTextureState.doTextureBind(texs.get(x).getTextureId(), 0, Texture.Type.TwoDimensional);
                    EXTFramebufferObject.glGenerateMipmapEXT(GL11.GL_TEXTURE_2D);
                }
            }
            
        } catch (Exception e) {
            logger.logp(Level.SEVERE, this.getClass().toString(),
                    "render(Spatial, Texture)", "Exception", e);
        } finally {
            deactivate();
        }
    }

    private void setupForSingleTexDraw(Texture tex, boolean doClear) {

        LWJGLTextureState.doTextureBind(tex.getTextureId(), 0, Texture.Type.TwoDimensional);

        if (tex.getRTTSource() == Texture.RenderToTextureType.Depth) {
            // Setup depth texture into FBO
            EXTFramebufferObject.glFramebufferTexture2DEXT(
                    EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                    EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT,
                    GL11.GL_TEXTURE_2D, tex.getTextureId(), 0);

            setDrawBuffer(GL11.GL_NONE);
            setReadBuffer(GL11.GL_NONE);
        } else {
            // Set textures into FBO
            EXTFramebufferObject.glFramebufferTexture2DEXT(
                    EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                    EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,
                    GL11.GL_TEXTURE_2D, tex.getTextureId(), 0);

            // setup depth RB
            EXTFramebufferObject.glFramebufferRenderbufferEXT(
                    EXTFramebufferObject.GL_FRAMEBUFFER_EXT,
                    EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT,
                    EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthRBID);
   
            setDrawBuffer(EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT);
            setReadBuffer(EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT);
        }

        // Check FBO complete
        checkFBOComplete();

        switchCameraIn(doClear);
    }

    private void setReadBuffer(int attachVal) {
        GL11.glReadBuffer(attachVal);
    }

    private void setDrawBuffer(int attachVal) {
        GL11.glDrawBuffer(attachVal);
    }

    private void setDrawBuffers(int maxEntry) {
        if (maxEntry <= 1) {
            setDrawBuffer(maxEntry != 0 ? EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT : GL11.GL_NONE);
        } else {
            // We should only get to this point if we support ARBDrawBuffers.

⌨️ 快捷键说明

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