refactored, and changed gl backend to use shaders

This commit is contained in:
DaniTheSkunk 2022-10-06 01:46:14 +00:00
parent 9c992aa27e
commit 721185c413
27 changed files with 231 additions and 94 deletions

View File

@ -1,11 +1,4 @@
import com.danitheskunk.skunkworks.*;
import com.danitheskunk.skunkworks.windows.*;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
@ -28,19 +21,19 @@ public class Test {
var engine = new Engine();
var window = engine.openWindow(new Vec2i(1280, 720), "Skunkworks");
//var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png");
//var tex2 = window.loadTexture("C:\\art\\pixel stuff.png");
//var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16));
//var font = window.loadFontTileset("EGA8x14.png");
//var font2 = window.loadFontTTF("fonts\\LiberationSans-Regular.ttf", 16*8.f);
//var tex = window.loadTexture(img);
var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png");
var tex2 = window.loadTexture("C:\\art\\pixel stuff.png");
var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16));
var font = window.loadFontTileset("EGA8x14.png");
var font2 = window.loadFontTTF("fonts\\LiberationSans-Regular.ttf", 16*8.f);
var tex = window.loadTexture(img);
var slice = window.loadNineSlice("demoassets\\9slice-1.png");
//window.setDebug(true);
while(!window.shouldClose()) {
window.tick();
var renderContext = window.renderStart();
/*renderContext.drawTextureRectangle(
renderContext.drawTextureRectangle(
new Recti(0, 0, 1280, 720),
tex
);
@ -57,7 +50,7 @@ public class Test {
renderContext.drawString(new Vec2i(100, 100), "hello world mew", font);
renderContext.drawString(new Vec2i(710, 140), "hello world mew", font2);
*/
renderContext.drawNineSlice(slice, new Recti(100, 100, 1080, 520));
window.renderFinish(renderContext);
}

View File

