Skip to content

Commit

Permalink
Update block selection
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Mar 24, 2024
1 parent cad9782 commit 77f58a0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void render(GLStateMgr gl, double partialTick) {
worldRenderer.compileChunks();
worldRenderer.renderChunks(gl);

final HitResult hitResult = worldRenderer.hitResult();
final HitResult hitResult = worldRenderer.selectBlock();
if (!hitResult.missed()) {
final AABBox box = hitResult.blockType().outlineShape().move(hitResult.x(), hitResult.y(), hitResult.z());
final float minX = (float) box.minX();
Expand All @@ -125,7 +125,7 @@ public void render(GLStateMgr gl, double partialTick) {
final float maxX = (float) box.maxX();
final float maxY = (float) box.maxY();
final float maxZ = (float) box.maxZ();
final float offset = 0.001f;
final float offset = 0.005f;
gl.setTextureBinding2D(0);
positionColorProgram.use(gl);
positionColorProgram.getUniform(GLProgram.UNIFORM_PROJECTION_VIEW_MATRIX).set(projectionViewMatrix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import io.github.xenfork.freeworld.core.math.AABBox;
import io.github.xenfork.freeworld.world.World;
import io.github.xenfork.freeworld.world.block.BlockType;
import io.github.xenfork.freeworld.world.chunk.Chunk;
import io.github.xenfork.freeworld.world.chunk.ChunkPos;
import org.jetbrains.annotations.NotNull;
import org.joml.*;

import java.lang.Math;
import java.lang.Runtime;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -41,9 +43,7 @@ public final class WorldRenderer implements GLResource {
private final FrustumRayBuilder frustumRayBuilder = new FrustumRayBuilder();
private final Vector3f frustumRayOrigin = new Vector3f();
private final Vector3f frustumRayDir = new Vector3f();
private final Vector2f frustumIntersectionResult = new Vector2f();
private final Vector2d blockIntersectionResult = new Vector2d();
private HitResult hitResult = new HitResult(null, 0, 0, 0, true);

public WorldRenderer(GameRenderer gameRenderer, World world) {
this.gameRenderer = gameRenderer;
Expand Down Expand Up @@ -87,14 +87,6 @@ public void compileChunks() {
}

public void renderChunks(GLStateMgr gl) {
final Matrix4f matrix = gameRenderer.projectionViewMatrix();
frustumIntersection.set(matrix);
frustumRayBuilder.set(matrix);
frustumRayBuilder.origin(frustumRayOrigin);
frustumRayBuilder.dir(0.5f, 0.5f, frustumRayDir);
frustumIntersectionResult.zero();
float nearestChunkDistance = Float.POSITIVE_INFINITY;
ClientChunk nearestChunk = null;
for (ClientChunk chunk : chunks) {
if (frustumIntersection.testAab(
chunk.fromX(),
Expand All @@ -105,37 +97,47 @@ public void renderChunks(GLStateMgr gl) {
chunk.toZ()
)) {
chunk.render(gl);
if (Intersectionf.intersectRayAab(
frustumRayOrigin.x(),
frustumRayOrigin.y(),
frustumRayOrigin.z(),
frustumRayDir.x(),
frustumRayDir.y(),
frustumRayDir.z(),
chunk.fromX(),
chunk.fromY(),
chunk.fromZ(),
chunk.toX(),
chunk.toY(),
chunk.toZ(),
frustumIntersectionResult
) && frustumIntersectionResult.x() < nearestChunkDistance) {
nearestChunkDistance = frustumIntersectionResult.x();
nearestChunk = chunk;
}
}
}
// TODO: 2024/3/24 squid233: This is buggy
if (nearestChunk != null) {
double nearestBlockDistance = Float.POSITIVE_INFINITY;
BlockType nearestBlock = null;
int nearestX = 0;
int nearestY = 0;
int nearestZ = 0;
for (int x = nearestChunk.fromX(); x < nearestChunk.toX(); x++) {
for (int y = nearestChunk.fromY(); y < nearestChunk.toY(); y++) {
for (int z = nearestChunk.fromZ(); z < nearestChunk.toZ(); z++) {
final BlockType blockType = nearestChunk.chunk().getBlockType(
}

public HitResult selectBlock() {
final Matrix4f matrix = gameRenderer.projectionViewMatrix();
frustumIntersection.set(matrix);
frustumRayBuilder.set(matrix);
frustumRayBuilder.origin(frustumRayOrigin);
frustumRayBuilder.dir(0.5f, 0.5f, frustumRayDir);
final float ox = frustumRayOrigin.x();
final float oy = frustumRayOrigin.y();
final float oz = frustumRayOrigin.z();

double nearestBlockDistance = Float.POSITIVE_INFINITY;
BlockType nearestBlock = null;
int nearestX = 0;
int nearestY = 0;
int nearestZ = 0;

final float radius = 5.0f;
final float radiusSquared = radius * radius;
final int x0 = Math.clamp((int) Math.floor(ox - radius), 0, world.width());
final int y0 = Math.clamp((int) Math.floor(oy - radius), 0, world.height());
final int z0 = Math.clamp((int) Math.floor(oz - radius), 0, world.depth());
final int x1 = Math.clamp((int) Math.ceil(ox + radius), 0, world.width());
final int y1 = Math.clamp((int) Math.ceil(oy + radius), 0, world.height());
final int z1 = Math.clamp((int) Math.ceil(oz + radius), 0, world.depth());
for (int x = x0; x <= x1; x++) {
final float xSquared = (x + 0.5f - ox) * (x + 0.5f - ox);
for (int y = y0; y <= y1; y++) {
final float ySquared = (y + 0.5f - oy) * (y + 0.5f - oy);
for (int z = z0; z <= z1; z++) {
final float zSquared = (z + 0.5f - oz) * (z + 0.5f - oz);
if ((xSquared + ySquared + zSquared) <= radiusSquared) {
final Chunk chunk = world.getChunk(
ChunkPos.absoluteToChunk(x),
ChunkPos.absoluteToChunk(y),
ChunkPos.absoluteToChunk(z)
);
final BlockType blockType = chunk.getBlockType(
ChunkPos.absoluteToRelative(x),
ChunkPos.absoluteToRelative(y),
ChunkPos.absoluteToRelative(z)
Expand All @@ -145,9 +147,9 @@ public void renderChunks(GLStateMgr gl) {
}
final AABBox box = blockType.outlineShape().move(x, y, z);
if (Intersectiond.intersectRayAab(
frustumRayOrigin.x(),
frustumRayOrigin.y(),
frustumRayOrigin.z(),
ox,
oy,
oz,
frustumRayDir.x(),
frustumRayDir.y(),
frustumRayDir.z(),
Expand All @@ -168,18 +170,15 @@ public void renderChunks(GLStateMgr gl) {
}
}
}
hitResult = new HitResult(nearestBlock, nearestX, nearestY, nearestZ, nearestBlock == null);
}

return new HitResult(nearestBlock, nearestX, nearestY, nearestZ, nearestBlock == null);
}

public VertexBuilderPool<DefaultVertexBuilder> vertexBuilderPool() {
return vertexBuilderPool;
}

public HitResult hitResult() {
return hitResult;
}

@Override
public void close(GLStateMgr gl) {
executor.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ public Chunk getChunk(int x, int y, int z) {
public Chunk createChunk(int x, int y, int z) {
return new Chunk(this, x, y, z);
}

public int width() {
return width;
}

public int height() {
return height;
}

public int depth() {
return depth;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public static int relativeToAbsolute(int chunkPos, int relativePos) {
public static int absoluteToRelative(int absolutePos) {
return Math.floorMod(absolutePos, Chunk.SIZE);
}

public static int absoluteToChunk(int absolutePos) {
return Math.floorDiv(absolutePos, Chunk.SIZE);
}
}

0 comments on commit 77f58a0

Please sign in to comment.