depth testing & poorly rendered trees

pull/1/head
Valerie 2022-05-20 04:26:12 -04:00
parent a35182e368
commit 42a234c810
13 changed files with 128 additions and 46 deletions

View File

@ -7,8 +7,8 @@ varying vec2 vTexCoord;
void main() { void main() {
vec4 texColor = texture2D(u_texture, vTexCoord); vec4 texColor = texture2D(u_texture, vTexCoord);
if(texColor == vec4(1, 0, 1, 1) || texColor == vec4(1, 0, 0, 1)) { if(texColor == vec4(1, 0, 1, 1) || texColor == vec4(1, 0, 0, 1) || texColor.w == 0) {
gl_FragColor = vec4(0, 0, 0, 0); discard;
} else { } else {
gl_FragColor = texColor * vColor; gl_FragColor = texColor * vColor;
} }

View File

@ -3,7 +3,7 @@ uniform mat4 uProjection;
uniform vec4 uColor; uniform vec4 uColor;
//"in" attributes from our SpriteBatch //"in" attributes from our SpriteBatch
attribute vec2 Position; attribute vec3 Position;
attribute vec2 TexCoord; attribute vec2 TexCoord;
//"out" varyings to our fragment shader //"out" varyings to our fragment shader
@ -13,5 +13,5 @@ varying vec2 vTexCoord;
void main() { void main() {
vColor = uColor; vColor = uColor;
vTexCoord = TexCoord; vTexCoord = TexCoord;
gl_Position = uProjection * vec4(Position, 0.0, 1.0); gl_Position = uProjection * vec4(Position, 1.0);
} }

View File

@ -20,7 +20,7 @@ public class App {
// The window handle // The window handle
private long window; private long window;
private int width = 1024, height = 576; private int width = 1024, height = 576;
private Matrix4f matrix = Matrix4f.orthographic(0, width, height, 0, 1, -1); private Matrix4f matrix = Matrix4f.orthographic(0, width, height, 0, 0, 100);
public static int mouseX, mouseY; public static int mouseX, mouseY;
public static boolean mouseLeft, mouseMiddle, mouseRight; public static boolean mouseLeft, mouseMiddle, mouseRight;
@ -126,6 +126,9 @@ public class App {
glClearColor(0.3f, 0.3f, 0.3f, 1.0f); glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(true);
game.start(); game.start();
} }

View File

@ -18,7 +18,11 @@ public class Drawing {
Drawing.layer = layer; Drawing.layer = layer;
} }
public static void drawSprite(Sprite sprite, int x, int y, int width, int height) { public static void drawSprite(Sprite sprite, int x, int y, int w, int h) {
drawSprite(sprite, x, y, w, h, 0);
}
public static void drawSprite(Sprite sprite, int x, int y, int width, int height, float lift) {
// lazy texture binding // lazy texture binding
if(bound != sprite.atlas) { if(bound != sprite.atlas) {
if(bound != null) bound.unbind(); if(bound != null) bound.unbind();
@ -27,10 +31,10 @@ public class Drawing {
glBegin(GL_QUADS); glBegin(GL_QUADS);
glVertexAttrib2f(SimpleShader.TEX_COORD, sprite.sourceBoxUV.x, sprite.sourceBoxUV.y); glVertexAttrib2f(SimpleShader.TEX_COORD, sprite.sourceBoxUV.x, sprite.sourceBoxUV.y);
glVertex3f(x, y, layer); glVertex3f(x, y, layer + height * lift);
glVertexAttrib2f(SimpleShader.TEX_COORD, sprite.sourceBoxUV.x + sprite.sourceBoxUV.z, sprite.sourceBoxUV.y); glVertexAttrib2f(SimpleShader.TEX_COORD, sprite.sourceBoxUV.x + sprite.sourceBoxUV.z, sprite.sourceBoxUV.y);
glVertex3f(x + width, y, layer); glVertex3f(x + width, y, layer + height * lift);
glVertexAttrib2f(SimpleShader.TEX_COORD, sprite.sourceBoxUV.x + sprite.sourceBoxUV.z, sprite.sourceBoxUV.y + sprite.sourceBoxUV.w); glVertexAttrib2f(SimpleShader.TEX_COORD, sprite.sourceBoxUV.x + sprite.sourceBoxUV.z, sprite.sourceBoxUV.y + sprite.sourceBoxUV.w);
glVertex3f(x + width, y + height, layer); glVertex3f(x + width, y + height, layer);

View File

@ -3,9 +3,9 @@ package xyz.valnet.engine.scenegraph;
import xyz.valnet.hadean.scenes.GameScene; import xyz.valnet.hadean.scenes.GameScene;
public class GameObject implements IRenderable, ITickable { public class GameObject implements IRenderable, ITickable {
private final GameScene scene; private GameScene scene;
public GameObject(GameScene scene) { public void link(GameScene scene) {
this.scene = scene; this.scene = scene;
} }
@ -13,6 +13,10 @@ public class GameObject implements IRenderable, ITickable {
return this.scene.get(clazz); return this.scene.get(clazz);
} }
protected final void add(GameObject obj) {
scene.add(obj);
}
@Override @Override
public void render() {} public void render() {}

View File

@ -1,35 +1,55 @@
package xyz.valnet.hadean; package xyz.valnet.hadean;
import java.util.ArrayList;
import java.util.List;
import xyz.valnet.engine.graphics.Drawing;
import xyz.valnet.engine.graphics.Sprite; import xyz.valnet.engine.graphics.Sprite;
import xyz.valnet.engine.math.Vector4f; import xyz.valnet.engine.math.Vector4f;
import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.hadean.gameobjects.Camera; import xyz.valnet.hadean.gameobjects.Camera;
import xyz.valnet.hadean.gameobjects.ITileThing;
import xyz.valnet.hadean.gameobjects.Tree;
import xyz.valnet.hadean.util.Assets; import xyz.valnet.hadean.util.Assets;
// TODO make these tiles REAL gameobjects... // TODO make these tiles REAL gameobjects...
public class Tile { public class Tile extends GameObject {
private Camera camera;
private final int x, y; private final int x, y;
private final Vector4f color = new Vector4f((float) Math.random() * 0.1f, 0.4f + (float) Math.random() * 0.15f, (float) Math.random() * 0.05f, 1f); private final Vector4f color = new Vector4f((float) Math.random() * 0.1f, 0.4f + (float) Math.random() * 0.15f, (float) Math.random() * 0.05f, 1f);
private final Sprite sprite = Assets.defaultTerrain[(int)Math.floor(Math.random() * Assets.defaultTerrain.length)]; private final Sprite sprite = Assets.defaultTerrain[(int)Math.floor(Math.random() * Assets.defaultTerrain.length)];
private boolean obstacle;
private List<ITileThing> stuff = new ArrayList<ITileThing>();
public Tile(int x, int y) { public Tile(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
this.obstacle = false;
// this.obstacle = Math.random() > 0.8f;
} }
public void render(Camera camera) { public void start() {
Assets.flat.pushColor(isWalkable() ? color : new Vector4f(0.1f, 0.1f, 0.1f, 1f)); camera = get(Camera.class);
if(Math.random() > 0.98) {
Tree tree = new Tree(x, y);
stuff.add(tree);
add(tree);
}
}
@Override
public void render() {
Drawing.setLayer(2f);
Assets.flat.pushColor(color);
camera.draw(sprite, x, y); camera.draw(sprite, x, y);
Assets.flat.popColor(); Assets.flat.popColor();
} }
public void wall() {
obstacle = true;
}
public boolean isWalkable() { public boolean isWalkable() {
return !obstacle; for(ITileThing thing : stuff) {
if(!thing.isWalkable()) return false;
}
return true;
} }
} }

View File

@ -4,7 +4,6 @@ import xyz.valnet.engine.graphics.Drawing;
import xyz.valnet.engine.graphics.Sprite; import xyz.valnet.engine.graphics.Sprite;
import xyz.valnet.engine.math.Vector2f; import xyz.valnet.engine.math.Vector2f;
import xyz.valnet.engine.scenegraph.GameObject; import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.hadean.scenes.GameScene;
public class Camera extends GameObject { public class Camera extends GameObject {
@ -14,10 +13,6 @@ public class Camera extends GameObject {
private Vector2f focus = new Vector2f(0, 0); private Vector2f focus = new Vector2f(0, 0);
public Camera(GameScene scene) {
super(scene);
}
public void focus(float x, float y) { public void focus(float x, float y) {
this.focus.x = x; this.focus.x = x;
this.focus.y = y; this.focus.y = y;
@ -32,4 +27,9 @@ public class Camera extends GameObject {
Drawing.drawSprite(sprite, (int)(screenPos.x), (int)(screenPos.y), tileWidth, tileWidth); Drawing.drawSprite(sprite, (int)(screenPos.x), (int)(screenPos.y), tileWidth, tileWidth);
} }
public void draw(Sprite sprite, float x, float y, float w, float h) {
Vector2f screenPos = world2screen(x, y);
Drawing.drawSprite(sprite, (int)(screenPos.x), (int)(screenPos.y), (int)(tileWidth * w), (int)(tileWidth * h));
}
} }

View File

@ -0,0 +1,5 @@
package xyz.valnet.hadean.gameobjects;
public interface ITileThing {
public boolean isWalkable();
}

View File

@ -32,10 +32,6 @@ public class Pawn extends GameObject {
private Terrain terrain; private Terrain terrain;
private IPathfinder pathfinder; private IPathfinder pathfinder;
public Pawn(GameScene scene) {
super(scene);
}
@Override @Override
public void start() { public void start() {
camera = get(Camera.class); camera = get(Camera.class);
@ -46,7 +42,7 @@ public class Pawn extends GameObject {
@Override @Override
public void render() { public void render() {
Drawing.setLayer(0.5f); Drawing.setLayer(3f);
if(path != null && !path.isComplete()) { if(path != null && !path.isComplete()) {
Node next = path.peek(); Node next = path.peek();
@ -128,8 +124,6 @@ public class Pawn extends GameObject {
x = nextNode.x + 0.5f; x = nextNode.x + 0.5f;
y = nextNode.y + 0.5f; y = nextNode.y + 0.5f;
counter = 0; counter = 0;
nextTile.wall();
} }
} }

View File

@ -18,14 +18,11 @@ public class Terrain extends GameObject implements IPathable {
private Camera camera; private Camera camera;
public Terrain(GameScene scene) {
super(scene);
}
public void start() { public void start() {
for (int i = 0; i < WORLD_SIZE; i++) { for (int i = 0; i < WORLD_SIZE; i++) {
for (int j = 0; j < WORLD_SIZE; j++) { for (int j = 0; j < WORLD_SIZE; j++) {
tiles[i][j] = new Tile(i, j); tiles[i][j] = new Tile(i, j);
add(tiles[i][j]);
} }
} }
camera = get(Camera.class); camera = get(Camera.class);
@ -42,12 +39,12 @@ public class Terrain extends GameObject implements IPathable {
// left = 400 - (WORLD_SIZE * TILE_SIZE / 2); // left = 400 - (WORLD_SIZE * TILE_SIZE / 2);
// top = 225 - (WORLD_SIZE * TILE_SIZE / 2); // top = 225 - (WORLD_SIZE * TILE_SIZE / 2);
Drawing.setLayer(0f); // Drawing.setLayer(0f);
for (int i = 0; i < WORLD_SIZE; i++) { // for (int i = 0; i < WORLD_SIZE; i++) {
for (int j = 0; j < WORLD_SIZE; j++) { // for (int j = 0; j < WORLD_SIZE; j++) {
tiles[i][j].render(camera); // tiles[i][j].render(camera);
} // }
} // }
} }
@Override @Override

View File

@ -0,0 +1,29 @@
package xyz.valnet.hadean.gameobjects;
import xyz.valnet.engine.scenegraph.GameObject;
import xyz.valnet.hadean.util.Assets;
public class Tree extends GameObject implements ITileThing {
private Camera camera;
private int x, y;
public Tree(int x, int y) {
this.x = x;
this.y = y;
}
public void start() {
camera = get(Camera.class);
}
@Override
public void render() {
camera.draw(Assets.tree, x - 1, y - 2, 3, 3);
}
@Override
public boolean isWalkable() {
return false;
}
}

View File

@ -13,6 +13,7 @@ public class GameScene implements IScene {
// generic // generic
private List<GameObject> objects = new ArrayList<GameObject>(); private List<GameObject> objects = new ArrayList<GameObject>();
private List<GameObject> newObjects = new ArrayList<GameObject>();
// private List<IRenderable> renderables = new ArrayList<IRenderable>(); // private List<IRenderable> renderables = new ArrayList<IRenderable>();
// specific // specific
@ -35,6 +36,20 @@ public class GameScene implements IScene {
@Override @Override
public void update(float dTime) { public void update(float dTime) {
if(!newObjects.isEmpty()) {
List<GameObject> added = new ArrayList<GameObject>();
for(GameObject obj : newObjects) {
objects.add(obj);
added.add(obj);
}
newObjects.clear();
for(GameObject obj : added) {
obj.start();
}
}
for(GameObject obj : objects) { for(GameObject obj : objects) {
obj.tick(dTime); obj.tick(dTime);
} }
@ -42,13 +57,17 @@ public class GameScene implements IScene {
@Override @Override
public void enable() { public void enable() {
objects.add(new Terrain(this)); objects.add(new Terrain());
for(int i = 0; i < 3; i ++) { for(int i = 0; i < 3; i ++) {
objects.add(new Pawn(this)); objects.add(new Pawn());
} }
Camera camera = new Camera(this); Camera camera = new Camera();
objects.add(camera); objects.add(camera);
for(GameObject obj : objects) {
obj.link(this);
}
for(GameObject obj : objects) { for(GameObject obj : objects) {
obj.start(); obj.start();
} }
@ -59,4 +78,9 @@ public class GameScene implements IScene {
objects.clear(); objects.clear();
} }
public void add(GameObject obj) {
newObjects.add(obj);
obj.link(this);
}
} }

View File

@ -20,6 +20,7 @@ public class Assets {
public static final Sprite[] defaultTerrain; public static final Sprite[] defaultTerrain;
public static final Sprite pawn; public static final Sprite pawn;
public static final Sprite tree;
public static final SimpleShader flat; public static final SimpleShader flat;
@ -36,6 +37,7 @@ public class Assets {
}; };
pawn = new Sprite(atlas, 48, 88, 8, 8); pawn = new Sprite(atlas, 48, 88, 8, 8);
tree = new Sprite(atlas, 64, 64, 24, 24);
Map<Character, Sprite> charset = new HashMap<Character, Sprite>(); Map<Character, Sprite> charset = new HashMap<Character, Sprite>();