Fixed issues with smooth lighting and added dynamic lighting. Fixed

issues with spawning and fixed issues with expensive pathfinding.
This commit is contained in:
josua 2020-06-22 23:35:29 +10:00
parent 7ffa6c1023
commit ecfb03d62d
26 changed files with 433 additions and 268 deletions

View File

@ -3,7 +3,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/mainloop.jar"/> <classpathentry kind="lib" path="/home/josua/code/Java Libraries/mainloop.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/binary-data-format-v1.9.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/GlEngine.jar"/> <classpathentry kind="lib" path="/home/josua/code/Java Libraries/GlEngine.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl.jar"/> <classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl-glfw.jar"/> <classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl-glfw.jar"/>
@ -42,5 +41,6 @@
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl-stb-natives-macos.jar"/> <classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl-stb-natives-macos.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl-stb-natives-windows.jar"/> <classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl-stb-natives-windows.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl-stb-sources.jar"/> <classpathentry kind="lib" path="/home/josua/code/Java Libraries/lwjgl-3.2.2-lightweight/lwjgl-stb-sources.jar"/>
<classpathentry kind="lib" path="/home/josua/code/Java Libraries/binary-data-format-v2.1.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

View File

@ -31,8 +31,6 @@ public class DisplayRender
GL33.glUniform3f(Main.window.glsl_day_low, (float)range.min.x, (float)range.min.y, (float)range.min.z); GL33.glUniform3f(Main.window.glsl_day_low, (float)range.min.x, (float)range.min.y, (float)range.min.z);
GL33.glUniform3f(Main.window.glsl_day_high, (float)range.max.x, (float)range.max.y, (float)range.max.z); GL33.glUniform3f(Main.window.glsl_day_high, (float)range.max.x, (float)range.max.y, (float)range.max.z);
GL33.glUniform3f(Main.window.glsl_src_low, 0, 0, 0);
GL33.glUniform3f(Main.window.glsl_src_high, 1, 1, 1);
if(Main.menu.doGameRender) if(Main.menu.doGameRender)
{ {
@ -44,13 +42,14 @@ public class DisplayRender
// Create the projection matrix // Create the projection matrix
Matrix4 projection = Matrix4.projection((double)w / (double)h, 45, 0.1, 1000); Matrix4 projection = Matrix4.projection((double)w / (double)h, 45, 0.1, 1000);
projection = Matrix4.multiply(camera.getMatrix(), projection);
Matrix4 rotated = Matrix4.rotate(-camera.angle, 0, 1, 0); Matrix4 rotated = Matrix4.rotate(-camera.angle, 0, 1, 0);
// Process all the light sources // Process all the light sources
//DynamicLighting.update(); //DynamicLighting.update();
Main.window.environmentRenderer.use(); 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_projection, true, projection.getArray());
GL33.glUniformMatrix4fv(Main.window.glsl_rotated, true, rotated.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_rotated, true, rotated.getArray());
GL33.glUniform1i(Main.window.glsl_time, (int)((System.currentTimeMillis() / 10) % 1000)); GL33.glUniform1i(Main.window.glsl_time, (int)((System.currentTimeMillis() / 10) % 1000));

View File

@ -92,11 +92,8 @@ public class DisplayRenderUI
GL33.glUniform3f(Main.window.glsl_day_low, 1, 1, 1); GL33.glUniform3f(Main.window.glsl_day_low, 1, 1, 1);
GL33.glUniform3f(Main.window.glsl_day_high, 1, 1, 1); GL33.glUniform3f(Main.window.glsl_day_high, 1, 1, 1);
GL33.glUniform3f(Main.window.glsl_src_low, 1, 1, 1);
GL33.glUniform3f(Main.window.glsl_src_high, 1, 1, 1);
GL33.glUniformMatrix4fv(Main.window.glsl_projection, true, projection.getArray()); GL33.glUniformMatrix4fv(Main.window.glsl_projection, true, projection.getArray());
GL33.glUniformMatrix4fv(Main.window.glsl_camera, true, camera.getArray());
GL33.glDisable(GL33.GL_DEPTH_TEST); GL33.glDisable(GL33.GL_DEPTH_TEST);

View File

@ -36,21 +36,16 @@ public class DisplayWindow implements IMainloopTask
public static int fps = 0; public static int fps = 0;
public GraphicsShader environmentRenderer; public GraphicsShader environmentRenderer;
private GraphicsShader lightmapRenderer;
private int lightmapVao;
public int glsl_color; public int glsl_color;
public int glsl_tex_cut; public int glsl_tex_cut;
public int glsl_model; public int glsl_model;
public int glsl_projection; public int glsl_projection;
public int glsl_rotated; public int glsl_rotated;
public int glsl_camera;
public int glsl_time; public int glsl_time;
public int glsl_day_low; public int glsl_day_low;
public int glsl_day_high; public int glsl_day_high;
public int glsl_src_low;
public int glsl_src_high;
public int glsl_lightmap_offset; public int glsl_lightmap_offset;
public int glsl_lightmap_size; public int glsl_lightmap_size;
@ -106,6 +101,8 @@ public class DisplayWindow implements IMainloopTask
// Make the cursor invisible // Make the cursor invisible
GLFW.glfwSetInputMode(this.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN); GLFW.glfwSetInputMode(this.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN);
GLFW.glfwSwapInterval(0);
// Show the window // Show the window
//GLFW.glfwShowWindow(this.window); //GLFW.glfwShowWindow(this.window);
@ -113,7 +110,6 @@ public class DisplayWindow implements IMainloopTask
environmentRenderer.use(); environmentRenderer.use();
glsl_model = GL33.glGetUniformLocation(environmentRenderer.program, "model"); glsl_model = GL33.glGetUniformLocation(environmentRenderer.program, "model");
glsl_camera = GL33.glGetUniformLocation(environmentRenderer.program, "camera");
glsl_rotated = GL33.glGetUniformLocation(environmentRenderer.program, "rotated"); glsl_rotated = GL33.glGetUniformLocation(environmentRenderer.program, "rotated");
glsl_projection = GL33.glGetUniformLocation(environmentRenderer.program, "projection"); glsl_projection = GL33.glGetUniformLocation(environmentRenderer.program, "projection");
glsl_time = GL33.glGetUniformLocation(environmentRenderer.program, "time"); glsl_time = GL33.glGetUniformLocation(environmentRenderer.program, "time");
@ -122,8 +118,6 @@ public class DisplayWindow implements IMainloopTask
glsl_day_low = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_low"); glsl_day_low = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_low");
glsl_day_high = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_high"); glsl_day_high = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_day_high");
glsl_src_low = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_src_low");
glsl_src_high = GL33.glGetUniformLocation(environmentRenderer.program, "lighting_src_high");
glsl_lightmap_offset = GL33.glGetUniformLocation(environmentRenderer.program, "lightmap_offset"); glsl_lightmap_offset = GL33.glGetUniformLocation(environmentRenderer.program, "lightmap_offset");
glsl_lightmap_size = GL33.glGetUniformLocation(environmentRenderer.program, "lightmap_size"); glsl_lightmap_size = GL33.glGetUniformLocation(environmentRenderer.program, "lightmap_size");
@ -131,31 +125,8 @@ public class DisplayWindow implements IMainloopTask
int glsl_atlas = GL33.glGetUniformLocation(environmentRenderer.program, "atlas"); int glsl_atlas = GL33.glGetUniformLocation(environmentRenderer.program, "atlas");
int glsl_lightmap = GL33.glGetUniformLocation(environmentRenderer.program, "lightmap"); int glsl_lightmap = GL33.glGetUniformLocation(environmentRenderer.program, "lightmap");
GL33.glUniform1ui(glsl_atlas, 1); GL33.glUniform1i(glsl_atlas, 0);
GL33.glUniform1ui(glsl_lightmap, 0); GL33.glUniform1i(glsl_lightmap, 1);
lightmapRenderer = new GraphicsShader("/resources/shader/lightmapRenderer");
lightmapRenderer.use();
lightmapVao = GL33.glGenVertexArrays();
GL33.glBindVertexArray(lightmapVao);
int vbo = GL33.glGenBuffers();
GL33.glBindBuffer(GL33.GL_ARRAY_BUFFER, vbo);
GL33.glBufferData(GL33.GL_ARRAY_BUFFER, new float[] {
-0.95f, -0.95f,
-0.65f, -0.95f,
-0.65f, -0.65f,
-0.95f, -0.95f,
-0.95f, -0.65f,
-0.65f, -0.65f,
}, GL33.GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, false, Float.BYTES * 2, 0);
glEnableVertexAttribArray(0);
} }
public void render() public void render()
@ -168,30 +139,19 @@ public class DisplayWindow implements IMainloopTask
height = h[0]; height = h[0];
environmentRenderer.use(); environmentRenderer.use();
TileLighting.updateLighting();
// Bind the texture atlas // Bind the texture atlas
GL33.glActiveTexture(GL33.GL_TEXTURE1); GL33.glActiveTexture(GL33.GL_TEXTURE0);
Resources.ATLAS.bind(); Resources.ATLAS.bind();
// Bind the lightmap // Bind the lightmap
GL33.glActiveTexture(GL33.GL_TEXTURE0); GL33.glActiveTexture(GL33.GL_TEXTURE1);
GL33.glBindTexture(GL33.GL_TEXTURE_2D, TileLighting.lightmap); GL33.glBindTexture(GL33.GL_TEXTURE_2D, TileLighting.lightmap);
// Render everything // Render everything
DisplayRender.render(w[0], h[0]); DisplayRender.render(w[0], h[0]);
// Check if the matrix count is ok
//GlHelpers.checkMatrixCount();
lightmapRenderer.use();
GL33.glActiveTexture(GL33.GL_TEXTURE0);
//GL33.glDisable(GL33.GL_DEPTH_TEST);
GL33.glBindVertexArray(lightmapVao);
GL33.glDrawArrays(GL33.GL_TRIANGLES, 0, 6);
// Swap the framebuffers and poll events // Swap the framebuffers and poll events
GLFW.glfwSwapBuffers(window); GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents(); GLFW.glfwPollEvents();

