package graphics; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.EXTFramebufferObject; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GLContext; import org.newdawn.slick.opengl.InternalTextureLoader; import org.newdawn.slick.opengl.Texture; public class OffscreenTexture { private int myFBOId; private int m_iWidth; private int m_iHeight; private Texture m_texture; //private int m_iTextureID; private boolean drawn; public OffscreenTexture(int width, int height) { drawn = false; m_iWidth = width; m_iHeight = height; CreateTexture(); boolean FBOEnabled = GLContext.getCapabilities().GL_EXT_framebuffer_object; if (!FBOEnabled) return; IntBuffer buffer = ByteBuffer.allocateDirect(1*4).order(ByteOrder.nativeOrder()).asIntBuffer(); // allocate a 1 int byte buffer EXTFramebufferObject.glGenFramebuffersEXT( buffer ); // generate myFBOId = buffer.get(); EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, myFBOId ); EXTFramebufferObject.glFramebufferTexture2DEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, m_texture.getTextureID(), 0); } private void CreateTexture() { /* m_iTextureID = GL11.glGenTextures(); // initialize color texture GL11.glBindTexture(GL11.GL_TEXTURE_2D, m_iTextureID); // Bind the colorbuffer texture GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR); // make it linear filterd 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 */ try { m_texture = InternalTextureLoader.get().createTexture(m_iWidth, m_iHeight, GL11.GL_TEXTURE_2D); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void TestDraw() { GL11.glLoadIdentity(); GL11.glColor4f(1,0,0,1); Graphics.DrawSquare(0, 0, m_iWidth, m_iHeight); drawn = true; } public void Bind() { GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, myFBOId); GL11.glPushAttrib(GL11.GL_VIEWPORT_BIT); GL11.glViewport( 0, 0, m_iWidth, m_iHeight ); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, m_iWidth, m_iHeight, 0, -1, 1); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); } public void UnBind() { EXTFramebufferObject.glBindFramebufferEXT( EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0); GL11.glPopAttrib(); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); } public int GetTextureID() { return m_texture.getTextureID(); } public boolean Drawn() { return drawn; } }