Search in sources :

Example 6 with CollisionBox

use of io.xol.chunkstories.api.physics.CollisionBox in project chunkstories by Hugobros3.

the class FarTerrainMeshRenderer method drawTerrainBits.

private int drawTerrainBits(RenderingInterface renderingContext, ReadyVoxelMeshesMask mask, Shader terrainShader) {
    // Starts asynch regeneration
    if (farTerrainUpdatesToTakeIntoAccount.get() > 0 && (System.currentTimeMillis() - this.lastTerrainUpdateTiming) > this.timeToWaitBetweenTerrainUpdates) {
        if (this.isTerrainUpdateRunning.compareAndSet(false, true))
            this.startAsynchSummaryRegeneration(renderingContext.getCamera());
    }
    // Setups stuff
    renderingContext.setCullingMode(CullingMode.COUNTERCLOCKWISE);
    renderingContext.setDepthTestMode(DepthTestMode.LESS_OR_EQUAL);
    // Camera is of position
    CameraInterface camera = renderingContext.getCamera();
    int camRX = (int) (camera.getCameraPosition().x() / 256);
    int camRZ = (int) (camera.getCameraPosition().z() / 256);
    int wrapRegionsDistance = world.getSizeInChunks() / 2;
    int worldSizeInRegions = world.getSizeInChunks() / 8;
    int cameraChunkX = (int) (camera.getCameraPosition().x() / 32);
    int cameraChunkZ = (int) (camera.getCameraPosition().z() / 32);
    // Update their displayed position to reflect where the camera is
    for (RegionMesh mesh : renderedRegions) {
        mesh.regionDisplayedX = mesh.regionSummary.getRegionX();
        mesh.regionDisplayedZ = mesh.regionSummary.getRegionZ();
        // We wrap the chunks if they are too far
        if (mesh.regionSummary.getRegionX() * 8 - cameraChunkX > wrapRegionsDistance)
            mesh.regionDisplayedX += -worldSizeInRegions;
        if (mesh.regionSummary.getRegionX() * 8 - cameraChunkX < -wrapRegionsDistance)
            mesh.regionDisplayedX += worldSizeInRegions;
        if (mesh.regionSummary.getRegionZ() * 8 - cameraChunkZ > wrapRegionsDistance)
            mesh.regionDisplayedZ += -worldSizeInRegions;
        if (mesh.regionSummary.getRegionZ() * 8 - cameraChunkZ < -wrapRegionsDistance)
            mesh.regionDisplayedZ += worldSizeInRegions;
    // System.out.println(mesh.regionDisplayedX + " : " + cameraChunkX);
    }
    // Sort to draw near first
    // µ-opt
    // List<FarTerrainBaker.RegionMesh> regionsMeshesToRenderSorted = new ArrayList<FarTerrainBaker.RegionMesh>(renderedRegions);
    regionsMeshesToRenderSorted.clear();
    regionsMeshesToRenderSorted.addAll(renderedRegions);
    regionsMeshesToRenderSorted.sort(new Comparator<FarTerrainBaker.RegionMesh>() {

        @Override
        public int compare(FarTerrainBaker.RegionMesh a, FarTerrainBaker.RegionMesh b) {
            int distanceA = Math.abs(a.regionDisplayedX - camRX) + Math.abs(a.regionDisplayedZ - camRZ);
            int distanceB = Math.abs(b.regionDisplayedX - camRX) + Math.abs(b.regionDisplayedZ - camRZ);
            return distanceA - distanceB;
        }
    });
    // µ-opt
    /*List<Integer> temp = new ArrayList<Integer>();
		List<Integer> temp2 = new ArrayList<Integer>();*/
    temp.clear();
    temp2.clear();
    int bitsDrew = 0;
    CollisionBox collisionBoxCheck = new CollisionBox(0, 0, 0, 0, 0, 0);
    for (FarTerrainBaker.RegionMesh regionMesh : regionsMeshesToRenderSorted) {
        // Frustrum checks (assuming maxHeight of 1024 blocks)
        // TODO do a simple max() and improve accuracy
        float height = 1024f;
        // Early-out
        if (!renderingContext.getCamera().isBoxInFrustrum(new CollisionBox(regionMesh.regionDisplayedX * 256, 0, regionMesh.regionDisplayedZ * 256, 256, height, 256)))
            continue;
        for (int i = 0; i < 8; i++) for (int j = 0; j < 8; j++) {
            int delta = regionMesh.regionSummary.max[i][j] - regionMesh.regionSummary.min[i][j];
            collisionBoxCheck.xpos = (regionMesh.regionDisplayedX * 8 + i) * 32;
            collisionBoxCheck.ypos = regionMesh.regionSummary.min[i][j];
            collisionBoxCheck.zpos = (regionMesh.regionDisplayedZ * 8 + j) * 32;
            collisionBoxCheck.xw = 32;
            collisionBoxCheck.h = delta + 1;
            collisionBoxCheck.zw = 32;
            if (renderingContext.getCamera().isBoxInFrustrum(collisionBoxCheck)) // if (renderingContext.getCamera().isBoxInFrustrum(new CollisionBox((regionMesh.regionDisplayedX * 8 + i) * 32, regionMesh.regionSummary.min[i][j], (regionMesh.regionDisplayedZ * 8 + j) * 32, 32, delta + 1, 32)))
            {
                if (mask != null) {
                    if (mask.shouldMaskSlab(regionMesh.regionDisplayedX * 8 + i, regionMesh.regionDisplayedZ * 8 + j, regionMesh.regionSummary.min[i][j], regionMesh.regionSummary.max[i][j]))
                        continue;
                }
                temp.add(regionMesh.vertexSectionsOffsets[i][j]);
                temp2.add(regionMesh.vertexSectionsSizes[i][j]);
            }
        }
        if (temp.size() == 0)
            continue;
        HeightmapImplementation regionSummaryData = regionMesh.regionSummary;
        // Skip unloaded regions immediately
        if (regionSummaryData.isUnloaded())
            continue;
        renderingContext.bindArrayTexture("heights", worldRenderer.getSummariesTexturesHolder().getHeightsArrayTexture());
        renderingContext.bindArrayTexture("topVoxels", worldRenderer.getSummariesTexturesHolder().getTopVoxelsArrayTexture());
        renderingContext.bindTexture1D("blocksTexturesSummary", getBlocksTexturesSummary());
        int index = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionSummaryData.getRegionX(), regionSummaryData.getRegionZ());
        if (index == -1) {
            // System.out.println("index == -1");
            continue;
        }
        // System.out.println("index:"+index);
        terrainShader.setUniform1i("arrayIndex", index);
        /*renderingContext.bindTexture2D("groundTexture", regionSummaryData.voxelTypesTexture);
			regionSummaryData.voxelTypesTexture.setTextureWrapping(false);
			regionSummaryData.voxelTypesTexture.setLinearFiltering(false);
			
			renderingContext.bindTexture2D("heightMap", regionSummaryData.heightsTexture);
			regionSummaryData.heightsTexture.setTextureWrapping(false);
			regionSummaryData.heightsTexture.setLinearFiltering(false);*/
        // Actual region position
        terrainShader.setUniform2f("regionPosition", regionSummaryData.getRegionX(), regionSummaryData.getRegionZ());
        // Displayed position
        terrainShader.setUniform2f("visualOffset", regionMesh.regionDisplayedX * 256, regionMesh.regionDisplayedZ * 256);
        // Checks this regionMesh instance has it's stuff uploaded already
        if (!regionMesh.verticesObject.isDataPresent())
            continue;
        // else
        // System.out.println("warning");
        int stride = 4 * 2 + 4 + 0 * 4;
        int vertices2draw = (int) (regionMesh.verticesObject.getVramUsage() / stride);
        renderingContext.bindAttribute("vertexIn", regionMesh.verticesObject.asAttributeSource(VertexFormat.SHORT, 3, stride, 0L));
        renderingContext.bindAttribute("normalIn", regionMesh.verticesObject.asAttributeSource(VertexFormat.UBYTE, 4, stride, 8L));
        bitsDrew += vertices2draw;
        int[] theStuff = new int[temp.size() * 2];
        for (int i = 0; i < temp.size(); i++) {
            theStuff[i * 2] = temp.get(i);
            theStuff[i * 2 + 1] = temp2.get(i);
        }
        temp.clear();
        temp2.clear();
        renderingContext.drawMany(Primitive.TRIANGLE, theStuff);
    }
    return bitsDrew;
}
Also used : RegionMesh(io.xol.chunkstories.renderer.terrain.FarTerrainBaker.RegionMesh) HeightmapImplementation(io.xol.chunkstories.world.summary.HeightmapImplementation) RegionMesh(io.xol.chunkstories.renderer.terrain.FarTerrainBaker.RegionMesh) CameraInterface(io.xol.chunkstories.api.rendering.CameraInterface) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox)