View File

@ -8,9 +8,11 @@ import org.lwjgl.opengl.GL33;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import gl_engine.MathHelpers; import gl_engine.MathHelpers;
import gl_engine.range.Range2i;
import gl_engine.vec.Vec2i; import gl_engine.vec.Vec2i;
import projectzombie.Main; import projectzombie.Main;
import projectzombie.display.Camera; import projectzombie.display.Camera;
import projectzombie.entity.Entity;
import projectzombie.entity.player.EntityPlayer; import projectzombie.entity.player.EntityPlayer;
import projectzombie.util.math.TileState; import projectzombie.util.math.TileState;
import projectzombie.world.chunk.Chunk; import projectzombie.world.chunk.Chunk;
@ -19,29 +21,31 @@ import projectzombie.world.layer.Layer;
public class TileLighting public class TileLighting
{ {
private static class LightingTask { private static class Lighting {
float[] b; float[] p;
int w, h; int w, h;
int x, y; int x, y;
private int getID(int x, int y) {
return (x + y * w) * 3 + 1;
}
} }
private static boolean lighting_dirty = false; private static boolean lighting_dirty = false;
public static int lightmap; public static int lightmap;
private static LightingTask task; private static Lighting lighting;
public static void setDirty() { public static void setDirty() {
lighting_dirty = true; lighting_dirty = true;
} }
public synchronized static LightingTask getTask() { private synchronized static Lighting getLighting() {
LightingTask t = task; return lighting;
task = null;
return t;
} }
public synchronized static void setTask(LightingTask task) { private synchronized static void setLighting(Lighting lighting) {
TileLighting.task = task; TileLighting.lighting = lighting;
} }
public TileLighting() { public TileLighting() {
@ -50,21 +54,6 @@ public class TileLighting
public static void update() public static void update()
{ {
{
LightingTask task = getTask();
if(task != null)
{
GL33.glBindTexture(GL33.GL_TEXTURE_2D, lightmap);
GL33.glTexImage2D(GL33.GL_TEXTURE_2D, 0, GL33.GL_RGB, task.w, task.h, 0, GL33.GL_RGB, GL33.GL_FLOAT, task.b);
GL33.glGenerateMipmap(GL33.GL_TEXTURE_2D);
Main.window.environmentRenderer.use();
GL33.glUniform2f(Main.window.glsl_lightmap_offset, task.x * 16 - 1, task.y * 16 - 1);
GL33.glUniform2f(Main.window.glsl_lightmap_size, task.w, task.h);
}
}
if(Camera.camera == null) return; if(Camera.camera == null) return;
if(!ChunkEventHandler.loaded) return; if(!ChunkEventHandler.loaded) return;
Layer layer = Main.world.getLayer(); Layer layer = Main.world.getLayer();
@ -140,6 +129,125 @@ public class TileLighting
MathHelpers.floor(player.pos.y / 16) - Chunk.RENDER_DISTANCE); MathHelpers.floor(player.pos.y / 16) - Chunk.RENDER_DISTANCE);
} }
private static void calculateLight(Layer layer, Lighting lighting, float[] pixels, int x, int y, float level, boolean ignore)
{
if(x < 0 || y < 0 || x >= lighting.w || y >= lighting.h) {
return;
}
int id = lighting.getID(x, y);
float level_current = pixels[id];
Vec2i tpos = new Vec2i(x + lighting.x * 16, y + lighting.y * 16);
int tid = tpos.getId(Chunk.CHUNK_SIZE);
if(level_current >= level && !ignore) {
return;
}
pixels[id] = level;
Chunk chunk = layer.getChunk(tpos);
TileState ft = chunk.getFrontTile(tid);
TileState bt = chunk.getBackTile(tid);
float dissipation = (float)Math.max(ft.tile.getLightDissipation(ft), bt.tile.getLightDissipation(bt));
float level_next = level - dissipation;
int[] adjacent = new int[] {
x+1, y+0,
x+0, y+1,
x-1, y-0,
x-0, y-1,
};
for(int i=0;i<8;i+=2) {
calculateLight(layer, lighting, pixels, adjacent[i], adjacent[i+1], level_next, false);
}
}
public static void updateLighting()
{
Lighting lighting = getLighting();
// Copy the pixels
float[] pixels = new float[lighting.p.length];
for(int i=0;i<pixels.length;i++) {
pixels[i] = lighting.p[i];
}
Layer layer = Main.world.getLayer();
lighting: {
double level = Main.player.getLightLevel();
if(level <= 0) {
break lighting;
}
if(level > 1) {
level = 1;
}
Vec2i lpos = Main.player.pos.toInt().subtract(new Vec2i(lighting.x * 16, lighting.y * 16));
calculateLight(layer, lighting, pixels, lpos.x, lpos.y, (float)level, true);
}
for(int cx=-Chunk.RENDER_DISTANCE;cx<=Chunk.RENDER_DISTANCE;cx++) {
for(int cy=-Chunk.RENDER_DISTANCE;cy<=Chunk.RENDER_DISTANCE;cy++)
{
Vec2i cpos = new Vec2i(
cx + MathHelpers.floor(Main.player.pos.x / 16),
cy + MathHelpers.floor(Main.player.pos.y / 16));
Chunk chunk = layer.chunks.get(cpos);
for(Entity entity : chunk.entities)
{
if(!entity.emitsLight) {
continue;
}
double level = entity.getLightLevel();
if(level <= 0) {
continue;
}
if(level > 1) {
level = 1;
}
Vec2i lpos = entity.pos.toInt().subtract(new Vec2i(lighting.x * 16, lighting.y * 16));
calculateLight(layer, lighting, pixels, lpos.x, lpos.y, (float)level, true);
}
}
}
// Update the texture
GL33.glBindTexture(GL33.GL_TEXTURE_2D, lightmap);
GL33.glTexImage2D(GL33.GL_TEXTURE_2D, 0, GL33.GL_RGB, lighting.w, lighting.h, 0, GL33.GL_RGB, GL33.GL_FLOAT, pixels);
GL33.glGenerateMipmap(GL33.GL_TEXTURE_2D);
// Set the texture location data
GL33.glUniform2f(Main.window.glsl_lightmap_offset, lighting.x * 16 - 0.5f, lighting.y * 16 - 0.5f);
GL33.glUniform2f(Main.window.glsl_lightmap_size, lighting.w, lighting.h);
}
public static void clearLighting()
{
Lighting lighting = new Lighting();
lighting.p = new float[3];
lighting.w = 1;
lighting.h = 1;
lighting.x = 0;
lighting.y = 0;
setLighting(lighting);
}
public static void updateLighting(BdfObject bdf) public static void updateLighting(BdfObject bdf)
{ {
BdfNamedList nl = bdf.getNamedList(); BdfNamedList nl = bdf.getNamedList();
@ -156,13 +264,13 @@ public class TileLighting
pixels[i*3+1] = light[i*2+1]; pixels[i*3+1] = light[i*2+1];
} }
LightingTask task = new LightingTask(); Lighting lighting = new Lighting();
task.w = width; lighting.p = pixels;
task.h = height; lighting.w = width;
task.b = pixels; lighting.h = height;
task.x = x; lighting.x = x;
task.y = y; lighting.y = y;
setTask(task); setLighting(lighting);
} }
} }

View File

@ -19,12 +19,26 @@ public class EntityFlare extends EntityTnt
public EntityFlare(Vec2d pos, double angle) { public EntityFlare(Vec2d pos, double angle) {
super(pos, angle, 0, 0); super(pos, angle, 0, 0);
this.explode_time = 1000; this.explode_time = 2000;
this.emitsLight = true;
} }
@Override @Override
public double getLightLevel() { public double getLightLevel()
return getLightWithHeight(1 - (this.pos.y * (1/12.0))) * ( rand.nextDouble() / 10.0 + 0.9 ); {
if(this.explode_time > 1950) {
return 0;
}
if(this.explode_time > 1900) {
return (1950 - this.explode_time) / 50.0;
}
if(this.explode_time < 200) {
return this.explode_time / 200.0;
}
return 1;
} }
@Override @Override

View File

@ -80,7 +80,6 @@ public class EntityTnt extends Entity implements EntityHeight, EntityHoldsEntiti
this.goThroughSolid = false; this.goThroughSolid = false;
this.explode_damage = explode_damage; this.explode_damage = explode_damage;
this.smoke_particles = new ParticleSpark[100]; this.smoke_particles = new ParticleSpark[100];
this.emitsLight = true;
// Set to 2.5 seconds // Set to 2.5 seconds
this.explode_time = 250; this.explode_time = 250;
@ -154,11 +153,6 @@ public class EntityTnt extends Entity implements EntityHeight, EntityHoldsEntiti
smoke_particles[dead_particle] = new ParticleSpark(new Vec3d(pos, height + 1)); smoke_particles[dead_particle] = new ParticleSpark(new Vec3d(pos, height + 1));
} }
@Override
public double getLightLevel() {
return getLightWithHeight(1 - (this.height * (1/12.0))) * ( rand.nextDouble() / 10.0 + 0.9 );
}
@Override @Override
protected void moveAwayFromSolidEntities(Layer layer) { protected void moveAwayFromSolidEntities(Layer layer) {
} }

View File

@ -111,54 +111,59 @@ public class EntityZombie extends Entity implements EntityAlive, EntityKillWithP
walk_scan_cooldown -= 1; walk_scan_cooldown -= 1;
} }
if( double player_distance = pos.squareDistance(Main.player.pos);
(walk_direction != null && pos.toInt().equal(walk_direction) &&
pos.squareDistance(Main.player.pos) > 2) || // Only pathfind if in range of the player
walk_scan_cooldown < 1 || walking_for > 200) if(player_distance < 16)
{ {
AStar astar = new AStar(pos.toInt(), 16, new AStarSearcher(layer, crossUnWalkable)); if(
Vec2i path[] = astar.getPath(Main.player.pos.toInt()); (walk_direction != null && pos.toInt().equal(walk_direction) &&
player_distance > 2) || walk_scan_cooldown < 1 || walking_for > 200)
walk_scan_cooldown = 100; {
walking_for = 0; AStar astar = new AStar(pos.toInt(), 16, new AStarSearcher(layer, crossUnWalkable));
Vec2i path[] = astar.getPath(Main.player.pos.toInt());
if(path != null && path.length > 1) {
walk_direction = path[1]; walk_scan_cooldown = 100;
} else { walking_for = 0;
walk_direction = Main.player.pos.toInt();
if(path != null && path.length > 1) {
walk_direction = path[1];
} else {
walk_direction = Main.player.pos.toInt();
}
can_see_player = (path != null);
} }
can_see_player = (path != null); // Walk towards the player
} if(walk_direction != null)
// Walk towards the player
if(walk_direction != null)
{
double angle = Math.toDegrees(Math.atan2(
walk_direction.x - (this.pos.x - 0.5) + noise_target_x.eval(time, pos.x/10, pos.y/10),
walk_direction.y - (this.pos.y - 0.5) + noise_target_y.eval(time, pos.x/10, pos.y/10)));
this.moveTowards(angle);
walking_for += 1;
}
if(can_see_player && noise_gun_fire.eval(time, 0) > 0 && !Main.player.dead && !Main.player.in_animation)
{
// Get the angle between the player and the zombie
double angle_fire = Math.atan2(pos.x - Main.player.pos.x, pos.y - Main.player.pos.y);
gun_interval += 1;
gun_interval %= 10;
if(gun_interval == 0)
{ {
// Aim the gun at the player double angle = Math.toDegrees(Math.atan2(
double angle_gun = Math.toDegrees(angle_fire) + 180; walk_direction.x - (this.pos.x - 0.5) + noise_target_x.eval(time, pos.x/10, pos.y/10),
angle_gun += noise_gun_angle.eval(time, 0)*20; walk_direction.y - (this.pos.y - 0.5) + noise_target_y.eval(time, pos.x/10, pos.y/10)));
this.moveTowards(angle);
// Fire the gun walking_for += 1;
int d = (int)(1 + gun_level / 5.0); }
layer.spawnEntity(new EntityBullet(pos.copy(), this, angle_gun, 20*d*d, 60));
if(can_see_player && noise_gun_fire.eval(time, 0) > 0 && !Main.player.dead && !Main.player.in_animation)
{
// Get the angle between the player and the zombie
double angle_fire = Math.atan2(pos.x - Main.player.pos.x, pos.y - Main.player.pos.y);
gun_interval += 1;
gun_interval %= 10;
if(gun_interval == 0)
{
// Aim the gun at the player
double angle_gun = Math.toDegrees(angle_fire) + 180;
angle_gun += noise_gun_angle.eval(time, 0)*20;
// Fire the gun
int d = (int)(1 + gun_level / 5.0);
layer.spawnEntity(new EntityBullet(pos.copy(), this, angle_gun, 20*d*d, 60));
}
} }
} }

View File

@ -107,10 +107,6 @@ public class EntityPlayer extends Entity implements EntityAlive, EntityInventory
// Create the inventory // Create the inventory
inventory = new Inventory(10); inventory = new Inventory(10);
inventory.addItem(new ItemStack(Items.SPAWN_ZOMBIE, 99, (short)0));
inventory.addItem(new ItemStack(Items.AMMO, 99, (short)0));
inventory.addItem(new ItemStack(Items.LANTERN, 99, (short)0));
} }
@Override @Override

View File

