86 lines
1.8 KiB
Java
86 lines
1.8 KiB
Java
package com.danitheskunk.skunkworks.backends.gl;
|
|
|
|
import com.danitheskunk.skunkworks.Vec2i;
|
|
|
|
import static org.lwjgl.opengl.GL11.*;
|
|
import static org.lwjgl.opengl.GL30.*;
|
|
import static org.lwjgl.opengl.GL32.glFramebufferTexture;
|
|
|
|
public class Framebuffer {
|
|
private final int framebuffer;
|
|
private final int framebufferTex;
|
|
private Vec2i size;
|
|
|
|
public Framebuffer(Vec2i size) {
|
|
framebuffer = glGenFramebuffers();
|
|
framebufferTex = glGenTextures();
|
|
setSize(size);
|
|
}
|
|
|
|
public void bind() {
|
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
|
}
|
|
|
|
public void bindTexture() {
|
|
glBindTexture(GL_TEXTURE_2D, framebufferTex);
|
|
}
|
|
|
|
public static void unbind() {
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
}
|
|
|
|
public Vec2i getSize() {
|
|
return size;
|
|
}
|
|
|
|
public void setSize(Vec2i size) {
|
|
this.size = size;
|
|
if(framebuffer != 0) {
|
|
glDeleteFramebuffers(framebuffer);
|
|
}
|
|
if(framebufferTex != 0) {
|
|
glDeleteTextures(framebufferTex);
|
|
}
|
|
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
|
|
glBindTexture(GL_TEXTURE_2D, framebufferTex);
|
|
glTexImage2D(GL_TEXTURE_2D,
|
|
0,
|
|
GL_RGBA,
|
|
size.getX(),
|
|
size.getY(),
|
|
0,
|
|
GL_RGBA,
|
|
GL_UNSIGNED_BYTE,
|
|
0
|
|
);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
glFramebufferTexture(GL_FRAMEBUFFER,
|
|
GL_COLOR_ATTACHMENT0,
|
|
framebufferTex,
|
|
0
|
|
);
|
|
|
|
int framebufferDepthTex = glGenTextures();
|
|
glBindTexture(GL_TEXTURE_2D, framebufferDepthTex);
|
|
glTexImage2D(GL_TEXTURE_2D,
|
|
0,
|
|
GL_DEPTH_COMPONENT,
|
|
size.getX(),
|
|
size.getY(),
|
|
0,
|
|
GL_DEPTH_COMPONENT,
|
|
GL_UNSIGNED_BYTE,
|
|
0
|
|
);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
glFramebufferTexture(GL_FRAMEBUFFER,
|
|
GL_DEPTH_ATTACHMENT,
|
|
framebufferDepthTex,
|
|
0
|
|
);
|
|
}
|
|
}
|