2015-03-30 14:19:39 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package itdelatrisu.opsu.render;
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import org.lwjgl.opengl.GL11;
|
|
|
|
import org.lwjgl.opengl.GL20;
|
|
|
|
import org.lwjgl.opengl.GL30;
|
|
|
|
import org.lwjgl.opengl.GL32;
|
|
|
|
|
|
|
|
/**
|
2015-06-08 21:02:28 +02:00
|
|
|
* Represents a rendertarget. For now this maps to an OpenGL FBO via LWJGL.
|
2015-03-30 14:19:39 +02:00
|
|
|
*
|
2015-06-08 21:02:28 +02:00
|
|
|
* @author Bigpet {@literal <dravorek (at) gmail.com>}
|
2015-03-30 14:19:39 +02:00
|
|
|
*/
|
|
|
|
public class Rendertarget {
|
2015-06-08 21:02:28 +02:00
|
|
|
/** The dimensions. */
|
|
|
|
public final int width, height;
|
|
|
|
|
|
|
|
/** The FBO ID. */
|
2015-03-30 14:19:39 +02:00
|
|
|
private final int fboID;
|
2015-06-08 21:02:28 +02:00
|
|
|
|
2015-06-24 23:55:05 +02:00
|
|
|
/** The texture ID. for the color buffer this rendertarget renders into. */
|
2015-03-30 14:19:39 +02:00
|
|
|
private final int textureID;
|
|
|
|
|
2015-06-24 23:55:05 +02:00
|
|
|
/** The renderbuffer ID for the depth buffer that this rendertarget renders into. */
|
|
|
|
private final int depthBufferID;
|
|
|
|
|
2015-03-30 14:19:39 +02:00
|
|
|
/**
|
2015-06-08 21:02:28 +02:00
|
|
|
* Create a new FBO.
|
|
|
|
* @param width the width
|
|
|
|
* @param height the height
|
2015-03-30 14:19:39 +02:00
|
|
|
*/
|
2015-06-08 21:02:28 +02:00
|
|
|
private Rendertarget(int width, int height) {
|
2015-03-30 14:19:39 +02:00
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
fboID = GL30.glGenFramebuffers();
|
|
|
|
textureID = GL11.glGenTextures();
|
2015-06-24 23:55:05 +02:00
|
|
|
depthBufferID = GL30.glGenRenderbuffers();
|
2015-03-30 14:19:39 +02:00
|
|
|
}
|
2015-06-08 21:02:28 +02:00
|
|
|
|
2015-03-30 14:19:39 +02:00
|
|
|
/**
|
|
|
|
* Bind this rendertarget as the primary framebuffer.
|
|
|
|
*/
|
2015-06-08 21:02:28 +02:00
|
|
|
public void bind() {
|
2015-03-30 14:19:39 +02:00
|
|
|
GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, fboID);
|
|
|
|
}
|
2015-06-08 21:02:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the FBO ID.
|
|
|
|
*/
|
|
|
|
// NOTE: use judiciously, try to avoid if possible and consider adding a
|
|
|
|
// method to this class if you find yourself calling this repeatedly.
|
|
|
|
public int getID() {
|
2015-03-30 14:19:39 +02:00
|
|
|
return fboID;
|
|
|
|
}
|
2015-06-08 21:02:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the texture ID.
|
|
|
|
*/
|
|
|
|
// NOTE: try not to use, could be moved into separate class.
|
|
|
|
public int getTextureID() {
|
2015-03-30 14:19:39 +02:00
|
|
|
return textureID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-08 21:02:28 +02:00
|
|
|
* Bind the default framebuffer.
|
2015-03-30 14:19:39 +02:00
|
|
|
*/
|
2015-06-08 21:02:28 +02:00
|
|
|
public static void unbind() {
|
2015-03-30 14:19:39 +02:00
|
|
|
GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, 0);
|
|
|
|
}
|
2015-06-08 21:02:28 +02:00
|
|
|
|
2015-03-30 14:19:39 +02:00
|
|
|
/**
|
|
|
|
* Creates a Rendertarget with a Texture that it renders the color buffer in
|
|
|
|
* and a renderbuffer that it renders the depth to.
|
2015-06-08 21:02:28 +02:00
|
|
|
* @param width the width
|
|
|
|
* @param height the height
|
2015-03-30 14:19:39 +02:00
|
|
|
*/
|
2015-06-08 21:02:28 +02:00
|
|
|
public static Rendertarget createRTTFramebuffer(int width, int height) {
|
2015-03-30 14:19:39 +02:00
|
|
|
int old_framebuffer = GL11.glGetInteger(GL30.GL_READ_FRAMEBUFFER_BINDING);
|
|
|
|
int old_texture = GL11.glGetInteger(GL11.GL_TEXTURE_BINDING_2D);
|
|
|
|
Rendertarget buffer = new Rendertarget(width,height);
|
|
|
|
buffer.bind();
|
|
|
|
|
|
|
|
int fboTexture = buffer.textureID;
|
|
|
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, fboTexture);
|
|
|
|
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, 4, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_INT, (ByteBuffer) null);
|
|
|
|
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
|
|
|
|
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
|
|
|
|
|
2015-06-24 23:55:05 +02:00
|
|
|
GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, buffer.depthBufferID);
|
2015-03-30 14:19:39 +02:00
|
|
|
GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL11.GL_DEPTH_COMPONENT, width, height);
|
2015-06-24 23:55:05 +02:00
|
|
|
GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER, buffer.depthBufferID);
|
2015-03-30 14:19:39 +02:00
|
|
|
|
|
|
|
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, fboTexture, 0);
|
|
|
|
GL20.glDrawBuffers(GL30.GL_COLOR_ATTACHMENT0);
|
|
|
|
|
|
|
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, old_texture);
|
|
|
|
GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, old_framebuffer);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
2015-06-24 23:55:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy the OpenGL objects associated with this Rendertarget. Do not try
|
|
|
|
* to use this rendertarget with OpenGL after calling this method.
|
|
|
|
*/
|
|
|
|
public void destroyRTT()
|
|
|
|
{
|
|
|
|
GL30.glDeleteFramebuffers(fboID);
|
|
|
|
GL30.glDeleteRenderbuffers(depthBufferID);
|
|
|
|
GL11.glDeleteTextures(textureID);
|
|
|
|
}
|
2015-03-30 14:19:39 +02:00
|
|
|
}
|