MultisampleMultitargetFbo.java 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.TextureUtils;
  10. import eu.tankernn.gameEngine.settings.Settings;
  11. public class MultisampleMultitargetFbo extends Fbo {
  12. private int colorBuffer;
  13. private int colorBuffer2;
  14. /**
  15. * Creates a multisampled FBO of a specified width and height.
  16. *
  17. * @param width - the width of the FBO.
  18. * @param height - the height of the FBO.
  19. */
  20. public MultisampleMultitargetFbo(int width, int height) {
  21. super(width, height, DEPTH_RENDER_BUFFER);
  22. }
  23. public void resolveToFbo(int readBuffer, Fbo outputFbo) {
  24. GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, outputFbo.frameBuffer);
  25. GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, this.frameBuffer);
  26. GL11.glReadBuffer(readBuffer);
  27. GL30.glBlitFramebuffer(0, 0, width, height, 0, 0, outputFbo.width, outputFbo.height, GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT, GL11.GL_NEAREST);
  28. this.unbindFrameBuffer();
  29. }
  30. public void resolveToScreen() {
  31. GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, 0);
  32. GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, this.frameBuffer);
  33. GL11.glDrawBuffer(GL11.GL_BACK);
  34. GL30.glBlitFramebuffer(0, 0, width, height, 0, 0, Display.getWidth(), Display.getHeight(), GL11.GL_COLOR_BUFFER_BIT, GL11.GL_NEAREST);
  35. this.unbindFrameBuffer();
  36. }
  37. protected int createMultisampleColorAttachment(int attachment) {
  38. int colorBuffer = GL30.glGenRenderbuffers();
  39. GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, colorBuffer);
  40. GL30.glRenderbufferStorageMultisample(GL30.GL_RENDERBUFFER, Settings.MULTISAMPLING, GL11.GL_RGBA8, width, height);
  41. GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, attachment, GL30.GL_RENDERBUFFER,
  42. colorBuffer);
  43. return colorBuffer;
  44. }
  45. @Override
  46. protected void createDepthBufferAttachment() {
  47. depthBuffer = GL30.glGenRenderbuffers();
  48. GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
  49. GL30.glRenderbufferStorageMultisample(GL30.GL_RENDERBUFFER, Settings.MULTISAMPLING, GL14.GL_DEPTH_COMPONENT24, width, height);
  50. GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER,
  51. depthBuffer);
  52. }
  53. @Override
  54. protected void initialiseFrameBuffer(int type) {
  55. createFrameBuffer();
  56. colorBuffer = createMultisampleColorAttachment(GL30.GL_COLOR_ATTACHMENT0);
  57. colorBuffer2 = createMultisampleColorAttachment(GL30.GL_COLOR_ATTACHMENT1);
  58. if (type == DEPTH_RENDER_BUFFER) {
  59. createDepthBufferAttachment();
  60. } else if (type == DEPTH_TEXTURE) {
  61. TextureUtils.createDepthTextureAttachment(width, height);
  62. }
  63. unbindFrameBuffer();
  64. }
  65. @Override
  66. protected void determineDrawBuffers() {
  67. IntBuffer drawBuffers = BufferUtils.createIntBuffer(2);
  68. drawBuffers.put(GL30.GL_COLOR_ATTACHMENT0);
  69. drawBuffers.put(GL30.GL_COLOR_ATTACHMENT1);
  70. drawBuffers.flip();
  71. GL20.glDrawBuffers(drawBuffers);
  72. };
  73. @Override
  74. public void cleanUp() {
  75. super.cleanUp();
  76. GL30.glDeleteRenderbuffers(colorBuffer);
  77. GL30.glDeleteRenderbuffers(colorBuffer2);
  78. }
  79. }