Actually got something rendering

This commit is contained in:
josua 2020-06-13 17:26:40 +10:00
parent 7dbe75a497
commit 95bd4df7ef
40 changed files with 315 additions and 293 deletions

View File

@ -72,6 +72,10 @@ public class Main
Tiles.init();
LayerGenerators.init();
// Create the display
window = new DisplayWindow("Project Zombie");
window.init();
// Load the resources
Resources.loadResources();
@ -83,26 +87,21 @@ public class Main
AudioSources.init();
Sounds.init();
// Create the mainloop
mainloop = new MainloopManager(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);
mainloop.register(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);
mainloop.register(ChunkEventHandler.CHUNK_EVENT_HANDLER);
mainloop.register(DisplayStatsEventHandler.DISPLAY_STATS_EVENT_HANDLER);
mainloop.register(JoystickCallback.JOYSTICK_CALLBACK);
mainloop.register(new GameTimer());
mainloop.register(new KeyCallback());
mainloop.register(new NoSleep());
mainloop.register(new MainloopHelpers());
// Create the display
window = new DisplayWindow("Project Zombie");
window.init();
// Initialize the gamepad
JoystickCallback.JOYSTICK_CALLBACK.init();
// Initialise the entities
// Create the mainloop
mainloop = new MainloopManager(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);
mainloop.register(DisplayStatsEventHandler.DISPLAY_STATS_EVENT_HANDLER);
mainloop.register(MainloopEventHandler.MAINLOOP_EVENT_HANDLER);
mainloop.register(EntityEventHandler.ENTITY_EVENT_HANDLER);
mainloop.register(ChunkEventHandler.CHUNK_EVENT_HANDLER);
mainloop.register(JoystickCallback.JOYSTICK_CALLBACK);
mainloop.register(new MainloopHelpers());
mainloop.register(new KeyCallback());
mainloop.register(new GameTimer());
mainloop.register(new NoSleep());
mainloop.register(window);
// Start the mainloop
menu = new MenuMain();

View File

@ -21,8 +21,10 @@ import org.lwjgl.stb.STBVorbis;
import org.lwjgl.system.MemoryStack;
import projectzombie.Main;
import projectzombie.display.Camera;
import projectzombie.resources.Resource;
import gl_engine.MathHelpers;
import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec3d;
@ -30,6 +32,7 @@ public class AudioObject
{
int bufferPointer;
Resource resource;
boolean error;
int channels;
int sample_rate;
int output;
@ -38,6 +41,7 @@ public class AudioObject
{
// Store the argument values
this.resource = resource;
this.error = false;
}
public void init()
@ -51,6 +55,11 @@ public class AudioObject
ByteBuffer resource_buffer = resource.getByteBuffer();
ShortBuffer audio = STBVorbis.stb_vorbis_decode_memory(resource_buffer, channels_buf, sample_rate_buf);
if(audio == null) {
error = true;
return;
}
// Get the channels and the sample rate
channels = channels_buf.get();
sample_rate = sample_rate_buf.get();
@ -65,6 +74,7 @@ public class AudioObject
// Send the data to OpenAL
bufferPointer = alGenBuffers();
alBufferData(bufferPointer, format, audio, sample_rate);
// Free some c buffers
@ -74,28 +84,19 @@ public class AudioObject
public void play(Vec3d pos, double volume)
{
// Get the player pos
Vec2d p_pos = Main.player.pos;
// Convert the angle to radians
double angle_r = Math.toRadians(Main.player.angle + 90);
// Calculate the translation
double x = ( pos.x - p_pos.x );
double y = ( pos.y - p_pos.y );
double z = pos.z;
if(error) {
return;
}
// Calculate the rotation
Vec2d rotated = MathHelpers.rotate2(new Vec2d(x, y), -angle_r);
x = rotated.x;
y = rotated.y;
Vec3d vec = Matrix4.multiply(Camera.camera.getMatrix(), pos);
// Play the sound with a new source
int source = AudioSources.getSource();
alSourceStop(source);
alSourcei(source, AL_BUFFER, bufferPointer);
alSourcef(source, AL_GAIN, (float)volume);
alSource3f(source, AL_POSITION, (float)x, (float)y, (float)z);
alSource3f(source, AL_POSITION, (float)vec.x, (float)vec.y, (float)vec.z);
alSourcePlay(source);
}
}

View File

@ -1,24 +1,30 @@
package projectzombie.display;
import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec3d;
import projectzombie.Main;
public class Camera
{
public double angle = 45;
public int renderDistance;
public int renderDistance = 3;
private Matrix4 matrix;
public static Camera camera;
public Camera(Vec3d pos)
public Camera()
{
Matrix4 identity = Matrix4.identity();
Vec2d pos = Main.player.pos;
angle = Main.player.angle;
identity = Matrix4.multiply(identity, Matrix4.translate(pos.x, 0, pos.y));
identity = Matrix4.multiply(identity, Matrix4.translate(-pos.x, 0, -pos.y));
identity = Matrix4.multiply(identity, Matrix4.rotate(angle, 0, 1, 0));
identity = Matrix4.multiply(identity, Matrix4.rotate(-50, 1, 0, 0));
identity = Matrix4.multiply(identity, Matrix4.translate(0, 0, -8));
identity = Matrix4.multiply(identity, Matrix4.translate(0, 0, -12));
matrix = identity;
}
public Matrix4 getMatrix() {

View File

@ -12,13 +12,16 @@ import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glViewport;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL33;
import org.lwjgl.system.MemoryStack;
import gl_engine.graphics.GraphicsShader;
import gl_engine.matrix.Matrix4;
import projectzombie.Main;
import projectzombie.display.lighting.DynamicLighting;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.gl.GlHelpers;
import gl_engine.vec.Vec2d;
import gl_engine.vec.Vec3d;
@ -29,10 +32,6 @@ public class DisplayRender
{
public static void render(int w, int h)
{
// Set the width and the height
Main.window.setWidth(w);
Main.window.setHeight(h);
// Setup GL and clear the colour
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, w, h);
@ -41,24 +40,35 @@ public class DisplayRender
{
if(ChunkEventHandler.loaded)
{
// Create the projection matrix
Matrix4 projection = Matrix4.projection((double)w / (double)h, 45, 1, 32);
EntityPlayer player = Main.player;
Camera camera = new Camera(new Vec3d(player.pos.x, 0, player.pos.y));
Camera camera = new Camera();
Camera.camera = camera;
// Create the projection matrix
Matrix4 projection = Matrix4.projection((double)w / (double)h, 45, 0.1, 1000);
Matrix4 rotated = Matrix4.rotate(-camera.angle, 0, 1, 0);
// Process all the light sources
DynamicLighting.update();
//DynamicLighting.update();
Main.window.environmentRenderer.use();
GL33.glUniformMatrix4fv(Main.window.glsl_camera, true, camera.getMatrix().getArray());
GL33.glUniformMatrix4fv(Main.window.glsl_projection, true, projection.getArray());
GL33.glUniformMatrix4fv(Main.window.glsl_rotated, true, rotated.getArray());
// Render the world and the player
Main.world.render(camera);
player.chunk = Main.world.getLayer().getChunk(player.pos);
player.doRender(player.pos, camera);
Model model = player.getModel();
Matrix4 matrix = Matrix4.translate(player.pos.x, 0, player.pos.y);
GL33.glUniformMatrix4fv(Main.window.glsl_model, true, matrix.getArray());
model.bind();
model.render();
}
}
// Render the user interface
DisplayRenderUI.render();
//DisplayRenderUI.render();
}
}

View File

@ -19,7 +19,7 @@ public class DisplayRenderUI
public static void render()
{
double s = GlHelpers.getScale() / 10.0;
/*double s = GlHelpers.getScale() / 10.0;
// Get some text settings
Vec2d text_size = new Vec2d(0.5, 0.5);
@ -186,6 +186,6 @@ public class DisplayRenderUI
}
// Render the loaded menu
Main.menu.render();
Main.menu.render();*/
}
}

View File

@ -9,10 +9,15 @@ import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL33;
import gl_engine.graphics.GraphicsHelpers;
import gl_engine.graphics.GraphicsShader;
import gl_engine.matrix.Matrix4;
import mainloop.task.IMainloopTask;
import projectzombie.Main;
import projectzombie.init.Models;
import projectzombie.init.Resources;
import projectzombie.input.CursorEnterCallback;
import projectzombie.input.CursorPosCallback;
import projectzombie.input.JoystickCallback;
@ -33,6 +38,13 @@ public class DisplayWindow implements IMainloopTask
private boolean fullscreen = true;
private boolean mouseVisibility_last = false;
public GraphicsShader environmentRenderer;
public int glsl_model;
public int glsl_projection;
public int glsl_rotated;
public int glsl_camera;
public int getWidth() {
return this.width;
}
@ -72,7 +84,7 @@ public class DisplayWindow implements IMainloopTask
window = GraphicsHelpers.initWindow("Project Zombie", width, height, monitor);
// Make the context current
GLFW.glfwMakeContextCurrent(this.window);
//GLFW.glfwMakeContextCurrent(this.window);
// Set the key handlers
GLFW.glfwSetCursorPosCallback(this.window, new CursorPosCallback());
@ -87,10 +99,17 @@ public class DisplayWindow implements IMainloopTask
GLFW.glfwSetInputMode(this.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN);
// Show the window
GLFW.glfwShowWindow(this.window);
//GLFW.glfwShowWindow(this.window);
// Register the display event loop
Main.mainloop.register(this);
GL33.glEnable(GL33.GL_DEPTH_TEST);
environmentRenderer = new GraphicsShader("/resources/shader/environmentRenderer");
environmentRenderer.use();
glsl_model = GL33.glGetUniformLocation(environmentRenderer.program, "model");
glsl_camera = GL33.glGetUniformLocation(environmentRenderer.program, "camera");
glsl_rotated = GL33.glGetUniformLocation(environmentRenderer.program, "rotated");
glsl_projection = GL33.glGetUniformLocation(environmentRenderer.program, "projection");
}
public void render()
@ -99,16 +118,21 @@ public class DisplayWindow implements IMainloopTask
int w[] = {0};
int h[] = {0};
GLFW.glfwGetFramebufferSize(this.window, w, h);
width = w[0];
height = h[0];
// Bind the texture atlas
Resources.ATLAS.bind();
// Render everything
DisplayRender.render(w[0], h[0]);
// Check if the matrix count is ok
GlHelpers.checkMatrixCount();
//GlHelpers.checkMatrixCount();
// Swap the framebuffers and poll events
this.swapBuffers();
this.pollEvents();
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents();
}
public void toggleFullscreen() {
@ -132,14 +156,6 @@ public class DisplayWindow implements IMainloopTask
}
}
public void swapBuffers() {
GLFW.glfwSwapBuffers(this.window);
}
public void pollEvents() {
GLFW.glfwPollEvents();
}
public boolean shouldClose() {
return GLFW.glfwWindowShouldClose(this.window);
}

View File

@ -20,7 +20,7 @@ public class BossBars
public static void render()
{
TextureReference health_fg = Models.UI_HEALTH_FG;
/*TextureReference health_fg = Models.UI_HEALTH_FG;
TextureReference health_bg = Models.UI_HEALTH_BG;
ArrayList<IBossBar> toRemove = new ArrayList<IBossBar>();
@ -54,6 +54,6 @@ public class BossBars
for(IBossBar bossbar : toRemove) {
bossbars.remove(bossbar);
}
}*/
}
}

View File

@ -32,10 +32,10 @@ public class TileLighting implements IMainloopTask
cx + MathHelpers.floor(player.pos.x / 16),
cy + MathHelpers.floor(player.pos.y / 16));
Chunk chunk = layer.chunks.get(cpos);
if(chunk.light_dirty) {
/*if(chunk.light_dirty) {
chunk.light_dirty = false;
dirty = true;
}
}*/
}
}

View File

@ -134,7 +134,7 @@ public class EntityItem extends Entity
@Override
public Model getModel() {
return Models.ITEM_NONE;
return stack.item.getModel(stack.meta);
}

View File

@ -7,7 +7,6 @@ import projectzombie.entity.EntityParticle;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.settings.SettingQuality;
import projectzombie.tiles.TileVertical;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.gl.texture.IHasTexture;
import projectzombie.util.gl.texture.TextureReference;
@ -52,9 +51,7 @@ public class ParticleBreak extends EntityParticle
int height = 1;
if(s.tile instanceof TileVertical) {
height = MathHelpers.floor(((TileVertical)s.tile).size.y);
}
for(int i=0;i<50 * height;i++) {
chunk.spawnEntity(new ParticleBreak(pos.copy(), s));
@ -98,12 +95,6 @@ public class ParticleBreak extends EntityParticle
double angle = RandomHelpers.randrange(rand, 360);
if(ts.tile instanceof TileVertical) {
TileVertical ts_v = (TileVertical) ts.tile;
height = RandomHelpers.randrange(rand, 0, MathHelpers.floor(ts_v.size.y));
}
Vec2d side_v = MathHelpers.moveTowards2(0.01, Math.toRadians(angle));
velocity = new Vec3d(
side_v.x, side_v.y,
@ -170,7 +161,7 @@ public class ParticleBreak extends EntityParticle
@Override
public Model getModel() {
return Models.ENTITY_NONE;
return Models.PARTICLE_BULLET;
}

View File

@ -4,8 +4,10 @@ import java.util.ArrayList;
import org.lwjgl.opengl.GL;
import gl_engine.vec.Vec2d;
import projectzombie.display.DisplayWindow;
import projectzombie.model.Model;
import projectzombie.model.ModelEmpty;
import projectzombie.model.ModelGui;
import projectzombie.model.ModelRandom;
import projectzombie.model.ModelTile;
@ -16,15 +18,13 @@ import projectzombie.util.gl.texture.TextureReferenceRandom;
public class Models
{
public static final Model TILE_NONE = new ModelTile(Resources.ATLAS.get("NONE"));
public static final Model ENTITY_NONE = new ModelTile(Resources.ATLAS.get("NONE"));
public static final Model ITEM_NONE = new ModelTile(Resources.ATLAS.get("NONE"));
public static final Model EMPTY = new ModelEmpty();
public static final Model TILE_GRASS = new ModelTile(Resources.ATLAS.get("/tile/grass.png"));
public static final Model TILE_SAND = new ModelTile(Resources.ATLAS.get("/tile/sand.png"));
public static final Model TILE_STONE = new ModelTile(Resources.ATLAS.get("/tile/stone.png"));
public static final Model TILE_DIRT = new ModelTile(Resources.ATLAS.get("/tile/dirt.png"));
public static final Model TILE_TREE = new ModelVertical(Resources.ATLAS.get("/tile/tree.png"));
public static final Model TILE_TREE = new ModelVertical(Resources.ATLAS.get("/tile/tree.png"), new Vec2d(1, 4));
public static final Model TILE_ROCK = new ModelVertical(Resources.ATLAS.get("/tile/rock.png"));
public static final Model TILE_LADDER = new ModelVertical(Resources.ATLAS.get("/tile/ladder.png"));
public static final Model TILE_PORTAL = new ModelVertical(Resources.ATLAS.get("/tile/portal.png"));

View File

@ -21,7 +21,6 @@ import projectzombie.tiles.TileVoid;
import projectzombie.tiles.TileWall;
import projectzombie.tiles.TileWallUnbreakable;
import projectzombie.tiles.TileWater;
import projectzombie.tiles.TileWaterFlow;
public class Tiles
{
@ -50,7 +49,6 @@ public class Tiles
register(LAVA);
register(WATER);
register(LAVA_FLOW);
register(WATER_FLOW);
register(LADDER);
register(PORTAL_DOWN);
register(WALL);
@ -71,7 +69,6 @@ public class Tiles
public static final Tile LAVA = new TileLava();
public static final Tile WATER = new TileWater();
public static final Tile LAVA_FLOW = new TileLavaFlow();
public static final Tile WATER_FLOW = new TileWaterFlow();
public static final Tile LADDER = new TileLadder();
public static final Tile PORTAL_DOWN = new TilePortalDown();
public static final Tile WALL = new TileWall();

View File

@ -4,6 +4,7 @@ import projectzombie.entity.Entity;
import projectzombie.entity.EntityInventory;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.inventory.Inventory;
import projectzombie.model.Model;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.gl.texture.TextureReference;
import projectzombie.util.math.ItemStack;
@ -11,23 +12,11 @@ import gl_engine.vec.Vec2d;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
public class Item
public abstract class Item
{
public TextureReference texture;
public int id;
public void render(Vec2d pos, Vec2d size)
{
GlHelpers.begin();
texture.texCoord(0, 1); GlHelpers.vertex2(pos.x+size.x , pos.y );
texture.texCoord(0, 0); GlHelpers.vertex2(pos.x+size.x , pos.y+size.y );
texture.texCoord(1, 0); GlHelpers.vertex2(pos.x , pos.y+size.y );
texture.texCoord(1, 1); GlHelpers.vertex2(pos.x , pos.y );
GlHelpers.end();
}
public void onPlayerAction(ItemStack stack, Layer layer, Chunk chunk, EntityPlayer player) {
stack.count -= 1;
}
@ -40,6 +29,8 @@ public class Item
return 0;
}
public abstract Model getModel(short meta);
public void onPickedUp(ItemStack stack, Layer layer, Chunk chunk, Entity entity)
{
// Update the stacks count

View File

@ -3,6 +3,7 @@ package projectzombie.items;
import projectzombie.entity.Entity;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
@ -10,9 +11,9 @@ import projectzombie.world.layer.Layer;
public class ItemAmmo extends Item
{
public ItemAmmo() {
this.texture = Models.ITEM_AMMO_BOX;
@Override
public Model getModel(short meta) {
return Models.ITEM_AMMO_BOX;
}
@Override

View File

@ -3,6 +3,7 @@ package projectzombie.items;
import projectzombie.Main;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
@ -10,9 +11,9 @@ import projectzombie.world.layer.Layer;
public class ItemDefenceUpgrade extends Item
{
public ItemDefenceUpgrade() {
this.texture = Models.ITEM_DEFENCE_UPGRADE;
@Override
public Model getModel(short meta) {
return Models.ITEM_DEFENCE_UPGRADE;
}
@Override

View File

@ -2,6 +2,7 @@ package projectzombie.items;
import projectzombie.entity.Entity;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import gl_engine.vec.Vec2d;
import projectzombie.world.chunk.Chunk;
@ -22,7 +23,8 @@ public class ItemEmpty extends Item
}
@Override
public void render(Vec2d pos, Vec2d size) {
public Model getModel(short meta) {
return null;
}
}

View File

@ -3,6 +3,7 @@ package projectzombie.items;
import projectzombie.entity.EntityFlare;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
@ -10,9 +11,9 @@ import projectzombie.world.layer.Layer;
public class ItemFlare extends Item
{
public ItemFlare() {
this.texture = Models.ENTITY_FLARE;
@Override
public Model getModel(short meta) {
return Models.ENTITY_FLARE;
}
@Override

View File

@ -3,6 +3,7 @@ package projectzombie.items;
import projectzombie.entity.EntityGrapplingHook;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import gl_engine.MathHelpers;
import gl_engine.vec.Vec2d;
@ -12,9 +13,9 @@ import projectzombie.world.layer.Layer;
public class ItemGrapplingHook extends Item
{
public ItemGrapplingHook() {
this.texture = Models.ITEM_GRAPPLING_HOOK;
@Override
public Model getModel(short meta) {
return Models.ITEM_GRAPPLING_HOOK;
}
@Override

View File

@ -3,6 +3,7 @@ package projectzombie.items;
import projectzombie.Main;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
@ -10,9 +11,9 @@ import projectzombie.world.layer.Layer;
public class ItemGunUpgrade extends Item
{
public ItemGunUpgrade() {
this.texture = Models.ITEM_GUN_UPGRADE;
@Override
public Model getModel(short meta) {
return Models.ITEM_GUN_UPGRADE;
}
@Override

View File

@ -2,6 +2,7 @@ package projectzombie.items;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
@ -9,9 +10,9 @@ import projectzombie.world.layer.Layer;
public class ItemHealthPotion extends Item
{
public ItemHealthPotion() {
this.texture = Models.ITEM_HEALTH_POTION;
@Override
public Model getModel(short meta) {
return Models.ITEM_HEALTH_POTION;
}
@Override

View File

@ -3,6 +3,7 @@ package projectzombie.items;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.init.Tiles;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import gl_engine.MathHelpers;
import gl_engine.vec.Vec2i;
@ -12,9 +13,9 @@ import projectzombie.world.layer.Layer;
public class ItemLantern extends Item
{
public ItemLantern() {
this.texture = Models.TILE_LANTERN;
@Override
public Model getModel(short meta) {
return Models.TILE_LANTERN;
}
@Override

View File

@ -2,6 +2,7 @@ package projectzombie.items;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
@ -9,9 +10,9 @@ import projectzombie.world.layer.Layer;
public class ItemRock extends Item
{
public ItemRock() {
this.texture = Models.TILE_ROCK;
@Override
public Model getModel(short meta) {
return Models.ITEM_ROCK;
}
@Override

View File

@ -6,9 +6,8 @@ import gl_engine.vec.Vec2d;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
public class ItemSpawn extends Item
public abstract class ItemSpawn extends Item
{
@Override
public void onPlayerAction(ItemStack stack, Layer layer, Chunk chunk, EntityPlayer player) {
super.onPlayerAction(stack, layer, chunk, player);
@ -19,7 +18,4 @@ public class ItemSpawn extends Item
public void spawnEntity(Layer layer, Chunk chunk, Vec2d pos) {
}
}

View File

@ -3,18 +3,18 @@ package projectzombie.items;
import projectzombie.entity.EntityTnt;
import projectzombie.entity.player.EntityPlayer;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.ItemStack;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
public class ItemTnt extends Item
{
public ItemTnt() {
this.texture = Models.ENTITY_TNT;
@Override
public Model getModel(short meta) {
return Models.ENTITY_TNT;
}
@Override
public String getName(short meta) {
return "TNT";

View File

@ -3,6 +3,7 @@ package projectzombie.items.spawner;
import projectzombie.entity.EntityZombie;
import projectzombie.init.Models;
import projectzombie.items.ItemSpawn;
import projectzombie.model.Model;
import gl_engine.vec.Vec2d;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
@ -10,9 +11,9 @@ import projectzombie.world.layer.Layer;
public class ItemSpawnZombie extends ItemSpawn
{
public ItemSpawnZombie() {
this.texture = Models.ENTITY_ZOMBIE_B;
@Override
public Model getModel(short meta) {
return Models.ENTITY_ZOMBIE_B;
}
@Override

View File

@ -3,9 +3,11 @@ package projectzombie.menu.gui;
import projectzombie.Main;
import projectzombie.init.Models;
import projectzombie.input.InputMode;
import projectzombie.model.Model;
import projectzombie.text.Text;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.gl.texture.TextureReference;
import gl_engine.matrix.Matrix4;
import gl_engine.vec.Vec2d;
public class Button implements GUIComponent, GUISelectable
@ -33,10 +35,8 @@ public class Button implements GUIComponent, GUISelectable
}
@Override
public void render(Vec2d mousePos) {
GlHelpers.pushMatrix();
GlHelpers.translate2(pos.x, pos.y);
public void render(Vec2d mousePos)
{
double m = 3;
double w = textSize.x * m * 8;
double h = textSize.x * m / 2;
@ -63,37 +63,24 @@ public class Button implements GUIComponent, GUISelectable
wt = -w/2;
}
TextureReference tex;
Model model;
boolean mouseHover = InputMode.Controller ? this.selected : this.checkMouseHover(mousePos);
if(mouseHover) {
tex = Models.BUTTON_HOVER;
model = Models.BUTTON_HOVER;
} else {
tex = Models.BUTTON;
model = Models.BUTTON;
}
GlHelpers.color4(1, 1, 1, 1);
GlHelpers.begin();
{
tex.texCoord(0, 0); GlHelpers.vertex2(w1, -h);
tex.texCoord(1, 0); GlHelpers.vertex2(w2, -h);
tex.texCoord(1, 1); GlHelpers.vertex2(w2, h);
tex.texCoord(0, 1); GlHelpers.vertex2(w1, h);
}
GlHelpers.end();
Matrix4 matrix = Matrix4.translate(pos.x, pos.y, 0);
GlHelpers.translate2(
(-textSize.x * text.length() / 2 + wt)/GlHelpers.getAspectRatio(),
-textSize.y / 2
);
model.bind();
if(mouseHover) {
GlHelpers.color3(0.8, 0.8, 0.8);
//GlHelpers.color3(0.8, 0.8, 0.8);
} else {
GlHelpers.color3(0.68, 0.68, 0.68);
//GlHelpers.color3(0.68, 0.68, 0.68);
}
Text.render(text, textSize);
GlHelpers.popMatrix();
}
@Override

View File

@ -5,11 +5,14 @@ import gl_engine.texture.TextureRef3D;
import static org.lwjgl.opengl.GL33.*;
import org.lwjgl.opengl.GL33;
public abstract class Model
{
int vao, size;
boolean loaded = false;
private static final int SIZE = 9;
private float[] verticies;
public int getSize() {
return size;
@ -19,6 +22,10 @@ public abstract class Model
protected abstract float[] getVerticies();
protected abstract TextureRef3D[] getTextures();
public float[] getLoadedVerticies() {
return verticies;
}
public void bind()
{
if(loaded) {
@ -27,7 +34,7 @@ public abstract class Model
else
{
float[] verticies = this.getVerticies();
verticies = this.getVerticies();
TextureRef3D[] refs = this.getTextures();
if(verticies.length % SIZE != 0 || refs.length * 3 != verticies.length / SIZE) {
@ -53,10 +60,20 @@ public abstract class Model
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, verticies, GL_STATIC_DRAW);
glVertexAttribPointer(0, SIZE, GL_FLOAT, false, Float.BYTES * SIZE, 0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, Float.BYTES * SIZE, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, Float.BYTES * SIZE, Float.BYTES * 3);
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, false, Float.BYTES * SIZE, Float.BYTES * 6);
glEnableVertexAttribArray(2);
loaded = true;
}
}
public void render() {
GL33.glDrawArrays(GL33.GL_TRIANGLES, 0, getSize());
}
}

View File

@ -0,0 +1,32 @@
package projectzombie.model;
import gl_engine.texture.TextureRef3D;
public class ModelEmpty extends Model
{
@Override
protected TextureRef3D[] getTextures() {
return new TextureRef3D[0];
}
@Override
protected float[] getVerticies() {
return new float[0];
}
@Override
public void bind() {}
@Override
public int getSize() {
return 0;
}
@Override
public void render() {}
@Override
public float[] getLoadedVerticies() {
return new float[0];
}
}

View File

@ -17,6 +17,10 @@ public class Resource
return "/resources/"+path;
}
public String getPath() {
return filePath(path);
}
public Resource(String path)
{
// Set the specified data

View File

@ -1,12 +1,14 @@
package projectzombie.tiles;
import projectzombie.init.Models;
import projectzombie.model.Model;
public class TileSand extends TileFlat
public class TileSand extends Tile
{
public TileSand() {
super(Models.TILE_SAND);
@Override
public Model getModel(byte meta) {
return Models.TILE_SAND;
}
}

View File

@ -1,12 +1,14 @@
package projectzombie.tiles;
import projectzombie.init.Models;
import projectzombie.model.Model;
public class TileStone extends TileFlatFaded
public class TileStone extends Tile
{
public TileStone() {
super(Models.TILE_STONE);
@Override
public Model getModel(byte meta) {
return Models.TILE_STONE;
}
}

View File

@ -1,16 +1,15 @@
package projectzombie.tiles;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.TileState;
import gl_engine.vec.Vec2d;
public class TileTree extends TileVertical implements TileBulletBreakable
public class TileTree extends Tile implements TileBulletBreakable
{
public TileTree() {
super(Models.TILE_TREE, new Vec2d(1, 4));
// Set some settings
this.opaqueTile = true;
this.tileSolid = true;
this.tileHitbox = 0.3;
}
@ -25,4 +24,9 @@ public class TileTree extends TileVertical implements TileBulletBreakable
return 35;
}
@Override
public Model getModel(byte meta) {
return Models.TILE_TREE;
}
}

View File

@ -1,5 +1,8 @@
package projectzombie.tiles;
import projectzombie.init.Models;
import projectzombie.model.Model;
public class TileVoid extends Tile {
public TileVoid() {
@ -8,5 +11,10 @@ public class TileVoid extends Tile {
this.tileSolid = false;
this.tileWalkable = true;
}
@Override
public Model getModel(byte meta) {
return Models.EMPTY;
}
}

View File

@ -1,13 +1,13 @@
package projectzombie.tiles;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.math.TileState;
public class TileWall extends TileFlat
public class TileWall extends Tile
{
public TileWall() {
super(Models.TILE_WALL);
this.tileWalkable = false;
this.tileSolid = true;
@ -21,4 +21,9 @@ public class TileWall extends TileFlat
return 1/2.0;
}
@Override
public Model getModel(byte meta) {
return Models.TILE_WALL;
}
}

View File

@ -4,6 +4,7 @@ import projectzombie.display.Camera;
import projectzombie.entity.Entity;
import projectzombie.entity.particle.ParticleWater;
import projectzombie.init.Models;
import projectzombie.model.Model;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.math.TileState;
import gl_engine.vec.Vec2d;
@ -11,24 +12,14 @@ import gl_engine.vec.Vec2i;
import projectzombie.world.chunk.Chunk;
import projectzombie.world.layer.Layer;
public class TileWater extends TileFlat
public class TileWater extends Tile
{
public TileWater() {
super(Models.TILE_WATER);
this.slowness = 0.5;
this.rotates = true;
this.unbreakable = true;
}
@Override
public void render(Vec2d pos, Camera camera, TileState state) {
GlHelpers.pushMatrix();
GlHelpers.translate3(0, 0, 0.001);
super.render(pos, camera, state);
GlHelpers.popMatrix();
}
@Override
public void onWalkedOn(Chunk chunk, Layer layer, Vec2i pos, Entity entity, TileState state)
{
@ -40,5 +31,10 @@ public class TileWater extends TileFlat
layer.spawnEntity(new ParticleWater(entity.pos.copy()));
}
}
@Override
public Model getModel(byte meta) {
return Models.TILE_WATER;
}
}

View File

@ -1,26 +0,0 @@
package projectzombie.tiles;
import projectzombie.display.Camera;
import projectzombie.init.Models;
import projectzombie.util.gl.GlHelpers;
import projectzombie.util.math.TileState;
import gl_engine.vec.Vec2d;
public class TileWaterFlow extends TileFlat
{
public TileWaterFlow() {
super(Models.TILE_WATER_FLOW);
this.unbreakable = true;
}
@Override
public void render(Vec2d pos, Camera camera, TileState state) {
GlHelpers.pushMatrix();
GlHelpers.translate3(0, 0, 0.001);
super.render(pos, camera, state);
GlHelpers.popMatrix();
}
}

View File

@ -4,20 +4,25 @@ import java.util.ArrayList;
import java.util.Random;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL33;
import bdf.classes.IBdfClassManager;
import bdf.types.BdfArray;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import projectzombie.Main;
import projectzombie.display.Camera;
import projectzombie.entity.Entity;
import projectzombie.entity.EntityAlive;
import projectzombie.entity.EntityKillWithParticles;
import projectzombie.entity.particle.ParticleBreak;
import projectzombie.init.Models;
import projectzombie.init.Tiles;
import projectzombie.model.Model;
import projectzombie.tiles.Tile;
import projectzombie.util.gl.GlHelpers;
import gl_engine.MathHelpers;
import gl_engine.matrix.Matrix4;
import projectzombie.util.math.TileState;
import projectzombie.util.math.random.RandomHelpers;
import gl_engine.range.Range2i;
@ -48,8 +53,21 @@ public class Chunk implements IBdfClassManager
public ArrayList<Entity> entities = new ArrayList<Entity>();
private Layer layer;
public Vec2i c_pos;
public boolean dirty;
public boolean light_dirty;
private boolean dirty;
private boolean light_dirty;
public boolean isDirty()
{
if(entities.size() > 0) {
return true;
}
return dirty;
}
public void clearDirty() {
dirty = false;
}
@Override
public void BdfClassLoad(BdfObject bdf)
@ -152,66 +170,31 @@ public class Chunk implements IBdfClassManager
public void render(Camera camera)
{
// Loop over all the tiles
for(int i=0;i<CHUNK_INDEX;i++)
for(int i=0;i<256;i++)
{
// Get the tile pos
Vec2i t_pos = Vec2i.fromId(CHUNK_SIZE, i);
int x = i / 16;
int y = i % 16;
// Render the tiles
TileState tsb = getBackTile(i);
TileState tsf = getFrontTile(i);
tiles_back[i].doRender(new Vec2d(t_pos.x + c_pos.x * 16, t_pos.y + c_pos.y * 16), camera, tsb);
tiles_front[i].doRender(new Vec2d(t_pos.x + c_pos.x * 16, t_pos.y + c_pos.y * 16), camera, tsf);
}
// Render all the entities
for(int i=0;i<entities.size();i++) {
Entity e = entities.get(i);
e.doRender(e.pos, camera);
}
// Draw chunk boundaries
if(SHOW_CHUNKS)
{
GlHelpers.disableTexture2d();
GlHelpers.color3(1, 0, 0);
GL11.glBegin(GL11.GL_LINES);
TileState tile_f = getFrontTile(i);
TileState tile_b = getBackTile(i);
GlHelpers.vertex3((c_pos.x + 0) * 16, (c_pos.y + 0) * 16, 0.1);
GlHelpers.vertex3((c_pos.x + 1) * 16, (c_pos.y + 0) * 16, 0.1);
Model model_f = tile_f.tile.getModel(tile_f.meta);
Model model_b = tile_b.tile.getModel(tile_b.meta);
GlHelpers.vertex3((c_pos.x + 0) * 16, (c_pos.y + 0) * 16, 0.1);
GlHelpers.vertex3((c_pos.x + 0) * 16, (c_pos.y + 1) * 16, 0.1);
Matrix4 matrix = Matrix4.translate(x + c_pos.x * 16, 0, y + c_pos.y * 16);
GL33.glUniformMatrix4fv(Main.window.glsl_model, true, matrix.getArray());
GlHelpers.color3(0, 0, 1);
model_f.bind();
model_f.render();
int l = 1000;
GlHelpers.vertex3((c_pos.x + 0) * 16, (c_pos.y + 0) * 16, -l);
GlHelpers.vertex3((c_pos.x + 0) * 16, (c_pos.y + 0) * 16, l);
GlHelpers.vertex3((c_pos.x + 0.5) * 16, (c_pos.y + 0) * 16, -l);
GlHelpers.vertex3((c_pos.x + 0.5) * 16, (c_pos.y + 0) * 16, l);
GlHelpers.vertex3((c_pos.x + 0) * 16, (c_pos.y + 0.5) * 16, -l);
GlHelpers.vertex3((c_pos.x + 0) * 16, (c_pos.y + 0.5) * 16, l);
GlHelpers.vertex3((c_pos.x + 0.5) * 16, (c_pos.y + 0.5) * 16, -l);
GlHelpers.vertex3((c_pos.x + 0.5) * 16, (c_pos.y + 0.5) * 16, l);
GlHelpers.vertex3((c_pos.x + 1) * 16, (c_pos.y + 0) * 16, -l);
GlHelpers.vertex3((c_pos.x + 1) * 16, (c_pos.y + 0) * 16, l);
GL11.glEnd();
GlHelpers.enableTexture2d();
model_b.bind();
model_b.render();
}
}
public void spawnEntity(Entity e) {
e.chunk = this;
entities.add(e);
this.dirty = true;
}
public void tickEntities()
@ -478,7 +461,6 @@ public class Chunk implements IBdfClassManager
}
entities.remove(e);
this.dirty = true;
}
public ArrayList<Entity> getNearbyEntities(Vec2d pos, double distance)

View File

@ -155,4 +155,13 @@ public class ChunkEmpty extends Chunk
@Override
public void setDaylightLevel(double light, Vec2i pos) {
}
@Override
public void clearDirty() {
}
@Override
public boolean isDirty() {
return false;
}
}

View File

@ -222,7 +222,7 @@ public class Layer implements IBdfClassManager
Chunk chunk = new Chunk(this, pos, rand);
chunks.set(pos, chunk);
layergen.generateChunk(chunk, this, new Random(cseed), pos);
chunk.dirty = false;
chunk.clearDirty();
}
}
@ -230,7 +230,7 @@ public class Layer implements IBdfClassManager
{
// Store the chunk if its a dirty chunk
Chunk chunk = chunks.get(pos);
if(chunk.dirty) {
if(chunk.isDirty()) {
dirty_chunks.set(pos, chunk);
}
@ -321,7 +321,7 @@ public class Layer implements IBdfClassManager
for(Map2DElement<Chunk> chunk : chunks) {
if(dirty_chunks.contains(chunk.pos))
continue;
if(!chunk.o.dirty)
if(!chunk.o.isDirty())
continue;
BdfNamedList chunk_nl = new BdfNamedList();

View File

@ -1,46 +1,28 @@
#version 330
layout (location = 0) in float[9] vertices;
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aTex;
layout (location = 2) in vec3 aFlags;
out vec3 pTexture;
uniform mat4 projection;
uniform mat4 model;
uniform mat4 camera;
uniform float cameraAngle;
mat4 rotate(float angle, float x, float y, float z)
{
mat4 result = mat4(1);
float csin = sin(angle);
float ccos = cos(angle);
float iccos = 1 - ccos;
result[0][0] = ccos + x * x * iccos;
result[0][1] = x * y * iccos - z * csin;
result[0][2] = x * z * iccos + y * csin;
result[1][0] = y * x * iccos + z * csin;
result[1][1] = ccos + y * y * iccos;
result[1][2] = y * z * iccos - x * csin;
result[2][0] = z * x * iccos - y * csin;
result[2][1] = z * y * iccos + x * csin;
result[2][2] = ccos + z * z * iccos;
result[3][3] = 1;
return result;
}
uniform mat4 rotated;
void main()
{
int type = int(vertices[8]);
int type = int(aFlags.z);
mat4 do_rotation = rotate(cameraAngle, 0, 1, 0);
float animate_count = aFlags.x;
float animate_speed = aFlags.y;
mat4 do_rotation = rotated;
mat4 no_rotation = mat4(1);
gl_Position = vec4(vertices[0], vertices[1], vertices[2], 1) *
(type == 1 ? do_rotation : no_rotation) * model * camera * projection;
gl_Position = vec4(aPos, 1) * (type == 1 ? do_rotation : no_rotation) * model * camera * projection;
pTexture = vec3(vertices[3], vertices[4], vertices[5]);
pTexture = vec3(aTex.x, aTex.y, aTex.z);
}