Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FBOTest {
- public static void main(String[] args) {
- int width = 400;
- int height = 400;
- int fps = 60;
- try {
- Display.setDisplayMode(new DisplayMode(width, height));
- Display.create();
- Display.setVSyncEnabled(true);
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_BLEND);
- glDisable(GL_DEPTH_TEST);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glViewport(0, 0, width, height);
- glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, width, height, 0, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.375f, 0.375f, 0.0f);
- // Initialize frame buffer object
- int fbo = glGenFramebuffersEXT();
- // Set up a texture to render to
- int texWidth = 2;
- int texHeight = 2;
- int texture = glGenTextures();
- ByteBuffer buffer =
- BufferUtils.createByteBuffer(texWidth*texHeight*8);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexImage2D(
- GL_TEXTURE_2D,
- 0,
- GL_RGBA8,
- texWidth,
- texHeight,
- 0,
- GL_RGBA,
- GL_UNSIGNED_BYTE,
- buffer);
- glBindTexture(GL_TEXTURE_2D, 0);
- while(!Display.isCloseRequested()) {
- // Render to the texture
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
- glFramebufferTexture2DEXT(
- GL_FRAMEBUFFER_EXT,
- GL_COLOR_ATTACHMENT0_EXT,
- GL_TEXTURE_2D,
- texture,
- 0);
- glViewport(0, 0, texWidth, texHeight);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, texWidth, texHeight, 0, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(0.375f, 0.375f, 0.0f);
- // Draw a square in the top left corner
- glColor3f(
- (float) Math.random(),
- (float) Math.random(),
- (float) Math.random());
- glBegin(GL_QUADS);
- {
- int xCenter = texWidth/2;
- int yCenter = texHeight/2;
- glVertex2i(0, 0);
- glVertex2i(xCenter, 0);
- glVertex2i(xCenter, yCenter);
- glVertex2i(0, yCenter);
- }
- glEnd();
- // Render to the screen
- glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
- glClear(GL_COLOR_BUFFER_BIT);
- // Draw a square using the texture
- glBindTexture(GL_TEXTURE_2D, texture);
- glColor3f(1.0f, 1.0f, 1.0f);
- glBegin(GL_QUADS);
- {
- glTexCoord2f(0.0f, 0.0f);
- glVertex2i(0, 0);
- glTexCoord2f(1.0f, 0.0f);
- glVertex2i(width, 0);
- glTexCoord2f(1.0f, 1.0f);
- glVertex2i(width, height);
- glTexCoord2f(0.0f, 1.0f);
- glVertex2i(0, height);
- }
- glEnd();
- Display.update();
- Display.sync(fps);
- }
- } catch(LWJGLException e) {
- e.printStackTrace();
- } finally {
- Display.destroy();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement