Started working on the worker for lighting

This commit is contained in:
jsrobson10 2020-06-21 18:48:01 +10:00
parent 0098d22927
commit 878410abbf
4 changed files with 126 additions and 37 deletions

View File

@ -42,6 +42,7 @@ public class Main
public static AudioEngine audio; public static AudioEngine audio;
public static Random rand = new Random(); public static Random rand = new Random();
public static Menu menu; public static Menu menu;
public static Worker worker;
public static boolean game_paused = false; public static boolean game_paused = false;
public static int tickrate = 10; public static int tickrate = 10;
@ -61,7 +62,8 @@ public class Main
public static void main(String[] args) throws IOException public static void main(String[] args) throws IOException
{ {
Worker worker = new Worker(); worker = new Worker();
worker.start();
// Initialize cheats, settings, and environment // Initialize cheats, settings, and environment
Environment.init(args); Environment.init(args);

View File

@ -2,12 +2,16 @@ package projectzombie.display.lighting;
import static org.lwjgl.opengl.GL11.GL_RGBA; import static org.lwjgl.opengl.GL11.GL_RGBA;
import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE; import static org.lwjgl.opengl.GL11.GL_UNSIGNED_BYTE;
import static org.lwjgl.opengl.GL12.GL_TEXTURE_3D;
import static org.lwjgl.opengl.GL12.glTexImage3D;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import org.lwjgl.opengl.GL33; import org.lwjgl.opengl.GL33;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import gl_engine.MathHelpers; import gl_engine.MathHelpers;
import gl_engine.range.Range4i; import gl_engine.range.Range4i;
import gl_engine.vec.Vec2i; import gl_engine.vec.Vec2i;
@ -56,6 +60,66 @@ public class TileLighting
return; return;
} }
int size = (Chunk.RENDER_DISTANCE * 2 + 1) * 16;
float[] lights = new float[size * size * 3];
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(player.pos.x / 16),
cy + MathHelpers.floor(player.pos.y / 16));
Chunk chunk = layer.chunks.get(cpos);
for(int x=0;x<16;x++) {
for(int y=0;y<16;y++)
{
Vec2i tpos = new Vec2i(x, y);
int tid = tpos.getId(Chunk.CHUNK_SIZE);
TileState ft = chunk.getFrontTile(tid);
TileState bt = chunk.getBackTile(tid);
float transparency = (float)MathHelpers.biggest(
ft.tile.getLightDissipation(ft),
bt.tile.getLightDissipation(bt));
float lightSrc = (float)MathHelpers.biggest(
ft.tile.getLightLevel(ft, tpos),
bt.tile.getLightLevel(bt, tpos));
float lightSun = ft.tile.passNaturalLight && bt.tile.passNaturalLight ? 1 : 0;
int id = ((cx * 16 + x + Chunk.RENDER_DISTANCE * 16) +
(cy * 16 + y + Chunk.RENDER_DISTANCE * 16) * size) * 3;
lights[id+0] = lightSun;
lights[id+1] = lightSrc;
lights[id+2] = transparency;
}
}
}
}
Main.worker.processLighting(lights, size, size);
}
public static void updateLighting(BdfObject bdf)
{
BdfNamedList nl = bdf.getNamedList();
float[] light = nl.get("light").getFloatArray();
int width = nl.get("w").getInteger();
int height = nl.get("h").getInteger();
float[] pixels = new float[width*height*3];
for(int i=0;i<width*height;i++) {
pixels[i*3+0] = light[i*2+0];
pixels[i*3+1] = light[i*2+1];
}
GL33.glBindTexture(GL33.GL_TEXTURE_2D, lightmap);
GL33.glTexImage2D(GL33.GL_TEXTURE_2D, 0, GL33.GL_RGB, width, height, 0, GL33.GL_RGB, GL33.GL_FLOAT, light);
} }
} }

View File

@ -11,8 +11,9 @@ import bdf.data.BdfDatabase;
import bdf.types.BdfIndent; import bdf.types.BdfIndent;
import bdf.types.BdfNamedList; import bdf.types.BdfNamedList;
import bdf.types.BdfObject; import bdf.types.BdfObject;
import projectzombie.display.lighting.TileLighting;
public class Worker public class Worker extends Thread
{ {
ProcessBuilder pb; ProcessBuilder pb;
Process process; Process process;
@ -24,56 +25,78 @@ public class Worker
{ {
pb = new ProcessBuilder("java", "-jar", "worker.jar"); pb = new ProcessBuilder("java", "-jar", "worker.jar");
pb.redirectError(Redirect.INHERIT); pb.redirectError(Redirect.INHERIT);
pb.redirectOutput(Redirect.PIPE);
pb.redirectInput(Redirect.PIPE);
process = pb.start(); process = pb.start();
in = process.getInputStream(); in = process.getInputStream();
out = process.getOutputStream(); out = process.getOutputStream();
}
byte[] lights = new byte[] {
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50, public void processLighting(float[] lights, int width, int height)
(byte)0, (byte)0, (byte)50, {
(byte)0, (byte)0, (byte)50,
(byte)255, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)0, (byte)50,
(byte)0, (byte)255, (byte)50,
};
BdfObject bdf = new BdfObject(); BdfObject bdf = new BdfObject();
BdfNamedList nl = bdf.getNamedList(); BdfNamedList nl = bdf.getNamedList();
nl.set("task", BdfObject.withString("light")); nl.set("task", BdfObject.withString("light"));
nl.set("light", BdfObject.withByteArray(lights)); nl.set("light", BdfObject.withFloatArray(lights));
nl.set("w", BdfObject.withInteger(4)); nl.set("w", BdfObject.withInteger(width));
nl.set("h", BdfObject.withInteger(4)); nl.set("h", BdfObject.withInteger(height));
byte[] data = bdf.serialize().getBytes(); byte[] data = bdf.serialize().getBytes();
ByteBuffer size_buff = ByteBuffer.allocate(4); ByteBuffer size_buff = ByteBuffer.allocate(4);
size_buff.putInt(0, data.length); size_buff.putInt(0, data.length);
out.write(size_buff.array()); try
out.write(data); {
out.write('\n'); out.write(size_buff.array());
out.write(data);
out.write('\n');
out.flush();
}
in.read(size_buff.array()); catch(IOException e)
data = new byte[size_buff.getInt(0)]; {
in.read(data);
in.read(new byte[1]); }
}
@Override
public void run() {
super.run();
bdf = new BdfObject(new BdfDatabase(data)); try
System.out.println(bdf.serializeHumanReadable(new BdfIndent(" ", "\n"))); {
while(true)
{
ByteBuffer size_buff = ByteBuffer.allocate(4);
byte[] data;
in.read(size_buff.array());
data = new byte[size_buff.getInt(0)];
in.read(data);
in.read(new byte[1]);
BdfObject bdf = new BdfObject(new BdfDatabase(data));
BdfNamedList nl = bdf.getNamedList();
switch(nl.get("task").getString())
{
case "light":
TileLighting.updateLighting(bdf);
break;
}
System.out.println("Hello");
}
}
catch(IOException e)
{
}
} }
} }

Binary file not shown.