/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. ***********************************************************************/
  18. package org.mt4j.util.opengl;
  19. import java.util.Stack;
  20. import javax.media.opengl.GL;
  21. import javax.media.opengl.glu.GLU;
  22. import org.mt4j.util.logging.ILogger;
  23. import org.mt4j.util.logging.MTLoggerFactory;
  24. /**
  25. * The FBO stack manages the current opengl drawing target. It allows to switch drawing to/from different
  26. * frame buffer objects.
  27. * Usage:<br>
  28. * <br>GLFboStack.getInstance(gl).pushFBO(); //saves the current render target/frame buffer
  29. * <br>GLFboStack.getInstance(gl).useFBO(glFBO); //changes to another frame buffer
  30. * <br>GLFboStack.getInstance(gl).popFBO() //switches back to the previously saved frame buffer
  31. * @author Christopher Ruff
  32. */
  33. public class GLFboStack{
  34. /** The Constant logger. */
  35. private static final ILogger logger = MTLoggerFactory.getLogger(GLFboStack.class.getName());
  36. static{
  37. logger.setLevel(ILogger.ERROR);
  38. }
  39. /** The gl. */
  40. public GL gl;
  41. /** The current fbo. */
  42. protected int currentFBO;
  43. /** The fbo name stack. */
  44. protected Stack<Integer> fboNameStack;
  45. /** The instance. */
  46. private static GLFboStack instance = null;
  47. /**
  48. * Instantiates a new gL fbo stack.
  49. * @param gl the gl
  50. */
  51. private GLFboStack(GL gl){
  52. this.gl = gl;
  53. fboNameStack = new Stack<Integer>();
  54. currentFBO = 0;
  55. }
  56. /**
  57. * Gets the single instance of GLFboStack.
  58. *
  59. * @return single instance of GLFboStack
  60. */
  61. public static GLFboStack getInstance(){
  62. if (instance == null){
  63. instance = new GLFboStack(GLU.getCurrentGL());
  64. return instance;
  65. }else{
  66. return instance;
  67. }
  68. }
  69. /**
  70. * Pushes the currently used render target ID on the stack.
  71. */
  72. public void pushFBO(){
  73. fboNameStack.push(currentFBO);
  74. }
  75. /**
  76. * Binds the specified render target ID and sets it as current.
  77. *
  78. * @param fbo the fbo
  79. */
  80. public void useFBO(int fbo){
  81. currentFBO = fbo;
  82. gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, currentFBO);
  83. }
  84. /**
  85. * Binds the specified frame buffer object and sets it as current.
  86. *
  87. * @param fbo the fbo
  88. */
  89. public void useFBO(GLFBO fbo){
  90. currentFBO = fbo.getName();
  91. fbo.bind();
  92. }
  93. /**
  94. * Peek fbo.
  95. *
  96. * @return the int
  97. */
  98. public int peekFBO(){
  99. if (fboNameStack.isEmpty()){
  100. return 0;
  101. }else{
  102. // return fboNameStack.peek();
  103. return currentFBO;
  104. }
  105. }
  106. //NOTE THIS UNBINDS A CURRENT FBO IF SET! -> no need for calling unbind()!
  107. /**
  108. * Pops the fbo.
  109. * This switches back (binds) to the formely pushed fbo.
  110. * <br>NOTE: THIS UNBINDS A CURRENT FBO IF SET! -> no need for calling unbind()!
  111. */
  112. public void popFBO(){
  113. if (fboNameStack.isEmpty()){
  114. logger.error("Trying to pop() from an empty framebuffer stack!"); //TODO -> just bind 0 !?
  115. }else{
  116. currentFBO = fboNameStack.pop();
  117. gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, currentFBO);
  118. }
  119. }
  120. }