framebuffer class

This commit is contained in:
DaniTheSkunk 2022-12-12 02:06:12 +00:00
parent 107e0a5c91
commit 9fa8eb11bb
2 changed files with 80 additions and 53 deletions

View File

@ -0,0 +1,69 @@
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;
public Framebuffer(Vec2i size) {
framebuffer = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
framebufferTex = glGenTextures();
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
);
}
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

@ -98,8 +98,7 @@ public class Window extends BaseWindow {
out_texCoord = texCoord;
}
""";
private final int framebuffer;
private final int framebufferTex;
private final Framebuffer framebuffer;
private final Program program;
private final Program program3D;
private final Program programScaler;
@ -140,7 +139,6 @@ public class Window extends BaseWindow {
GL.createCapabilities();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0.0f, size.getX(), size.getY(), 0.0f, 0.0f, 1.0f);
glEnable(GL_COLOR);
glEnable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -150,51 +148,7 @@ public class Window extends BaseWindow {
glfwSetWindowSizeCallback(window, this::windowSizeCallback);
glfwSetMouseButtonCallback(window, this::mouseButtonCallback);
framebuffer = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
framebufferTex = glGenTextures();
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
);
glDrawBuffers(GL_COLOR_ATTACHMENT0);
framebuffer = new Framebuffer(size);
textureAtlas = new TextureAtlas();
program = new Program(vertexSource, fragmentSource);
program3D = new Program(vertexSource3D, fragmentSource3D);
@ -226,9 +180,13 @@ public class Window extends BaseWindow {
System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE));
}
@Override
public BaseGamepad getGamepad(int id) {
return new Gamepad(id);
}
@Override
public Vec2i getMousePos() {
//todo: scale mouse to stretchMode
double[] x = {0.0};
double[] y = {0.0};
glfwGetCursorPos(window, x, y);
@ -342,7 +300,7 @@ public class Window extends BaseWindow {
public IRenderContext renderStart() {
program.use();
textureAtlas.bind();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
framebuffer.bind();
glViewport(0, 0, size.getX(), size.getY());
textureAtlas.update();
glClearColor(0.f, 0.f, 0.f, 0.0f);
@ -356,7 +314,7 @@ public class Window extends BaseWindow {
public IRenderContext3D renderStart3D() {
program3D.use();
textureAtlas.bind();
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
framebuffer.bind();
glViewport(0, 0, size.getX(), size.getY());
textureAtlas.update();
glClearColor(0.f, 0.f, 0.f, 0.0f);
@ -376,14 +334,14 @@ public class Window extends BaseWindow {
//glEnd();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Framebuffer.unbind();
glViewport(0, 0, windowSize.getX(), windowSize.getY());
programScaler.use();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, framebufferTex);
framebuffer.bindTexture();
glProgramUniform2f(programScaler.getProgram(),
programScaler.getUniformLocation("windowSize"),
windowSize.getX(),