@ -1,12 +1,15 @@
package com.danitheskunk.skunkworks;
import org.lwjgl.stb.STBTTFontinfo;
import static org.lwjgl.stb.STBTruetype.*;
import com.danitheskunk.skunkworks.gfx.Image;
import com.danitheskunk.skunkworks.gfx.NineSlice;
import com.danitheskunk.skunkworks.gfx.font.FontTTF;
import com.danitheskunk.skunkworks.gfx.font.FontTileset;
import com.danitheskunk.skunkworks.gfx.font.IFont;
abstract class BaseWindow implements IWindow {
abstract public class BaseWindow implements IWindow {
protected final Engine engine;
BaseWindow(Engine engine) {
public BaseWindow(Engine engine) {
this.engine = engine;
}

View File

@ -1,12 +1,15 @@
package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.backends.gl.Window;
import com.danitheskunk.skunkworks.gfx.GraphicsBackend;
import com.danitheskunk.skunkworks.gfx.Image;
import org.lwjgl.BufferUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import static com.danitheskunk.skunkworks.GraphicsBackend.*;
import static com.danitheskunk.skunkworks.gfx.GraphicsBackend.*;
public class Engine {
private final GraphicsBackend graphicsBackend;
@ -40,7 +43,7 @@ public class Engine {
public IWindow openWindow(Vec2i size, String title) {
return switch(graphicsBackend) {
case OPENGL -> new GLWindow(size, title, this);
case OPENGL -> new Window(size, title, this);
};
}
}

View File

@ -1,5 +0,0 @@
package com.danitheskunk.skunkworks;
public interface ITexture {
Vec2i getSize();
}

View File

@ -1,5 +1,11 @@
package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image;
import com.danitheskunk.skunkworks.gfx.NineSlice;
import com.danitheskunk.skunkworks.gfx.font.IFont;
import java.util.List;
public interface IWindow {

View File

@ -0,0 +1,38 @@
package com.danitheskunk.skunkworks.backends.gl;
import static org.lwjgl.opengl.GL46.*;
public class Program {
Shader vertex;
Shader fragment;
int program;
public Program(String vertexSource, String fragmentSource) {
vertex = new Shader(vertexSource, GL_VERTEX_SHADER);
fragment = new Shader(fragmentSource, GL_FRAGMENT_SHADER);
program = glCreateProgram();
glAttachShader(program, vertex.getShader());
glAttachShader(program, fragment.getShader());
glLinkProgram(program);
}
public void use() {
glUseProgram(program);
}
public int getAttribLocation(String attrib) {
return glGetAttribLocation(program, attrib);
}
public int getUniformLocation(String attrib) {
return glGetUniformLocation(program, attrib);
}
public Shader getVertex() {
return vertex;
}
public Shader getFragment() {
return fragment;
}
}

View File

@ -1,12 +1,17 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.backends.gl;
import static org.lwjgl.opengl.GL11.*;
import com.danitheskunk.skunkworks.*;
import com.danitheskunk.skunkworks.gfx.*;
class GLRenderContext extends BaseRenderContext implements IRenderContext{
private final GLTextureAtlas atlas;
import static org.lwjgl.opengl.GL46.*;
public GLRenderContext(GLTextureAtlas atlas) {
class RenderContext extends BaseRenderContext implements IRenderContext {
private final TextureAtlas atlas;
private final int texCoordIndex;
public RenderContext(TextureAtlas atlas, int texCoordIndex) {
this.atlas = atlas;
this.texCoordIndex = texCoordIndex;
}
@Override
@ -62,7 +67,7 @@ class GLRenderContext extends BaseRenderContext implements IRenderContext{
@Override
public void drawTextureRectangle(Recti rect, ITexture texture, Color color) {
var tex = (GLTexture)texture;
var tex = (Texture)texture;
var tl = rect.getTopLeft();
var tr = rect.getTopRight();
@ -82,18 +87,18 @@ class GLRenderContext extends BaseRenderContext implements IRenderContext{
);
//counterclockwise triangles
glTexCoord2d(tbl.getX(), tbl.getY());
glVertexAttrib2d(texCoordIndex, tbl.getX(), tbl.getY());
glVertex2i(bl.getX(), bl.getY());
glTexCoord2d(ttr.getX(), ttr.getY());
glVertexAttrib2d(texCoordIndex, ttr.getX(), ttr.getY());
glVertex2i(tr.getX(), tr.getY());
glTexCoord2d(ttl.getX(), ttl.getY());
glVertexAttrib2d(texCoordIndex, ttl.getX(), ttl.getY());
glVertex2i(tl.getX(), tl.getY());
glTexCoord2d(ttr.getX(), ttr.getY());
glVertexAttrib2d(texCoordIndex, ttr.getX(), ttr.getY());
glVertex2i(tr.getX(), tr.getY());
glTexCoord2d(tbl.getX(), tbl.getY());
glVertexAttrib2d(texCoordIndex, tbl.getX(), tbl.getY());
glVertex2i(bl.getX(), bl.getY());
glTexCoord2d(tbr.getX(), tbr.getY());
glVertexAttrib2d(texCoordIndex, tbr.getX(), tbr.getY());
glVertex2i(br.getX(), bl.getY());
}
}

View File

@ -0,0 +1,28 @@
package com.danitheskunk.skunkworks.backends.gl;
import static org.lwjgl.opengl.GL46.*;
public class Shader {
private int shader;
private int type;
public Shader(String source, int type) {
this.type = type;
shader = glCreateShader(type);
glShaderSource(shader, source);
glCompileShader(shader);
int[] ret = {0};
glGetShaderiv(shader, GL_COMPILE_STATUS, ret);
if(ret[0] == GL_FALSE) {
var errmsg = glGetShaderInfoLog(shader);
glDeleteShader(shader);
throw new RuntimeException("Shader error:\n" + errmsg);
}
}
public int getShader() {
return shader;
}
}

View File

@ -1,12 +1,17 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.backends.gl;
class GLTexture implements ITexture {
import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image;
import com.danitheskunk.skunkworks.Recti;
import com.danitheskunk.skunkworks.Vec2i;
class Texture implements ITexture {
private Recti texArea;
private final Image img; //for re-blitting onto texture atlas
GLTexture(Recti texArea, Image img) {
Texture(Recti texArea, Image img) {
this.texArea = texArea;
this.img = img;
}

View File

@ -1,5 +1,9 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image;
import com.danitheskunk.skunkworks.Recti;
import com.danitheskunk.skunkworks.Vec2i;
import org.lwjgl.BufferUtils;
import java.util.ArrayList;
@ -8,13 +12,13 @@ import java.util.List;
import static org.lwjgl.opengl.GL11.*;
class GLTextureAtlas {
private GLTexture atlasTexture; //for debugging
class TextureAtlas {
private Texture atlasTexture; //for debugging
private Image img;
private boolean shouldUpdate;
private final List<GLTexture> textures;
private final List<Texture> textures;
GLTextureAtlas() {
TextureAtlas() {
img = new Image(new Vec2i(32, 32));
int textureID = glGenTextures();
textures = new ArrayList<>();
@ -31,7 +35,7 @@ class GLTextureAtlas {
ITexture addTexture(Image img) {
//todo: do the actual texture stuff
//this.img = img;
var texture = new GLTexture(new Recti(new Vec2i(0, 0), img.getSize()), img);
var texture = new Texture(new Recti(new Vec2i(0, 0), img.getSize()), img);
textures.add(texture);
shouldUpdate = true;
return texture;
@ -40,7 +44,7 @@ class GLTextureAtlas {
void doubleAtlasSize() {
img = new Image(Vec2i.mul(img.getSize(), 2));
System.out.printf("Resized atlas to %dx%d\n", img.getSize().getX(), img.getSize().getY());
atlasTexture = new GLTexture(new Recti(Vec2i.ZERO, img.getSize()), img);
atlasTexture = new Texture(new Recti(Vec2i.ZERO, img.getSize()), img);
}
public void update() {
@ -114,7 +118,7 @@ class GLTextureAtlas {
);
}
public GLTexture getAtlasTexture() {
public Texture getAtlasTexture() {
return atlasTexture;
}
@ -122,9 +126,9 @@ class GLTextureAtlas {
return img.getSize();
}
private static class TextureHeightComparator implements Comparator<GLTexture> {
private static class TextureHeightComparator implements Comparator<Texture> {
@Override
public int compare(GLTexture o1, GLTexture o2) {
public int compare(Texture o1, Texture o2) {
return o1.getImg().getHeight() - o2.getImg().getHeight();
}
}

View File

@ -1,5 +1,9 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.*;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
@ -8,18 +12,19 @@ import java.util.ArrayList;
import java.util.List;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL46.*;
import static org.lwjgl.system.MemoryUtil.NULL;
class GLWindow extends BaseWindow {
public class Window extends BaseWindow {
private boolean debug;
private final GLRenderContext renderContext;
private final Program program;
private final RenderContext renderContext;
private boolean shouldClose;
private final Vec2i size;
private final GLTextureAtlas textureAtlas;
private final TextureAtlas textureAtlas;
private final long window;
public GLWindow(Vec2i size, String title, Engine engine) {
public Window(Vec2i size, String title, Engine engine) {
super(engine);
GLFWErrorCallback.createPrint(System.err).set();
if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
@ -44,8 +49,11 @@ class GLWindow extends BaseWindow {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
textureAtlas = new GLTextureAtlas();
renderContext = new GLRenderContext(textureAtlas);
textureAtlas = new TextureAtlas();
program = new Program(vertexSource, fragmentSource);
program.use();
renderContext = new RenderContext(textureAtlas, program.getAttribLocation("texCoord"));
glProgramUniform2f(program.program, program.getUniformLocation("windowSize"), size.getX(), size.getY());
shouldClose = false;
@ -102,6 +110,7 @@ class GLWindow extends BaseWindow {
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
program.use();
glBegin(GL_TRIANGLES);
return renderContext;
}
@ -126,4 +135,27 @@ class GLWindow extends BaseWindow {
glfwPollEvents();
}
private static String vertexSource = """
#version 450
layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 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 String fragmentSource = """
#version 450
layout(location = 1) in vec2 texCoord;
layout(binding = 0) uniform sampler2D tex;
out vec4 color;
void main() {
//color = vec4(texCoord.x, texCoord.y, 0.7f, 1.0f);
color = texture(tex, texCoord);
}
""";
}

View File

@ -1,10 +1,14 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.gfx;
import com.danitheskunk.skunkworks.gfx.font.IFont;
import com.danitheskunk.skunkworks.Recti;
import com.danitheskunk.skunkworks.Vec2i;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
abstract class BaseRenderContext implements IRenderContext {
abstract public class BaseRenderContext implements IRenderContext {
public void drawString(Vec2i pos, String string, IFont font) {
int x = pos.getX();
int y = pos.getY();

View File

@ -1,4 +1,4 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.gfx;
public final class Color {
private final int r, g, b, a;

View File

@ -1,4 +1,4 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.gfx;
public enum GraphicsBackend {
OPENGL

View File

@ -1,4 +1,7 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.gfx;
import com.danitheskunk.skunkworks.*;
import com.danitheskunk.skunkworks.gfx.font.IFont;
public interface IRenderContext {
void drawNineSlice(NineSlice slice, Recti rect); //todo: add rounding mode

View File

@ -0,0 +1,7 @@
package com.danitheskunk.skunkworks.gfx;
import com.danitheskunk.skunkworks.Vec2i;
public interface ITexture {
Vec2i getSize();
}

View File

@ -1,5 +1,7 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.gfx;
import com.danitheskunk.skunkworks.Recti;
import com.danitheskunk.skunkworks.Vec2i;
import org.lwjgl.stb.STBImage;
import java.nio.ByteBuffer;
@ -9,7 +11,7 @@ public class Image {
private final Vec2i size;
//constructors
Image(ByteBuffer buffer) { //png or similar
public Image(ByteBuffer buffer) { //png or similar
//todo: resource system
int[] x = {0}, y = {0}, n = {0};
var img = STBImage.stbi_load_from_memory(buffer, x, y, n, 4);
@ -19,12 +21,12 @@ public class Image {
img.get(data);
}
Image(Vec2i size) {
public Image(Vec2i size) {
this.size = size;
data = new byte[size.getX() * size.getY() * 4];
}
Image(ByteBuffer buffer, Vec2i size) {
public Image(ByteBuffer buffer, Vec2i size) {
data = new byte[buffer.remaining()];
buffer.get(data);
this.size = size;

View File

@ -1,4 +1,6 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.gfx;
import com.danitheskunk.skunkworks.gfx.ITexture;
public class NineSlice {
private ITexture topLeft;
@ -10,7 +12,7 @@ public class NineSlice {
private ITexture bottom;
private ITexture left;
private ITexture center;
NineSlice(ITexture topLeft, ITexture topRight, ITexture bottomLeft, ITexture bottomRight, ITexture top, ITexture right, ITexture bottom, ITexture left, ITexture center) {
public NineSlice(ITexture topLeft, ITexture topRight, ITexture bottomLeft, ITexture bottomRight, ITexture top, ITexture right, ITexture bottom, ITexture left, ITexture center) {
this.topLeft = topLeft;
this.topRight = topRight;
this.bottomLeft = bottomLeft;
@ -22,39 +24,39 @@ public class NineSlice {
this.center = center;
}
ITexture getTopLeft() {
public ITexture getTopLeft() {
return topLeft;
}
ITexture getTopRight() {
public ITexture getTopRight() {
return topRight;
}
ITexture getBottomLeft() {
public ITexture getBottomLeft() {
return bottomLeft;
}
ITexture getBottomRight() {
public ITexture getBottomRight() {
return bottomRight;
}
ITexture getTop() {
public ITexture getTop() {
return top;
}
ITexture getRight() {
public ITexture getRight() {
return right;
}
ITexture getBottom() {
public ITexture getBottom() {
return bottom;
}
ITexture getLeft() {
public ITexture getLeft() {
return left;
}
ITexture getCenter() {
public ITexture getCenter() {
return center;
}
}

View File

@ -1,10 +1,11 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.gfx.font;
import org.lwjgl.BufferUtils;
import com.danitheskunk.skunkworks.IWindow;
import com.danitheskunk.skunkworks.Vec2i;
import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image;
import org.lwjgl.stb.STBTTFontinfo;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
@ -19,7 +20,7 @@ public class FontTTF implements IFont {
STBTTFontinfo info;
IWindow window;
FontTTF(ByteBuffer buffer, float size, IWindow window) {
public FontTTF(ByteBuffer buffer, float size, IWindow window) {
int[] ascent = {0};
int[] descent = {0};
int[] lineGap = {0};

View File

@ -1,12 +1,15 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.gfx.font;
import com.danitheskunk.skunkworks.Vec2i;
import com.danitheskunk.skunkworks.gfx.ITexture;
import java.util.List;
class FontTileset implements IFont {
public class FontTileset implements IFont {
private List<ITexture> textures;
private Vec2i charSize;
FontTileset(List<ITexture> textures) {
public FontTileset(List<ITexture> textures) {
this.textures = textures;
this.charSize = textures.get(0).getSize();
}

View File

@ -1,4 +1,7 @@
package com.danitheskunk.skunkworks;
package com.danitheskunk.skunkworks.gfx.font;
import com.danitheskunk.skunkworks.Vec2i;
import com.danitheskunk.skunkworks.gfx.ITexture;
public interface IFont {
int getLineHeight(int ch);

View File

@ -1,4 +1,4 @@
package com.danitheskunk.skunkworks.windows;
package com.danitheskunk.skunkworks.os.windows;
import com.sun.jna.Library;
import com.sun.jna.Native;

View File

@ -1,4 +1,4 @@
package com.danitheskunk.skunkworks.windows;
package com.danitheskunk.skunkworks.os.windows;
import com.sun.jna.Structure;

View File

@ -1,4 +1,4 @@
package com.danitheskunk.skunkworks.windows;
package com.danitheskunk.skunkworks.os.windows;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef;

View File

@ -1,4 +1,4 @@
package com.danitheskunk.skunkworks.windows;
package com.danitheskunk.skunkworks.os.windows;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;

View File

@ -1,4 +1,4 @@
package com.danitheskunk.skunkworks.windows;
package com.danitheskunk.skunkworks.os.windows;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinUser;

View File

@ -1,4 +1,4 @@
package com.danitheskunk.skunkworks.windows;
package com.danitheskunk.skunkworks.os.windows;
import com.danitheskunk.skunkworks.Recti;