2015-03-30 14:19:39 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2015-06-30 02:22:38 +02:00
|
|
|
*
|
2015-03-30 14:19:39 +02:00
|
|
|
* 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.
|
2015-06-30 02:22:38 +02:00
|
|
|
*
|
2015-03-30 14:19:39 +02:00
|
|
|
* 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.
|
2015-06-30 02:22:38 +02:00
|
|
|
*
|
2015-03-30 14:19:39 +02:00
|
|
|
* 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;
|
2015-06-30 02:22:38 +02:00
|
|
|
|
2015-07-09 18:37:39 +02:00
|
|
|
import org.lwjgl.opengl.EXTFramebufferObject;
|
2015-03-30 14:19:39 +02:00
|
|
|
import org.lwjgl.opengl.GL11;
|
2015-09-16 17:19:23 +02:00
|
|
|
import org.lwjgl.opengl.GL15;
|
2015-03-30 14:19:39 +02:00
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
|
2015-09-16 17:19:23 +02:00
|
|
|
/** ID of the vertex buffer associated with this rendertarget*/
|
|
|
|
private final int vboID;
|
|
|
|
|
2015-06-08 21:02:28 +02:00
|
|
|
/** 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-28 09:38:50 +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-06-25 00:21:15 +02:00
|
|
|
|
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;
|
2015-07-09 18:37:39 +02:00
|
|
|
fboID = EXTFramebufferObject.glGenFramebuffersEXT();
|
2015-09-16 17:19:23 +02:00
|
|
|
vboID = GL15.glGenBuffers();
|
2015-03-30 14:19:39 +02:00
|
|
|
textureID = GL11.glGenTextures();
|
2015-07-09 18:37:39 +02:00
|
|
|
depthBufferID = EXTFramebufferObject.glGenRenderbuffersEXT();
|
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-07-09 18:37:39 +02:00
|
|
|
EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fboID);
|
2015-03-30 14:19:39 +02:00
|
|
|
}
|
2015-06-08 21:02:28 +02:00
|
|
|
|
2015-09-16 17:19:23 +02:00
|
|
|
|
2015-06-08 21:02:28 +02:00
|
|
|
/**
|
2015-09-16 17:19:23 +02:00
|
|
|
* Get the ID of the VBO associated with this Rendertarget.
|
|
|
|
* @return OpenGL buffer ID for the VBO
|
|
|
|
*/
|
|
|
|
public int getVbo() {
|
|
|
|
return vboID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the FBO ID.
|
|
|
|
* @return the OpenGL FBO ID
|
2015-06-08 21:02:28 +02:00
|
|
|
*/
|
|
|
|
public int getID() {
|
2015-03-30 14:19:39 +02:00
|
|
|
return fboID;
|
|
|
|
}
|
2015-06-08 21:02:28 +02:00
|
|
|
|
|
|
|
/**
|
2015-09-16 17:19:23 +02:00
|
|
|
* Get the texture ID of the texture this rendertarget renders into.
|
|
|
|
* @return the OpenGL texture ID
|
2015-06-08 21:02:28 +02:00
|
|
|
*/
|
|
|
|
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-07-09 18:37:39 +02:00
|
|
|
EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, 0);
|
2015-03-30 14:19:39 +02:00
|
|
|
}
|
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-09-16 17:19:23 +02:00
|
|
|
* @return the newly created Rendertarget instance
|
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-07-09 18:37:39 +02:00
|
|
|
int old_framebuffer = GL11.glGetInteger(EXTFramebufferObject.GL_FRAMEBUFFER_BINDING_EXT);
|
2015-03-30 14:19:39 +02:00
|
|
|
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-07-09 18:37:39 +02:00
|
|
|
EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, buffer.depthBufferID);
|
|
|
|
EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, GL11.GL_DEPTH_COMPONENT, width, height);
|
|
|
|
EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, EXTFramebufferObject.GL_RENDERBUFFER_EXT, buffer.depthBufferID);
|
2015-03-30 14:19:39 +02:00
|
|
|
|
2015-07-09 18:37:39 +02:00
|
|
|
EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, fboTexture, 0);
|
2015-03-30 14:19:39 +02:00
|
|
|
|
|
|
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, old_texture);
|
2015-07-09 18:37:39 +02:00
|
|
|
EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, old_framebuffer);
|
2015-07-09 18:59:53 +02:00
|
|
|
|
2015-03-30 14:19:39 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
2015-06-25 00:21:15 +02:00
|
|
|
|
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.
|
|
|
|
*/
|
2015-06-28 09:38:50 +02:00
|
|
|
public void destroyRTT() {
|
2015-07-09 18:37:39 +02:00
|
|
|
EXTFramebufferObject.glDeleteFramebuffersEXT(fboID);
|
|
|
|
EXTFramebufferObject.glDeleteRenderbuffersEXT(depthBufferID);
|
2015-06-24 23:55:05 +02:00
|
|
|
GL11.glDeleteTextures(textureID);
|
2015-09-16 17:19:23 +02:00
|
|
|
GL15.glDeleteBuffers(vboID);
|
2015-06-24 23:55:05 +02:00
|
|
|
}
|
2015-03-30 14:19:39 +02:00
|
|
|
}
|