ShadowFrameBuffer.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package eu.tankernn.gameEngine.renderEngine.shadows;
  2. import org.lwjgl.opengl.Display;
  3. import org.lwjgl.opengl.GL11;
  4. import org.lwjgl.opengl.GL30;
  5. import eu.tankernn.gameEngine.loader.textures.Texture;
  6. import eu.tankernn.gameEngine.loader.textures.TextureUtils;
  7. /**
  8. * The frame buffer for the shadow pass. This class sets up the depth texture
  9. * which can be rendered to during the shadow render pass, producing a shadow
  10. * map.
  11. *
  12. * @author Karl
  13. *
  14. */
  15. public class ShadowFrameBuffer {
  16. private final int WIDTH;
  17. private final int HEIGHT;
  18. private int fbo;
  19. private Texture shadowMap;
  20. /**
  21. * Initialises the frame buffer and shadow map of a certain size.
  22. *
  23. * @param width
  24. * - the width of the shadow map in pixels.
  25. * @param height
  26. * - the height of the shadow map in pixels.
  27. */
  28. protected ShadowFrameBuffer(int width, int height) {
  29. this.WIDTH = width;
  30. this.HEIGHT = height;
  31. initialiseFrameBuffer();
  32. }
  33. /**
  34. * Deletes the frame buffer and shadow map texture when the game closes.
  35. */
  36. protected void finalize() {
  37. GL30.glDeleteFramebuffers(fbo);
  38. shadowMap.delete();
  39. }
  40. /**
  41. * Binds the frame buffer, setting it as the current render target.
  42. */
  43. protected void bindFrameBuffer() {
  44. bindFrameBuffer(fbo, WIDTH, HEIGHT);
  45. }
  46. /**
  47. * Unbinds the frame buffer, setting the default frame buffer as the current
  48. * render target.
  49. */
  50. protected void unbindFrameBuffer() {
  51. GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
  52. GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
  53. }
  54. /**
  55. * @return The ID of the shadow map texture.
  56. */
  57. protected Texture getShadowMap() {
  58. return shadowMap;
  59. }
  60. /**
  61. * Creates the frame buffer and adds its depth attachment texture.
  62. */
  63. private void initialiseFrameBuffer() {
  64. fbo = createFrameBuffer();
  65. shadowMap = TextureUtils.createDepthTextureAttachment(WIDTH, HEIGHT);
  66. unbindFrameBuffer();
  67. }
  68. /**
  69. * Binds the frame buffer as the current render target.
  70. *
  71. * @param frameBuffer
  72. * - the frame buffer.
  73. * @param width
  74. * - the width of the frame buffer.
  75. * @param height
  76. * - the height of the frame buffer.
  77. */
  78. private static void bindFrameBuffer(int frameBuffer, int width, int height) {
  79. GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
  80. GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, frameBuffer);
  81. GL11.glViewport(0, 0, width, height);
  82. }
  83. /**
  84. * Creates a frame buffer and binds it so that attachments can be added to
  85. * it. The draw buffer is set to none, indicating that there's no colour
  86. * buffer to be rendered to.
  87. *
  88. * @return The newly created frame buffer's ID.
  89. */
  90. private static int createFrameBuffer() {
  91. int frameBuffer = GL30.glGenFramebuffers();
  92. GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
  93. GL11.glDrawBuffer(GL11.GL_NONE);
  94. return frameBuffer;
  95. }
  96. }