implemented FBO's

pull/1/head
hYdos 2020-02-23 17:49:46 +10:00
parent f40c042b19
commit 54ba0d5e07
12 changed files with 122 additions and 181 deletions

View File

@ -138,7 +138,7 @@ public class Example {
system.setSpeedError(0);
system.setScaleError(1f);
Fbo fbo = new Fbo(Window.width, Window.height, Fbo.DEPTH_RENDER_BUFFER);
Fbo fbo = new Fbo();
PostProcessing.init();
while(!Window.closed()) {
@ -154,7 +154,6 @@ public class Example {
camera.move();
entity.move(terrain);
text.setOutlineColour(new Vector3f(colour, colour /2, colour / 3));
Vector3f terrainPoint = picker.getCurrentTerrainPoint();
if(terrainPoint!=null) {
@ -169,21 +168,18 @@ public class Example {
barrel.increaseRotation(0, 1, 0);
GingerMain.preRenderScene(masterRenderer);
fbo.bindFrameBuffer();
masterRenderer.renderScene(entities, normalMapEntities, terrains, lights, camera, new Vector4f(0, -1, 0, 100000));
ParticleMaster.renderParticles(camera);
fbo.unbindFrameBuffer();
PostProcessing.doPostProcessing(fbo.getColourTexture());
fbo.bindFBO();
masterRenderer.renderScene(entities, normalMapEntities, terrains, lights, camera, new Vector4f(0, -1, 0, 100000));
fbo.unbindFBO();
PostProcessing.doPostProcessing(fbo.colorTexture);
// TODO: get fbo's working
button.update();
if(button.isClicked()) {
System.out.println("click");
button.hide(guis);
}
masterRenderer.renderScene(entities, normalMapEntities, terrains, lights, camera, new Vector4f(0, -1, 0, 100000));
// masterRenderer.renderScene(entities, normalMapEntities, terrains, lights, camera, new Vector4f(0, -1, 0, 100000));
masterRenderer.renderGuis(guis);
TextMaster.render();
@ -193,7 +189,6 @@ public class Example {
}
Window.stop();
PostProcessing.cleanUp();
fbo.cleanUp();
ParticleMaster.cleanUp();
masterRenderer.cleanUp();
TextMaster.cleanUp();

View File

@ -1,175 +1,120 @@
package io.github.hydos.ginger.engine.postprocessing;
import static org.lwjgl.opengl.ARBFramebufferObject.*;
import static org.lwjgl.opengl.GL11.*;
import java.nio.ByteBuffer;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL14;
import org.lwjgl.opengl.GL30;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.system.Callback;
import io.github.hydos.ginger.engine.io.Window;
public class Fbo {
long window;
int width = 1024;
int height = 768;
boolean resetFramebuffer;
boolean destroyed;
Object lock = new Object();
public static final int NONE = 0;
public static final int DEPTH_TEXTURE = 1;
public static final int DEPTH_RENDER_BUFFER = 2;
/* Multisampled FBO objects */
public int multisampledColorRenderBuffer;
int multisampledDepthRenderBuffer;
int multisampledFbo;
int samples = 1;
private final int width;
private final int height;
/* Single-sampled FBO objects */
public int colorTexture;
int fbo;
private int frameBuffer;
private int colourTexture;
private int depthTexture;
private int depthBuffer;
private int colourBuffer;
/**
* Creates an FBO of a specified width and height, with the desired type of
* depth buffer attachment.
*
* @param width
* - the width of the FBO.
* @param height
* - the height of the FBO.
* @param depthBufferType
* - an int indicating the type of depth buffer attachment that
* this FBO should use.
*/
public Fbo(int width, int height, int depthBufferType) {
this.width = width;
this.height = height;
initialiseFrameBuffer(depthBufferType);
GLFWErrorCallback errorCallback;
GLFWKeyCallback keyCallback;
GLFWFramebufferSizeCallback fbCallback;
Callback debugProc;
public Fbo() {
this.window = Window.window;
width = Window.actuallWidth;
height = Window.actuallHeight;
createFBO();
}
public void createFBO() {
this.window = Window.window;
/* Create multisampled FBO */
multisampledColorRenderBuffer = glGenRenderbuffers();
multisampledDepthRenderBuffer = glGenRenderbuffers();
multisampledFbo = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, multisampledFbo);
glBindRenderbuffer(GL_RENDERBUFFER, multisampledColorRenderBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_RGBA8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, multisampledColorRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, multisampledDepthRenderBuffer);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, multisampledDepthRenderBuffer);
int fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fboStatus != GL_FRAMEBUFFER_COMPLETE) {
throw new AssertionError("Could not create FBO: " + fboStatus);
}
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
/**
* Deletes the frame buffer and its attachments when the game closes.
*/
public void cleanUp() {
GL30.glDeleteFramebuffers(frameBuffer);
GL11.glDeleteTextures(colourTexture);
GL11.glDeleteTextures(depthTexture);
GL30.glDeleteRenderbuffers(depthBuffer);
GL30.glDeleteRenderbuffers(colourBuffer);
}
/* Create single-sampled FBO */
colorTexture = glGenTextures();
fbo = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glBindTexture(GL_TEXTURE_2D, colorTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // we also want to sample this texture later
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // we also want to sample this texture later
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) null);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);
fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fboStatus != GL_FRAMEBUFFER_COMPLETE) {
throw new AssertionError("Could not create FBO: " + fboStatus);
}
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
public void resizeFBOs() {
/* Delete multisampled FBO objects */
glDeleteRenderbuffers(multisampledDepthRenderBuffer);
glDeleteRenderbuffers(multisampledColorRenderBuffer);
glDeleteFramebuffers(multisampledFbo);
/* Delete single-sampled FBO objects */
glDeleteTextures(colorTexture);
glDeleteFramebuffers(fbo);
/* Recreate everything */
createFBO();
}
public void update() {
if (resetFramebuffer) {
resizeFBOs();
resetFramebuffer = false;
}
}
public void bindFBO() {
glBindFramebuffer(GL_FRAMEBUFFER, multisampledFbo);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, width, height);
/**
* Binds the frame buffer, setting it as the current render target. Anything
* rendered after this will be rendered to this FBO, and not to the screen.
*/
public void bindFrameBuffer() {
GL30.glBindFramebuffer(GL30.GL_DRAW_FRAMEBUFFER, frameBuffer);
GL11.glViewport(0, 0, width, height);
}
/**
* Unbinds the frame buffer, setting the default frame buffer as the current
* render target. Anything rendered after this will be rendered to the
* screen, and not this FBO.
*/
public void unbindFrameBuffer() {
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
GL11.glViewport(0, 0, Window.width, Window.height);
}
/**
* Binds the current FBO to be read from (not used in tutorial 43).
*/
public void bindToRead() {
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GL30.glBindFramebuffer(GL30.GL_READ_FRAMEBUFFER, frameBuffer);
GL11.glReadBuffer(GL30.GL_COLOR_ATTACHMENT0);
}
/**
* @return The ID of the texture containing the colour buffer of the FBO.
*/
public int getColourTexture() {
return colourTexture;
}
/**
* @return The texture containing the FBOs depth buffer.
*/
public int getDepthTexture() {
return depthTexture;
}
/**
* Creates the FBO along with a colour buffer texture attachment, and
* possibly a depth buffer.
*
* @param type
* - the type of depth buffer attachment to be attached to the
* FBO.
*/
private void initialiseFrameBuffer(int type) {
createFrameBuffer();
createTextureAttachment();
if (type == DEPTH_RENDER_BUFFER) {
createDepthBufferAttachment();
} else if (type == DEPTH_TEXTURE) {
createDepthTextureAttachment();
}
unbindFrameBuffer();
}
/**
* Creates a new frame buffer object and sets the buffer to which drawing
* will occur - colour attachment 0. This is the attachment where the colour
* buffer texture is.
*
*/
private void createFrameBuffer() {
frameBuffer = GL30.glGenFramebuffers();
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBuffer);
GL11.glDrawBuffer(GL30.GL_COLOR_ATTACHMENT0);
}
/**
* Creates a texture and sets it as the colour buffer attachment for this
* FBO.
*/
private void createTextureAttachment() {
colourTexture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, colourTexture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE,
(ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, colourTexture,
0);
}
/**
* Adds a depth buffer to the FBO in the form of a texture, which can later
* be sampled.
*/
private void createDepthTextureAttachment() {
depthTexture = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, depthTexture);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL14.GL_DEPTH_COMPONENT24, width, height, 0, GL11.GL_DEPTH_COMPONENT,
GL11.GL_FLOAT, (ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, depthTexture, 0);
}
/**
* Adds a depth buffer to the FBO in the form of a render buffer. This can't
* be used for sampling in the shaders.
*/
private void createDepthBufferAttachment() {
depthBuffer = GL30.glGenRenderbuffers();
GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBuffer);
GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL14.GL_DEPTH_COMPONENT24, width, height);
GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL30.GL_RENDERBUFFER,
depthBuffer);
}
}
public void unbindFBO() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
/* Resolve by blitting to non-multisampled FBO */
glBindFramebuffer(GL_READ_FRAMEBUFFER, multisampledFbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
}

