📄 jogltexturerenderer.java
字号:
/*
* 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.jogl;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
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.jogl.JOGLTextureState;
import com.jme.scene.state.jogl.records.TextureRecord;
import com.jme.scene.state.jogl.records.TextureStateRecord;
import com.jme.system.jogl.JOGLDisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.geom.BufferUtils;
/**
* This class is used by JOGL 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
* @author Steve Vaughan - JOGL port
* @version $Id: JOGLTextureRenderer.java,v 1.50 2007/11/05 01:51:54 renanse
* Exp $
* @see com.jme.system.DisplaySystem#createTextureRenderer
*/
public class JOGLTextureRenderer implements TextureRenderer {
private static final Logger logger = Logger
.getLogger(JOGLTextureRenderer.class.getName());
private JOGLCamera 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 JOGLDisplaySystem display;
private final JOGLRenderer parentRenderer;
public JOGLTextureRenderer(int width, int height,
JOGLDisplaySystem display, JOGLRenderer parentRenderer) {
final GL gl = GLU.getCurrentGL();
this.display = display;
this.parentRenderer = parentRenderer;
if (!inited) {
isSupported = gl.isExtensionAvailable("GL_EXT_framebuffer_object");
supportsMultiDraw = gl.isExtensionAvailable("GL_ARB_draw_buffers");
if (supportsMultiDraw) {
IntBuffer buf = BufferUtils.createIntBuffer(16);
gl.glGetIntegerv(GL.GL_MAX_COLOR_ATTACHMENTS_EXT, buf); // TODO Check for integer
maxDrawBuffers = buf.get(0);
if (maxDrawBuffers > 1) {
attachBuffer = BufferUtils.createIntBuffer(maxDrawBuffers);
for (int i = 0; i < maxDrawBuffers; i++) {
attachBuffer.put(GL.GL_COLOR_ATTACHMENT0_EXT+i);
}
} else {
maxDrawBuffers = 1;
}
}
if (!isSupported) {
logger.warning("FBO not supported.");
return;
} else {
logger.info("FBO support detected.");
}
}
if (!gl.isExtensionAvailable("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);
gl.glGenFramebuffersEXT(buffer.limit(),buffer); // TODO Check <size> // generate id
fboID = buffer.get(0);
if (fboID <= 0) {
logger.severe("Invalid FBO id returned! " + fboID);
isSupported = false;
return;
}
gl.glGenRenderbuffersEXT(buffer.limit(),buffer); // TODO Check <size> // generate id
depthRBID = buffer.get(0);
gl.glBindRenderbufferEXT(
GL.GL_RENDERBUFFER_EXT, depthRBID);
gl.glRenderbufferStorageEXT(
GL.GL_RENDERBUFFER_EXT,
GL.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 = (JOGLCamera) 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) {
final GL gl = GLU.getCurrentGL();
if (!isSupported) {
return;
}
IntBuffer ibuf = BufferUtils.createIntBuffer(1);
if (tex.getTextureId() != 0) {
ibuf.put(tex.getTextureId());
gl.glDeleteTextures(ibuf.limit(),ibuf); // TODO Check <size>
ibuf.clear();
}
// Create the texture
gl.glGenTextures(ibuf.limit(),ibuf); // TODO Check <size>
tex.setTextureId(ibuf.get(0));
TextureManager.registerForCleanup(tex.getTextureKey(), tex
.getTextureId());
JOGLTextureState.doTextureBind(tex.getTextureId(), 0,
Texture.Type.TwoDimensional);
int components = GL.GL_RGBA8;
int format = GL.GL_RGBA;
int dataType = GL.GL_UNSIGNED_BYTE;
switch (tex.getRTTSource()) {
case RGBA:
case RGBA8:
break;
case RGB:
case RGB8:
format = GL.GL_RGB;
components = GL.GL_RGB8;
break;
case Alpha:
case Alpha8:
format = GL.GL_ALPHA;
components = GL.GL_ALPHA8;
break;
case Depth:
format = GL.GL_DEPTH_COMPONENT;
components = GL.GL_DEPTH_COMPONENT;
break;
case Intensity:
case Intensity8:
format = GL.GL_INTENSITY;
components = GL.GL_INTENSITY8;
break;
case Luminance:
case Luminance8:
format = GL.GL_LUMINANCE;
components = GL.GL_LUMINANCE8;
break;
case LuminanceAlpha:
case Luminance8Alpha8:
format = GL.GL_LUMINANCE_ALPHA;
components = GL.GL_LUMINANCE8_ALPHA8;
break;
case Alpha4:
format = GL.GL_ALPHA;
components = GL.GL_ALPHA4;
break;
case Alpha12:
format = GL.GL_ALPHA;
components = GL.GL_ALPHA12;
break;
case Alpha16:
format = GL.GL_ALPHA;
components = GL.GL_ALPHA16;
break;
case Luminance4:
format = GL.GL_LUMINANCE;
components = GL.GL_LUMINANCE4;
break;
case Luminance12:
format = GL.GL_LUMINANCE;
components = GL.GL_LUMINANCE12;
break;
case Luminance16:
format = GL.GL_LUMINANCE;
components = GL.GL_LUMINANCE16;
break;
case Luminance4Alpha4:
format = GL.GL_LUMINANCE_ALPHA;
components = GL.GL_LUMINANCE4_ALPHA4;
break;
case Luminance6Alpha2:
format = GL.GL_LUMINANCE_ALPHA;
components = GL.GL_LUMINANCE6_ALPHA2;
break;
case Luminance12Alpha4:
format = GL.GL_LUMINANCE_ALPHA;
components = GL.GL_LUMINANCE12_ALPHA4;
break;
case Luminance12Alpha12:
format = GL.GL_LUMINANCE_ALPHA;
components = GL.GL_LUMINANCE12_ALPHA12;
break;
case Luminance16Alpha16:
format = GL.GL_LUMINANCE_ALPHA;
components = GL.GL_LUMINANCE16_ALPHA16;
break;
case Intensity4:
format = GL.GL_INTENSITY;
components = GL.GL_INTENSITY4;
break;
case Intensity12:
format = GL.GL_INTENSITY;
components = GL.GL_INTENSITY12;
break;
case Intensity16:
format = GL.GL_INTENSITY;
components = GL.GL_INTENSITY4;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -