From 7d2863ada65f9fc2707e58c76548bbe4dc1af955 Mon Sep 17 00:00:00 2001 From: midsage Date: Sat, 22 Sep 2018 02:26:57 -0700 Subject: [PATCH] Add files via upload --- shaders/bg.frag | 6 +- shaders/bg.vert | 7 +- shaders/bird.frag | 5 +- shaders/bird.vert | 4 +- src/com/flappybird/Main.java | 10 +- src/com/flappybird/graphics/Shader.java | 8 +- src/com/flappybird/level/Level.java | 160 +++++++++++++++------- src/com/flappybird/math/Matrix4f.java | 90 ++---------- src/com/flappybird/math/Vector3f.java | 4 +- src/com/flappybird/utils/BufferUtils.java | 40 +----- src/com/flappybird/utils/FileUtils.java | 34 +---- src/com/flappybird/utils/ShaderUtils.java | 68 +-------- 12 files changed, 159 insertions(+), 277 deletions(-) diff --git a/shaders/bg.frag b/shaders/bg.frag index 353df52..1e771f1 100644 --- a/shaders/bg.frag +++ b/shaders/bg.frag @@ -5,14 +5,14 @@ layout (location = 0) out vec4 color; in DATA { vec2 tc; + vec3 position; } fs_in; +uniform vec2 bird; uniform sampler2D tex; void main() { color = texture(tex, fs_in.tc); + color *= 2.0 / (length(bird - fs_in.position.xy) + 2.5) + 0.5; } - - - diff --git a/shaders/bg.vert b/shaders/bg.vert index 416be74..3bb285d 100644 --- a/shaders/bg.vert +++ b/shaders/bg.vert @@ -9,11 +9,12 @@ uniform mat4 vw_matrix; out DATA { vec2 tc; + vec3 position; } vs_out; void main() { - gl_Position = pr_matrix * vw_matrix * position; + 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/shaders/bird.frag b/shaders/bird.frag index 353df52..a7b072e 100644 --- a/shaders/bird.frag +++ b/shaders/bird.frag @@ -12,7 +12,6 @@ uniform sampler2D tex; void main() { color = texture(tex, fs_in.tc); + if (color.w < 1.0) + discard; } - - - diff --git a/shaders/bird.vert b/shaders/bird.vert index e99310d..8dfae85 100644 --- a/shaders/bird.vert +++ b/shaders/bird.vert @@ -6,7 +6,6 @@ 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; @@ -16,5 +15,4 @@ void main() { gl_Position = pr_matrix * vw_matrix * ml_matrix * position; vs_out.tc = tc; -} - +} \ No newline at end of file diff --git a/src/com/flappybird/Main.java b/src/com/flappybird/Main.java index b1f7750..698295f 100644 --- a/src/com/flappybird/Main.java +++ b/src/com/flappybird/Main.java @@ -66,12 +66,11 @@ private void init() { glfwShowWindow(window); GL.createCapabilities(); // Same as GLContext.CreateFromCurrent - //glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glEnable(GL_DEPTH_TEST); glActiveTexture(GL_TEXTURE1); System.out.println("OpenGL: " + glGetString(GL_VERSION)); Shader.loadAll(); - Matrix4f pr_matrix = Matrix4f.orthographic(-10.0f, 10.0f, -10.0f * 9.0f / 16.0f, 10.0f * 9.0f / 16.0f, -1.0f, 1.0f); Shader.BG.setUniformMat4f("pr_matrix", pr_matrix); @@ -79,8 +78,6 @@ private void init() { Shader.BIRD.setUniformMat4f("pr_matrix", pr_matrix); Shader.BIRD.setUniform1i("tex", 1); - - level = new Level(); @@ -115,14 +112,11 @@ public void run() { frames = 0; } - if (glfwWindowShouldClose(window) == true) { + if (glfwWindowShouldClose(window)) { running = false; } } - - //glfwDestroyWindow(window); - //glfwTerminate(); } private void update() { diff --git a/src/com/flappybird/graphics/Shader.java b/src/com/flappybird/graphics/Shader.java index 3ec861d..6f10db7 100644 --- a/src/com/flappybird/graphics/Shader.java +++ b/src/com/flappybird/graphics/Shader.java @@ -4,9 +4,12 @@ import com.flappybird.math.Vector3f; import com.flappybird.utils.ShaderUtils; import static org.lwjgl.opengl.GL20.*; +import static org.lwjgl.opengl.GL11.*; import java.util.HashMap; import java.util.Map; +import org.lwjgl.opengl.GL20; + public class Shader { public static final int VERTEX_ATTRIB = 0; @@ -66,8 +69,9 @@ public void setUniform3f(String name, Vector3f vector) { public void setUniformMat4f(String name, Matrix4f matrix) { if (!enabled) enable(); - //glUniformMatrix4fv(getUniform(name), false, matrix.toFloatBuffer()); - glUniformMatrix4(getUniform(name), false, matrix.toFloatBuffer()); + glUniformMatrix4fv(getUniform(name), false, matrix.toFloatBuffer()); + + } public void enable() { diff --git a/src/com/flappybird/level/Level.java b/src/com/flappybird/level/Level.java index 326ad2f..bf4f96f 100644 --- a/src/com/flappybird/level/Level.java +++ b/src/com/flappybird/level/Level.java @@ -1,14 +1,19 @@ package com.flappybird.level; +import java.util.Random; + import com.flappybird.graphics.Shader; import com.flappybird.graphics.Texture; import com.flappybird.graphics.VertexArray; +import com.flappybird.input.Input; import com.flappybird.math.Matrix4f; import com.flappybird.math.Vector3f; +import static org.lwjgl.glfw.GLFW.*; + public class Level { - - private VertexArray background; + + private VertexArray background, fade; private Texture bgTexture; private int xScroll = 0; @@ -16,18 +21,26 @@ public class Level { private Bird bird; + //private Pipe[] pipes = new Pipe[5 * 2]; + private int index = 0; + private float OFFSET = 5.0f; + private boolean control = true, reset = false; + + private Random random = new Random(); + + private float time = 0.0f; + 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 - + -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 + 0, 1, 2, + 2, 3, 0 }; float[] tcs = new float[] { @@ -35,65 +48,120 @@ public Level() { 0, 0, 1, 0, 1, 1 - }; background = new VertexArray(vertices, indices, tcs); bgTexture = new Texture("res/bg.jpeg"); - bird = new Bird(); + + //createPipes(); + } + + /* + private void createPipes() { + Pipe.create(); + for (int i = 0; i < 5 * 2; i += 2) { + pipes[i] = new Pipe(OFFSET + index * 3.0f, random.nextFloat() * 4.0f); + pipes[i + 1] = new Pipe(pipes[i].getX(), pipes[i].getY() - 11.5f); + index += 2; + } + } + + private void updatePipes() { + pipes[index % 10] = new Pipe(OFFSET + index * 3.0f, random.nextFloat() * 4.0f); + pipes[(index + 1) % 10] = new Pipe(pipes[index % 10].getX(), pipes[index % 10].getY() - 11.5f); + index += 2; } + */ public void update() { - xScroll--; - if(-xScroll % 355 == 0) map++; + if (control) { + xScroll--; + if (-xScroll % 335 == 0) map++; + if (-xScroll > 250 && -xScroll % 120 == 0); + //updatePipes(); + } bird.update(); + + //if (control && collision()) { + //bird.fall(); + // control = false; + //} + + //if (!control && Input.isKeyDown(GLFW_KEY_SPACE)) + // reset = true; + // + //time += 0.01f; + } + + /* + private void renderPipes() { + Shader.PIPE.enable(); + Shader.PIPE.setUniform2f("bird", 0, bird.getY()); + Shader.PIPE.setUniformMat4f("vw_matrix", Matrix4f.translate(new Vector3f(xScroll * 0.05f, 0.0f, 0.0f))); + Pipe.getTexture().bind(); + Pipe.getMesh().bind(); + + for (int i = 0; i < 5 * 2; i++) { + Shader.PIPE.setUniformMat4f("ml_matrix", pipes[i].getModelMatrix()); + Shader.PIPE.setUniform1i("top", i % 2 == 0 ? 1 : 0); + Pipe.getMesh().draw(); + } + Pipe.getMesh().unbind(); + Pipe.getTexture().unbind(); + } + + private boolean collision() { + for (int i = 0; i < 5 * 2; i++) { + float bx = -xScroll * 0.05f; + float by = bird.getY(); + float px = pipes[i].getX(); + float py = pipes[i].getY(); + + float bx0 = bx - bird.getSize() / 2.0f; + float bx1 = bx + bird.getSize() / 2.0f; + float by0 = by - bird.getSize() / 2.0f; + float by1 = by + bird.getSize() / 2.0f; + + float px0 = px; + float px1 = px + Pipe.getWidth(); + float py0 = py; + float py1 = py + Pipe.getHeight(); + + if (bx1 > px0 && bx0 < px1) { + if (by1 > py0 && by0 < py1) { + return true; + } + } + } + return false; + } + + public boolean isGameOver() { + return reset; } + */ public void render() { bgTexture.bind(); Shader.BG.enable(); + //Shader.BG.setUniform2f("bird", 0, bird.getY()); background.bind(); - for(int i = map; i < map; i++) { - Shader.BG.setUniformMat4f("vw_matrix", Matrix4f.translate(new Vector3f(i * 10 + xScroll * 0.030f, 0.0f, 0.0f))); + for (int i = map; i < map + 4; i++) { + Shader.BG.setUniformMat4f("vw_matrix", Matrix4f.translate(new Vector3f(i * 10 + xScroll * 0.03f, 0.0f, 0.0f))); background.draw(); } Shader.BG.disable(); bgTexture.unbind(); + //renderPipes(); bird.render(); - + //Shader.FADE.enable(); + //Shader.FADE.setUniform1f("time", time); + //fade.render(); + //Shader.FADE.disable(); } - -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - +} \ No newline at end of file diff --git a/src/com/flappybird/math/Matrix4f.java b/src/com/flappybird/math/Matrix4f.java index 2fc6d9a..765ede3 100644 --- a/src/com/flappybird/math/Matrix4f.java +++ b/src/com/flappybird/math/Matrix4f.java @@ -5,12 +5,10 @@ import com.flappybird.utils.BufferUtils; - public class Matrix4f { - - public final static int SIZE = 4 * 4; + + public static final int SIZE = 4 * 4; public float[] elements = new float[SIZE]; - private float sum; public Matrix4f() { @@ -18,8 +16,8 @@ public Matrix4f() { public static Matrix4f identity() { Matrix4f result = new Matrix4f(); - for(int i = 0; i < SIZE; i++) { - result.elements[i] = 0.0f; + for (int i = 0; i < SIZE; i++) { + result.elements[i] = 0.0f; } result.elements[0 + 0 * 4] = 1.0f; result.elements[1 + 1 * 4] = 1.0f; @@ -27,24 +25,22 @@ public static Matrix4f identity() { result.elements[3 + 3 * 4] = 1.0f; return result; - } public static Matrix4f orthographic(float left, float right, float bottom, float top, float near, float far) { Matrix4f result = identity(); result.elements[0 + 0 * 4] = 2.0f / (right - left); - + result.elements[1 + 1 * 4] = 2.0f / (top - bottom); - + result.elements[2 + 2 * 4] = 2.0f / (near - far); result.elements[0 + 3 * 4] = (left + right) / (left - right); result.elements[1 + 3 * 4] = (bottom + top) / (bottom - top); - result.elements[3 + 3 * 4] = (far + near) / (far - near); + result.elements[2 + 3 * 4] = (far + near) / (far - near); return result; - } public static Matrix4f translate(Vector3f vector) { @@ -52,9 +48,7 @@ public static Matrix4f translate(Vector3f vector) { result.elements[0 + 3 * 4] = vector.x; result.elements[1 + 3 * 4] = vector.y; result.elements[2 + 3 * 4] = vector.z; - return result; - } public static Matrix4f rotate(float angle) { @@ -70,19 +64,16 @@ public static Matrix4f rotate(float angle) { result.elements[1 + 1 * 4] = cos; return result; - } public Matrix4f multiply(Matrix4f matrix) { Matrix4f result = new Matrix4f(); - - for(int y = 0; y < 4; y++) { - for(int x = 0; x < 4; x++) { + for (int y = 0; y < 4; y++) { + for (int x = 0; x < 4; x++) { float sum = 0.0f; - for(int e = 0; e < 4; e++) { - sum += this.elements[x + e * 4] * matrix.elements[e + y * 4]; - - } + for (int e = 0; e < 4; e++) { + sum += this.elements[x + e * 4] * matrix.elements[e + y * 4]; + } result.elements[x + y * 4] = sum; } } @@ -93,59 +84,4 @@ public FloatBuffer toFloatBuffer() { return BufferUtils.createFloatBuffer(elements); } - - - - - - -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +} \ No newline at end of file diff --git a/src/com/flappybird/math/Vector3f.java b/src/com/flappybird/math/Vector3f.java index d961c2f..94afdc9 100644 --- a/src/com/flappybird/math/Vector3f.java +++ b/src/com/flappybird/math/Vector3f.java @@ -8,14 +8,12 @@ public Vector3f() { x = 0.0f; y = 0.0f; z = 0.0f; - } public Vector3f(float x, float y, float z) { this.x = x; this.y = y; this.z = z; - } -} +} \ No newline at end of file diff --git a/src/com/flappybird/utils/BufferUtils.java b/src/com/flappybird/utils/BufferUtils.java index b35a470..48e5962 100644 --- a/src/com/flappybird/utils/BufferUtils.java +++ b/src/com/flappybird/utils/BufferUtils.java @@ -1,22 +1,21 @@ package com.flappybird.utils; + import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; public class BufferUtils { - + private BufferUtils() { - } public static ByteBuffer createByteBuffer(byte[] array) { ByteBuffer result = ByteBuffer.allocateDirect(array.length).order(ByteOrder.nativeOrder()); result.put(array).flip(); return result; - } - + } public static FloatBuffer createFloatBuffer(float[] array) { FloatBuffer result = ByteBuffer.allocateDirect(array.length << 2).order(ByteOrder.nativeOrder()).asFloatBuffer(); @@ -24,41 +23,10 @@ public static FloatBuffer createFloatBuffer(float[] array) { return result; } - public static IntBuffer createIntBuffer(int[] array) { IntBuffer result = ByteBuffer.allocateDirect(array.length << 2).order(ByteOrder.nativeOrder()).asIntBuffer(); result.put(array).flip(); return result; } -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +} \ No newline at end of file diff --git a/src/com/flappybird/utils/FileUtils.java b/src/com/flappybird/utils/FileUtils.java index 0e27d57..e2f3fad 100644 --- a/src/com/flappybird/utils/FileUtils.java +++ b/src/com/flappybird/utils/FileUtils.java @@ -7,7 +7,6 @@ public class FileUtils { private FileUtils() { - } public static String loadAsString(String file) { @@ -15,41 +14,14 @@ public static String loadAsString(String file) { try { BufferedReader reader = new BufferedReader(new FileReader(file)); String buffer = ""; - while((buffer = reader.readLine()) != null) { + while ((buffer = reader.readLine()) != null) { result.append(buffer + '\n'); - } reader.close(); - } catch(IOException e) { + } catch (IOException e) { e.printStackTrace(); } return result.toString(); - - - - } -} - - - - - - - - - - - - - - - - - - - - - - +} \ No newline at end of file diff --git a/src/com/flappybird/utils/ShaderUtils.java b/src/com/flappybird/utils/ShaderUtils.java index 98ee608..34baeab 100644 --- a/src/com/flappybird/utils/ShaderUtils.java +++ b/src/com/flappybird/utils/ShaderUtils.java @@ -2,42 +2,37 @@ import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL20.*; -import static org.lwjgl.opengl.GL30.*; public class ShaderUtils { private ShaderUtils() { - } public static int load(String vertPath, String fragPath) { - String vert = FileUtils.loadAsString(vertPath); + String vert = FileUtils.loadAsString(vertPath); String frag = FileUtils.loadAsString(fragPath); - return create(vert,frag); - - + return create(vert, frag); } public static int create(String vert, String frag) { - int program = glCreateProgram(); + int program = glCreateProgram(); int vertID = glCreateShader(GL_VERTEX_SHADER); int fragID = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vertID, vert); glShaderSource(fragID, frag); glCompileShader(vertID); - if(glGetShaderi(vertID, GL_COMPILE_STATUS) == GL_FALSE) { + if (glGetShaderi(vertID, GL_COMPILE_STATUS) == GL_FALSE) { System.err.println("Failed to compile vertex shader!"); System.err.println(glGetShaderInfoLog(vertID)); return -1; } glCompileShader(fragID); - if(glGetShaderi(fragID, GL_COMPILE_STATUS) == GL_FALSE) { + if (glGetShaderi(fragID, GL_COMPILE_STATUS) == GL_FALSE) { System.err.println("Failed to compile fragment shader!"); System.err.println(glGetShaderInfoLog(fragID)); return -1; - } glAttachShader(program, vertID); @@ -48,58 +43,7 @@ public static int create(String vert, String frag) { glDeleteShader(vertID); glDeleteShader(fragID); - - return program; - - } - - -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +} \ No newline at end of file