Advertisement
Guest User

Untitled

a guest
Mar 20th, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. package space.engine;
  2.  
  3. import static org.lwjgl.opengl.GL11.*;
  4. import org.lwjgl.opengl.GLContext;
  5. import org.newdawn.slick.SlickException;
  6. import org.newdawn.slick.opengl.Texture;
  7. import org.newdawn.slick.opengl.renderer.Renderer;
  8.  
  9. import static org.lwjgl.opengl.EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT;
  10. import static org.lwjgl.opengl.EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT;
  11. import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_COMPLETE_EXT;
  12. import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_EXT;
  13. import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
  14. import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
  15. import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
  16. import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
  17. import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
  18. import static org.lwjgl.opengl.EXTFramebufferObject.GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
  19. import static org.lwjgl.opengl.EXTFramebufferObject.GL_RENDERBUFFER_EXT;
  20. import static org.lwjgl.opengl.EXTFramebufferObject.glBindFramebufferEXT;
  21. import static org.lwjgl.opengl.EXTFramebufferObject.glBindRenderbufferEXT;
  22. import static org.lwjgl.opengl.EXTFramebufferObject.glCheckFramebufferStatusEXT;
  23. import static org.lwjgl.opengl.EXTFramebufferObject.glDeleteFramebuffersEXT;
  24. import static org.lwjgl.opengl.EXTFramebufferObject.glDeleteRenderbuffersEXT;
  25. import static org.lwjgl.opengl.EXTFramebufferObject.glFramebufferRenderbufferEXT;
  26. import static org.lwjgl.opengl.EXTFramebufferObject.glFramebufferTexture2DEXT;
  27. import static org.lwjgl.opengl.EXTFramebufferObject.glGenFramebuffersEXT;
  28. import static org.lwjgl.opengl.EXTFramebufferObject.glGenRenderbuffersEXT;
  29. import static org.lwjgl.opengl.EXTFramebufferObject.glRenderbufferStorageEXT;
  30.  
  31. public class FBO {
  32.    
  33.     public static boolean isSupported() {
  34.         return GLContext.getCapabilities().GL_EXT_framebuffer_object;
  35.     }
  36.    
  37.     /** Our render texture. */
  38.     private Texture texture;
  39.     /** The ID of the FBO in use */
  40.     private int id;
  41.     private int width, height;
  42.     private int pushAttrib = GL_VIEWPORT_BIT | GL_TRANSFORM_BIT | GL_COLOR_BUFFER_BIT;
  43.    
  44.     public static final int NO_BITS = 0;
  45.    
  46.     public FBO(Texture texture) throws SlickException {
  47.         if (!isSupported()) {
  48.             throw new SlickException("Your OpenGL card does not support FBO and hence can't handle " +
  49.                     "the dynamic images required for this application.");
  50.         }
  51.         this.texture = texture;
  52.         this.width = texture.getImageWidth();
  53.         this.height = texture.getImageHeight();
  54.         try {
  55.             texture.bind();
  56.             id = glGenFramebuffersEXT();
  57.             glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
  58.             glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  59.                                       GL_TEXTURE_2D, texture.getTextureID(), 0);
  60.             completeCheck();
  61.             glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  62.         } catch (SlickException ex) {
  63.             throw ex;
  64.         } catch (Exception e) {
  65.             throw new SlickException("Failed to create new texture for FBO");
  66.         }
  67.     }
  68.    
  69.     public FBO(int width, int height) throws SlickException {
  70.         this(TextureUtils.createEmptyTexture(width, height));
  71.     }
  72.    
  73.     public void setPushAttrib(int attrib) {
  74.         this.pushAttrib = attrib;
  75.     }
  76.    
  77.     public int getPushAttrib() {
  78.         return pushAttrib;
  79.     }
  80.    
  81.     public int getWidth() {
  82.         return width;
  83.     }
  84.    
  85.     public int getHeight() {
  86.         return height;
  87.     }
  88.    
  89.     public int getID() {
  90.         return id;
  91.     }
  92.    
  93.     public Texture getTexture() {
  94.         return texture;
  95.     }
  96.    
  97.     /**
  98.      * Check the FBO for completeness as shown in the LWJGL tutorial
  99.      *
  100.      * @throws SlickException Indicates an incomplete FBO
  101.      */
  102.     private void completeCheck() throws SlickException {
  103.         int framebuffer = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  104.         switch ( framebuffer ) {
  105.             case GL_FRAMEBUFFER_COMPLETE_EXT:
  106.                 break;
  107.             case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
  108.                 throw new SlickException( "FrameBuffer: " + id
  109.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT exception" );
  110.             case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
  111.                 throw new SlickException( "FrameBuffer: " + id
  112.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT exception" );
  113.             case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
  114.                 throw new SlickException( "FrameBuffer: " + id
  115.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT exception" );
  116.             case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
  117.                 throw new SlickException( "FrameBuffer: " + id
  118.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT exception" );
  119.             case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
  120.                 throw new SlickException( "FrameBuffer: " + id
  121.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT exception" );
  122.             case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
  123.                 throw new SlickException( "FrameBuffer: " + id
  124.                         + ", has caused a GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT exception" );
  125.             default:
  126.                 throw new SlickException( "Unexpected reply from glCheckFramebufferStatusEXT: " + framebuffer);
  127.         }
  128.     }
  129.    
  130.     /**
  131.      * Bind to the FBO created
  132.      */
  133.     public void bind() {
  134.         if (id == 0)
  135.             throw new IllegalStateException("can't use FBO as it has been destroyed..");
  136.         if (pushAttrib != NO_BITS)
  137.             glPushAttrib(pushAttrib);
  138.        
  139.         glViewport(0, 0, width, height);
  140.         glMatrixMode(GL_PROJECTION);
  141.         glPushMatrix();
  142.         glLoadIdentity();
  143.         glOrtho(0, width, 0, height, 1, -1);
  144.         glMatrixMode(GL_MODELVIEW);
  145.         glPushMatrix();
  146.         glLoadIdentity();
  147.        
  148.         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, id);
  149.     }
  150.    
  151.     /**
  152.      * Unbind from the FBO created
  153.      */
  154.     public void unbind() {
  155.         if (id==0)
  156.             return;
  157.         Renderer.get().flush();
  158.         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  159.        
  160.         glMatrixMode(GL_PROJECTION);
  161.         glPopMatrix();
  162.         glMatrixMode(GL_MODELVIEW);
  163.         glPopMatrix();
  164.         if (pushAttrib != NO_BITS)
  165.             glPopAttrib();
  166.     }
  167.    
  168.        
  169.     /**
  170.      * @see org.newdawn.slick.Graphics#destroy()
  171.      */
  172.     public void release() {
  173.         glDeleteFramebuffersEXT(id);
  174.         id = 0;
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement