Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LWJGL Working.... #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions shaders/bg.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}



7 changes: 4 additions & 3 deletions shaders/bg.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 2 additions & 3 deletions shaders/bird.frag
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ uniform sampler2D tex;
void main()
{
color = texture(tex, fs_in.tc);
if (color.w < 1.0)
discard;
}



4 changes: 1 addition & 3 deletions shaders/bird.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,5 +15,4 @@ void main()
{
gl_Position = pr_matrix * vw_matrix * ml_matrix * position;
vs_out.tc = tc;
}

}
10 changes: 2 additions & 8 deletions src/com/flappybird/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,18 @@ 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);
Shader.BG.setUniform1i("tex", 1);

Shader.BIRD.setUniformMat4f("pr_matrix", pr_matrix);
Shader.BIRD.setUniform1i("tex", 1);


level = new Level();


Expand Down Expand Up @@ -115,14 +112,11 @@ public void run() {
frames = 0;
}

if (glfwWindowShouldClose(window) == true) {
if (glfwWindowShouldClose(window)) {
running = false;
}

}

//glfwDestroyWindow(window);
//glfwTerminate();
}

private void update() {
Expand Down
8 changes: 6 additions & 2 deletions src/com/flappybird/graphics/Shader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
160 changes: 114 additions & 46 deletions src/com/flappybird/level/Level.java
Original file line number Diff line number Diff line change
@@ -1,99 +1,167 @@
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;
private int map = 0;

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[] {
0, 1,
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();
}


}




























}
Loading