diff --git a/.idea/statistic.xml b/.idea/statistic.xml new file mode 100644 index 0000000..c3b5544 --- /dev/null +++ b/.idea/statistic.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/com/danitheskunk/skunkworks/backends/gl/BasePipeline.java b/com/danitheskunk/skunkworks/backends/gl/BasePipeline.java index b1279de..c6e7bc4 100644 --- a/com/danitheskunk/skunkworks/backends/gl/BasePipeline.java +++ b/com/danitheskunk/skunkworks/backends/gl/BasePipeline.java @@ -1,5 +1,22 @@ package com.danitheskunk.skunkworks.backends.gl; -public class BasePipeline { +public abstract class BasePipeline { + 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(); + } } diff --git a/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java b/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java new file mode 100644 index 0000000..8bfe0e5 --- /dev/null +++ b/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java @@ -0,0 +1,40 @@ +package com.danitheskunk.skunkworks.backends.gl; + +import com.danitheskunk.skunkworks.Vec2i; + +public class Pipeline2D extends BasePipeline { + 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); + } +} diff --git a/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java b/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java new file mode 100644 index 0000000..a403711 --- /dev/null +++ b/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java @@ -0,0 +1,34 @@ +package com.danitheskunk.skunkworks.backends.gl; + +public class Pipeline3D extends BasePipeline { + 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); + } +} diff --git a/com/danitheskunk/skunkworks/backends/gl/RenderContext.java b/com/danitheskunk/skunkworks/backends/gl/RenderContext.java index 1546850..bd7f8b6 100644 --- a/com/danitheskunk/skunkworks/backends/gl/RenderContext.java +++ b/com/danitheskunk/skunkworks/backends/gl/RenderContext.java @@ -11,16 +11,15 @@ class RenderContext extends BaseRenderContext implements IRenderContext { private final int texOffsetIndex; private final int texSizeIndex; private final int tintIndex; + private final int windowSizeIndex; - public RenderContext( - Vec2i size, int texCoordIndex, int texOffsetIndex, - int texSizeIndex, int tintIndex - ) { + public RenderContext(Program program, Vec2i size) { super(size); - this.texCoordIndex = texCoordIndex; - this.texOffsetIndex = texOffsetIndex; - this.texSizeIndex = texSizeIndex; - this.tintIndex = tintIndex; + texCoordIndex = program.getAttribLocation("texCoord"); + texOffsetIndex = program.getUniformLocation("texOffset"); + texSizeIndex = program.getUniformLocation("texSize"); + tintIndex = program.getUniformLocation("tint"); + windowSizeIndex = program.getUniformLocation("windowSize"); } @Override @@ -186,4 +185,9 @@ class RenderContext extends BaseRenderContext implements IRenderContext { glVertex2i(br.getX(), bl.getY()); glEnd(); } + + @Override + public void preFrame() { + glUniform2f(windowSizeIndex, size.getX(), size.getY()); + } } diff --git a/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java b/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java index ae26748..0df95e3 100644 --- a/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java +++ b/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java @@ -14,14 +14,14 @@ public class RenderContext3D extends BaseRenderContext3D { private final int tintIndex; private TextureAtlas textureAtlas; - public RenderContext3D(int texCoordIndex, int tintIndex) { - this.texCoordIndex = texCoordIndex; - this.tintIndex = tintIndex; + public RenderContext3D(Program program) { + texCoordIndex = program.getAttribLocation("texCoord"); + tintIndex = program.getUniformLocation("tint"); } @Override - public void renderMesh(Mesh mesh) { - renderMesh(mesh, Color.WHITE); + public void clearDepth() { + glClear(GL_DEPTH_BUFFER_BIT); } @Override @@ -32,7 +32,8 @@ public class RenderContext3D extends BaseRenderContext3D { var texOff = Vec2f.div(texArea.getPos(), textureAtlas.getSize()); var texMult = Vec2f.div(texArea.getSize(), textureAtlas.getSize()); - glUniform4f(tintIndex, + glUniform4f( + tintIndex, tint.getR() / 255.0f, tint.getG() / 255.0f, tint.getB() / 255.0f, @@ -61,8 +62,6 @@ public class RenderContext3D extends BaseRenderContext3D { t3 = Vec2f.add(texOff, Vec2f.mul(texMult, t3)); - - glVertexAttrib2d(texCoordIndex, t1.getX(), t1.getY()); glVertex3d(p1.getX(), p1.getY() - 0.5, p1.getZ() + 0.5); glVertexAttrib2d(texCoordIndex, t2.getX(), t2.getY()); @@ -74,8 +73,8 @@ public class RenderContext3D extends BaseRenderContext3D { } @Override - public void clearDepth() { - glClear(GL_DEPTH_BUFFER_BIT); + public void renderMesh(Mesh mesh) { + renderMesh(mesh, Color.WHITE); } @Override @@ -101,6 +100,11 @@ public class RenderContext3D extends BaseRenderContext3D { glEnd(); } + @Override + public void preFrame() { + + } + public void setTextureAtlas(TextureAtlas textureAtlas) { this.textureAtlas = textureAtlas; } diff --git a/com/danitheskunk/skunkworks/backends/gl/Window.java b/com/danitheskunk/skunkworks/backends/gl/Window.java index 98e7a13..7a90f8f 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Window.java +++ b/com/danitheskunk/skunkworks/backends/gl/Window.java @@ -18,37 +18,6 @@ import static org.lwjgl.opengl.GL46.*; import static org.lwjgl.system.MemoryUtil.NULL; 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 = """ #version 450 layout(location = 1) in vec2 texCoord; @@ -65,28 +34,6 @@ public class Window extends BaseWindow { //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 = """ #version 450 layout(location = 0) in vec2 pos; @@ -99,12 +46,8 @@ public class Window extends BaseWindow { } """; private final Framebuffer framebuffer; - private final Program program; - private final Program program3D; private final Program programScaler; private final Mat4f projection; - private final RenderContext renderContext; - private final RenderContext3D renderContext3D; private final Vec2i size; private final boolean[] stateMouseClicked; private final boolean[] stateMouseDown; @@ -113,6 +56,8 @@ public class Window extends BaseWindow { private boolean debug; private boolean shouldClose; private Vec2i windowSize; + private final Pipeline2D pipeline2D; + private final Pipeline3D pipeline3D; public Window(Vec2i size, String title, Engine engine) { super(engine); @@ -150,25 +95,16 @@ public class Window extends BaseWindow { framebuffer = new Framebuffer(size); textureAtlas = new TextureAtlas(); - program = new Program(vertexSource, fragmentSource); - program3D = new Program(vertexSource3D, fragmentSource3D); programScaler = new Program(vertexSourceScaler, fragmentSourceScaler); projection = Mat4f.perspective(Math.PI / 2, 16.0 / 9.0, 0.1, 100.0); - renderContext = new RenderContext(size, - program.getAttribLocation("texCoord"), - program.getUniformLocation("texOffset"), - program.getUniformLocation("texSize"), - program.getUniformLocation("tint") - ); + pipeline2D = new Pipeline2D(size); + pipeline3D = new Pipeline3D(textureAtlas); - renderContext3D = new RenderContext3D(program3D.getAttribLocation( - "texCoord"), program3D.getUniformLocation("tint")); - renderContext3D.setTextureAtlas(textureAtlas); - glProgramUniform2f(program.getProgram(), - program.getUniformLocation("windowSize"), + glProgramUniform2f(programScaler.getProgram(), + programScaler.getUniformLocation("windowSize"), size.getX(), size.getY() ); @@ -298,7 +234,7 @@ public class Window extends BaseWindow { @Override public IRenderContext renderStart() { - program.use(); + pipeline2D.use(); textureAtlas.bind(); framebuffer.bind(); glViewport(0, 0, size.getX(), size.getY()); @@ -307,12 +243,12 @@ public class Window extends BaseWindow { glClear(GL_DEPTH_BUFFER_BIT); glDepthFunc(GL_ALWAYS); //glBegin(GL_TRIANGLES); - return renderContext; + return pipeline2D.getRenderContext(); } @Override public IRenderContext3D renderStart3D() { - program3D.use(); + pipeline3D.use(); textureAtlas.bind(); framebuffer.bind(); 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); glDepthFunc(GL_LESS); //glBegin(GL_TRIANGLES); - glUniformMatrix4fv(program3D.getUniformLocation("projection"), + glUniformMatrix4fv(pipeline3D.program.getUniformLocation("projection"), true, projection.asFloatArray() ); - return renderContext3D; + return pipeline3D.getRenderContext(); } @Override diff --git a/com/danitheskunk/skunkworks/gfx/IRenderContext.java b/com/danitheskunk/skunkworks/gfx/IRenderContext.java index 7e5b4fb..3d31d76 100644 --- a/com/danitheskunk/skunkworks/gfx/IRenderContext.java +++ b/com/danitheskunk/skunkworks/gfx/IRenderContext.java @@ -27,5 +27,7 @@ public interface IRenderContext { void drawTextureRectangle( Recti rect, ITexture texture, Color color, boolean repeat ); + + void preFrame(); //todo: drawTextureRectangleRepeat } diff --git a/com/danitheskunk/skunkworks/gfx/threedee/IRenderContext3D.java b/com/danitheskunk/skunkworks/gfx/threedee/IRenderContext3D.java index 722f76f..dfe3989 100644 --- a/com/danitheskunk/skunkworks/gfx/threedee/IRenderContext3D.java +++ b/com/danitheskunk/skunkworks/gfx/threedee/IRenderContext3D.java @@ -15,4 +15,6 @@ public interface IRenderContext3D { void renderModel(Model model); void renderTriangle(Vec3f p1, Vec3f p2, Vec3f p3); + + void preFrame(); }