Skip to content

Commit

Permalink
Added a chunk debug render task to the pipeline that is enabled with f8
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Dec 30, 2023
1 parent 8e867f0 commit fb1c3b9
Show file tree
Hide file tree
Showing 16 changed files with 250 additions and 39 deletions.
9 changes: 5 additions & 4 deletions src/main/java/dev/hilligans/ourcraft/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ public static void main(String[] args) throws IOException {
gameInstance.side = Side.CLIENT;
gameInstance.loadContent();

Thread serverThread = null;
if(argumentContainer.getBoolean("--integratedServer", false)) {
try {
Thread thread = new Thread(() -> ServerMain.server(gameInstance, argumentContainer));
thread.setName("Server-Thread");
thread.setDaemon(true);
thread.start();
serverThread = new Thread(() -> ServerMain.server(gameInstance, argumentContainer));
serverThread.setName("Server-Thread");
serverThread.setDaemon(true);
serverThread.start();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/dev/hilligans/ourcraft/Ourcraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,10 @@ public static void registerCoreExtensions(CoreExtensionView view) {
view.registerGraphicsEngine(new OpenGLEngine());
view.registerGraphicsEngine(new FixedFunctionGLEngine());

view.registerRenderPipelines(new RenderPipeline("world_pipeline"));

view.registerRenderTarget(new RenderTarget("solid_world_renderer", "ourcraft:world_pipeline")
.setPipelineState(new PipelineState().setDepth(true)));
view.registerRenderTarget(new RenderTarget("entity_renderer", "ourcraft:world_pipeline").afterTarget("solid_world_renderer", "ourcraft")
.setPipelineState(new PipelineState().setDepth(true)));
view.registerRenderTarget(new RenderTarget("particle_renderer", "ourcraft:world_pipeline").afterTarget("entity_renderer", "ourcraft")
.setPipelineState(new PipelineState().setDepth(true)));
view.registerRenderTarget(new RenderTarget("translucent_world_renderer", "ourcraft:world_pipeline").afterTarget("particle_renderer", "ourcraft")
.setPipelineState(new PipelineState().setDepth(true)));
view.registerRenderTarget(new RenderTarget("gui_renderer", "ourcraft:world_pipeline").afterTarget("translucent_world_renderer", "ourcraft")
.setPipelineState(new PipelineState()));

view.registerRenderPipelines(new RenderPipeline("new_world_pipeline"));

view.registerRenderTarget(new RenderTarget("new_solid_world_renderer", "ourcraft:new_world_pipeline")
view.registerRenderTarget(new RenderTarget("debug_world_renderer", "ourcraft:new_world_pipeline")
.setPipelineState(new PipelineState().setDepth(false)));
view.registerRenderTarget(new RenderTarget("new_solid_world_renderer", "ourcraft:new_world_pipeline").afterTarget("debug_world_renderer", "ourcraft")
.setPipelineState(new PipelineState().setDepth(true)));
view.registerRenderTarget(new RenderTarget("entity_renderer", "ourcraft:new_world_pipeline").afterTarget("new_solid_world_renderer", "ourcraft")
.setPipelineState(new PipelineState().setDepth(true)));
Expand Down Expand Up @@ -166,15 +154,17 @@ public static void registerCoreExtensions(CoreExtensionView view) {
view.registerRenderTask(new WorldTransparentRenderTask());
view.registerRenderTask(new ChatRenderTask());
view.registerRenderTask(new SplitWindowRenderTask());
view.registerRenderTask(new ChunkDebugRenderTask());


view.registerVertexFormat(position_texture_color, position_color_texture, position_texture_globalColor, position_texture, position_texture_animatedWrap_shortenedColor, position_color);
view.registerVertexFormat(position2_texture_color);
view.registerVertexFormat(position2_texture_color, position_color_lines);

// view.registerShader(new ShaderSource("world_shader","ourcraft:position_texture_color", "Shaders/WorldVertexShader.glsl","Shaders/WorldFragmentShader.glsl"));
view.registerShader(new ShaderSource("world_shader", "ourcraft:position_color_texture", "Shaders/WorldVertexShader.glsl", "Shaders/WorldFragmentShader.glsl").withUniform("transform", "4fv").withUniform("color", "4f"));
view.registerShader(new ShaderSource("position_color_shader", "ourcraft:position_color", "Shaders/WorldVertexColorShader.glsl", "Shaders/WorldFragmentShader.glsl").withUniform("transform", "4fv").withUniform("color", "4f"));
view.registerShader(new ShaderSource("position_texture", "ourcraft:position_texture", "Shaders/PositionTexture.vsh", "Shaders/PositionTexture.fsh").withUniform("transform", "4fv").withUniform("color", "4f"));
view.registerShader(new ShaderSource("position_color_lines_shader", "ourcraft:position_color_lines", "Shaders/WorldVertexColorShader.glsl", "Shaders/WorldFragmentColorShader.glsl").withUniform("transform", "4fv").withUniform("color", "4f"));
view.registerShader(new ShaderSource("nk_shader", "position2_texture_color", "Shaders/NkVertexShader.glsl", "Shaders/NkFragmentShader.glsl").withUniform("transform", "4fv"));

view.registerInputHandlerProviders(new ControllerHandlerProvider(), new KeyPressHandlerProvider(), new MouseHandlerProvider());
Expand Down Expand Up @@ -263,6 +253,14 @@ public void press(RenderWindow renderWindow, float strength) {
}
});

modContent.registerKeybinds(new Input("ourcraft:key_press_handler::" + GLFW_KEY_F8){
@Override
public void press(RenderWindow renderWindow, float strength) {
super.press(renderWindow, strength);
renderWindow.getClient().getPlayerData().debugChunkRendering = !renderWindow.getClient().getPlayerData().debugChunkRendering;
}
});

modContent.registerKeybinds(new RepeatingInput("ourcraft:key_press_handler::" + GLFW_KEY_W,
(window, strength) -> window.getCamera().moveForward(5f * strength)).onlyWithPipelines("ourcraft:new_world_pipeline"));

Expand Down Expand Up @@ -369,6 +367,10 @@ public static DoubleBuffer getMousePos(long window) {
.addPart("position", VertexFormat.FLOAT, 3)
.addPart("color", VertexFormat.FLOAT, 4);

public static final VertexFormat position_color_lines = new VertexFormat("ourcraft", "position_color_lines", VertexFormat.LINES)
.addPart("position", VertexFormat.FLOAT, 3)
.addPart("color", VertexFormat.FLOAT, 4);

public static final VertexFormat position_RGB = new VertexFormat("ourcraft", "position_RGB", VertexFormat.TRIANGLES)
.addPart("position", VertexFormat.FLOAT, 3)
.addPart("color", VertexFormat.FLOAT, 3);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/dev/hilligans/ourcraft/ServerMain.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hilligans.ourcraft;

import dev.hilligans.ourcraft.server.IServer;
import dev.hilligans.ourcraft.server.MultiPlayerServer;
import dev.hilligans.ourcraft.util.ArgumentContainer;
import dev.hilligans.ourcraft.util.Profiler;
Expand Down Expand Up @@ -33,7 +34,7 @@ public static void main(String[] args) {
server(gameInstance, new ArgumentContainer(args));
}

public static void server(GameInstance gameInstance, ArgumentContainer argumentContainer) {
public static IServer server(GameInstance gameInstance, ArgumentContainer argumentContainer) {
ServerMain.argumentContainer = argumentContainer;

//ServerWorld world = new ServerWorld(gameInstance);
Expand All @@ -55,6 +56,7 @@ public static void server(GameInstance gameInstance, ArgumentContainer argumentC
server.addWorld(world1);
System.out.println("starting server");
server.startServer("25588");
return server;
}

public static void newServer(GameInstance gameInstance) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ public long getRenderTime() {
return renderTime;
}

public ClientPlayerData getPlayerData() {
return playerData;
}

@Override
public Client getClient() {
return this;
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/client/ClientPlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import dev.hilligans.ourcraft.item.ItemStack;
import dev.hilligans.ourcraft.util.Settings;

import java.util.HashMap;

public class ClientPlayerData {

public HashMap<String, Object> arbDataMap = new HashMap<>();
public Inventory inventory = new Inventory(Settings.playerInventorySize);
public ItemStack heldStack = ItemStack.emptyStack();
public Container openContainer;
Expand All @@ -23,4 +26,18 @@ public class ClientPlayerData {
public boolean valid_account = false;


//DEBUG
public boolean debugChunkRendering = false;

public Object get(String key) {
return arbDataMap.get(key);
}

public <T> T get(String key, T clazz) {
return (T) arbDataMap.get(key);
}

public <T> T set(String key, T newValue) {
return (T) arbDataMap.put(key, newValue);
}
}
4 changes: 4 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/client/MatrixStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public void translate(float x, float y, float z) {
matrix4f.translate(x,y,z);
}

public void translate(double x, double y, double z) {
matrix4f.translate((float) x, (float) y, (float) z);
}

public void translateMinusOffset(float x, float y, float z) {
// matrix4f.translate(x - (Camera.playerChunkPos.x << 4),y,z - (Camera.playerChunkPos.z << 4));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ default MatrixStack getScreenStack() {
}

default Vector3d getCameraPos() {
Vector3d vector3d = getPosition();
double x = vector3d.x - ((int)vector3d.x >> 5);
double y = vector3d.y - ((int)vector3d.y >> 5);
double z = vector3d.z - ((int)vector3d.z >> 5);

return new Vector3d(x,y,z);
return getPosition().get(new Vector3d());
}

MatrixStack getMatrixStack(int W, int H, int x, int y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public long createMesh(GraphicsContext graphicsContext, VertexMesh mesh) {
mesh.destroy();

meshReferences.put(VAO, mesh);
meshData.put(VAO, new Tuple<>(GL_TRIANGLES, mesh.elementSize == 4 ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT));
meshData.put(VAO, new Tuple<>(mesh.vertexFormat.primitiveType, mesh.elementSize == 4 ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT));
vertexArrayObjects.put(VAO, ((long)VBO << 32) | (long)EBO);
if(trackingResourceAllocations) {
vertexArrayAllocationTracker.put(VAO, new Exception());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,90 @@
package dev.hilligans.ourcraft.client.rendering.graphics.tasks;

import dev.hilligans.ourcraft.client.Camera;
import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.MatrixStack;
import dev.hilligans.ourcraft.client.rendering.graphics.*;
import dev.hilligans.ourcraft.client.rendering.graphics.api.GraphicsContext;
import dev.hilligans.ourcraft.client.rendering.graphics.api.ICamera;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IDefaultEngineImpl;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine;
import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.client.rendering.newrenderer.PrimitiveBuilder;
import dev.hilligans.ourcraft.data.other.BoundingBox;
import dev.hilligans.ourcraft.data.primitives.FloatList;
import dev.hilligans.ourcraft.util.Loops;
import dev.hilligans.ourcraft.util.TriConsumer;
import dev.hilligans.ourcraft.world.newworldsystem.IChunk;
import dev.hilligans.ourcraft.world.newworldsystem.IWorld;
import org.joml.Vector3d;
import org.joml.Vector3f;
import org.joml.Vector3i;
import org.joml.Vector4f;

import java.awt.*;
import java.util.function.Consumer;

public class ChunkDebugRenderTask extends RenderTaskSource {

public ShaderSource shaderSource;

public ChunkDebugRenderTask() {
super("", "");
super("chunk_debug_render_task", "ourcraft:debug_world_renderer");
}

@Override
public RenderTask getDefaultTask() {
return new RenderTask() {
@Override
public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphicsEngine<?, ?, ?> engine, Client client, MatrixStack worldStack, MatrixStack screenStack, float delta) {
if (client.getPlayerData().debugChunkRendering) {
int renderYDist = client.renderYDistance;

Vector3d pos = window.getCamera().getCameraPos();
IWorld world = client.getWorld();

int chunkWidth = world.getChunkWidth();
int chunkHeight = world.getChunkHeight();

float minX = 0;
float minY = 0;
float minZ = 0;
float maxX = chunkWidth;
float maxY = chunkHeight;
float maxZ = chunkWidth;

PrimitiveBuilder meshBuilder = new PrimitiveBuilder(shaderSource);
IDefaultEngineImpl<?, ?> impl = engine.getDefaultImpl();
meshBuilder.addBoundingBox(new BoundingBox(minX, minY, minZ, maxX, maxY, maxZ), floatList -> floatList.add(1, 1, 1, 1));

long mesh = impl.createMesh(graphicsContext, meshBuilder.toVertexMesh());

Vector3i playerChunkPos = new Vector3i(Math.floorDiv((int)pos.x, chunkWidth), Math.floorDiv((int)pos.y, chunkHeight), Math.floorDiv((int)pos.z, chunkWidth));

int rDist = Math.min(client.renderDistance, 4);
int rYDist = Math.min(client.renderYDistance, 3);

Loops.furthestToNearestLoop(rDist, rYDist, rDist, playerChunkPos, (chunkX, chunkY, chunkZ) -> {
IChunk chunk = getChunk(chunkX, chunkY, chunkZ, client.getWorld());
worldStack.push();
worldStack.translate((chunkX) * chunkWidth, (chunkY) * chunkHeight, (chunkZ) * chunkWidth);
if (chunk != null) {
if(chunkX-playerChunkPos.x == 0 && chunkY-playerChunkPos.y == 0 && chunkZ-playerChunkPos.z == 0) {
worldStack.setColor(0, 255, 0, 255);
} else {
worldStack.setColor(255, 255, 0, 255);
}
impl.uploadMatrix(graphicsContext, worldStack, shaderSource);
impl.drawMesh(graphicsContext, worldStack, mesh, 0, meshBuilder.indices.size());
}
worldStack.pop();
});
impl.destroyMesh(graphicsContext, mesh);
}
}

IChunk getChunk(int chunkX, int chunkY, int chunkZ, IWorld world) {
return world.getChunk((long) chunkX * world.getChunkContainer().getChunkWidth(), (long) chunkY * world.getChunkContainer().getChunkHeight(), (long) chunkZ * world.getChunkContainer().getChunkWidth());
}

@Override
Expand All @@ -33,6 +97,6 @@ public PipelineState getPipelineState() {
@Override
public void load(GameInstance gameInstance) {
super.load(gameInstance);
shaderSource = gameInstance.SHADERS.get("ourcraft:position_color_shader");
shaderSource = gameInstance.SHADERS.getExcept("ourcraft:position_color_lines_shader");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
int chunkWidth = world.getChunkContainer().getChunkWidth();
int chunkHeight = world.getChunkContainer().getChunkHeight();
int renderYDist = client.renderYDistance;
Vector3i playerChunkPos = new Vector3i((int) pos.x / chunkWidth, (int) pos.y / chunkHeight, (int) pos.z / chunkWidth);
Vector3i playerChunkPos = new Vector3i(Math.floorDiv((int)pos.x, chunkWidth), Math.floorDiv((int)pos.y, chunkHeight), Math.floorDiv((int)pos.z, chunkWidth));
if (client.renderWorld) {
engine.getDefaultImpl().bindPipeline(graphicsContext, shaderSource.program);
engine.getDefaultImpl().bindTexture(graphicsContext, textAtlas.texture);

for (int x = 0; x < client.renderDistance; x++) {
for (int y = 0; y < renderYDist; y++) {
for (int z = 0; z < client.renderDistance; z++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
int chunkHeight = world.getChunkContainer().getChunkHeight();
int renderYDist = client.renderYDistance;
//renderYDist = 1;
Vector3i playerChunkPos = new Vector3i((int) pos.x / chunkWidth, (int) pos.y / chunkHeight, (int) pos.z / chunkWidth);
Vector3i playerChunkPos = new Vector3i(Math.floorDiv((int)pos.x, chunkWidth), Math.floorDiv((int)pos.y, chunkHeight), Math.floorDiv((int)pos.z, chunkWidth));
if (client.renderWorld) {
engine.getDefaultImpl().bindPipeline(graphicsContext, shaderSource.program);
engine.getDefaultImpl().bindTexture(graphicsContext, textAtlas.texture);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package dev.hilligans.ourcraft.client.rendering.newrenderer;

import dev.hilligans.ourcraft.client.rendering.graphics.ShaderSource;
import dev.hilligans.ourcraft.client.rendering.graphics.VertexFormat;
import dev.hilligans.ourcraft.client.rendering.VertexMesh;
import dev.hilligans.ourcraft.data.other.BoundingBox;
import dev.hilligans.ourcraft.data.primitives.FloatList;
import dev.hilligans.ourcraft.data.primitives.IntList;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Vector3f;

import java.util.function.Consumer;

import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
Expand All @@ -27,6 +31,10 @@ public PrimitiveBuilder(VertexFormat vertexFormat) {
this.vertexFormat = vertexFormat;
}

public PrimitiveBuilder(ShaderSource shaderSource) {
this(shaderSource.vertexFormat);
}

public PrimitiveBuilder setVertexFormat(VertexFormat vertexFormat) {
this.vertexFormat = vertexFormat;
return this;
Expand Down Expand Up @@ -58,13 +66,18 @@ public void add(float[] vertices, int[] indices) {
size++;
}

public void add(int[] indices) {
public void add(int... indices) {
int count = sizeVal;
for(int index : indices) {
this.indices.add(index + count);
}
}

public int addIndex(float... vals) {
vertices.add(vals);
return this.size++;
}

public int sizeVal = 0;

public void addQuad(float... vertices) {
Expand All @@ -90,6 +103,18 @@ public void buildQuad(float minX, float minY, float z, float minTexX, float minT
maxX,maxY,z,maxTexX,maxTexY);
}

public void addBoundingBox(BoundingBox boundingBox, Consumer<FloatList> extraData) {
float[] vertices = new float[]{boundingBox.minX, boundingBox.minY, boundingBox.minZ, boundingBox.maxX, boundingBox.minY, boundingBox.minZ, boundingBox.maxX, boundingBox.minY, boundingBox.maxZ, boundingBox.minX, boundingBox.minY, boundingBox.maxZ, boundingBox.minX, boundingBox.maxY, boundingBox.minZ, boundingBox.maxX, boundingBox.maxY, boundingBox.minZ, boundingBox.maxX, boundingBox.maxY, boundingBox.maxZ, boundingBox.minX, boundingBox.maxY, boundingBox.maxZ,};
int s = this.size;
this.size += 12;
for(int x = 0; x < vertices.length; x+=3) {
this.vertices.add(vertices[x], vertices[x+1], vertices[x+2]);
extraData.accept(this.vertices);
}
this.indices.add(s+0,s+1,s+1,s+2,s+2,s+3,s+3,s+0,s+0,s+4,s+1,s+5,s+2,s+6,s+3,s+7,s+4,s+5,s+5,s+6,s+6,s+7,s+7,s+4);
//int[] indices = new int[]{0,1,1,2,2,3,3,0,0,4,1,5,2,6,3,7,4,5,5,6,6,7,7,4};
}

public void addQuadIndices() {
int count = getVerticesCount();
this.indices.add(count,count + 1, count + 2,count + 3, count + 2, count + 1);
Expand Down
Loading

0 comments on commit fb1c3b9

Please sign in to comment.