View File

@ -7,30 +7,27 @@ public class ImageRenderer {
private Fbo fbo;
protected ImageRenderer(int width, int height) {
this.fbo = new Fbo(width, height, Fbo.NONE);
this.fbo = new Fbo();
}
protected ImageRenderer() {}
protected void renderQuad() {
if (fbo != null) {
fbo.bindFrameBuffer();
fbo.bindFBO();
}
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glDrawArrays(GL11.GL_TRIANGLE_STRIP, 0, 4);
if (fbo != null) {
fbo.unbindFrameBuffer();
fbo.unbindFBO();
}
}
protected int getOutputTexture() {
return fbo.getColourTexture();
return fbo.colorTexture;
}
protected void cleanUp() {
if (fbo != null) {
fbo.cleanUp();
}
}
}

View File

@ -21,7 +21,7 @@ import io.github.hydos.ginger.engine.render.models.TexturedModel;
*/
public class ShadowMapMasterRenderer {
private static final int SHADOW_MAP_SIZE = 5120;
private static final int SHADOW_MAP_SIZE = 5120*2;
private ShadowFrameBuffer shadowFbo;
private ShadowShader shader;

View File

@ -24,4 +24,6 @@ void main(void){
// calculate brightness
out_Colour.rgb *= brightness;
}

View File

@ -8,9 +8,9 @@ uniform sampler2D modelTexture;
void main(void){
float alpha = texture(modelTexture, textureCoords).a;
if(alpha < 0.5){
if(alpha < 0.4){
discard;
}
out_Colour = vec4(1.0);
out_Colour = vec4(1.0, 1.0, 1.0, 0.1);
}

View File

@ -24,4 +24,6 @@ void main(void){
// calculate brightness
out_Colour.rgb *= brightness;
}

View File

@ -8,9 +8,9 @@ uniform sampler2D modelTexture;
void main(void){
float alpha = texture(modelTexture, textureCoords).a;
if(alpha < 0.5){
if(alpha < 0.4){
discard;
}
out_Colour = vec4(1.0);
out_Colour = vec4(1.0, 1.0, 1.0, 0.1);
}