framebuffer stores size, and can be resized

This commit is contained in:
DaniTheSkunk 2022-12-12 08:40:33 +00:00
parent 2521a52ba6
commit 3102e1c909
4 changed files with 71 additions and 28 deletions

View File

@ -9,11 +9,40 @@ 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();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
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,
@ -53,17 +82,4 @@ public class Framebuffer {
0
);
}
public void bind() {
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
}
public void bindTexture() {
glBindTexture(GL_TEXTURE_2D, framebufferTex);
}
public static void unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
}

View File

@ -39,13 +39,11 @@ public class Pipeline2D extends BasePipeline<RenderContext, IRenderContext> impl
""";
private final TextureAtlas atlas;
private boolean debug;
private Vec2i size;
private final Framebuffer framebuffer;
public Pipeline2D(Vec2i size, Framebuffer framebuffer, TextureAtlas atlas) {
public Pipeline2D(Framebuffer framebuffer, TextureAtlas atlas) {
super(new Program(vertexSource, fragmentSource));
this.size = size;
renderContext = new RenderContext(program, size);
renderContext = new RenderContext(program, framebuffer.getSize());
this.atlas = atlas;
this.framebuffer = framebuffer;
debug = false;
@ -54,7 +52,7 @@ public class Pipeline2D extends BasePipeline<RenderContext, IRenderContext> impl
@Override
public void finishFrame() {
if(debug) {
renderContext.drawTextureRectangle(new Recti(Vec2i.ZERO, size),
renderContext.drawTextureRectangle(new Recti(Vec2i.ZERO, framebuffer.getSize()),
atlas.getAtlasTexture(),
false
);
@ -71,6 +69,7 @@ public class Pipeline2D extends BasePipeline<RenderContext, IRenderContext> impl
use();
atlas.bind();
framebuffer.bind();
var size = framebuffer.getSize();
glViewport(0, 0, size.getX(), size.getY());
atlas.update();
glClear(GL_DEPTH_BUFFER_BIT);

View File

@ -34,15 +34,13 @@ public class Pipeline3D extends BasePipeline<RenderContext3D, IRenderContext3D>
}
""";
private Vec2i size;
private final Framebuffer framebuffer;
private final TextureAtlas atlas;
public Pipeline3D(Vec2i size, Framebuffer framebuffer, TextureAtlas atlas) {
public Pipeline3D(Framebuffer framebuffer, TextureAtlas atlas) {
super(new Program(vertexSource, fragmentSource));
renderContext = new RenderContext3D(program);
renderContext.setTextureAtlas(atlas);
this.size = size;
this.framebuffer = framebuffer;
this.atlas = atlas;
}
@ -57,6 +55,7 @@ public class Pipeline3D extends BasePipeline<RenderContext3D, IRenderContext3D>
use();
atlas.bind();
framebuffer.bind();
var size = framebuffer.getSize();
glViewport(0, 0, size.getX(), size.getY());
atlas.update();
glClear(GL_DEPTH_BUFFER_BIT);

View File

@ -47,13 +47,13 @@ public class Window extends BaseWindow {
}
""";
private final Framebuffer framebuffer;
private final Framebuffer framebuffer2;
private final Program programScaler;
private final Vec2i size;
private final boolean[] stateMouseClicked;
private final boolean[] stateMouseDown;
private final TextureAtlas textureAtlas;
private final long window;
private boolean debug;
private boolean shouldClose;
private Vec2i windowSize;
private final Pipeline2D pipeline2D;
@ -66,7 +66,6 @@ public class Window extends BaseWindow {
"Unable to initialize GLFW");
glfwDefaultWindowHints();
debug = false;
this.size = size;
windowSize = size;
@ -94,11 +93,12 @@ public class Window extends BaseWindow {
glfwSetMouseButtonCallback(window, this::mouseButtonCallback);
framebuffer = new Framebuffer(size);
framebuffer2 = new Framebuffer(size);
textureAtlas = new TextureAtlas();
programScaler = new Program(vertexSourceScaler, fragmentSourceScaler);
pipeline2D = new Pipeline2D(size, framebuffer, textureAtlas);
pipeline3D = new Pipeline3D(size, framebuffer, textureAtlas);
pipeline2D = new Pipeline2D(framebuffer, textureAtlas);
pipeline3D = new Pipeline3D(framebuffer, textureAtlas);
glProgramUniform2f(programScaler.getProgram(),
@ -238,7 +238,8 @@ public class Window extends BaseWindow {
//glEnd();
Framebuffer.unbind();
//Framebuffer.unbind();
framebuffer2.bind();
glViewport(0, 0, windowSize.getX(), windowSize.getY());
programScaler.use();
@ -313,11 +314,39 @@ public class Window extends BaseWindow {
glVertex2i(tlx2, tly2);
glEnd();
Framebuffer.unbind();
framebuffer2.bindTexture();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
tlx1 = 0;
tly1 = 0;
tlx2 = windowSize.getX();
tly2 = windowSize.getY();
glBegin(GL_TRIANGLES);
//counterclockwise triangles
glVertexAttrib2f(texCoordIndex, 0, 1);
glVertex2i(tlx1, tly2);
glVertexAttrib2f(texCoordIndex, 1, 0);
glVertex2i(tlx2, tly1);
glVertexAttrib2f(texCoordIndex, 0, 0);
glVertex2i(tlx1, tly1);
glVertexAttrib2f(texCoordIndex, 1, 0);
glVertex2i(tlx2, tly1);
glVertexAttrib2f(texCoordIndex, 0, 1);
glVertex2i(tlx1, tly2);
glVertexAttrib2f(texCoordIndex, 1, 1);
glVertex2i(tlx2, tly2);
glEnd();
}
@Override
public void setDebug(boolean on) {
debug = on;
pipeline2D.setDebug(on);
}
@Override