@ -22,7 +22,6 @@ public class MenuDeath extends Menu
this.input = new InputGUI(this.gui); this.input = new InputGUI(this.gui);
gui.add(new OverlayBackground()); gui.add(new OverlayBackground());
gui.add(new LabelPause("You were slain"));
ButtonGroup group = new ButtonGroupPause(); ButtonGroup group = new ButtonGroupPause();

View File

@ -56,7 +56,7 @@ public class MenuGamePause extends Menu
System.out.println("Time for BdfClassSave: " + (cur - now)); System.out.println("Time for BdfClassSave: " + (cur - now));
now = cur; now = cur;
System.out.println("Size on file: " + bdf.serialize().length()); System.out.println("Size on file: " + bdf.serialize().size());
//System.out.println(bdf.serializeHumanReadable()); //System.out.println(bdf.serializeHumanReadable());
try { try {

View File

@ -20,14 +20,16 @@ import projectzombie.Main;
public abstract class Model public abstract class Model
{ {
int vao, vbo; int vao, vbo, ibo;
boolean loaded = false; boolean loaded = false;
public static final int SIZE = 14; public static final int SIZE = 14;
// px, py, pz, tx, ty // px, py, pz, tx, ty
public abstract int[] getIndicies();
public abstract float[] getVerticies(); public abstract float[] getVerticies();
public abstract TextureRef3D[] getTextures(); public abstract TextureRef3D[] getTextures();
public abstract double getHeight(); public abstract double getHeight();
public abstract int getIndexSize();
public abstract int getSize(); public abstract int getSize();
private static Model bound = null; private static Model bound = null;
@ -38,11 +40,12 @@ public abstract class Model
return; return;
} }
int[] indicies = this.getIndicies();
float[] verticies = this.getVerticies(); float[] verticies = this.getVerticies();
TextureRef3D[] refs = this.getTextures(); TextureRef3D[] refs = this.getTextures();
if(verticies.length % SIZE != 0 || refs.length * 3 != verticies.length / SIZE) { if(verticies.length % SIZE != 0 || refs.length * SIZE != verticies.length) {
System.err.println("Invalid model"); System.err.println("Invalid model: " + verticies.length + ", " + refs.length + ", " + indicies.length);
System.exit(1); System.exit(1);
return; return;
} }
@ -51,7 +54,7 @@ public abstract class Model
double k = 0.001; double k = 0.001;
for(int i=0;i<size;i++) { for(int i=0;i<size;i++) {
TextureRef3D ref = refs[i / 3]; TextureRef3D ref = refs[i];
verticies[i*SIZE + 3] = (float)MathHelpers.map(verticies[i*SIZE + 3], 0-k, 1+k, ref.sx, ref.ex); verticies[i*SIZE + 3] = (float)MathHelpers.map(verticies[i*SIZE + 3], 0-k, 1+k, ref.sx, ref.ex);
verticies[i*SIZE + 4] = (float)MathHelpers.map(verticies[i*SIZE + 4], 0-k, 1+k, ref.sy, ref.ey); verticies[i*SIZE + 4] = (float)MathHelpers.map(verticies[i*SIZE + 4], 0-k, 1+k, ref.sy, ref.ey);
verticies[i*SIZE + 5] = ref.z; verticies[i*SIZE + 5] = ref.z;
@ -79,6 +82,10 @@ public abstract class Model
glVertexAttribPointer(4, 3, GL_FLOAT, false, Float.BYTES * SIZE, Float.BYTES * 11); glVertexAttribPointer(4, 3, GL_FLOAT, false, Float.BYTES * SIZE, Float.BYTES * 11);
glEnableVertexAttribArray(4); glEnableVertexAttribArray(4);
ibo = glGenBuffers();
glBindBuffer(GL33.GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL33.GL_ELEMENT_ARRAY_BUFFER, indicies, GL33.GL_STATIC_DRAW);
loaded = true; loaded = true;
} }
@ -101,7 +108,7 @@ public abstract class Model
bind(); bind();
} }
GL33.glDrawArrays(GL33.GL_TRIANGLES, 0, getSize()); GL33.glDrawElements(GL33.GL_TRIANGLES, getIndexSize(), GL33.GL_UNSIGNED_INT, 0);
} }
public void free() public void free()

View File

@ -5,11 +5,13 @@ import gl_engine.texture.TextureRef3D;
public class ModelChunk extends Model public class ModelChunk extends Model
{ {
private int size; private int size;
private int[] indicies;
private float[] verticies; private float[] verticies;
private TextureRef3D[] textures; private TextureRef3D[] textures;
public ModelChunk(float[] verticies, TextureRef3D[] textures, int size) { public ModelChunk(float[] verticies, int[] indicies, TextureRef3D[] textures, int size) {
this.verticies = verticies; this.verticies = verticies;
this.indicies = indicies;
this.textures = textures; this.textures = textures;
this.size = size; this.size = size;
} }
@ -28,6 +30,16 @@ public class ModelChunk extends Model
public double getHeight() { public double getHeight() {
return 1; return 1;
} }
@Override
public int getIndexSize() {
return indicies.length;
}
@Override
public int[] getIndicies() {
return indicies;
}
@Override @Override
public int getSize() { public int getSize() {

View File

@ -22,6 +22,16 @@ public class ModelEmpty extends Model
return 0; return 0;
} }
@Override
public int getIndexSize() {
return 0;
}
@Override
public int[] getIndicies() {
return new int[0];
}
@Override @Override
public void render() {} public void render() {}

View File

@ -34,7 +34,7 @@ public class ModelGui extends Model
@Override @Override
public int getSize() { public int getSize() {
return 6; return 4;
} }
@Override @Override
@ -52,11 +52,21 @@ public class ModelGui extends Model
return new float[] { return new float[] {
0, 0, 0, 0, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10, 0, 0, 0, 0, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10,
x, 0, 0, 1, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10, x, 0, 0, 1, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10,
x, y, 0, 1, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10,
x, y, 0, 1, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10, x, y, 0, 1, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10,
0, y, 0, 0, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10, 0, y, 0, 0, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10,
0, 0, 0, 0, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0x10, };
}
@Override
public int getIndexSize() {
return 6;
}
@Override
public int[] getIndicies() {
return new int[] {
0, 1, 2,
2, 3, 0,
}; };
} }
@ -64,7 +74,7 @@ public class ModelGui extends Model
public TextureRef3D[] getTextures() public TextureRef3D[] getTextures()
{ {
return new TextureRef3D[] { return new TextureRef3D[] {
ref, ref ref, ref, ref, ref
}; };
} }

View File

@ -21,7 +21,15 @@ public class ModelTile extends Model
@Override @Override
public int getSize() { public int getSize() {
return 6; return 4;
}
@Override
public int[] getIndicies() {
return new int[] {
0, 1, 2,
2, 3, 0,
};
} }
@Override @Override
@ -34,18 +42,20 @@ public class ModelTile extends Model
0.5f, 0, 0.5f, 1, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0, 0.5f, 0, 0.5f, 1, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0,
0.5f, 0, -0.5f, 1, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0, 0.5f, 0, -0.5f, 1, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0,
-0.5f, 0, -0.5f, 0, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0, -0.5f, 0, -0.5f, 0, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0,
-0.5f, 0, -0.5f, 0, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0,
-0.5f, 0, 0.5f, 0, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0, -0.5f, 0, 0.5f, 0, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0,
0.5f, 0, 0.5f, 1, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 0,
}; };
} }
@Override
public int getIndexSize() {
return 6;
}
@Override @Override
public TextureRef3D[] getTextures() public TextureRef3D[] getTextures()
{ {
return new TextureRef3D[] { return new TextureRef3D[] {
ref, ref ref, ref, ref, ref
}; };
} }

View File

@ -34,6 +34,11 @@ public class ModelVertical extends Model
@Override @Override
public int getSize() { public int getSize() {
return 4;
}
@Override
public int getIndexSize() {
return 6; return 6;
} }
@ -52,10 +57,15 @@ public class ModelVertical extends Model
-x, 0, 0, 0, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1, -x, 0, 0, 0, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1,
x, 0, 0, 1, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1, x, 0, 0, 1, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1,
x, y, 0, 1, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1, x, y, 0, 1, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1,
x, y, 0, 1, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1,
-x, y, 0, 0, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1, -x, y, 0, 0, 1, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1,
-x, 0, 0, 0, 0, 0, ref.sy, ref.ey, 0, 0, 0, asi, asp, 1, };
}
@Override
public int[] getIndicies() {
return new int[] {
0, 1, 2,
2, 3, 0,
}; };
} }
@ -63,7 +73,7 @@ public class ModelVertical extends Model
public TextureRef3D[] getTextures() public TextureRef3D[] getTextures()
{ {
return new TextureRef3D[] { return new TextureRef3D[] {
ref, ref ref, ref, ref, ref
}; };
} }

View File

@ -34,6 +34,7 @@ public class World implements IBdfClassManager
public void setLayer(int id) public void setLayer(int id)
{ {
ChunkEventHandler.loaded = false; ChunkEventHandler.loaded = false;
TileLighting.clearLighting();
TileLighting.setDirty(); TileLighting.setDirty();
this.loaded = layers.get(id); this.loaded = layers.get(id);

View File

@ -1,6 +1,7 @@
package projectzombie.world.chunk; package projectzombie.world.chunk;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random; import java.util.Random;
import bdf.classes.IBdfClassManager; import bdf.classes.IBdfClassManager;
@ -221,86 +222,67 @@ public class Chunk implements IBdfClassManager
{ {
this.render_dirty = false; this.render_dirty = false;
int verticies_size = 0; int verticies_size = 0;
int indicies_size = 0;
int index_offset = 0;
for(int i=0;i<CHUNK_INDEX;i++) { for(int i=0;i<CHUNK_INDEX;i++) {
TileState bt = getBackTile(i); TileState bt = getBackTile(i);
TileState ft = getFrontTile(i); TileState ft = getFrontTile(i);
verticies_size += bt.tile.getModel(bt.meta).getSize(); verticies_size += bt.tile.getModel(bt.meta).getSize();
verticies_size += ft.tile.getModel(ft.meta).getSize(); verticies_size += ft.tile.getModel(ft.meta).getSize();
indicies_size += bt.tile.getModel(bt.meta).getIndexSize();
indicies_size += ft.tile.getModel(ft.meta).getIndexSize();
} }
int[] indicies = new int[indicies_size];
float[] verticies = new float[verticies_size * Model.SIZE]; float[] verticies = new float[verticies_size * Model.SIZE];
TextureRef3D[] textures = new TextureRef3D[verticies_size / 3]; TextureRef3D[] textures = new TextureRef3D[verticies_size];
int upto = 0; int upto_v = 0;
int upto_i = 0;
for(int i=0;i<CHUNK_INDEX;i++) for(int i=0;i<CHUNK_INDEX;i++)
{ {
TileState bt = getBackTile(i);
TileState ft = getFrontTile(i);
Model bm = bt.tile.getModel(bt.meta);
Model fm = ft.tile.getModel(ft.meta);
float[] bv = bm.getVerticies();
float[] fv = fm.getVerticies();
TextureRef3D[] btex = bm.getTextures();
TextureRef3D[] ftex = fm.getTextures();
Vec2i pos = Vec2i.fromId(CHUNK_SIZE, i); Vec2i pos = Vec2i.fromId(CHUNK_SIZE, i);
for(int v=0;v<fm.getSize()/3;v++) { for(TileState state : new TileState[] {getFrontTile(pos), getBackTile(pos)})
textures[upto / 3 + v] = ftex[v];
}
for(int v=0;v<fm.getSize();v++)
{ {
for(int i2=0;i2<Model.SIZE;i2++) Model model = state.tile.getModel(state.meta);
TextureRef3D[] textures2 = model.getTextures();
float[] verticies2 = model.getVerticies();
int[] indicies2 = model.getIndicies();
for(int j=0;j<model.getSize();j++)
{ {
switch(i2) textures[upto_v] = textures2[j];
for(int k=0;k<Model.SIZE;k++)
{ {
case 8: switch(k)
verticies[upto * Model.SIZE + i2 ] = pos.x; {
continue; case 8:
verticies[upto_v * Model.SIZE + k ] = pos.x;
case 10: continue;
verticies[upto * Model.SIZE + i2 ] = pos.y;
continue; case 10:
verticies[upto_v * Model.SIZE + k ] = pos.y;
default: continue;
verticies[upto * Model.SIZE + i2 ] = fv[v * Model.SIZE + i2 ];
continue; default:
verticies[upto_v * Model.SIZE + k ] = verticies2[j * Model.SIZE + k ];
continue;
}
} }
upto_v += 1;
} }
upto += 1; for(int j=0;j<indicies2.length;j++) {
} indicies[j + upto_i] = indicies2[j] + index_offset;
for(int v=0;v<bm.getSize()/3;v++) {
textures[upto / 3 + v] = btex[v];
}
for(int v=0;v<bm.getSize();v++)
{
for(int i2=0;i2<Model.SIZE;i2++)
{
switch(i2)
{
case 8:
verticies[upto * Model.SIZE + i2 ] = pos.x;
continue;
case 10:
verticies[upto * Model.SIZE + i2 ] = pos.y;
continue;
default:
verticies[upto * Model.SIZE + i2 ] = bv[v * Model.SIZE + i2 ];
continue;
}
} }
upto += 1; upto_i += indicies2.length;
index_offset += model.getSize();
} }
} }
@ -308,7 +290,7 @@ public class Chunk implements IBdfClassManager
model.free(); model.free();
} }
model = new ModelChunk(verticies, textures, verticies_size); model = new ModelChunk(verticies, indicies, textures, verticies_size);
} }
// Render all the tiles in the chunk as a block // Render all the tiles in the chunk as a block

