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

📄 lwjgltexturerenderer.java

📁 java 3d game jme 工程开发源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * Copyright (c) 2003-2009 jMonkeyEngine
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jme.renderer.lwjgl;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.lwjgl.opengl.ARBDrawBuffers;
import org.lwjgl.opengl.ARBTextureFloat;
import org.lwjgl.opengl.EXTFramebufferObject;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;

import com.jme.image.Texture;
import com.jme.image.Texture2D;
import com.jme.math.FastMath;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.RenderContext;
import com.jme.renderer.TextureRenderer;
import com.jme.scene.Spatial;
import com.jme.scene.state.RenderState;
import com.jme.scene.state.lwjgl.LWJGLTextureState;
import com.jme.scene.state.lwjgl.records.TextureRecord;
import com.jme.scene.state.lwjgl.records.TextureStateRecord;
import com.jme.system.lwjgl.LWJGLDisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.geom.BufferUtils;

/**
 * This class is used by LWJGL to render textures. Users should <b>not </b>
 * create this class directly. Instead, allow DisplaySystem to create it for
 * you.
 * 
 * @author Joshua Slack, Mark Powell
 * @version $Id: LWJGLTextureRenderer.java,v 1.50 2007/11/05 01:51:54 renanse
 *          Exp $
 * @see com.jme.system.DisplaySystem#createTextureRenderer
 */
public class LWJGLTextureRenderer implements TextureRenderer {
    private static final Logger logger = Logger
            .getLogger(LWJGLTextureRenderer.class.getName());

    private LWJGLCamera camera;

    private ColorRGBA backgroundColor = new ColorRGBA(1, 1, 1, 1);

    private int active, fboID, depthRBID, width, height;

    private static boolean inited = false;
    private static boolean isSupported = true;
    private static boolean supportsMultiDraw = false;
    private static int maxDrawBuffers = 1;
    private static IntBuffer attachBuffer = null;
    private boolean usingDepthRB = false;

    private final LWJGLDisplaySystem display;
    
    private final LWJGLRenderer parentRenderer;