Example 7 with CollisionBox

use of io.xol.chunkstories.api.physics.CollisionBox in project chunkstories-api by Hugobros3.

the class EntityBase method getTranslatedBoundingBox.

@Override
public CollisionBox getTranslatedBoundingBox() {
    CollisionBox box = getBoundingBox();
    box.translate(getLocation());
    return box;
}
Also used : CollisionBox(io.xol.chunkstories.api.physics.CollisionBox)

Example 8 with CollisionBox

use of io.xol.chunkstories.api.physics.CollisionBox in project chunkstories by Hugobros3.

the class PhysicsWireframeDebugger method render.

public void render(RenderingInterface renderer) {
    Vector3dc cameraPosition = renderer.getCamera().getCameraPosition();
    // int id, data;
    int drawDebugDist = 6;
    // Iterate over nearby voxels
    for (int i = ((int) (double) cameraPosition.x()) - drawDebugDist; i <= ((int) (double) cameraPosition.x()) + drawDebugDist; i++) for (int j = ((int) (double) cameraPosition.y()) - drawDebugDist; j <= ((int) (double) cameraPosition.y()) + drawDebugDist; j++) for (int k = ((int) (double) cameraPosition.z()) - drawDebugDist; k <= ((int) (double) cameraPosition.z()) + drawDebugDist; k++) {
        // data = world.peekSimple(i, j, k);
        // id = VoxelFormat.id(data);
        CellData cell = world.peekSafely(i, j, k);
        // System.out.println(i+":"+j+":"+k);
        // System.out.println(cell.getX() + ":"+cell.getY()+":"+cell.getZ());
        CollisionBox[] tboxes = cell.getTranslatedCollisionBoxes();
        // System.out.println(tboxes.length);
        if (tboxes != null) {
            // Draw all their collision boxes
            for (CollisionBox box : tboxes) {
                if (cell.getVoxel().getDefinition().isSolid())
                    // Red if solid
                    FakeImmediateModeDebugRenderer.renderCollisionBox(box, new Vector4f(1, 0, 0, 1.0f));
                else
                    // Yellow otherwise
                    FakeImmediateModeDebugRenderer.renderCollisionBox(box, new Vector4f(1, 1, 0, 0.25f));
            }
        }
    }
    // Iterate over each entity
    Iterator<Entity> ie = world.getAllLoadedEntities();
    while (ie.hasNext()) {
        Entity e = ie.next();
        // Entities with hitboxes see all of those being drawn
        if (e instanceof EntityLiving) {
            EntityLiving eli = (EntityLiving) e;
            for (HitBox hitbox : eli.getHitBoxes()) {
                hitbox.draw(renderer);
            }
        }
        // Get the entity bounding box
        if (e.getTranslatedBoundingBox().lineIntersection(cameraPosition, new Vector3d(renderer.getCamera().getViewDirection())) != null)
            FakeImmediateModeDebugRenderer.renderCollisionBox(e.getTranslatedBoundingBox(), new Vector4f(0, 0, 0.5f, 1.0f));
        else
            FakeImmediateModeDebugRenderer.renderCollisionBox(e.getTranslatedBoundingBox(), new Vector4f(0, 1f, 1f, 1.0f));
        // And the collision box
        for (CollisionBox box : e.getCollisionBoxes()) {
            box.translate(e.getLocation());
            FakeImmediateModeDebugRenderer.renderCollisionBox(box, new Vector4f(0, 1, 0.5f, 1.0f));
        }
    }
}
Also used : Vector3dc(org.joml.Vector3dc) Entity(io.xol.chunkstories.api.entity.Entity) HitBox(io.xol.chunkstories.api.entity.EntityLiving.HitBox) Vector4f(org.joml.Vector4f) EntityLiving(io.xol.chunkstories.api.entity.EntityLiving) Vector3d(org.joml.Vector3d) CellData(io.xol.chunkstories.api.world.cell.CellData) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox)

Example 9 with CollisionBox

use of io.xol.chunkstories.api.physics.CollisionBox in project chunkstories by Hugobros3.

the class FarTerrainGSMeshRenderer method renderTerrain.

@Override
public void renderTerrain(RenderingInterface renderer, ReadyVoxelMeshesMask mask) {
    Shader terrainShader = renderer.useShader("terrain_blocky");
    renderer.setBlendMode(BlendMode.DISABLED);
    renderer.getCamera().setupShader(terrainShader);
    worldRenderer.getSkyRenderer().setupShader(terrainShader);
    terrainShader.setUniform1f("viewDistance", world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance"));
    Texture2D waterTexture = renderer.textures().getTexture("./textures/water/shallow.png");
    waterTexture.setLinearFiltering(true);
    waterTexture.setMipMapping(true);
    Texture2D waterTexture2 = renderer.textures().getTexture("./textures/water/deep.png");
    waterTexture2.setLinearFiltering(true);
    waterTexture2.setMipMapping(true);
    // renderer.bindCubemap("environmentCubemap", worldRenderer.renderBuffers.rbEnvironmentMap);
    renderer.bindTexture2D("blockLightmap", TexturesHandler.getTexture("./textures/environement/light.png"));
    Texture2D lightColors = TexturesHandler.getTexture("./textures/environement/lightcolors.png");
    renderer.bindTexture2D("lightColors", lightColors);
    renderer.bindTexture2D("waterNormalShallow", waterTexture);
    renderer.bindTexture2D("waterNormalDeep", waterTexture2);
    world.getGenerator().getEnvironment().setupShadowColors(renderer, terrainShader);
    renderer.bindTexture2D("vegetationColorTexture", world.getGenerator().getEnvironment().getGrassTexture(renderer));
    terrainShader.setUniform1f("mapSize", worldRenderer.getWorld().getSizeInChunks() * 32);
    terrainShader.setUniform1f("animationTimer", worldRenderer.getAnimationTimer());
    renderer.bindArrayTexture("heights", worldRenderer.getSummariesTexturesHolder().getHeightsArrayTexture());
    renderer.bindArrayTexture("topVoxels", worldRenderer.getSummariesTexturesHolder().getTopVoxelsArrayTexture());
    renderer.bindTexture1D("blocksTexturesSummary", colours.get());
    if (renderer.getClient().getInputsManager().getInputByName("wireframeFarTerrain").isPressed() && ClientLimitations.isDebugAllowed)
        renderer.setPolygonFillMode(PolygonFillMode.WIREFRAME);
    if (!renderer.getClient().getInputsManager().getInputByName("hideFarTerrain").isPressed() && ClientLimitations.isDebugAllowed) {
        renderer.setCullingMode(CullingMode.DISABLED);
        // renderer.setCullingMode(CullingMode.COUNTERCLOCKWISE);
        renderer.setDepthTestMode(DepthTestMode.LESS_OR_EQUAL);
        Player player = Client.getInstance().getPlayer();
        Location playerPosition = player.getLocation();
        if (playerPosition == null)
            // We won't do shit with that going on
            return;
        Vector2d playerCenter = new Vector2d(playerPosition.x, playerPosition.z);
        int chunkX = (int) Math.floor(playerPosition.x / 32.0);
        int chunkZ = (int) Math.floor(playerPosition.z / 32.0);
        int regionX = chunkX / 8;
        int regionZ = chunkZ / 8;
        int[] lodInstanceCount = new int[detailLevels.length];
        ByteBuffer[] lodByteBuffer = new ByteBuffer[detailLevels.length];
        // ByteBuffer summariesAttributes = MemoryUtil.memAlloc(9 * 9 * (4 + 2 * 4));
        // MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
        // int count = 0;
        double lodBias = -0.2;
        double lodExponent = 0.35;
        if (!world.getClient().getConfiguration().getBooleanOption("client.rendering.hqTerrain")) {
            lodBias = 0.3;
            lodExponent = 0.35;
        } else {
            lodBias = 0.0;
            lodExponent = 0.45;
        }
        if (mask == null) {
            lodExponent = 1.0;
            lodBias = 0.6;
        }
        Vector2d center = new Vector2d();
        for (int i = -4; i <= 4; i++) for (int j = -4; j <= 4; j++) {
            int regionI = regionX + i;
            int regionJ = regionZ + j;
            int index = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ);
            if (index == -1)
                continue;
            // For the extra row of triangles to mask the seams
            int index10 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ);
            int index01 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ + 1);
            int index11 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ + 1);
            if (index10 == -1 || index01 == -1 || index11 == -1)
                continue;
            HeightmapImplementation sum = (HeightmapImplementation) world.getRegionsSummariesHolder().getHeightmap(regionI, regionJ);
            // Early out
            if (sum == null || !sum.isLoaded())
                continue;
            if (!renderer.getCamera().isBoxInFrustrum(new CollisionBox(regionI * 256, 0, regionJ * 256, 256, 1024, /*+ sum.getHeightMipmapped(0, 0, 9)*/
            256)))
                continue;
            for (int l = 0; l < 8; l++) for (int m = 0; m < 8; m++) {
                if (mask != null) {
                    if (mask.shouldMaskSlab(regionI * 8 + l, regionJ * 8 + m, sum.min[l][m], sum.max[l][m]))
                        continue;
                }
                center.set(regionI * 256 + l * 32 + 16, regionJ * 256 + m * 32 + 16);
                int lod = detailLevels.length - (int) ((lodBias + Math.pow(Math.min(1024, center.distance(playerCenter)) / 1024, lodExponent)) * detailLevels.length);
                // System.out.println(center.distance(playerCenter));
                if (lod <= 0)
                    lod = 0;
                if (lod >= detailLevels.length)
                    lod = detailLevels.length - 1;
                // lod = 2;
                // lod = 2;
                // System.out.println("lod:"+lod);
                // lod = Math.random() > 0.5 ? 1 : 2;
                ByteBuffer summariesAttributes = lodByteBuffer[lod];
                if (summariesAttributes == null) {
                    summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
                    lodByteBuffer[lod] = summariesAttributes;
                }
                summariesAttributes.putFloat(regionI * 256 + l * 32);
                summariesAttributes.putFloat(regionJ * 256 + m * 32);
                summariesAttributes.put((byte) index);
                summariesAttributes.put((byte) index10);
                summariesAttributes.put((byte) index01);
                summariesAttributes.put((byte) index11);
                lodInstanceCount[lod]++;
            // Always add both so lod 1 is drew under
            /*if(lod != 0) {
								lod = 0;
								
								summariesAttributes = lodByteBuffer[lod];
								
								if(summariesAttributes == null) {
									summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
									lodByteBuffer[lod] = summariesAttributes;
								}
								
								summariesAttributes.putFloat(regionI * 256 + l * 32);
								summariesAttributes.putFloat(regionJ * 256 + m * 32);
								summariesAttributes.put((byte)index);
								summariesAttributes.put((byte)index10);
								summariesAttributes.put((byte)index01);
								summariesAttributes.put((byte)index11);
								lodInstanceCount[lod]++;
							}*/
            }
        }
        // for(int lod = 0; lod < detailLevels.length; lod++) {
        for (int lod = detailLevels.length - 1; lod >= 0; lod--) {
            // Check if anything was supposed to be drew at this lod
            ByteBuffer summariesAttributes = lodByteBuffer[lod];
            if (summariesAttributes == null)
                continue;
            if (lod < 0) {
                MemoryUtil.memFree(summariesAttributes);
                continue;
            }
            // Flip buffer, box it for autodeletion, upload it
            summariesAttributes.flip();
            MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
            gridAttributes.uploadData(auto_free_summariesAttributes);
            terrainShader.setUniform1i("lodLevel", lod);
            terrainShader.setUniform1f("textureLodLevel", lod - 5);
            terrainShader.setUniform1i("maskPresence", mask == null ? 0 : 1);
            renderer.bindAttribute("vertexIn", grids[lod].asAttributeSource(VertexFormat.FLOAT, 3, 0, 0L));
            renderer.bindAttribute("displacementIn", gridAttributes.asAttributeSource(VertexFormat.FLOAT, 2, (4 + 2 * 4), 0L, 1));
            renderer.bindAttribute("indexIn", gridAttributes.asIntegerAttributeSource(VertexFormat.BYTE, 4, (4 + 2 * 4), 8L, 1));
            renderer.draw(Primitive.POINT, 0, (detailLevels[lod] + 1) * (detailLevels[lod] + 1) * 1, lodInstanceCount[lod]);
        }
    }
    renderer.setPolygonFillMode(PolygonFillMode.FILL);
}
Also used : Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Player(io.xol.chunkstories.api.player.Player) Shader(io.xol.chunkstories.api.rendering.shader.Shader) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) ByteBuffer(java.nio.ByteBuffer) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) HeightmapImplementation(io.xol.chunkstories.world.summary.HeightmapImplementation) Vector2d(org.joml.Vector2d) Location(io.xol.chunkstories.api.Location) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox)

Example 10 with CollisionBox

use of io.xol.chunkstories.api.physics.CollisionBox in project chunkstories by Hugobros3.

the class FarTerrainNoMeshRenderer method renderTerrain.

@Override
public void renderTerrain(RenderingInterface renderer, ReadyVoxelMeshesMask mask) {
    Shader terrainShader = renderer.useShader("terrain");
    renderer.setBlendMode(BlendMode.DISABLED);
    renderer.getCamera().setupShader(terrainShader);
    worldRenderer.getSkyRenderer().setupShader(terrainShader);
    terrainShader.setUniform1f("viewDistance", world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance"));
    Texture2D waterTexture = renderer.textures().getTexture("./textures/water/shallow.png");
    waterTexture.setLinearFiltering(true);
    waterTexture.setMipMapping(true);
    // renderer.bindCubemap("environmentCubemap", worldRenderer.renderBuffers.rbEnvironmentMap);
    renderer.bindTexture2D("blockLightmap", TexturesHandler.getTexture("./textures/environement/light.png"));
    Texture2D lightColors = TexturesHandler.getTexture("./textures/environement/lightcolors.png");
    renderer.bindTexture2D("lightColors", lightColors);
    renderer.bindTexture2D("normalTexture", waterTexture);
    world.getGenerator().getEnvironment().setupShadowColors(renderer, terrainShader);
    // worldRenderer.setupShadowColors(terrainShader);
    renderer.bindTexture2D("vegetationColorTexture", world.getGenerator().getEnvironment().getGrassTexture(renderer));
    terrainShader.setUniform1f("mapSize", worldRenderer.getWorld().getSizeInChunks() * 32);
    renderer.bindArrayTexture("heights", worldRenderer.getSummariesTexturesHolder().getHeightsArrayTexture());
    renderer.bindArrayTexture("topVoxels", worldRenderer.getSummariesTexturesHolder().getTopVoxelsArrayTexture());
    renderer.bindTexture1D("blocksTexturesSummary", colours.get());
    if (renderer.getClient().getInputsManager().getInputByName("wireframeFarTerrain").isPressed() && ClientLimitations.isDebugAllowed)
        renderer.setPolygonFillMode(PolygonFillMode.WIREFRAME);
    if (!renderer.getClient().getInputsManager().getInputByName("hideFarTerrain").isPressed() && ClientLimitations.isDebugAllowed) {
        renderer.setCullingMode(CullingMode.COUNTERCLOCKWISE);
        renderer.setDepthTestMode(DepthTestMode.LESS_OR_EQUAL);
        Player player = Client.getInstance().getPlayer();
        Location playerPosition = player.getLocation();
        if (playerPosition == null)
            // We won't do shit with that going on
            return;
        Vector2d playerCenter = new Vector2d(playerPosition.x, playerPosition.z);
        int chunkX = (int) Math.floor(playerPosition.x / 32.0);
        int chunkZ = (int) Math.floor(playerPosition.z / 32.0);
        int regionX = chunkX / 8;
        int regionZ = chunkZ / 8;
        int[] lodInstanceCount = new int[detailLevels.length];
        ByteBuffer[] lodByteBuffer = new ByteBuffer[detailLevels.length];
        // ByteBuffer summariesAttributes = MemoryUtil.memAlloc(9 * 9 * (4 + 2 * 4));
        // MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
        // int count = 0;
        double lodBias = -0.0;
        double lodExponent = 0.35;
        if (!world.getClient().getConfiguration().getBooleanOption("client.rendering.hqTerrain")) {
            lodBias = 0.4;
            lodExponent = 0.35;
        }
        if (mask == null) {
            lodExponent = 1.0;
            lodBias = 0.6;
        }
        Vector2d center = new Vector2d();
        for (int i = -4; i <= 4; i++) for (int j = -4; j <= 4; j++) {
            int regionI = regionX + i;
            int regionJ = regionZ + j;
            int index = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ);
            if (index == -1)
                continue;
            // For the extra row of triangles to mask the seams
            int index10 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ);
            int index01 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI, regionJ + 1);
            int index11 = worldRenderer.getSummariesTexturesHolder().getSummaryIndex(regionI + 1, regionJ + 1);
            if (i < 4 && index10 == -1)
                continue;
            if (j < 4 && index01 == -1)
                continue;
            if (i < 4 && j < 4 && index11 == -1)
                continue;
            HeightmapImplementation sum = (HeightmapImplementation) world.getRegionsSummariesHolder().getHeightmap(regionI, regionJ);
            // Early out
            if (sum == null || !sum.isLoaded())
                continue;
            if (!renderer.getCamera().isBoxInFrustrum(new CollisionBox(regionI * 256, 0, regionJ * 256, 256, 1024, /*+ sum.getHeightMipmapped(0, 0, 9)*/
            256)))
                continue;
            for (int l = 0; l < 8; l++) for (int m = 0; m < 8; m++) {
                if (mask != null) {
                    if (mask.shouldMaskSlab(regionI * 8 + l, regionJ * 8 + m, sum.min[l][m], sum.max[l][m]))
                        continue;
                }
                center.set(regionI * 256 + l * 32 + 16, regionJ * 256 + m * 32 + 16);
                int lod = detailLevels.length - (int) ((lodBias + Math.pow(Math.min(1024, center.distance(playerCenter)) / 1024, lodExponent)) * detailLevels.length);
                // System.out.println(center.distance(playerCenter));
                if (lod <= 0)
                    lod = 0;
                if (lod >= detailLevels.length)
                    lod = detailLevels.length - 1;
                // lod = 2;
                // System.out.println("lod:"+lod);
                // lod = Math.random() > 0.5 ? 1 : 2;
                ByteBuffer summariesAttributes = lodByteBuffer[lod];
                if (summariesAttributes == null) {
                    summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
                    lodByteBuffer[lod] = summariesAttributes;
                }
                summariesAttributes.putFloat(regionI * 256 + l * 32);
                summariesAttributes.putFloat(regionJ * 256 + m * 32);
                summariesAttributes.put((byte) index);
                summariesAttributes.put((byte) index10);
                summariesAttributes.put((byte) index01);
                summariesAttributes.put((byte) index11);
                lodInstanceCount[lod]++;
                // Always add both so lod 1 is drew under
                if (lod != 0) {
                    lod = 0;
                    summariesAttributes = lodByteBuffer[lod];
                    if (summariesAttributes == null) {
                        summariesAttributes = MemoryUtil.memAlloc(9 * 9 * 8 * 8 * (4 + 2 * 4));
                        lodByteBuffer[lod] = summariesAttributes;
                    }
                    summariesAttributes.putFloat(regionI * 256 + l * 32);
                    summariesAttributes.putFloat(regionJ * 256 + m * 32);
                    summariesAttributes.put((byte) index);
                    summariesAttributes.put((byte) index10);
                    summariesAttributes.put((byte) index01);
                    summariesAttributes.put((byte) index11);
                    lodInstanceCount[lod]++;
                }
            }
        }
        // for(int lod = 0; lod < detailLevels.length; lod++) {
        for (int lod = detailLevels.length - 1; lod >= 0; lod--) {
            // Check if anything was supposed to be drew at this lod
            ByteBuffer summariesAttributes = lodByteBuffer[lod];
            if (summariesAttributes == null)
                continue;
            if (lod < 0) {
                MemoryUtil.memFree(summariesAttributes);
                continue;
            }
            // Flip buffer, box it for autodeletion, upload it
            summariesAttributes.flip();
            MemFreeByteBuffer auto_free_summariesAttributes = new MemFreeByteBuffer(summariesAttributes);
            gridAttributes.uploadData(auto_free_summariesAttributes);
            terrainShader.setUniform1i("lodLevel", lod);
            terrainShader.setUniform1f("textureLodLevel", lod - 5);
            terrainShader.setUniform1i("maskPresence", mask == null ? 0 : 1);
            renderer.bindAttribute("vertexIn", grids[lod].asAttributeSource(VertexFormat.FLOAT, 3, 0, 0L));
            renderer.bindAttribute("displacementIn", gridAttributes.asAttributeSource(VertexFormat.FLOAT, 2, (4 + 2 * 4), 0L, 1));
            renderer.bindAttribute("indexIn", gridAttributes.asIntegerAttributeSource(VertexFormat.BYTE, 4, (4 + 2 * 4), 8L, 1));
            renderer.draw(Primitive.TRIANGLE, 0, (detailLevels[lod] + 1) * (detailLevels[lod] + 1) * 2 * 3, lodInstanceCount[lod]);
        }
    }
    renderer.setPolygonFillMode(PolygonFillMode.FILL);
}
Also used : Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Player(io.xol.chunkstories.api.player.Player) Shader(io.xol.chunkstories.api.rendering.shader.Shader) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) ByteBuffer(java.nio.ByteBuffer) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) HeightmapImplementation(io.xol.chunkstories.world.summary.HeightmapImplementation) Vector2d(org.joml.Vector2d) Location(io.xol.chunkstories.api.Location) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox)

Aggregations

CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)14 Location (io.xol.chunkstories.api.Location)5 CellData (io.xol.chunkstories.api.world.cell.CellData)5 Vector3d (org.joml.Vector3d)5 Voxel (io.xol.chunkstories.api.voxel.Voxel)4 Vector3dc (org.joml.Vector3dc)4 Entity (io.xol.chunkstories.api.entity.Entity)3 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)3 HitBox (io.xol.chunkstories.api.entity.EntityLiving.HitBox)3 Shader (io.xol.chunkstories.api.rendering.shader.Shader)3 HeightmapImplementation (io.xol.chunkstories.world.summary.HeightmapImplementation)3 ByteBuffer (java.nio.ByteBuffer)3 Player (io.xol.chunkstories.api.player.Player)2 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)2 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)2 MemFreeByteBuffer (io.xol.chunkstories.client.util.MemFreeByteBuffer)2 EntityPlayer (io.xol.chunkstories.core.entity.EntityPlayer)2 Vector2d (org.joml.Vector2d)2 Vector4f (org.joml.Vector4f)2 EntityComponentRotation (io.xol.chunkstories.api.entity.components.EntityComponentRotation)1