refactored out pipelines
This commit is contained in:
parent
007be3e9f3
commit
baddb2cca8
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Statistic">
|
||||||
|
<option name="fileTypesIncluded" value="java;" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -1,5 +1,22 @@
|
||||||
package com.danitheskunk.skunkworks.backends.gl;
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
public class BasePipeline {
|
public abstract class BasePipeline<TRenderContext> {
|
||||||
|
protected Program program;
|
||||||
|
protected TRenderContext renderContext;
|
||||||
|
|
||||||
|
public BasePipeline(Program program) {
|
||||||
|
this.program = program;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TRenderContext getRenderContext() {
|
||||||
|
return renderContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRenderContext(TRenderContext renderContext) {
|
||||||
|
this.renderContext = renderContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void use() {
|
||||||
|
program.use();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
|
|
||||||
|
public class Pipeline2D extends BasePipeline<RenderContext> {
|
||||||
|
private static final String fragmentSource = """
|
||||||
|
#version 450
|
||||||
|
layout(location = 1) in vec2 texCoord;
|
||||||
|
layout(binding = 0) uniform sampler2D tex;
|
||||||
|
layout(location = 3) uniform ivec2 texOffset;
|
||||||
|
layout(location = 4) uniform ivec2 texSize;
|
||||||
|
layout(location = 5) uniform vec4 tint;
|
||||||
|
out vec4 color;
|
||||||
|
void main() {
|
||||||
|
//color = vec4(vec2(texCoord).x/1000.f, 0.2f, 0.7f, 1.0f);
|
||||||
|
//color = texture(tex, texCoord);
|
||||||
|
vec4 col = texelFetch(tex, ivec2(mod(texCoord, texSize)) + texOffset, 0);
|
||||||
|
|
||||||
|
if(tint.a > 0.0) {
|
||||||
|
color = col * tint;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
private static final String vertexSource = """
|
||||||
|
#version 450
|
||||||
|
layout(location = 0) in vec2 pos;
|
||||||
|
layout(location = 1) in ivec2 texCoord;
|
||||||
|
layout(location = 2) uniform vec2 windowSize;
|
||||||
|
layout(location = 1) out vec2 out_texCoord;
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(pos / windowSize * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f);
|
||||||
|
out_texCoord = texCoord;
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
public Pipeline2D(Vec2i size) {
|
||||||
|
super(new Program(vertexSource, fragmentSource));
|
||||||
|
renderContext = new RenderContext(program, size);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
|
public class Pipeline3D extends BasePipeline<RenderContext3D> {
|
||||||
|
private static final String fragmentSource = """
|
||||||
|
#version 450
|
||||||
|
layout(location = 1) in vec2 texCoord;
|
||||||
|
layout(binding = 0) uniform sampler2D tex;
|
||||||
|
layout(location = 5) uniform vec4 tint;
|
||||||
|
out vec4 color;
|
||||||
|
void main() {
|
||||||
|
//color = vec4(vec2(texCoord).x/1000.f, 0.2f, 0.7f, 1.0f);
|
||||||
|
color = texture(tex, texCoord) * tint;
|
||||||
|
//color = vec4(0.0, texCoord.xy, 1.0);
|
||||||
|
//color = vec4(1.0, texCoord.x / 2, 1.0, 1.0);
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
private static final String vertexSource = """
|
||||||
|
#version 450
|
||||||
|
layout(location = 0) in vec3 pos;
|
||||||
|
layout(location = 1) in vec2 texCoord;
|
||||||
|
layout(location = 2) uniform mat4 projection;
|
||||||
|
layout(location = 1) out vec2 out_texCoord;
|
||||||
|
void main() {
|
||||||
|
gl_Position = projection * vec4(pos, 1.0f);
|
||||||
|
out_texCoord = texCoord;
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
public Pipeline3D(TextureAtlas atlas) {
|
||||||
|
super(new Program(vertexSource, fragmentSource));
|
||||||
|
renderContext = new RenderContext3D(program);
|
||||||
|
renderContext.setTextureAtlas(atlas);
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,16 +11,15 @@ class RenderContext extends BaseRenderContext implements IRenderContext {
|
||||||
private final int texOffsetIndex;
|
private final int texOffsetIndex;
|
||||||
private final int texSizeIndex;
|
private final int texSizeIndex;
|
||||||
private final int tintIndex;
|
private final int tintIndex;
|
||||||
|
private final int windowSizeIndex;
|
||||||
|
|
||||||
public RenderContext(
|
public RenderContext(Program program, Vec2i size) {
|
||||||
Vec2i size, int texCoordIndex, int texOffsetIndex,
|
|
||||||
int texSizeIndex, int tintIndex
|
|
||||||
) {
|
|
||||||
super(size);
|
super(size);
|
||||||
this.texCoordIndex = texCoordIndex;
|
texCoordIndex = program.getAttribLocation("texCoord");
|
||||||
this.texOffsetIndex = texOffsetIndex;
|
texOffsetIndex = program.getUniformLocation("texOffset");
|
||||||
this.texSizeIndex = texSizeIndex;
|
texSizeIndex = program.getUniformLocation("texSize");
|
||||||
this.tintIndex = tintIndex;
|
tintIndex = program.getUniformLocation("tint");
|
||||||
|
windowSizeIndex = program.getUniformLocation("windowSize");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -186,4 +185,9 @@ class RenderContext extends BaseRenderContext implements IRenderContext {
|
||||||
glVertex2i(br.getX(), bl.getY());
|
glVertex2i(br.getX(), bl.getY());
|
||||||
glEnd();
|
glEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void preFrame() {
|
||||||
|
glUniform2f(windowSizeIndex, size.getX(), size.getY());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,14 +14,14 @@ public class RenderContext3D extends BaseRenderContext3D {
|
||||||
private final int tintIndex;
|
private final int tintIndex;
|
||||||
private TextureAtlas textureAtlas;
|
private TextureAtlas textureAtlas;
|
||||||
|
|
||||||
public RenderContext3D(int texCoordIndex, int tintIndex) {
|
public RenderContext3D(Program program) {
|
||||||
this.texCoordIndex = texCoordIndex;
|
texCoordIndex = program.getAttribLocation("texCoord");
|
||||||
this.tintIndex = tintIndex;
|
tintIndex = program.getUniformLocation("tint");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void renderMesh(Mesh mesh) {
|
public void clearDepth() {
|
||||||
renderMesh(mesh, Color.WHITE);
|
glClear(GL_DEPTH_BUFFER_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -32,7 +32,8 @@ public class RenderContext3D extends BaseRenderContext3D {
|
||||||
var texOff = Vec2f.div(texArea.getPos(), textureAtlas.getSize());
|
var texOff = Vec2f.div(texArea.getPos(), textureAtlas.getSize());
|
||||||
var texMult = Vec2f.div(texArea.getSize(), textureAtlas.getSize());
|
var texMult = Vec2f.div(texArea.getSize(), textureAtlas.getSize());
|
||||||
|
|
||||||
glUniform4f(tintIndex,
|
glUniform4f(
|
||||||
|
tintIndex,
|
||||||
tint.getR() / 255.0f,
|
tint.getR() / 255.0f,
|
||||||
tint.getG() / 255.0f,
|
tint.getG() / 255.0f,
|
||||||
tint.getB() / 255.0f,
|
tint.getB() / 255.0f,
|
||||||
|
@ -61,8 +62,6 @@ public class RenderContext3D extends BaseRenderContext3D {
|
||||||
t3 = Vec2f.add(texOff, Vec2f.mul(texMult, t3));
|
t3 = Vec2f.add(texOff, Vec2f.mul(texMult, t3));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
glVertexAttrib2d(texCoordIndex, t1.getX(), t1.getY());
|
glVertexAttrib2d(texCoordIndex, t1.getX(), t1.getY());
|
||||||
glVertex3d(p1.getX(), p1.getY() - 0.5, p1.getZ() + 0.5);
|
glVertex3d(p1.getX(), p1.getY() - 0.5, p1.getZ() + 0.5);
|
||||||
glVertexAttrib2d(texCoordIndex, t2.getX(), t2.getY());
|
glVertexAttrib2d(texCoordIndex, t2.getX(), t2.getY());
|
||||||
|
@ -74,8 +73,8 @@ public class RenderContext3D extends BaseRenderContext3D {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearDepth() {
|
public void renderMesh(Mesh mesh) {
|
||||||
glClear(GL_DEPTH_BUFFER_BIT);
|
renderMesh(mesh, Color.WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -101,6 +100,11 @@ public class RenderContext3D extends BaseRenderContext3D {
|
||||||
glEnd();
|
glEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void preFrame() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public void setTextureAtlas(TextureAtlas textureAtlas) {
|
public void setTextureAtlas(TextureAtlas textureAtlas) {
|
||||||
this.textureAtlas = textureAtlas;
|
this.textureAtlas = textureAtlas;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,37 +18,6 @@ import static org.lwjgl.opengl.GL46.*;
|
||||||
import static org.lwjgl.system.MemoryUtil.NULL;
|
import static org.lwjgl.system.MemoryUtil.NULL;
|
||||||
|
|
||||||
public class Window extends BaseWindow {
|
public class Window extends BaseWindow {
|
||||||
private static final String fragmentSource = """
|
|
||||||
#version 450
|
|
||||||
layout(location = 1) in vec2 texCoord;
|
|
||||||
layout(binding = 0) uniform sampler2D tex;
|
|
||||||
layout(location = 3) uniform ivec2 texOffset;
|
|
||||||
layout(location = 4) uniform ivec2 texSize;
|
|
||||||
layout(location = 5) uniform vec4 tint;
|
|
||||||
out vec4 color;
|
|
||||||
void main() {
|
|
||||||
//color = vec4(vec2(texCoord).x/1000.f, 0.2f, 0.7f, 1.0f);
|
|
||||||
//color = texture(tex, texCoord);
|
|
||||||
vec4 col = texelFetch(tex, ivec2(mod(texCoord, texSize)) + texOffset, 0);
|
|
||||||
|
|
||||||
if(tint.a > 0.0) {
|
|
||||||
color = col * tint;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
""";
|
|
||||||
private static final String fragmentSource3D = """
|
|
||||||
#version 450
|
|
||||||
layout(location = 1) in vec2 texCoord;
|
|
||||||
layout(binding = 0) uniform sampler2D tex;
|
|
||||||
layout(location = 5) uniform vec4 tint;
|
|
||||||
out vec4 color;
|
|
||||||
void main() {
|
|
||||||
//color = vec4(vec2(texCoord).x/1000.f, 0.2f, 0.7f, 1.0f);
|
|
||||||
color = texture(tex, texCoord) * tint;
|
|
||||||
//color = vec4(0.0, texCoord.xy, 1.0);
|
|
||||||
//color = vec4(1.0, texCoord.x / 2, 1.0, 1.0);
|
|
||||||
}
|
|
||||||
""";
|
|
||||||
private static final String fragmentSourceScaler = """
|
private static final String fragmentSourceScaler = """
|
||||||
#version 450
|
#version 450
|
||||||
layout(location = 1) in vec2 texCoord;
|
layout(location = 1) in vec2 texCoord;
|
||||||
|
@ -65,28 +34,6 @@ public class Window extends BaseWindow {
|
||||||
//color = vec4(texCoord.xy, 0.0, 1.0);
|
//color = vec4(texCoord.xy, 0.0, 1.0);
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
private static final String vertexSource = """
|
|
||||||
#version 450
|
|
||||||
layout(location = 0) in vec2 pos;
|
|
||||||
layout(location = 1) in ivec2 texCoord;
|
|
||||||
layout(location = 2) uniform vec2 windowSize;
|
|
||||||
layout(location = 1) out vec2 out_texCoord;
|
|
||||||
void main() {
|
|
||||||
gl_Position = vec4(pos / windowSize * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f);
|
|
||||||
out_texCoord = texCoord;
|
|
||||||
}
|
|
||||||
""";
|
|
||||||
private static final String vertexSource3D = """
|
|
||||||
#version 450
|
|
||||||
layout(location = 0) in vec3 pos;
|
|
||||||
layout(location = 1) in vec2 texCoord;
|
|
||||||
layout(location = 2) uniform mat4 projection;
|
|
||||||
layout(location = 1) out vec2 out_texCoord;
|
|
||||||
void main() {
|
|
||||||
gl_Position = projection * vec4(pos, 1.0f);
|
|
||||||
out_texCoord = texCoord;
|
|
||||||
}
|
|
||||||
""";
|
|
||||||
private static final String vertexSourceScaler = """
|
private static final String vertexSourceScaler = """
|
||||||
#version 450
|
#version 450
|
||||||
layout(location = 0) in vec2 pos;
|
layout(location = 0) in vec2 pos;
|
||||||
|
@ -99,12 +46,8 @@ public class Window extends BaseWindow {
|
||||||
}
|
}
|
||||||
""";
|
""";
|
||||||
private final Framebuffer framebuffer;
|
private final Framebuffer framebuffer;
|
||||||
private final Program program;
|
|
||||||
private final Program program3D;
|
|
||||||
private final Program programScaler;
|
private final Program programScaler;
|
||||||
private final Mat4f projection;
|
private final Mat4f projection;
|
||||||
private final RenderContext renderContext;
|
|
||||||
private final RenderContext3D renderContext3D;
|
|
||||||
private final Vec2i size;
|
private final Vec2i size;
|
||||||
private final boolean[] stateMouseClicked;
|
private final boolean[] stateMouseClicked;
|
||||||
private final boolean[] stateMouseDown;
|
private final boolean[] stateMouseDown;
|
||||||
|
@ -113,6 +56,8 @@ public class Window extends BaseWindow {
|
||||||
private boolean debug;
|
private boolean debug;
|
||||||
private boolean shouldClose;
|
private boolean shouldClose;
|
||||||
private Vec2i windowSize;
|
private Vec2i windowSize;
|
||||||
|
private final Pipeline2D pipeline2D;
|
||||||
|
private final Pipeline3D pipeline3D;
|
||||||
|
|
||||||
public Window(Vec2i size, String title, Engine engine) {
|
public Window(Vec2i size, String title, Engine engine) {
|
||||||
super(engine);
|
super(engine);
|
||||||
|
@ -150,25 +95,16 @@ public class Window extends BaseWindow {
|
||||||
|
|
||||||
framebuffer = new Framebuffer(size);
|
framebuffer = new Framebuffer(size);
|
||||||
textureAtlas = new TextureAtlas();
|
textureAtlas = new TextureAtlas();
|
||||||
program = new Program(vertexSource, fragmentSource);
|
|
||||||
program3D = new Program(vertexSource3D, fragmentSource3D);
|
|
||||||
programScaler = new Program(vertexSourceScaler, fragmentSourceScaler);
|
programScaler = new Program(vertexSourceScaler, fragmentSourceScaler);
|
||||||
|
|
||||||
projection = Mat4f.perspective(Math.PI / 2, 16.0 / 9.0, 0.1, 100.0);
|
projection = Mat4f.perspective(Math.PI / 2, 16.0 / 9.0, 0.1, 100.0);
|
||||||
|
|
||||||
renderContext = new RenderContext(size,
|
pipeline2D = new Pipeline2D(size);
|
||||||
program.getAttribLocation("texCoord"),
|
pipeline3D = new Pipeline3D(textureAtlas);
|
||||||
program.getUniformLocation("texOffset"),
|
|
||||||
program.getUniformLocation("texSize"),
|
|
||||||
program.getUniformLocation("tint")
|
|
||||||
);
|
|
||||||
|
|
||||||
renderContext3D = new RenderContext3D(program3D.getAttribLocation(
|
|
||||||
"texCoord"), program3D.getUniformLocation("tint"));
|
|
||||||
renderContext3D.setTextureAtlas(textureAtlas);
|
|
||||||
|
|
||||||
glProgramUniform2f(program.getProgram(),
|
glProgramUniform2f(programScaler.getProgram(),
|
||||||
program.getUniformLocation("windowSize"),
|
programScaler.getUniformLocation("windowSize"),
|
||||||
size.getX(),
|
size.getX(),
|
||||||
size.getY()
|
size.getY()
|
||||||
);
|
);
|
||||||
|
@ -298,7 +234,7 @@ public class Window extends BaseWindow {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IRenderContext renderStart() {
|
public IRenderContext renderStart() {
|
||||||
program.use();
|
pipeline2D.use();
|
||||||
textureAtlas.bind();
|
textureAtlas.bind();
|
||||||
framebuffer.bind();
|
framebuffer.bind();
|
||||||
glViewport(0, 0, size.getX(), size.getY());
|
glViewport(0, 0, size.getX(), size.getY());
|
||||||
|
@ -307,12 +243,12 @@ public class Window extends BaseWindow {
|
||||||
glClear(GL_DEPTH_BUFFER_BIT);
|
glClear(GL_DEPTH_BUFFER_BIT);
|
||||||
glDepthFunc(GL_ALWAYS);
|
glDepthFunc(GL_ALWAYS);
|
||||||
//glBegin(GL_TRIANGLES);
|
//glBegin(GL_TRIANGLES);
|
||||||
return renderContext;
|
return pipeline2D.getRenderContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IRenderContext3D renderStart3D() {
|
public IRenderContext3D renderStart3D() {
|
||||||
program3D.use();
|
pipeline3D.use();
|
||||||
textureAtlas.bind();
|
textureAtlas.bind();
|
||||||
framebuffer.bind();
|
framebuffer.bind();
|
||||||
glViewport(0, 0, size.getX(), size.getY());
|
glViewport(0, 0, size.getX(), size.getY());
|
||||||
|
@ -321,12 +257,12 @@ public class Window extends BaseWindow {
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
glDepthFunc(GL_LESS);
|
glDepthFunc(GL_LESS);
|
||||||
//glBegin(GL_TRIANGLES);
|
//glBegin(GL_TRIANGLES);
|
||||||
glUniformMatrix4fv(program3D.getUniformLocation("projection"),
|
glUniformMatrix4fv(pipeline3D.program.getUniformLocation("projection"),
|
||||||
true,
|
true,
|
||||||
projection.asFloatArray()
|
projection.asFloatArray()
|
||||||
);
|
);
|
||||||
|
|
||||||
return renderContext3D;
|
return pipeline3D.getRenderContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -27,5 +27,7 @@ public interface IRenderContext {
|
||||||
void drawTextureRectangle(
|
void drawTextureRectangle(
|
||||||
Recti rect, ITexture texture, Color color, boolean repeat
|
Recti rect, ITexture texture, Color color, boolean repeat
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void preFrame();
|
||||||
//todo: drawTextureRectangleRepeat
|
//todo: drawTextureRectangleRepeat
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,4 +15,6 @@ public interface IRenderContext3D {
|
||||||
void renderModel(Model model);
|
void renderModel(Model model);
|
||||||
|
|
||||||
void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3);
|
void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3);
|
||||||
|
|
||||||
|
void preFrame();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue