1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.graphics.glutils; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.Cubemap; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.Pixmap; 23 import com.badlogic.gdx.graphics.Texture.TextureFilter; 24 import com.badlogic.gdx.graphics.Texture.TextureWrap; 25 import com.badlogic.gdx.graphics.g2d.Gdx2DPixmap; 26 import com.badlogic.gdx.utils.GdxRuntimeException; 27 28 /** <p> 29 * Encapsulates OpenGL ES 2.0 frame buffer objects. This is a simple helper class which should cover most FBO uses. It will 30 * automatically create a cubemap for the color attachment and a renderbuffer for the depth buffer. You can get a hold of the 31 * cubemap by {@link FrameBufferCubemap#getColorBufferTexture()}. This class will only work with OpenGL ES 2.0. 32 * </p> 33 * 34 * <p> 35 * FrameBuffers are managed. In case of an OpenGL context loss, which only happens on Android when a user switches to another 36 * application or receives an incoming call, the framebuffer will be automatically recreated. 37 * </p> 38 * 39 * <p> 40 * A FrameBuffer must be disposed if it is no longer needed 41 * </p> 42 * 43 * <p> 44 * Typical use: <br /> 45 * FrameBufferCubemap frameBuffer = new FrameBufferCubemap(Format.RGBA8888, fSize, fSize, true); <br /> 46 * frameBuffer.begin(); <br /> 47 * while( frameBuffer.nextSide() ) { <br /> 48 * frameBuffer.getSide().getUp(camera.up); <br /> 49 * frameBuffer.getSide().getDirection(camera.direction);<br /> 50 * camera.update(); <br /> 51 * 52 * Gdx.gl.glClearColor(0, 0, 0, 1); <br /> 53 * Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); <br /> 54 * modelBatch.begin(camera); <br /> 55 * modelBatch.render(renderableProviders); <br /> 56 * modelBatch.end(); <br /> 57 * } <br /> 58 * frameBuffer.end(); <br /> 59 * Cubemap cubemap = frameBuffer.getColorBufferCubemap(); 60 * </p> 61 * 62 * @author realitix */ 63 public class FrameBufferCubemap extends GLFrameBuffer<Cubemap> { 64 65 /** the zero-based index of the active side **/ 66 private int currentSide; 67 68 /** Creates a new FrameBuffer having the given dimensions and potentially a depth buffer attached. 69 * 70 * @param format 71 * @param width 72 * @param height 73 * @param hasDepth */ FrameBufferCubemap(Pixmap.Format format, int width, int height, boolean hasDepth)74 public FrameBufferCubemap (Pixmap.Format format, int width, int height, boolean hasDepth) { 75 this(format, width, height, hasDepth, false); 76 } 77 78 /** Creates a new FrameBuffer having the given dimensions and potentially a depth and a stencil buffer attached. 79 * 80 * @param format the format of the color buffer; according to the OpenGL ES 2.0 spec, only RGB565, RGBA4444 and RGB5_A1 are 81 * color-renderable 82 * @param width the width of the cubemap in pixels 83 * @param height the height of the cubemap in pixels 84 * @param hasDepth whether to attach a depth buffer 85 * @param hasStencil whether to attach a stencil buffer 86 * @throws com.badlogic.gdx.utils.GdxRuntimeException in case the FrameBuffer could not be created */ FrameBufferCubemap(Pixmap.Format format, int width, int height, boolean hasDepth, boolean hasStencil)87 public FrameBufferCubemap (Pixmap.Format format, int width, int height, boolean hasDepth, boolean hasStencil) { 88 super(format, width, height, hasDepth, hasStencil); 89 } 90 91 @Override createColorTexture()92 protected Cubemap createColorTexture () { 93 int glFormat = Pixmap.Format.toGlFormat(format); 94 int glType = Pixmap.Format.toGlType(format); 95 GLOnlyTextureData data = new GLOnlyTextureData(width, height, 0, glFormat, glFormat, glType); 96 Cubemap result = new Cubemap(data, data, data, data, data, data); 97 result.setFilter(TextureFilter.Linear, TextureFilter.Linear); 98 result.setWrap(TextureWrap.ClampToEdge, TextureWrap.ClampToEdge); 99 return result; 100 } 101 102 @Override disposeColorTexture(Cubemap colorTexture)103 protected void disposeColorTexture (Cubemap colorTexture) { 104 colorTexture.dispose(); 105 } 106 107 /** Makes the frame buffer current so everything gets drawn to it, must be followed by call to either {@link #nextSide()} or 108 * {@link #bindSide(com.badlogic.gdx.graphics.Cubemap.CubemapSide)} to activate the side to render onto. */ 109 @Override bind()110 public void bind () { 111 currentSide = -1; 112 super.bind(); 113 } 114 115 /** Bind the next side of cubemap and return false if no more side. Should be called in between a call to {@link #begin()} and 116 * #end to cycle to each side of the cubemap to render on. */ nextSide()117 public boolean nextSide () { 118 if (currentSide > 5) { 119 throw new GdxRuntimeException("No remaining sides."); 120 } else if (currentSide == 5) { 121 return false; 122 } 123 124 currentSide++; 125 bindSide(getSide()); 126 return true; 127 } 128 129 /** Bind the side, making it active to render on. Should be called in between a call to {@link #begin()} and {@link #end()}. 130 * @param side The side to bind */ bindSide(final Cubemap.CubemapSide side)131 protected void bindSide (final Cubemap.CubemapSide side) { 132 Gdx.gl20.glFramebufferTexture2D(GL20.GL_FRAMEBUFFER, GL20.GL_COLOR_ATTACHMENT0, side.glEnum, 133 colorTexture.getTextureObjectHandle(), 0); 134 } 135 136 /** Get the currently bound side. */ getSide()137 public Cubemap.CubemapSide getSide () { 138 return currentSide < 0 ? null : Cubemap.CubemapSide.values()[currentSide]; 139 } 140 } 141