diff --git a/res/playerBlue.png b/res/playerBlue.png new file mode 100644 index 0000000..c6462cc Binary files /dev/null and b/res/playerBlue.png differ diff --git a/res/SmoothFloor.png b/res/smoothFloor.png similarity index 100% rename from res/SmoothFloor.png rename to res/smoothFloor.png diff --git a/shaders/player.frag b/shaders/player.frag new file mode 100644 index 0000000..dd1ccb7 --- /dev/null +++ b/shaders/player.frag @@ -0,0 +1,17 @@ +#version 330 core + +layout (location = 0) out vec4 color; + +in DATA +{ + vec2 tc; +} fs_in; + +uniform sampler2D tex; + +void main() +{ + color = texture(tex, fs_in.tc); + if (color.w < 1.0) + discard; +} \ No newline at end of file diff --git a/shaders/player.vert b/shaders/player.vert new file mode 100644 index 0000000..0c337bf --- /dev/null +++ b/shaders/player.vert @@ -0,0 +1,19 @@ +#version 330 core + +layout (location = 0) in vec4 position; +layout (location = 1) in vec2 tc; + +uniform mat4 pr_matrix; +uniform mat4 vw_matrix = mat4(1.0); +uniform mat4 ml_matrix = mat4(1.0); + +out DATA +{ + vec2 tc; +} vs_out; + +void main() +{ + gl_Position = pr_matrix * vw_matrix * ml_matrix * position; + vs_out.tc = tc; +} \ No newline at end of file diff --git a/shaders/SmoothFloor.frag b/shaders/smoothFloor.frag similarity index 100% rename from shaders/SmoothFloor.frag rename to shaders/smoothFloor.frag diff --git a/shaders/SmoothFloor.vert b/shaders/smoothFloor.vert similarity index 96% rename from shaders/SmoothFloor.vert rename to shaders/smoothFloor.vert index 6ca2a70..c651c30 100644 --- a/shaders/SmoothFloor.vert +++ b/shaders/smoothFloor.vert @@ -10,7 +10,7 @@ out DATA { vec2 tc; vec3 position; -} +} vs_out; void main() { diff --git a/src/game/Main.java b/src/game/Main.java index 7f5f451..c79e01b 100644 --- a/src/game/Main.java +++ b/src/game/Main.java @@ -7,6 +7,7 @@ import static org.lwjgl.system.MemoryUtil.*; import org.lwjgl.glfw.GLFWVidMode; import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.GL11; import game.graphics.Shader; import game.input.Input; @@ -56,6 +57,8 @@ public class Main implements Runnable { glEnable(GL_DEPTH_TEST); glActiveTexture(GL_TEXTURE1); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); System.out.println("Loaded OpenGL Version: " + glGetString(GL_VERSION)); Shader.loadAll(); @@ -64,6 +67,8 @@ public class Main implements Runnable { Shader.SMOOTH_FLOOR.setUniformMat4f("pr_matrix", projectionMatrix); Shader.SMOOTH_FLOOR.setUniform1i("tex", 1); + Shader.PLAYER.setUniformMat4f("pr_matrix", projectionMatrix); + Shader.PLAYER.setUniform1i("tex", 1); level = new Level(); } @@ -78,6 +83,8 @@ public class Main implements Runnable { int currentFrames = 0; while (running) { + GL11.glClearColor(.753f, .753f, .753f, .753f); + // Frames/Updates per second counter and limiter long currentTime = System.nanoTime(); deltaT += (currentTime - prevTime) / nanoSecConversion; diff --git a/src/game/entity/Player.java b/src/game/entity/Player.java new file mode 100644 index 0000000..e70becd --- /dev/null +++ b/src/game/entity/Player.java @@ -0,0 +1,70 @@ +package game.entity; + +import static org.lwjgl.glfw.GLFW.*; +import org.lwjgl.glfw.GLFW; + +import game.graphics.Shader; +import game.graphics.Texture; +import game.graphics.VertexArray; +import game.input.Input; +import game.math.Matrix4f; +import game.math.Vector3f; + +public class Player { + + private float SIZE = 1.0f; + private VertexArray mesh; + private Texture texture; + + private Vector3f position = new Vector3f(); + + public Player() { + float[] vertices = new float[] { + -SIZE / 2.0f, -SIZE / 2.0f, 0.2f, + -SIZE / 2.0f, SIZE / 2.0f, 0.2f, + SIZE / 2.0f, SIZE / 2.0f, 0.2f, + SIZE / 2.0f, -SIZE / 2.0f, 0.2f, + }; + + byte[] indices = new byte[] { + 0, 1, 2, + 2, 3, 0 + }; + + float[] tcs = new float[] { + 0, 1, + 0, 0, + 1, 0, + 1, 1 + }; + + mesh = new VertexArray(vertices, indices, tcs); + // TODO: More color options? + texture = new Texture("res/playerBlue.png"); + } + + public void update() { + if (Input.keys[GLFW.GLFW_KEY_UP]) + position.y += 0.1f; + if (Input.keys[GLFW.GLFW_KEY_DOWN]) + position.y -= 0.1f; + if (Input.keys[GLFW.GLFW_KEY_LEFT]) + position.x -= 0.1f; + if (Input.keys[GLFW.GLFW_KEY_RIGHT]) + position.x += 0.1f; + + System.out.println("At (" + position.x + ", " + position.y + ")"); + } + + public void render() { + Shader.PLAYER.enable(); + Shader.PLAYER.setUniformMat4f("ml_matrix", Matrix4f.translate(position)); + texture.bind(); + mesh.render(); + Shader.PLAYER.disable(); + } + + public float getY() { + return position.y; + } +} diff --git a/src/game/entity/PlayerBall.java b/src/game/entity/PlayerBall.java deleted file mode 100644 index 7021951..0000000 --- a/src/game/entity/PlayerBall.java +++ /dev/null @@ -1,5 +0,0 @@ -package game.entity; - -public class PlayerBall { - -} diff --git a/src/game/graphics/Shader.java b/src/game/graphics/Shader.java index dccf658..11d1fed 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 SMOOTH_FLOOR; + public static Shader SMOOTH_FLOOR, PLAYER; private boolean enabled = false; @@ -27,8 +27,8 @@ public class Shader { } public static void loadAll() { - SMOOTH_FLOOR = new Shader("shaders/SmoothFloor.vert", - "shaders/SmoothFloor.frag"); + SMOOTH_FLOOR = new Shader("shaders/smoothFloor.vert", "shaders/smoothFloor.frag"); + PLAYER = new Shader("shaders/player.vert", "shaders/player.frag"); } public int getUniform(String name) { diff --git a/src/game/graphics/VertexArray.java b/src/game/graphics/VertexArray.java index 7764140..fff1afb 100644 --- a/src/game/graphics/VertexArray.java +++ b/src/game/graphics/VertexArray.java @@ -9,35 +9,35 @@ import game.utils.BufferUtils; public class VertexArray { - private int vertexArrayObj, vertexBufferObj, indexBufferObj, textureBufferObj; + private int vao, vbo, ibo, tbo; private int numVertices; public VertexArray(int numVertices) { this.numVertices = numVertices; - vertexArrayObj = glGenVertexArrays(); + vao = glGenVertexArrays(); } public VertexArray(float[] vertices, byte[] indices, float[] textureCoords) { numVertices = indices.length; - vertexArrayObj = glGenVertexArrays(); - glBindVertexArray(vertexArrayObj); + vao = glGenVertexArrays(); + glBindVertexArray(vao); // Note: pointerOffset parameters must be zero - vertexBufferObj = glGenBuffers(); - glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObj); + vbo = glGenBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(vertices), GL_STATIC_DRAW); glVertexAttribPointer(Shader.VERTEX_ATTRIB, 3, GL_FLOAT, false, 0, 0); glEnableVertexAttribArray(Shader.VERTEX_ATTRIB); - textureBufferObj = glGenBuffers(); - glBindBuffer(GL_ARRAY_BUFFER, textureBufferObj); + tbo = glGenBuffers(); + glBindBuffer(GL_ARRAY_BUFFER, tbo); glBufferData(GL_ARRAY_BUFFER, BufferUtils.createFloatBuffer(textureCoords), GL_STATIC_DRAW); - glVertexAttribPointer(Shader.VERTEX_ATTRIB, 2, GL_FLOAT, false, 0, 0); + glVertexAttribPointer(Shader.TCOORD_ATTRIB, 2, GL_FLOAT, false, 0, 0); glEnableVertexAttribArray(Shader.TCOORD_ATTRIB); - indexBufferObj = glGenBuffers(); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObj); + ibo = glGenBuffers(); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, BufferUtils.createByteBuffer(indices), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -46,21 +46,21 @@ public class VertexArray { } public void bind() { - glBindVertexArray(vertexArrayObj); - if (indexBufferObj > 0) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObj); + glBindVertexArray(vao); + if (ibo > 0) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); } } public void unbind() { - if (indexBufferObj > 0) { + if (ibo > 0) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } glBindVertexArray(0); } public void draw() { - if (indexBufferObj > 0) { + if (ibo > 0) { glDrawElements(GL_TRIANGLES, numVertices, GL_UNSIGNED_BYTE, 0); } else { glDrawArrays(GL_TRIANGLES, 0, numVertices); diff --git a/src/game/level/Level.java b/src/game/level/Level.java index 134a7c7..232d7a3 100644 --- a/src/game/level/Level.java +++ b/src/game/level/Level.java @@ -1,17 +1,15 @@ package game.level; +import game.entity.Player; import game.graphics.Shader; import game.graphics.Texture; import game.graphics.VertexArray; public class Level { + // number of tiles +// private int width, height; - private int width, height; -// private VertexArray[][] tiles; -// private Texture[][] tileTextures; - - private VertexArray tile; - private Texture tileTexture; + private Player player; public Level() { float[] vertices = new float[] { @@ -33,26 +31,23 @@ public class Level { 1, 1 }; -// width = 20; -// height = 20; -// tiles = new VertexArray[width][height]; -// tileTextures = new Texture[width][height]; + player = new Player(); - tile = new VertexArray(vertices, indices, tcs); - tileTexture = new Texture("res/SmoothFloor.png"); } public void update() { - // player update // tile update + // player update + // enemy update + + player.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(); + // render tiles + // render player + // render other entities + + player.render(); } } diff --git a/src/game/level/tiles/Tile.java b/src/game/level/tiles/Tile.java index 142f9bc..6c54f7d 100644 --- a/src/game/level/tiles/Tile.java +++ b/src/game/level/tiles/Tile.java @@ -10,7 +10,7 @@ public abstract class Tile { protected VertexArray vertexArray; protected float[] vertices; protected byte[] indices; - protected float[] textureCoords; + protected float[] tcs; protected Tile() { @@ -26,7 +26,7 @@ public abstract class Tile { 2, 3, 0 }; - textureCoords = new float[] { + tcs = new float[] { 0, 1, 0, 0, 1, 0,