PageRenderTime 31ms CodeModel.GetById 21ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/mt4j/util/opengl/GLFboStack.java

http://mt4j.googlecode.com/
Java | 136 lines | 55 code | 16 blank | 65 comment | 4 complexity | d2f644334c932e479843ce552af8b5fc MD5 | raw file
  1/***********************************************************************
  2 * mt4j Copyright (c) 2008 - 2010 Christopher Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3 *  
  4 *   This program is free software: you can redistribute it and/or modify
  5 *   it under the terms of the GNU General Public License as published by
  6 *   the Free Software Foundation, either version 3 of the License, or
  7 *   (at your option) any later version.
  8 *
  9 *   This program is distributed in the hope that it will be useful,
 10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *   GNU General Public License for more details.
 13 *
 14 *   You should have received a copy of the GNU General Public License
 15 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16 *
 17 ***********************************************************************/
 18package org.mt4j.util.opengl;
 19
 20import java.util.Stack;
 21
 22import javax.media.opengl.GL;
 23import javax.media.opengl.glu.GLU;
 24
 25import org.mt4j.util.logging.ILogger;
 26import org.mt4j.util.logging.MTLoggerFactory;
 27
 28/**
 29 * The FBO stack manages the current opengl drawing target. It allows to switch drawing to/from different
 30 * frame buffer objects.
 31 * Usage:<br>
 32 * <br>GLFboStack.getInstance(gl).pushFBO(); //saves the current render target/frame buffer 
 33 * <br>GLFboStack.getInstance(gl).useFBO(glFBO); //changes to another frame buffer
 34 * <br>GLFboStack.getInstance(gl).popFBO() //switches back to the previously saved frame buffer
 35 * @author Christopher Ruff
 36 */
 37public class GLFboStack{
 38	/** The Constant logger. */
 39	private static final ILogger logger = MTLoggerFactory.getLogger(GLFboStack.class.getName());
 40	static{
 41		logger.setLevel(ILogger.ERROR);
 42	}
 43	
 44	/** The gl. */
 45	public GL gl;
 46	
 47	/** The current fbo. */
 48	protected int currentFBO;
 49	
 50	/** The fbo name stack. */
 51	protected Stack<Integer> fboNameStack;
 52	
 53	/** The instance. */
 54	private static GLFboStack instance = null;
 55
 56	/**
 57	 * Instantiates a new gL fbo stack.
 58	 * @param gl the gl
 59	 */
 60	private GLFboStack(GL gl){
 61		this.gl = gl;
 62		fboNameStack = new Stack<Integer>();
 63		currentFBO = 0;
 64	}
 65	
 66	/**
 67	 * Gets the single instance of GLFboStack.
 68	 *
 69	 * @return single instance of GLFboStack
 70	 */
 71	public static GLFboStack getInstance(){
 72		if (instance == null){
 73			instance = new GLFboStack(GLU.getCurrentGL());
 74			return instance;
 75		}else{
 76			return instance;
 77		}
 78	}
 79
 80	/**
 81	 * Pushes the currently used render target ID on the stack.
 82	 */
 83	public void pushFBO(){
 84		fboNameStack.push(currentFBO);
 85	}
 86
 87	/**
 88	 * Binds the specified render target ID and sets it as current.
 89	 * 
 90	 * @param fbo the fbo
 91	 */
 92	public void useFBO(int fbo){
 93		currentFBO = fbo;
 94		gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, currentFBO);
 95	}
 96
 97	/**
 98	 * Binds the specified frame buffer object and sets it as current.
 99	 * 
100	 * @param fbo the fbo
101	 */
102	public void useFBO(GLFBO fbo){
103		currentFBO = fbo.getName();
104		fbo.bind();
105	}
106	
107	/**
108	 * Peek fbo.
109	 *
110	 * @return the int
111	 */
112	public int peekFBO(){
113		if (fboNameStack.isEmpty()){
114			return 0;
115		}else{
116//			return fboNameStack.peek();
117			return currentFBO;
118		}
119	}
120
121	//NOTE THIS UNBINDS A CURRENT FBO IF SET! -> no need for calling unbind()!
122	/**
123	 * Pops the fbo.
124	 * This switches back (binds) to the formely pushed fbo. 
125	 * <br>NOTE: THIS UNBINDS A CURRENT FBO IF SET! -> no need for calling unbind()!
126	 */
127	public void popFBO(){
128		if (fboNameStack.isEmpty()){
129			logger.error("Trying to pop() from an empty framebuffer stack!"); //TODO -> just bind 0 !?
130		}else{
131			currentFBO = fboNameStack.pop();
132			gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, currentFBO);
133		}
134	}
135	
136}