    public LWJGLTextureRenderer(int width, int height,
            LWJGLDisplaySystem display, LWJGLRenderer parentRenderer) {
        this.display = display;
        this.parentRenderer = parentRenderer;
        
        if (!inited) {
            isSupported = GLContext.getCapabilities().GL_EXT_framebuffer_object;
            supportsMultiDraw = GLContext.getCapabilities().GL_ARB_draw_buffers;
            if (supportsMultiDraw) {
                IntBuffer buf = BufferUtils.createIntBuffer(16);
                GL11.glGetInteger(EXTFramebufferObject.GL_MAX_COLOR_ATTACHMENTS_EXT, buf);
                maxDrawBuffers = buf.get(0);
                if (maxDrawBuffers > 1) {
                    attachBuffer = BufferUtils.createIntBuffer(maxDrawBuffers);
                    for (int i = 0; i < maxDrawBuffers; i++) {
                        attachBuffer.put(EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT+i);
                    }

                } else {
                    maxDrawBuffers = 1;
                }
            }
            if (!isSupported) {
                logger.warning("FBO not supported.");
                return;
            } else {
                logger.info("FBO support detected.");
            }
        }

        if (!GLContext.getCapabilities().GL_ARB_texture_non_power_of_two) {
            // Check if we have non-power of two sizes. If so,
            // find the smallest power of two size that is greater than
            // the provided size.
            if (!FastMath.isPowerOfTwo(width)) {
                int newWidth = 2;
                do {
                    newWidth <<= 1;

                } while (newWidth < width);
                width = newWidth;
            }

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

                } while (newHeight < height);
                height = newHeight;
            }
        }

        logger.fine("Creating FBO sized: "+width+" x "+height);

        IntBuffer buffer = BufferUtils.createIntBuffer(1);
        EXTFramebufferObject.glGenFramebuffersEXT(buffer); // generate id
        fboID = buffer.get(0);

        if (fboID <= 0) {
            logger.severe("Invalid FBO id returned! " + fboID);
            isSupported = false;
            return;
        }

        EXTFramebufferObject.glGenRenderbuffersEXT(buffer); // generate id
        depthRBID = buffer.get(0);
        EXTFramebufferObject.glBindRenderbufferEXT(
                EXTFramebufferObject.GL_RENDERBUFFER_EXT, depthRBID);
        EXTFramebufferObject.glRenderbufferStorageEXT(
                EXTFramebufferObject.GL_RENDERBUFFER_EXT,
                GL11.GL_DEPTH_COMPONENT, width, height);

        this.width = width;
        this.height = height;

        initCamera();
    }

    /**
     * <code>isSupported</code> obtains the capability of the graphics card.
     * If the graphics card does not have pbuffer support, false is returned,
     * otherwise, true is returned. TextureRenderer will not process any scene
     * elements if pbuffer is not supported.
     * 
     * @return if this graphics card supports pbuffers or not.
     */
    public boolean isSupported() {
        return isSupported;
    }

    /**
     * <code>getCamera</code> retrieves the camera this renderer is using.
     * 
     * @return the camera this renderer is using.
     */
    public Camera getCamera() {
        return camera;
    }

    /**
     * <code>setCamera</code> sets the camera this renderer should use.
     * 
     * @param camera
     *            the camera this renderer should use.
     */
    public void setCamera(Camera camera) {

        this.camera = (LWJGLCamera) camera;
    }

    /**
     * <code>setBackgroundColor</code> sets the OpenGL clear color to the
     * color specified.
     * 
     * @see com.jme.renderer.TextureRenderer#setBackgroundColor(com.jme.renderer.ColorRGBA)
     * @param c
     *            the color to set the background color to.
     */
    public void setBackgroundColor(ColorRGBA c) {
        // if color is null set background to white.
        if (c == null) {
            backgroundColor.a = 1.0f;
            backgroundColor.b = 1.0f;
            backgroundColor.g = 1.0f;
            backgroundColor.r = 1.0f;
        } else {
            backgroundColor = c;
        }
    }

    /**
     * <code>getBackgroundColor</code> retrieves the clear color of the
     * current OpenGL context.
     * 
     * @see com.jme.renderer.Renderer#getBackgroundColor()
     * @return the current clear color.
     */
    public ColorRGBA getBackgroundColor() {
        return backgroundColor;
    }

    /**
     * <code>setupTexture</code> initializes a new Texture object for use with
     * TextureRenderer. Generates a valid OpenGL texture id for this texture and
     * initializes the data type for the texture.
     */
    public void setupTexture(Texture2D tex) {
        if (!isSupported) {
            return;
        }

        IntBuffer ibuf = BufferUtils.createIntBuffer(1);

        if (tex.getTextureId() != 0) {
            ibuf.put(tex.getTextureId());
            GL11.glDeleteTextures(ibuf);
            ibuf.clear();
        }

        // Create the texture
        GL11.glGenTextures(ibuf);
        tex.setTextureId(ibuf.get(0));
        TextureManager.registerForCleanup(tex.getTextureKey(), tex
                .getTextureId());

        LWJGLTextureState.doTextureBind(tex.getTextureId(), 0, Texture.Type.TwoDimensional);
        int format = GL11.GL_RGBA;
        int components = GL11.GL_RGBA8;
        int dataType = GL11.GL_UNSIGNED_BYTE;
	switch (tex.getRTTSource()) {
	case RGBA:
	case RGBA8:
	    break;
	case RGB:
	case RGB8:
	    format = GL11.GL_RGB;
	    components = GL11.GL_RGB8;
	    break;
	case Alpha:
	case Alpha8:
	    format = GL11.GL_ALPHA;
	    components = GL11.GL_ALPHA8;
	    break;
	case Depth:
	    format = GL11.GL_DEPTH_COMPONENT;
	    components = GL11.GL_DEPTH_COMPONENT;
	    break;
	case Intensity:
	case Intensity8:
	    format = GL11.GL_INTENSITY;
	    components = GL11.GL_INTENSITY8;
	    break;
	case Luminance:
	case Luminance8:
	    format = GL11.GL_LUMINANCE;
	    components = GL11.GL_LUMINANCE8;
	    break;
	case LuminanceAlpha:
	case Luminance8Alpha8:
	    format = GL11.GL_LUMINANCE_ALPHA;
	    components = GL11.GL_LUMINANCE8_ALPHA8;
	    break;
	case Alpha4:
	    format = GL11.GL_ALPHA;
	    components = GL11.GL_ALPHA4;
	    break;
	case Alpha12:
	    format = GL11.GL_ALPHA;
	    components = GL11.GL_ALPHA12;
	    break;
	case Alpha16:
	    format = GL11.GL_ALPHA;
	    components = GL11.GL_ALPHA16;
	    break;
	case Luminance4:
	    format = GL11.GL_LUMINANCE;
	    components = GL11.GL_LUMINANCE4;
	    break;
	case Luminance12:
	    format = GL11.GL_LUMINANCE;
	    components = GL11.GL_LUMINANCE12;
	    break;
	case Luminance16:
	    format = GL11.GL_LUMINANCE;
	    components = GL11.GL_LUMINANCE16;
	    break;
	case Luminance4Alpha4:
	    format = GL11.GL_LUMINANCE_ALPHA;
	    components = GL11.GL_LUMINANCE4_ALPHA4;
	    break;
	case Luminance6Alpha2:
	    format = GL11.GL_LUMINANCE_ALPHA;
	    components = GL11.GL_LUMINANCE6_ALPHA2;
	    break;
	case Luminance12Alpha4:
	    format = GL11.GL_LUMINANCE_ALPHA;
	    components = GL11.GL_LUMINANCE12_ALPHA4;
	    break;
	case Luminance12Alpha12:
	    format = GL11.GL_LUMINANCE_ALPHA;
	    components = GL11.GL_LUMINANCE12_ALPHA12;
	    break;
	case Luminance16Alpha16:
	    format = GL11.GL_LUMINANCE_ALPHA;
	    components = GL11.GL_LUMINANCE16_ALPHA16;
	    break;
	case Intensity4:
	    format = GL11.GL_INTENSITY;
	    components = GL11.GL_INTENSITY4;
	    break;
	case Intensity12:
	    format = GL11.GL_INTENSITY;

⌨️ 快捷键说明

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