Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Basic rendering and WASD controls complete! Need to finish tile rende…
…ring.
  • Loading branch information
wjg12004 committed Apr 14, 2016
1 parent ce3f79f commit 3720964
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 46 deletions.
Binary file added res/playerBlue.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
17 changes: 17 additions & 0 deletions 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;
}
19 changes: 19 additions & 0 deletions 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;
}
File renamed without changes.
2 changes: 1 addition & 1 deletion shaders/SmoothFloor.vert → shaders/smoothFloor.vert
Expand Up @@ -10,7 +10,7 @@ out DATA
{
vec2 tc;
vec3 position;
}
} vs_out;

void main()
{
Expand Down
7 changes: 7 additions & 0 deletions src/game/Main.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -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();
}

Expand All @@ -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;
Expand Down
70 changes: 70 additions & 0 deletions 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;
}
}
5 changes: 0 additions & 5 deletions src/game/entity/PlayerBall.java

This file was deleted.

6 changes: 3 additions & 3 deletions src/game/graphics/Shader.java
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
32 changes: 16 additions & 16 deletions src/game/graphics/VertexArray.java
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
33 changes: 14 additions & 19 deletions 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[] {
Expand All @@ -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();
}
}
4 changes: 2 additions & 2 deletions src/game/level/tiles/Tile.java
Expand Up @@ -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() {
Expand All @@ -26,7 +26,7 @@ public abstract class Tile {
2, 3, 0
};

textureCoords = new float[] {
tcs = new float[] {
0, 1,
0, 0,
1, 0,
Expand Down

0 comments on commit 3720964

Please sign in to comment.