Fbo.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package eu.tankernn.gameEngine.renderEngine;
  2. import java.nio.IntBuffer;
  3. import org.lwjgl.BufferUtils;
  4. import org.lwjgl.opengl.Display;
  5. import org.lwjgl.opengl.GL11;
  6. import org.lwjgl.opengl.GL14;
  7. import org.lwjgl.opengl.GL20;
  8. import org.lwjgl.opengl.GL30;
  9. import eu.tankernn.gameEngine.loader.textures.Texture;
  10. import eu.tankernn.gameEngine.loader.textures.TextureUtils;
  11. public class Fbo {
  12. public static final int NONE = 0, DEPTH_TEXTURE = 1, DEPTH_RENDER_BUFFER = 2;
  13. protected final int width;
  14. protected final int height;
  15. protected int frameBuffer;
  16. private Texture colourTexture;
  17. private Texture depthTexture;
  18. protected int depthBuffer;
  19. /**
  20. * Creates an FBO of a specified width and height, with the desired type of
  21. * depth buffer attachment.
  22. *
  23. * @param width
  24. * - the width of the FBO.
  25. * @param height
  26. * - the height of the FBO.
  27. * @param depthBufferType
  28. * - an int indicating the type of depth buffer attachment that
  29. * this FBO should use.
  30. */
  31. public Fbo(int width, int height, int depthBufferType) {
  32. this.width = width;
  33. this.height = height;
  34. initialiseFrameBuffer(depthBufferType);
  35. }
  36. /**
  37. * Deletes the frame buffer and its attachments when the game closes.
  38. */
  39. public void cleanUp() {
  40. GL30.glDeleteFramebuffers(frameBuffer);
  41. if (colourTexture != null)
  42. colourTexture.delete();
  43. if (depthTexture != null)
  44. depthTexture.delete();
  45. GL30.glDeleteRenderbuffers(depthBuffer);
  46. }
  47. /**
  48. * Binds the frame buffer, setting it as the current render target. Anything
  49. * rendered after this will be rendered to this FBO, and not to the screen.
  50. */
  51. public void bindFrameBuffer() {
  52. GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, frameBuffer);
  53. GL11.glViewport(0, 0, width, height);
  54. }
  55. /**
  56. * Unbinds the frame buffer, setting the default frame buffer as the current
  57. * render target. Anything rendered after this will be rendered to the
  58. * screen, and not this FBO.
  59. */
  60. public void unbindFrameBuffer() {
  61. GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
  62. GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
  63. }
  64. /**
  65. * Binds the current FBO to be read from (not used in tutorial 43).
  66. */
  67. public void bindToRead() {
  68. GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
  69. GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, frameBuffer);
  70. GL11.glReadBuffer(GL30.GL_COLOR_ATTACHMENT0);
  71. }
  72. /**
  73. * @return The texture containing the color buffer of the FBO.
  74. */
  75. public Texture getColourTexture() {
  76. return colourTexture;
  77. }
  78. /**
  79. * @return The texture containing the FBOs depth buffer.
  80. */
  81. public Texture getDepthTexture() {
  82. return depthTexture;
  83. }
  84. /**
  85. * Creates the FBO along with a colour buffer texture attachment, and
  86. * possibly a depth buffer.
  87. *
  88. * @param type
  89. * - the type of depth buffer attachment to be attached to the
  90. * FBO.
  91. */
  92. protected void initialiseFrameBuffer(int type) {
  93. createFrameBuffer();
  94. colourTexture = TextureUtils.createTextureAttachment(width, height);
  95. if (type == DEPTH_RENDER_BUFFER) {
  96. createDepthBufferAttachment();
  97. } else if (type == DEPTH_TEXTURE) {
  98. depthTexture = TextureUtils.createDepthTextureAttachment(width, height);
  99. }
  100. unbindFrameBuffer();
  101. }
  102. protected void determineDrawBuffers() {
  103. IntBuffer drawBuffers = BufferUtils.createIntBuffer(2);
  104. drawBuffers.put(GL30.GL_COLOR_ATTACHMENT0);
  105. drawBuffers.flip();
  106. GL20.glDrawBuffers(drawBuffers);
  107. }
  108. /**
  109. * Creates a new frame buffer object and sets the buffer to which drawing
  110. * will occur - colour attachment 0. This is the attachment where the colour
  111. * buffer texture is.
  112. */
  113. protected void createFrameBuffer() {
  114. frameBuffer = GL30.glGenFramebuffers();
  115. GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
  116. determineDrawBuffers();
  117. }
  118. /**
  119. * Adds a depth buffer to the FBO in the form of a render buffer. This can't
  120. * be used for sampling in the shaders.
  121. */
  122. protected void createDepthBufferAttachment() {
  123. depthBuffer = GL30.glGenRenderbuffers();
  124. GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
  125. GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL14.GL_DEPTH_COMPONENT24, width, height);
  126. GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER,
  127. depthBuffer);
  128. }
  129. }