Finish adding simple frustum culling

This commit is contained in:
James Seibel
2021-08-21 18:54:20 -05:00
parent ca57c15d46
commit 8d5c8bf7b8
3 changed files with 18 additions and 27 deletions
@@ -62,15 +62,16 @@ public class RegionPos
z = Math.floorDiv(pos.z, LodUtil.REGION_WIDTH_IN_CHUNKS);
}
/** Returns the ChunkPos at the center of this region */
public ChunkPos chunkPos()
{
return new ChunkPos(x * LodUtil.REGION_WIDTH_IN_CHUNKS, z * LodUtil.REGION_WIDTH_IN_CHUNKS);
return new ChunkPos((x * LodUtil.REGION_WIDTH_IN_CHUNKS) + LodUtil.REGION_WIDTH_IN_CHUNKS/2, (z * LodUtil.REGION_WIDTH_IN_CHUNKS) + LodUtil.REGION_WIDTH_IN_CHUNKS/2);
}
/** Returns the BlockPos at the center of this region */
public BlockPos blockPos()
{
return chunkPos().getWorldPosition();
return chunkPos().getWorldPosition().offset(LodUtil.CHUNK_WIDTH/2, 0, LodUtil.CHUNK_WIDTH/2);
}
@@ -319,21 +319,18 @@ public class LodRenderer
if (vbos != null)
{
int rendered = 0;
int skipped = 0;
Vector3d cameraDir = mc.cameraEntity.getLookAngle();
Vector3d cameraDir = mc.cameraEntity.getLookAngle().normalize();
// used to determine what type of fog to render
int halfWidth = vbos.length/2;
int quarterWidth = vbos.length/4;
for (int i = 0; i < vbos.length; i++)
for (int i = 0; i < vbos.length; i++)
{
for (int j = 0; j < vbos.length; j++)
{
RegionPos vboPos = new RegionPos(i + lodDim.getCenterX() - lodDim.getWidth()/2, j + lodDim.getCenterZ() - lodDim.getWidth()/2);
if (RenderUtil.isRegionInViewFrustum(player.blockPosition(), cameraDir, vboPos.blockPos()))
if (RenderUtil.isRegionInViewFrustum(mc.cameraEntity.blockPosition(), cameraDir, vboPos.blockPos()))
{
if ((i > halfWidth - quarterWidth && i < halfWidth + quarterWidth) && (j > halfWidth - quarterWidth && j < halfWidth + quarterWidth))
setupFog(fogSettings.near.distance, fogSettings.near.quality);
@@ -342,16 +339,9 @@ public class LodRenderer
sendLodsToGpuAndDraw(vbos[i][j], modelViewMatrix);
rendered++;
}
else
{
skipped++;
}
}
}
ClientProxy.LOGGER.info(rendered + " - " + skipped);
}
@@ -389,7 +379,6 @@ public class LodRenderer
}
/**
* This is where the actual drawing happens.
*
@@ -110,7 +110,7 @@ public class RenderUtil
/**
* Returns true if one of the regions 4 corners is in front
* Returns true if one of the region's 4 corners is in front
* of the camera.
*/
public static boolean isRegionInViewFrustum(BlockPos playerBlockPos, Vector3d cameraDir, BlockPos vboCenterPos)
@@ -124,17 +124,17 @@ public class RenderUtil
int halfRegionWidth = LodUtil.REGION_WIDTH / 2;
Vector3d vboSeVec = new Vector3d(vboCenterVec.x + halfRegionWidth, 0, vboCenterVec.z + halfRegionWidth).normalize();
Vector3d vboSwVec = new Vector3d(vboCenterVec.x - halfRegionWidth, 0, vboCenterVec.z + halfRegionWidth).normalize();
Vector3d vboNwVec = new Vector3d(vboCenterVec.x - halfRegionWidth, 0, vboCenterVec.z - halfRegionWidth).normalize();
Vector3d vboNeVec = new Vector3d(vboCenterVec.x + halfRegionWidth, 0, vboCenterVec.z - halfRegionWidth).normalize();
// calculate the 4 corners
Vector3d vboSeVec = new Vector3d(vboCenterVec.x + halfRegionWidth, vboCenterVec.y, vboCenterVec.z + halfRegionWidth);//.normalize();
Vector3d vboSwVec = new Vector3d(vboCenterVec.x - halfRegionWidth, vboCenterVec.y, vboCenterVec.z + halfRegionWidth);//.normalize();
Vector3d vboNwVec = new Vector3d(vboCenterVec.x - halfRegionWidth, vboCenterVec.y, vboCenterVec.z - halfRegionWidth);//.normalize();
Vector3d vboNeVec = new Vector3d(vboCenterVec.x + halfRegionWidth, vboCenterVec.y, vboCenterVec.z - halfRegionWidth);//.normalize();
// if any corner is visible, this region should be rendered
return isNormalizedVectorInViewFrustum(vboSeVec, cameraDir) ||
isNormalizedVectorInViewFrustum(vboSwVec, cameraDir) ||
isNormalizedVectorInViewFrustum(vboNwVec, cameraDir) ||
isNormalizedVectorInViewFrustum(vboNeVec, cameraDir);
// return isNormalizedVectorInViewFrustum(vboCenterVec.normalize(), cameraDir);
}
/**
@@ -143,8 +143,9 @@ public class RenderUtil
*/
private static boolean isNormalizedVectorInViewFrustum(Vector3d objectVector, Vector3d cameraDir)
{
// take the dot product
double dot = objectVector.dot(cameraDir);
return dot > 0;
// the -0.1 is to offer a slight buffer so we are
// more likely to render LODs and thus, hopefully prevent
// flickering or odd disappearences
return objectVector.dot(cameraDir) > -0.1;
}
}