View File

@ -84,16 +84,28 @@ public class LayerGenCaves extends LayerGen
@Override @Override
public void spawnEntities(Layer layer, Random rand) public void spawnEntities(Layer layer, Random rand)
{ {
if(rand.nextDouble() > 0.95) if(rand.nextDouble() > 0.9)
{ {
Entity zombie = new EntityZombie(new Vec2d( Entity zombie = new EntityZombie(new Vec2d(
RandomHelpers.randrange(rand, (int)Main.player.pos.x - 128, (int)Main.player.pos.x + 128), RandomHelpers.randrange(rand, (int)Main.player.pos.x - 128, (int)Main.player.pos.x + 128),
RandomHelpers.randrange(rand, (int)Main.player.pos.y - 128, (int)Main.player.pos.y + 128))); RandomHelpers.randrange(rand, (int)Main.player.pos.y - 128, (int)Main.player.pos.y + 128)));
if(layer.getBackTile(new Vec2i((int)zombie.pos.x, boolean exists = false;
(int)zombie.pos.y)).tile == getTileDestroyed().tile &&
zombie.pos.squareDistance(Main.player.pos) > 32) for(Entity e : layer.getChunk(zombie.pos).entities) {
if(e.getClass() == zombie.getClass()) {
exists = true;
break;
}
}
if(
!exists && layer.getBackTile(new Vec2i((int)zombie.pos.x,
(int)zombie.pos.y)).tile == getTileDestroyed().tile &&
zombie.pos.squareDistance(Main.player.pos) > 32
) {
layer.spawnEntity(zombie); layer.spawnEntity(zombie);
}
} }
} }

View File

@ -83,8 +83,18 @@ public class LayerGenEarth extends LayerGen
RandomHelpers.randrange(rand, (int)Main.player.pos.x - 128, (int)Main.player.pos.x + 128), RandomHelpers.randrange(rand, (int)Main.player.pos.x - 128, (int)Main.player.pos.x + 128),
RandomHelpers.randrange(rand, (int)Main.player.pos.y - 128, (int)Main.player.pos.y + 128))); RandomHelpers.randrange(rand, (int)Main.player.pos.y - 128, (int)Main.player.pos.y + 128)));
if(zombie.pos.squareDistance(Main.player.pos) > 64) boolean exists = false;
for(Entity e : layer.getChunk(zombie.pos).entities) {
if(e.getClass() == zombie.getClass()) {
exists = true;
break;
}
}
if(!exists && zombie.pos.squareDistance(Main.player.pos) > 64) {
layer.spawnEntity(zombie); layer.spawnEntity(zombie);
}
} }
} }

View File

@ -119,21 +119,34 @@ public class LayerGenLavaCaves extends LayerGen
@Override @Override
public void spawnEntities(Layer layer, Random rand) public void spawnEntities(Layer layer, Random rand)
{ {
if(rand.nextDouble() > 0.98) if(rand.nextDouble() > 0.9)
{ {
Entity zombie = new EntityZombieArmored(new Vec2d( Entity zombie = new EntityZombieArmored(new Vec2d(
RandomHelpers.randrange(rand, (int)Main.player.pos.x - 128, (int)Main.player.pos.x + 128), RandomHelpers.randrange(rand, (int)Main.player.pos.x - 128, (int)Main.player.pos.x + 128),
RandomHelpers.randrange(rand, (int)Main.player.pos.y - 128, (int)Main.player.pos.y + 128))); RandomHelpers.randrange(rand, (int)Main.player.pos.y - 128, (int)Main.player.pos.y + 128)));
TileState tsb = layer.getBackTile(new Vec2i((int)zombie.pos.x, boolean exists = false;
(int)zombie.pos.y));
TileState tsf = layer.getFrontTile(new Vec2i((int)zombie.pos.x, for(Entity e : layer.getChunk(zombie.pos).entities) {
(int)zombie.pos.y)); if(e.getClass() == zombie.getClass()) {
if( exists = true;
tsb.tile == getTileDestroyed().tile && break;
tsf.tile == Tiles.VOID && }
zombie.pos.squareDistance(Main.player.pos) > 32) { }
layer.spawnEntity(zombie);
if(!exists)
{
TileState tsb = layer.getBackTile(new Vec2i((int)zombie.pos.x,
(int)zombie.pos.y));
TileState tsf = layer.getFrontTile(new Vec2i((int)zombie.pos.x,
(int)zombie.pos.y));
if(
tsb.tile == getTileDestroyed().tile &&
tsf.tile == Tiles.VOID &&
zombie.pos.squareDistance(Main.player.pos) > 32) {
layer.spawnEntity(zombie);
}
} }
} }
} }

