123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- 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;
- }
- }
|