Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.10 KB | None | 0 0
  1. class FBOTest {
  2.     public static void main(String[] args) {
  3.         int width = 400;
  4.         int height = 400;
  5.         int fps = 60;
  6.        
  7.         try {
  8.             Display.setDisplayMode(new DisplayMode(width, height));
  9.             Display.create();
  10.             Display.setVSyncEnabled(true);
  11.            
  12.             glEnable(GL_TEXTURE_2D);
  13.             glEnable(GL_BLEND);
  14.             glDisable(GL_DEPTH_TEST);
  15.             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  16.             glViewport(0, 0, width, height);
  17.             glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
  18.            
  19.             glMatrixMode(GL_PROJECTION);
  20.             glLoadIdentity();
  21.             glOrtho(0, width, height, 0, -1, 1);
  22.            
  23.             glMatrixMode(GL_MODELVIEW);
  24.             glLoadIdentity();
  25.             glTranslatef(0.375f, 0.375f, 0.0f);
  26.            
  27.             // Initialize frame buffer object
  28.             int fbo = glGenFramebuffersEXT();
  29.  
  30.             // Set up a texture to render to
  31.             int texWidth = 2;
  32.             int texHeight = 2;
  33.             int texture = glGenTextures();
  34.             ByteBuffer buffer =
  35.                 BufferUtils.createByteBuffer(texWidth*texHeight*8);
  36.             glBindTexture(GL_TEXTURE_2D, texture);
  37.             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  38.             glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  39.             glTexImage2D(
  40.                 GL_TEXTURE_2D,
  41.                 0,
  42.                 GL_RGBA8,
  43.                 texWidth,
  44.                 texHeight,
  45.                 0,
  46.                 GL_RGBA,
  47.                 GL_UNSIGNED_BYTE,
  48.                 buffer);
  49.             glBindTexture(GL_TEXTURE_2D, 0);
  50.            
  51.             while(!Display.isCloseRequested()) {
  52.                 // Render to the texture
  53.                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
  54.                 glFramebufferTexture2DEXT(
  55.                     GL_FRAMEBUFFER_EXT,
  56.                     GL_COLOR_ATTACHMENT0_EXT,
  57.                     GL_TEXTURE_2D,
  58.                     texture,
  59.                     0);
  60.                
  61.                 glViewport(0, 0, texWidth, texHeight);
  62.                 glMatrixMode(GL_PROJECTION);
  63.                 glLoadIdentity();
  64.                 glOrtho(0, texWidth, texHeight, 0, -1, 1);
  65.                 glMatrixMode(GL_MODELVIEW);
  66.                 glLoadIdentity();
  67.                 glTranslatef(0.375f, 0.375f, 0.0f);
  68.                
  69.                 // Draw a square in the top left corner
  70.                 glColor3f(
  71.                     (float) Math.random(),
  72.                     (float) Math.random(),
  73.                     (float) Math.random());
  74.                 glBegin(GL_QUADS);
  75.                 {
  76.                     int xCenter = texWidth/2;
  77.                     int yCenter = texHeight/2;
  78.                     glVertex2i(0, 0);
  79.                     glVertex2i(xCenter, 0);
  80.                     glVertex2i(xCenter, yCenter);
  81.                     glVertex2i(0, yCenter);
  82.                    
  83.                 }
  84.                 glEnd();
  85.                
  86.                 // Render to the screen
  87.                 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  88.                
  89.                 glClear(GL_COLOR_BUFFER_BIT);
  90.                
  91.                 // Draw a square using the texture
  92.                 glBindTexture(GL_TEXTURE_2D, texture);
  93.                 glColor3f(1.0f, 1.0f, 1.0f);
  94.                 glBegin(GL_QUADS);
  95.                 {
  96.                     glTexCoord2f(0.0f, 0.0f);
  97.                     glVertex2i(0, 0);
  98.                     glTexCoord2f(1.0f, 0.0f);
  99.                     glVertex2i(width, 0);
  100.                     glTexCoord2f(1.0f, 1.0f);
  101.                     glVertex2i(width, height);
  102.                     glTexCoord2f(0.0f, 1.0f);
  103.                     glVertex2i(0, height);
  104.                 }
  105.                 glEnd();
  106.                
  107.                 Display.update();
  108.                 Display.sync(fps);
  109.             }
  110.         } catch(LWJGLException e) {
  111.             e.printStackTrace();
  112.         } finally {
  113.             Display.destroy();
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement