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