From 5d348e1b9a22d5b292f92553d0f60968bcb67bc2 Mon Sep 17 00:00:00 2001 From: Billy Gallagher Date: Thu, 7 Apr 2016 15:01:47 -0400 Subject: [PATCH] More rendering progress. Added a (temporary?) basic smooth floor texture --- res/SmoothFloor.png | Bin 0 -> 241 bytes res/levels/test.txt | 7 ---- shaders/SmoothFloor.frag | 17 +++++++++ shaders/SmoothFloor.vert | 20 +++++++++++ src/game/Main.java | 12 +++++++ src/game/graphics/Shader.java | 10 ++++-- src/game/level/Level.java | 48 ++++++++++++++++++++++++-- src/game/level/tiles/SmoothFloor.java | 26 ++++++++++++++ src/game/level/tiles/Tile.java | 42 ++++++++++++++++++++++ 9 files changed, 170 insertions(+), 12 deletions(-) create mode 100644 res/SmoothFloor.png delete mode 100644 res/levels/test.txt create mode 100644 shaders/SmoothFloor.frag create mode 100644 shaders/SmoothFloor.vert create mode 100644 src/game/level/tiles/SmoothFloor.java create mode 100644 src/game/level/tiles/Tile.java diff --git a/res/SmoothFloor.png b/res/SmoothFloor.png new file mode 100644 index 0000000000000000000000000000000000000000..b07509e8e2a2eed4f4b5cd01430a718cc7fec7fe GIT binary patch literal 241 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1SD0tpLGH$&H|6fVg?3oVGw3ym^DWND9B#o z>Fdh=fQ3y+N7AGDj~`G-vcxr_#5q4VH#M(>!MP|ku_QG`p**uBL&4qCHz2%`PaLQy z+tbA{B;(%O8yh(t6nPvRr|u6dJGJfR$w&DL9W3um%Nmu#X3n&<+V1vw-~Y}I>C3BM zef#iD8mJKiSY%kOw?9{TR~+P2G>|dl^RL%tw|@#qlt$HPfFwO#{an^LB{Ts5*)&3b literal 0 HcmV?d00001 diff --git a/res/levels/test.txt b/res/levels/test.txt deleted file mode 100644 index 0d9df1a..0000000 --- a/res/levels/test.txt +++ /dev/null @@ -1,7 +0,0 @@ -test -1 1 1 1 1 1 -1 1 1 1 1 1 -1 1 1 1 1 1 -1 1 1 1 1 1 -1 1 1 1 1 1 -1 1 1 1 1 1 \ No newline at end of file diff --git a/shaders/SmoothFloor.frag b/shaders/SmoothFloor.frag new file mode 100644 index 0000000..6001a40 --- /dev/null +++ b/shaders/SmoothFloor.frag @@ -0,0 +1,17 @@ +#version 330 core + +layout (location = 0) out vec4 color; + +in DATA +{ + vec2 tc; + vec3 position; +} fs_in; + +uniform vec2 player; +uniform sampler2D tex; + +void main() +{ + color = texture(tex, fs_in.tc); +} \ No newline at end of file diff --git a/shaders/SmoothFloor.vert b/shaders/SmoothFloor.vert new file mode 100644 index 0000000..6ca2a70 --- /dev/null +++ b/shaders/SmoothFloor.vert @@ -0,0 +1,20 @@ +#version 330 core + +layout (location = 0) in vec4 position; +layout (location = 1) in vec2 tc; + +uniform mat4 pr_matrix; +uniform mat4 vw_matrix; + +out DATA +{ + vec2 tc; + vec3 position; +} + +void main() +{ + gl_Position = pr_matrix * vw_matrix * position; + vs_out.tc = tc; + vs_out.position = vec3(vw_matrix * position); +} \ No newline at end of file diff --git a/src/game/Main.java b/src/game/Main.java index 35138fd..7f5f451 100644 --- a/src/game/Main.java +++ b/src/game/Main.java @@ -32,11 +32,21 @@ public class Main implements Runnable { } private void init() { + if (glfwInit() == GL_FALSE) { + System.out.println("glfwInit = False"); + return; + } + glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // Windowed mode, don't share resources gameWindow = glfwCreateWindow(width, height, "Game Noodle", NULL, NULL); + if (gameWindow == NULL) { + System.out.println("Game Window Null"); + return; + } + GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); glfwSetWindowPos(gameWindow, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2); glfwSetKeyCallback(gameWindow, input); @@ -51,6 +61,8 @@ public class Main implements Runnable { // Set all shader uniform variables here Matrix4f projectionMatrix = Matrix4f.orthographic(-10.0f, 10.0f, -10.0f * 9.0f / 16.0f, 10.0f * 9.0f / 16.0f, -1.0f, 1.0f); + Shader.SMOOTH_FLOOR.setUniformMat4f("pr_matrix", projectionMatrix); + Shader.SMOOTH_FLOOR.setUniform1i("tex", 1); level = new Level(); } diff --git a/src/game/graphics/Shader.java b/src/game/graphics/Shader.java index 3275424..dccf658 100644 --- a/src/game/graphics/Shader.java +++ b/src/game/graphics/Shader.java @@ -15,7 +15,7 @@ public class Shader { public static final int VERTEX_ATTRIB = 0; public static final int TCOORD_ATTRIB = 1; -// public static Shader SHADERHERE; + public static Shader SMOOTH_FLOOR; private boolean enabled = false; @@ -27,7 +27,8 @@ public class Shader { } public static void loadAll() { -// SHADERHERE = new Shader(vertPathStr, fragPathStr); + SMOOTH_FLOOR = new Shader("shaders/SmoothFloor.vert", + "shaders/SmoothFloor.frag"); } public int getUniform(String name) { @@ -82,4 +83,9 @@ public class Shader { glUseProgram(ID); enabled = true; } + + public void disable() { + glUseProgram(0); + enabled = false; + } } diff --git a/src/game/level/Level.java b/src/game/level/Level.java index 0e06e68..134a7c7 100644 --- a/src/game/level/Level.java +++ b/src/game/level/Level.java @@ -1,16 +1,58 @@ package game.level; -public class Level { +import game.graphics.Shader; +import game.graphics.Texture; +import game.graphics.VertexArray; +public class Level { + + private int width, height; +// private VertexArray[][] tiles; +// private Texture[][] tileTextures; + + private VertexArray tile; + private Texture tileTexture; + public Level() { + float[] vertices = new float[] { + -10.0f, -10.0f * 9.0f / 16.0f, 0.0f, + -10.0f, 10.0f * 9.0f / 16.0f, 0.0f, + 0.0f, 10.0f * 9.0f / 16.0f, 0.0f, + 0.0f, -10.0f * 9.0f / 16.0f, 0.0f, + }; + byte[] indices = new byte[] { + 0, 1, 2, + 2, 3, 0 + }; + + float[] tcs = new float[] { + 0, 1, + 0, 0, + 1, 0, + 1, 1 + }; + +// width = 20; +// height = 20; +// tiles = new VertexArray[width][height]; +// tileTextures = new Texture[width][height]; + + tile = new VertexArray(vertices, indices, tcs); + tileTexture = new Texture("res/SmoothFloor.png"); } public void update() { - + // player update + // tile update } public void render() { - + tileTexture.bind(); + Shader.SMOOTH_FLOOR.enable(); + Shader.SMOOTH_FLOOR.setUniform2f("player", 0, 0); + tile.bind(); + Shader.SMOOTH_FLOOR.disable(); + tileTexture.unbind(); } } diff --git a/src/game/level/tiles/SmoothFloor.java b/src/game/level/tiles/SmoothFloor.java new file mode 100644 index 0000000..a00fb01 --- /dev/null +++ b/src/game/level/tiles/SmoothFloor.java @@ -0,0 +1,26 @@ +package game.level.tiles; + +import game.graphics.Texture; +import game.graphics.VertexArray; + +public class SmoothFloor extends Tile{ + + public SmoothFloor() { + super(); + texture = new Texture("/res/SmoothFloor.png"); + } + + @Override + public void render() { + // TODO Auto-generated method stub + texture.bind(); + + } + + @Override + public void update() { + // TODO Auto-generated method stub + + } + +} diff --git a/src/game/level/tiles/Tile.java b/src/game/level/tiles/Tile.java new file mode 100644 index 0000000..142f9bc --- /dev/null +++ b/src/game/level/tiles/Tile.java @@ -0,0 +1,42 @@ +package game.level.tiles; + +import game.graphics.Texture; +import game.graphics.VertexArray; + +public abstract class Tile { + + protected final float SIZE = 1.0f; + protected Texture texture; + protected VertexArray vertexArray; + protected float[] vertices; + protected byte[] indices; + protected float[] textureCoords; + + + protected Tile() { + vertices = new float[] { + -SIZE / 2.0f, -SIZE / 2.0f, 0.0f, + -SIZE / 2.0f, SIZE / 2.0f, 0.0f, + SIZE / 2.0f, SIZE / 2.0f, 0.0f, + SIZE / 2.0f, -SIZE / 2.0f, 0.0f, + }; + + indices = new byte[] { + 0, 1, 2, + 2, 3, 0 + }; + + textureCoords = new float[] { + 0, 1, + 0, 0, + 1, 0, + 1, 1 + }; + + } + + public abstract void update(); + + public abstract void render(); + +}