package eu.tankernn.gameEngine.renderEngine.shadows; import org.lwjgl.opengl.Display; import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL30; import eu.tankernn.gameEngine.loader.textures.Texture; import eu.tankernn.gameEngine.loader.textures.TextureUtils; /** * The frame buffer for the shadow pass. This class sets up the depth texture * which can be rendered to during the shadow render pass, producing a shadow * map. * * @author Karl * */ public class ShadowFrameBuffer { private final int WIDTH; private final int HEIGHT; private int fbo; private Texture shadowMap; /** * Initialises the frame buffer and shadow map of a certain size. * * @param width * - the width of the shadow map in pixels. * @param height * - the height of the shadow map in pixels. */ protected ShadowFrameBuffer(int width, int height) { this.WIDTH = width; this.HEIGHT = height; initialiseFrameBuffer(); } /** * Deletes the frame buffer and shadow map texture when the game closes. */ protected void finalize() { GL30.glDeleteFramebuffers(fbo); shadowMap.delete(); } /** * Binds the frame buffer, setting it as the current render target. */ protected void bindFrameBuffer() { bindFrameBuffer(fbo, WIDTH, HEIGHT); } /** * Unbinds the frame buffer, setting the default frame buffer as the current * render target. */ protected void unbindFrameBuffer() { GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0); GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight()); } /** * @return The ID of the shadow map texture. */ protected Texture getShadowMap() { return shadowMap; } /** * Creates the frame buffer and adds its depth attachment texture. */ private void initialiseFrameBuffer() { fbo = createFrameBuffer(); shadowMap = TextureUtils.createDepthTextureAttachment(WIDTH, HEIGHT); unbindFrameBuffer(); } /** * Binds the frame buffer as the current render target. * * @param frameBuffer * - the frame buffer. * @param width * - the width of the frame buffer. * @param height * - the height of the frame buffer. */ private static void bindFrameBuffer(int frameBuffer, int width, int height) { GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, frameBuffer); GL11.glViewport(0, 0, width, height); } /** * Creates a frame buffer and binds it so that attachments can be added to * it. The draw buffer is set to none, indicating that there's no colour * buffer to be rendered to. * * @return The newly created frame buffer's ID. */ private static int createFrameBuffer() { int frameBuffer = GL30.glGenFramebuffers(); GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer); GL11.glDrawBuffer(GL11.GL_NONE); return frameBuffer; } }