Skip to content

Commit

Permalink
Fixed world not being meshed properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Dec 30, 2023
1 parent fb1c3b9 commit e12d9f1
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 157 deletions.
13 changes: 12 additions & 1 deletion src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import dev.hilligans.ourcraft.tag.CompoundNBTTag;
import dev.hilligans.ourcraft.util.ArgumentContainer;
import dev.hilligans.ourcraft.util.Logger;
import dev.hilligans.ourcraft.util.ThreadContext;
import dev.hilligans.ourcraft.world.newworldsystem.ClientCubicWorld;
import dev.hilligans.ourcraft.world.newworldsystem.CubicWorld;
import dev.hilligans.ourcraft.world.newworldsystem.IWorld;
import dev.hilligans.ourcraft.save.WorldLoader;
Expand Down Expand Up @@ -92,7 +94,7 @@ public class Client implements IClientPacketHandler {

public Client(GameInstance gameInstance, ArgumentContainer argumentContainer) {
this.gameInstance = gameInstance;
this.newClientWorld = new CubicWorld(gameInstance, 0,"", 64);
this.newClientWorld = new ClientCubicWorld(gameInstance, 0,"", 64);
logger = gameInstance.LOGGER.withKey("client");
graphicsEngine = gameInstance.GRAPHICS_ENGINES.get("ourcraft:openglEngine");
((OpenGLEngine)graphicsEngine).client = this;
Expand Down Expand Up @@ -163,6 +165,15 @@ public void startClient() {
cleanUp();
}

public void tick(ThreadContext threadContext) {
try(var $0 = threadContext.getSection().startSection("tick_world")) {
newClientWorld.tick();
}
try(var $0 = threadContext.getSection().startSection("process_packets")) {
network.processPackets();
}
}

public RenderWindow rWindow;

public void cleanUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ default Runnable createRenderLoop(GameInstance gameInstance, RenderWindow w) {
break out;
}
try (var $9 = section.startSection(window.getWindowName())) {
try (var $2 = section.startSection("tick")) {
window.getClient().tick(graphicsContext);
}
try (var $2 = section.startSection("render")) {
render(window, graphicsContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public RenderTask getDefaultTask() {

@Override
public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphicsEngine<?, ?, ?> engine, Client client, MatrixStack worldStack, MatrixStack screenStack, float delta) {
int a = 0;

IWorld world = client.newClientWorld;
//engine.getDefaultImpl().setState(window, graphicsContext, new PipelineState().setDepth(true));
Vector3d pos = window.camera.getSavedPosition();
Expand Down Expand Up @@ -118,69 +116,36 @@ IChunk getChunk(int chunkX, int chunkY, int chunkZ, IWorld world) {

public static int rebuildDistance = 10;



void drawChunk(RenderWindow window, GraphicsContext graphicsContext, Client client, IGraphicsEngine<?, ?,?> engine, MatrixStack matrixStack, Vector3i playerChunkPos, IWorld world, int x, int y, int z, int chunkWidth, int chunkHeight) {
MeshHolder meshHolder = getMesh(x, y, z);
for (Tuple<IChunk, PrimitiveBuilder> tuple : primitiveBuilders) {
asyncedChunks.remove(((tuple.getTypeA().getX()) << 32) | (tuple.getTypeA().getZ() & 0xffffffffL));
tuple.typeB.setVertexFormat(shaderSource.vertexFormat);
int meshID = (int) window.getGraphicsEngine().getDefaultImpl().createMesh(graphicsContext, tuple.typeB.toVertexMesh());
meshes.setChunk(tuple.getTypeA().getX(), tuple.getTypeA().getY(), tuple.getTypeA().getZ(), new MeshHolder().set(meshID, tuple.getTypeB().indices.size()));
primitiveBuilders.remove(tuple);
}
if (meshHolder != null) {
if (meshHolder.id != -1 && meshHolder.length != 0) {
matrixStack.updateFrustum();
if (matrixStack.frustumIntersection.testAab((x) * chunkWidth, (y) * chunkHeight, (z) * chunkWidth, (x + 1) * chunkWidth, (y + 1) * chunkHeight, (z + 1) * chunkWidth)) {
matrixStack.push();
matrixStack.translate(x * chunkWidth, y * chunkHeight, z * chunkWidth);

engine.getDefaultImpl().uploadMatrix(graphicsContext, matrixStack, shaderSource);
try(var $ = graphicsContext.getSection().startSection("submitChunkDrawCall")) {
engine.getDefaultImpl().drawMesh(graphicsContext, matrixStack, meshHolder.getId(), meshHolder.index, meshHolder.length);
}
matrixStack.pop();
IChunk chunk = getChunk(x, y, z, world);
if (chunk != null) {
MeshHolder meshHolder = getMesh(x, y, z);
if (chunk.isDirty() || meshHolder == null) {
if ((Math.abs(x - playerChunkPos.x) < rebuildDistance && Math.abs(y - playerChunkPos.y) < rebuildDistance && Math.abs(z - playerChunkPos.z) < rebuildDistance)) {
buildMesh(window, graphicsContext, chunk);
}
}
} else {
IChunk chunk = getChunk(x, y, z, world);
if (chunk != null) {
if (chunk.isDirty()) {
if ( getChunk(x + 1, y, z, world) != null &&
getChunk(x - 1, y, z, world) != null &&
getChunk(x, y + 1, z, world) != null &&
getChunk(x, y - 1, z, world) != null &&
getChunk(x, y, z + 1, world) != null &&
getChunk(x, y, z - 1, world) != null) {
if ((Math.abs(x - playerChunkPos.x) < rebuildDistance && Math.abs(y - playerChunkPos.y) < rebuildDistance && Math.abs(z - playerChunkPos.z) < rebuildDistance)) {
buildMesh(window, graphicsContext, chunk);
//System.out.println(chunk.getX() + " " + chunk.getY() + " " + chunk.getZ());
//System.out.println(chunk.getBlockState1(0,0,0).getBlock().getName());
// chunk.setDirty(false);
if (meshHolder != null) {
if (meshHolder.id != -1 && meshHolder.length != 0) {
matrixStack.updateFrustum();
if (matrixStack.frustumIntersection.testAab((x) * chunkWidth, (y) * chunkHeight, (z) * chunkWidth, (x + 1) * chunkWidth, (y + 1) * chunkHeight, (z + 1) * chunkWidth)) {
matrixStack.push();
matrixStack.translate(x * chunkWidth, y * chunkHeight, z * chunkWidth);

engine.getDefaultImpl().uploadMatrix(graphicsContext, matrixStack, shaderSource);
try (var $ = graphicsContext.getSection().startSection("submitChunkDrawCall")) {
engine.getDefaultImpl().drawMesh(graphicsContext, matrixStack, meshHolder.getId(), meshHolder.index, meshHolder.length);
}
matrixStack.pop();
}
/* if (!asyncedChunks.getOrDefault(((long) x << 32) ^ (int) (z & 0xffffffffL), false)) {
if (getChunk(x + 1, y, z, world) != null &&
getChunk(x - 1, y, z, world) != null &&
getChunk(x, y, z + 1, world) != null &&
getChunk(x, y, z - 1, world) != null) {
if ((x < rebuildDistance && y < rebuildDistance && z < rebuildDistance)) {
buildMesh(window, graphicsContext, chunk);
} else {
if (putChunk(x, z)) {
chunkBuilder.submit(() -> {
PrimitiveBuilder primitiveBuilder = getPrimitiveBuilder(chunk);
primitiveBuilders.add(new Tuple<>(chunk, primitiveBuilder));
});
}
}
chunk.setDirty(false);
}
}
}
*/
}
}
}
Expand All @@ -189,43 +154,6 @@ void drawChunk(RenderWindow window, GraphicsContext graphicsContext, Client clie
public ConcurrentLinkedQueue<Tuple<IChunk, PrimitiveBuilder>> primitiveBuilders = new ConcurrentLinkedQueue<>();
Long2BooleanOpenHashMap asyncedChunks = new Long2BooleanOpenHashMap();

boolean putChunk(int chunkX, int chunkZ) {
if (!asyncedChunks.getOrDefault((((long) chunkX) << 32) | (chunkZ & 0xffffffffL), false)) {
asyncedChunks.put((((long)chunkX) << 32) | (chunkZ & 0xffffffffL), true);
return true;
}
return false;
}

/*
public PrimitiveBuilder getPrimitiveBuilder(IChunk chunk) {
PrimitiveBuilder primitiveBuilder = new PrimitiveBuilder(shaderSource.vertexFormat);
for(int x = 0; x < chunk.getWidth(); x++) {
for(int y = chunk.getHeight() - 1; y >= 0; y--) {
for(int z = 0; z < chunk.getWidth(); z++) {
IBlockState block = chunk.getBlockState1(x,y,z);
if(block.getBlock() != Blocks.AIR && !block.getBlock().blockProperties.translucent) {
for (int a = 0; a < 6; a++) {
BlockPos p = new BlockPos(x, y, z).add(Block.getBlockPos(block.getBlock().getSide(block, a)));
IBlockState newState;
if (!p.inRange(0, 0, 0, 16, 255, 16)) {
newState = chunk.getWorld().getBlockState(p.add(chunk.getBlockX(), chunk.getBlockY(), chunk.getBlockZ()));
} else {
newState = chunk.getBlockState1(new BlockPos(x, y, z).add(Block.getBlockPos(block.getBlock().getSide(block, a))));
}
if (newState.getBlock().blockProperties.transparent && (Settings.renderSameTransparent || block.getBlock() != newState.getBlock()) || block.getBlock().blockProperties.alwaysRender) {
block.getBlock().addVertices(primitiveBuilder, a, 1f, block, new BlockPos(x + chunk.getX(), y + chunk.getY(), z + chunk.getY()), x, z);
}
}
}
}
}
}
return primitiveBuilder;
}
*/

public PrimitiveBuilder getPrimitiveBuilder(IChunk chunk, PrimitiveBuilder primitiveBuilder) {
BlockPos p = new BlockPos(0, 0, 0);
primitiveBuilder = (primitiveBuilder == null ? new PrimitiveBuilder(shaderSource.vertexFormat) : primitiveBuilder);
Expand Down Expand Up @@ -255,47 +183,15 @@ public PrimitiveBuilder getPrimitiveBuilder(IChunk chunk, PrimitiveBuilder primi

public PrimitiveBuilder primitiveBuilder = null;
public void buildMesh(RenderWindow window, GraphicsContext graphicsContext, IChunk chunk) {
long start = System.currentTimeMillis();
primitiveBuilder = getPrimitiveBuilder(chunk, primitiveBuilder);
primitiveBuilder.setVertexFormat(shaderSource.vertexFormat);
int meshID = (int) window.getGraphicsEngine().getDefaultImpl().createMesh(graphicsContext, primitiveBuilder.toVertexMesh());
meshes.setChunk(chunk.getX(),chunk.getY(),chunk.getZ(),new MeshHolder().set(meshID,primitiveBuilder.indices.size()));
primitiveBuilder.size = 0;
primitiveBuilder.vertices.size = 0;
primitiveBuilder.indices.size = 0;
long end = System.currentTimeMillis();
//System.out.println("Time to build:" + (end - start));
}

/*
public IPrimitiveBuilder getPrimitiveBuilder1(IChunk chunk) {
BufferPrimitiveBuilder primitiveBuilder = new BufferPrimitiveBuilder(128, 128).setVertexFormat(shaderSource.vertexFormat);
for(int x = 0; x < chunk.getWidth(); x++) {
for(int y = chunk.getHeight() - 1; y >= 0; y--) {
for(int z = 0; z < chunk.getWidth(); z++) {
IBlockState block = chunk.getBlockState1(x,y,z);
if(block.getBlock() != Blocks.AIR && !block.getBlock().blockProperties.translucent) {
for (int a = 0; a < 6; a++) {
BlockPos p = new BlockPos(x, y, z).add(Block.getBlockPos(block.getBlock().getSide(block, a)));
IBlockState newState;
if (!p.inRange(0, 0, 0, 16, 255, 16)) {
newState = chunk.getWorld().getBlockState(p.add(chunk.getBlockX(), chunk.getBlockY(), chunk.getBlockZ()));
} else {
newState = chunk.getBlockState1(new BlockPos(x, y, z).add(Block.getBlockPos(block.getBlock().getSide(block, a))));
}
if (newState.getBlock().blockProperties.transparent && (Settings.renderSameTransparent || block.getBlock() != newState.getBlock()) || block.getBlock().blockProperties.alwaysRender) {
block.getBlock().addVertices(primitiveBuilder, a, 1f, block, new BlockPos(x + chunk.getX(), y + chunk.getY(), z + chunk.getY()), x, y, z);
}
}
}
}
}
}
return primitiveBuilder;
}
*/

@Override
public void cleanup(GameInstance gameInstance, IGraphicsEngine<?, ?, ?> graphicsEngine, GraphicsContext graphicsContext) {
super.cleanup(gameInstance, graphicsEngine, graphicsContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,40 +122,39 @@ IChunk getChunk(int chunkX, int chunkY, int chunkZ, IWorld world) {
public static int rebuildDistance = 4;

void drawChunk(RenderWindow window, GraphicsContext graphicsContext, Client client, IGraphicsEngine<?, ?,?> engine, MatrixStack matrixStack, Vector3i playerChunkPos, IWorld world, int x, int y, int z, int chunkWidth, int chunkHeight) {
MeshHolder meshHolder = getMesh(x, y, z);
for (Tuple<IChunk, PrimitiveBuilder> tuple : primitiveBuilders) {
asyncedChunks.remove(((tuple.getTypeA().getX()) << 32) | (tuple.getTypeA().getZ() & 0xffffffffL));
tuple.typeB.setVertexFormat(shaderSource.vertexFormat);
int meshID = (int) window.getGraphicsEngine().getDefaultImpl().createMesh(graphicsContext, tuple.typeB.toVertexMesh());
meshes.setChunk(tuple.getTypeA().getX(), tuple.getTypeA().getY(), tuple.getTypeA().getZ(), new MeshHolder().set(meshID, tuple.getTypeB().indices.size()));
primitiveBuilders.remove(tuple);
}
if (meshHolder != null) {
if (meshHolder.id != -1 && meshHolder.length != 0) {
matrixStack.updateFrustum();
if (matrixStack.frustumIntersection.testAab((x) * chunkWidth, (y) * chunkHeight, (z) * chunkWidth, (x + 1) * chunkWidth, (y + 1) * chunkHeight, (z + 1) * chunkWidth)) {
matrixStack.push();
matrixStack.translate(x * chunkWidth, y * chunkHeight, z * chunkWidth);
IDefaultEngineImpl<?,?> impl = engine.getDefaultImpl();
impl.uploadMatrix(graphicsContext, matrixStack, shaderSource);
impl.drawMesh(graphicsContext, matrixStack, meshHolder.getId(), meshHolder.index, meshHolder.length);
matrixStack.pop();
IChunk chunk = getChunk(x, y, z, world);
if (chunk != null) {
MeshHolder meshHolder = getMesh(x, y, z);
if(chunk.isDirty() || meshHolder == null) {
/*if (getChunk(x + 1, y, z, world) != null &&
getChunk(x - 1, y, z, world) != null &&
getChunk(x, y + 1, z, world) != null &&
getChunk(x, y - 1, z, world) != null &&
getChunk(x, y, z + 1, world) != null &&
getChunk(x, y, z - 1, world) != null) {
*/
if ((Math.abs(x - playerChunkPos.x) < rebuildDistance && Math.abs(y - playerChunkPos.y) < rebuildDistance && Math.abs(z - playerChunkPos.z) < rebuildDistance)) {
buildMesh(window, graphicsContext, chunk);
}
}
} else {
IChunk chunk = getChunk(x, y, z, world);
if (chunk != null) {
if (chunk.isDirty()) {
if (getChunk(x + 1, y, z, world) != null &&
getChunk(x - 1, y, z, world) != null &&
getChunk(x, y + 1, z, world) != null &&
getChunk(x, y - 1, z, world) != null &&
getChunk(x, y, z + 1, world) != null &&
getChunk(x, y, z - 1, world) != null) {
if ((Math.abs(x - playerChunkPos.x) < rebuildDistance && Math.abs(y - playerChunkPos.y) < rebuildDistance && Math.abs(z - playerChunkPos.z) < rebuildDistance)) {
buildMesh(window, graphicsContext, chunk);
chunk.setDirty(false);
}
if (meshHolder != null) {
if (meshHolder.id != -1 && meshHolder.length != 0) {
matrixStack.updateFrustum();
if (matrixStack.frustumIntersection.testAab((x) * chunkWidth, (y) * chunkHeight, (z) * chunkWidth, (x + 1) * chunkWidth, (y + 1) * chunkHeight, (z + 1) * chunkWidth)) {
matrixStack.push();
matrixStack.translate(x * chunkWidth, y * chunkHeight, z * chunkWidth);
IDefaultEngineImpl<?,?> impl = engine.getDefaultImpl();
impl.uploadMatrix(graphicsContext, matrixStack, shaderSource);
impl.drawMesh(graphicsContext, matrixStack, meshHolder.getId(), meshHolder.index, meshHolder.length);
matrixStack.pop();
}
}
}
Expand Down
Loading

0 comments on commit e12d9f1

Please sign in to comment.