View File

@ -2,16 +2,46 @@
in vec3 pPos; in vec3 pPos;
in vec3 pTexture; in vec3 pTexture;
in vec3 pLighting; in vec3 pLightMapPos;
out vec4 FragColor; out vec4 FragColor;
uniform sampler3D atlas; uniform sampler3D atlas;
uniform sampler2D lightmap;
uniform vec3 lighting_day_low;
uniform vec3 lighting_day_high;
uniform vec2 lightmap_offset;
uniform vec2 lightmap_size;
uniform vec2 tex_cut; uniform vec2 tex_cut;
uniform vec4 color; uniform vec4 color;
float map(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
vec3 mapVec(float x, float in_min, float in_max, vec3 out_min, vec3 out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
vec3 biggest(vec3 a, vec3 b) {
return vec3(a.x > b.x ? a.x : b.x,
a.y > b.y ? a.y : b.y,
a.z > b.z ? a.z : b.z);
}
void main() void main()
{ {
FragColor = texture(atlas, pTexture) * color * vec4(pLighting, 1); vec4 light = texture(lightmap, vec2(
map(pLightMapPos.x, lightmap_offset.x, lightmap_offset.x + lightmap_size.x, 0, 1),
map(pLightMapPos.z, lightmap_offset.y, lightmap_offset.y + lightmap_size.y, 0, 1)));
vec3 light_day = mapVec(light.r, 0, 1, lighting_day_low, lighting_day_high);
vec3 light_src = vec3(1, 1, 1) * (light.g - abs(pLightMapPos.y) * 0.1);
FragColor = texture(atlas, pTexture) * color * vec4(biggest(light_day, light_src), 1);
discard(FragColor.a == 0 || (pPos.x > tex_cut.y && tex_cut.x > 0.5)); discard(FragColor.a == 0 || (pPos.x > tex_cut.y && tex_cut.x > 0.5));
} }

View File

@ -6,37 +6,20 @@ layout (location = 2) in vec2 aTexY;
layout (location = 3) in vec3 aChunkOffset; layout (location = 3) in vec3 aChunkOffset;
layout (location = 4) in vec3 aFlags; layout (location = 4) in vec3 aFlags;
out vec3 pLightMapPos;
out vec3 pLighting; out vec3 pLighting;
out vec3 pTexture; out vec3 pTexture;
out vec3 pPos; out vec3 pPos;
uniform sampler2D lightmap;
uniform mat4 projection; uniform mat4 projection;
uniform mat4 model; uniform mat4 model;
uniform mat4 camera;
uniform mat4 rotated; uniform mat4 rotated;
uniform int time; uniform int time;
uniform vec3 lighting_day_low;
uniform vec3 lighting_day_high;
uniform vec2 lightmap_offset;
uniform vec2 lightmap_size;
float map(float x, float in_min, float in_max, float out_min, float out_max) { float map(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
} }
vec3 mapVec(float x, float in_min, float in_max, vec3 out_min, vec3 out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
vec3 biggest(vec3 a, vec3 b) {
return vec3(a.x > b.x ? a.x : b.x,
a.y > b.y ? a.y : b.y,
a.z > b.z ? a.z : b.z);
}
mat4 translate(vec3 vec) mat4 translate(vec3 vec)
{ {
mat4 result = mat4(1); mat4 result = mat4(1);
@ -48,6 +31,16 @@ mat4 translate(vec3 vec)
return result; return result;
} }
vec3 mapVec(float x, float in_min, float in_max, vec3 out_min, vec3 out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
vec3 biggest(vec3 a, vec3 b) {
return vec3(a.x > b.x ? a.x : b.x,
a.y > b.y ? a.y : b.y,
a.z > b.z ? a.z : b.z);
}
float getTexY() float getTexY()
{ {
float animate_count = aFlags.x; float animate_count = aFlags.x;
@ -74,17 +67,9 @@ void main()
vec4 pos = vec4(aPos, 1) * (mod(type, 2) == 1 ? do_rotation : no_rotation) * vec4 pos = vec4(aPos, 1) * (mod(type, 2) == 1 ? do_rotation : no_rotation) *
translate(aChunkOffset) * model; translate(aChunkOffset) * model;
gl_Position = pos * camera * projection; gl_Position = pos * projection;
pLightMapPos = pos.xyz;
pTexture = vec3(aTex.x, getTexY(), aTex.z); pTexture = vec3(aTex.x, getTexY(), aTex.z);
pPos = aPos; pPos = aPos;
vec4 light = texture(lightmap, vec2(
map(floor(pos.x), lightmap_offset.x, lightmap_offset.x + lightmap_size.x, 0, 1),
map(floor(pos.z), lightmap_offset.y, lightmap_offset.y + lightmap_size.y, 0, 1)));
vec3 light_day = mapVec(light.r, 0, 1, lighting_day_low, lighting_day_high);
vec3 light_src = vec3(1, 1, 1) * light.g;
pLighting = biggest(light_day, light_src);
} }

View File

@ -1,11 +1,12 @@
#version 330 #version 330
layout (location = 0) in vec2 aPos; layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTex;
out vec2 TexCoord; out vec2 TexCoord;
void main() void main()
{ {
gl_Position = vec4(aPos, 0, 1); gl_Position = vec4(aPos, 0, 1);
TexCoord = aPos; TexCoord = aTex;
} }

Binary file not shown.