1. package graphics;
  2.  
  3. import java.io.IOException;
  4. import java.nio.ByteBuffer;
  5. import java.nio.ByteOrder;
  6. import java.nio.IntBuffer;
  7.  
  8. import org.lwjgl.opengl.Display;
  9. import org.lwjgl.opengl.EXTFramebufferObject;
  10. import org.lwjgl.opengl.GL11;
  11. import org.lwjgl.opengl.GLContext;
  12. import org.newdawn.slick.opengl.InternalTextureLoader;
  13. import org.newdawn.slick.opengl.Texture;
  14.  
  15. public class OffscreenTexture
  16. {
  17.     private int myFBOId;
  18.     private int m_iWidth;
  19.     private int m_iHeight;
  20.     private Texture m_texture;
  21.     //private int m_iTextureID;
  22.     private boolean drawn;
  23.    
  24.     public OffscreenTexture(int width, int height)
  25.     {
  26.         drawn = false;
  27.         m_iWidth = width;
  28.         m_iHeight = height;
  29.         CreateTexture();
  30.         boolean FBOEnabled = GLContext.getCapabilities().GL_EXT_framebuffer_object;
  31.        
  32.         if (!FBOEnabled)
  33.             return;
  34.        
  35.         IntBuffer buffer = ByteBuffer.allocateDirect(1*4).order(ByteOrder.nativeOrder()).asIntBuffer(); // allocate a 1 int byte buffer
  36.         EXTFramebufferObject.glGenFramebuffersEXT( buffer ); // generate
  37.         myFBOId = buffer.get();
  38.        
  39.         EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, myFBOId );
  40.         EXTFramebufferObject.glFramebufferTexture2DEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT,
  41.                         GL11.GL_TEXTURE_2D, m_texture.getTextureID(), 0);
  42.     }
  43.    
  44.     private void CreateTexture()
  45.     {
  46.         /*  m_iTextureID = GL11.glGenTextures();
  47.             // initialize color texture
  48.             GL11.glBindTexture(GL11.GL_TEXTURE_2D, m_iTextureID);                                   // Bind the colorbuffer texture
  49.             GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);               // make it linear filterd
  50.             GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, m_iWidth, m_iHeight, 0,GL11.GL_RGBA, GL11.GL_INT, (java.nio.ByteBuffer) null);  // Create the texture data
  51.         */
  52.        
  53.         try
  54.         {
  55.             m_texture = InternalTextureLoader.get().createTexture(m_iWidth, m_iHeight, GL11.GL_TEXTURE_2D);
  56.         } catch (IOException e)
  57.         {
  58.             // TODO Auto-generated catch block
  59.             e.printStackTrace();
  60.         }
  61.     }
  62.  
  63.     public void TestDraw()
  64.     {
  65.         GL11.glLoadIdentity();
  66.        
  67.         GL11.glColor4f(1,0,0,1);
  68.         Graphics.DrawSquare(0, 0, m_iWidth, m_iHeight);
  69.        
  70.         drawn = true;
  71.     }
  72.    
  73.     public void Bind()
  74.     {
  75.         GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
  76.         EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, myFBOId);
  77.         GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT);
  78.         GL11.glViewport( 0, 0, m_iWidth, m_iHeight );
  79.        
  80.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  81.         GL11.glLoadIdentity();
  82.        
  83.         GL11.glOrtho(0, m_iWidth, m_iHeight, 0, -1, 1);
  84.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  85.         GL11.glLoadIdentity();
  86.        
  87.         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
  88.     }
  89.    
  90.     public void UnBind()
  91.     {
  92.         EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
  93.         GL11.glPopAttrib();
  94.        
  95.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  96.         GL11.glLoadIdentity();
  97.         GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
  98.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  99.         GL11.glLoadIdentity();
  100.     }
  101.    
  102.     public int GetTextureID()
  103.     {
  104.         return m_texture.getTextureID();
  105.     }
  106.  
  107.     public boolean Drawn()
  108.     {
  109.         return drawn;
  110.     }
  111. }
  112.  
  113.