Search in sources :

Example 1 with MemFreeByteBuffer

use of io.xol.chunkstories.client.util.MemFreeByteBuffer in project chunkstories by Hugobros3.

the class FarTerrainGSMeshRenderer method generateGrid.

VertexBufferGL generateGrid(int gridSubdivisions) {
    VertexBufferGL grid = new VertexBufferGL();
    ByteBuffer bb = MemoryUtil.memAlloc((gridSubdivisions + 1) * (gridSubdivisions + 1) * 1 * (3 * 4));
    bb.order(ByteOrder.LITTLE_ENDIAN);
    MemFreeByteBuffer mbb = new MemFreeByteBuffer(bb);
    int s = 32 / gridSubdivisions;
    for (int a = 0; a < gridSubdivisions + 1; a++) for (int b = 0; b < gridSubdivisions + 1; b++) {
        int i = a * s;
        int j = b * s;
        bb.putFloat(i + 0);
        bb.putFloat(0);
        bb.putFloat(j + 0);
    /*bb.putFloat(i + s);
				bb.putFloat(0);
				bb.putFloat(j + s);
				
				bb.putFloat(i + s);
				bb.putFloat(0);
				bb.putFloat(j + 0);
				
				bb.putFloat(i + 0);
				bb.putFloat(0);
				bb.putFloat(j + 0);
				
				bb.putFloat(i + 0);
				bb.putFloat(0);
				bb.putFloat(j + s);
				
				bb.putFloat(i + s);
				bb.putFloat(0);
				bb.putFloat(j + s);*/
    }
    bb.flip();
    grid.uploadData(mbb);
    return grid;
}
Also used : VertexBufferGL(io.xol.chunkstories.renderer.opengl.vbo.VertexBufferGL) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) ByteBuffer(java.nio.ByteBuffer) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer)

Example 2 with MemFreeByteBuffer

use of io.xol.chunkstories.client.util.MemFreeByteBuffer in project chunkstories by Hugobros3.

the class TaskBakeChunk method task.

@Override
protected boolean task(TaskExecutor taskExecutor) {
    if (!(taskExecutor instanceof BakeChunkTaskExecutor))
        throw new UnexecutableTaskException(this, "This class requires to be executed by a BakeChunkTaskExecutor");
    this.cmd = ((BakeChunkTaskExecutor) taskExecutor).getBuffers();
    if (chunk == null) {
        throw new RuntimeException("Fuck off no");
    }
    Vector3dc camera = ((WorldClient) chunk.getWorld()).getWorldRenderer().getRenderingInterface().getCamera().getCameraPosition();
    // Check we aren't too far from the camera, and thus that our request hasn't been yet cancelled
    int vx = Math2.floor(camera.x() / 32);
    int vy = Math2.floor(camera.y() / 32);
    int vz = Math2.floor(camera.z() / 32);
    int dx = LoopingMathHelper.moduloDistance(chunk.getChunkX(), vx, chunk.getWorld().getSizeInChunks());
    int dz = LoopingMathHelper.moduloDistance(chunk.getChunkZ(), vz, chunk.getWorld().getSizeInChunks());
    int dy = Math.abs(chunk.getChunkY() - vy);
    int chunksViewDistance = (int) (world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance") / 32);
    if (dx > chunksViewDistance || dz > chunksViewDistance || dy > 2) {
        // logger.info("unscheduled chunk mesh render task for it being too far to be rendered anyway");
        return true;
    }
    // Require the chunk and nearby ones to be already loaded in the world
    ChunkRenderable chunkWithinWorld = (ChunkRenderable) world.getChunk(chunk.getChunkX(), chunk.getChunkY(), chunk.getChunkZ());
    if (chunkWithinWorld != null) {
        // Require the chunks ARROUND it to be already loaded in the world
        int nearChunks = 0;
        if (world.isChunkLoaded(chunk.getChunkX() + 1, chunk.getChunkY(), chunk.getChunkZ()))
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX() - 1, chunk.getChunkY(), chunk.getChunkZ()))
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY(), chunk.getChunkZ() + 1))
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY(), chunk.getChunkZ() - 1))
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY() + 1, chunk.getChunkZ()) || chunk.getChunkY() == world.getWorldInfo().getSize().heightInChunks - 1)
            nearChunks++;
        if (world.isChunkLoaded(chunk.getChunkX(), chunk.getChunkY() - 1, chunk.getChunkZ()) || chunk.getChunkY() == 0)
            nearChunks++;
        if (nearChunks != 6) {
            // We wait until that's the case
            return false;
        }
    } else {
        // We wait until the chunk is loaded in the world ( or destroyed, then the task is cancelled )
        return false;
    }
    // If the chunk has pending light updates, wait until THOSE are done
    if (chunk.lightBaker.pendingUpdates() > 0) {
        chunk.lightBaker.spawnUpdateTaskIfNeeded();
        return false;
    }
    int updatesToConsider = chunk.chunkRenderData.unbakedUpdates.get();
    // Don't waste time rendering void chunks m8
    if (chunk.isAirChunk())
        i = 32;
    int cx = chunk.getChunkX();
    int cy = chunk.getChunkY();
    int cz = chunk.getChunkZ();
    // Fill chunk caches ( saves much time avoiding slow-ass world->regions hashmap->chunk holder access for each vert )
    for (int relx = -1; relx <= 1; relx++) for (int rely = -1; rely <= 1; rely++) for (int relz = -1; relz <= 1; relz++) {
        CubicChunk chunk2 = (CubicChunk) world.getChunk(cx + relx, cy + rely, cz + relz);
        if (chunk2 != null)
            cmd.cache[((relx + 1) * 3 + (rely + 1)) * 3 + (relz + 1)] = chunk2.chunkVoxelData;
        else
            cmd.cache[((relx + 1) * 3 + (rely + 1)) * 3 + (relz + 1)] = null;
    }
    // Make sure we clear each sub-buffer type.
    for (int i = 0; i < ChunkMeshDataSubtypes.VertexLayout.values().length; i++) {
        for (int j = 0; j < ChunkMeshDataSubtypes.LodLevel.values().length; j++) {
            for (int k = 0; k < ChunkMeshDataSubtypes.ShadingType.values().length; k++) {
                cmd.byteBuffers[i][j][k].clear();
            }
        }
    }
    // Creates wrapper/interfaces for all the elements
    ChunkRenderer chunkRendererOutput = new ChunkRenderer() {

        @Override
        public VoxelBakerHighPoly getHighpolyBakerFor(LodLevel lodLevel, ShadingType renderPass) {
            return (VoxelBakerHighPoly) cmd.byteBuffersWrappers[VertexLayout.INTRICATE.ordinal()][lodLevel.ordinal()][renderPass.ordinal()];
        }

        @Override
        public VoxelBakerCubic getLowpolyBakerFor(LodLevel lodLevel, ShadingType renderPass) {
            return (VoxelBakerCubic) cmd.byteBuffersWrappers[VertexLayout.WHOLE_BLOCKS.ordinal()][lodLevel.ordinal()][renderPass.ordinal()];
        }
    };
    ChunkBakerRenderContext chunkRenderingContext = new ChunkBakerRenderContext(chunk, cx, cy, cz);
    bakedBlockId = -1;
    Map<Voxel, DynamicallyRenderedVoxelType> dynamicVoxels = new HashMap<>();
    BakeChunkScratchCell cell = new BakeChunkScratchCell(world);
    // Render the fucking thing!
    for (i = 0; i < 32; i++) {
        for (j = 0; j < 32; j++) {
            for (k = 0; k < 32; k++) {
                peek(i, k, j, cell);
                if (cell.voxel.isAir())
                    continue;
                // Fill near-blocks info
                // chunkRenderingContext.prepareVoxelLight(); // lol nope
                VoxelRenderer voxelRenderer = cell.getVoxelRenderer();
                if (voxelRenderer == null)
                    voxelRenderer = world.getContent().voxels().getDefaultVoxelRenderer();
                // Run the VoxelRenderer
                voxelRenderer.bakeInto(chunkRendererOutput, chunkRenderingContext, chunk, cell);
                // We handle voxels with a dynamic renderer here too - we just add them to a list !
                if (voxelRenderer instanceof VoxelDynamicRenderer) {
                    DynamicallyRenderedVoxelType drvt = dynamicVoxels.get(cell.voxel);
                    if (drvt == null) {
                        drvt = new DynamicallyRenderedVoxelType((VoxelDynamicRenderer) voxelRenderer, cell.voxel);
                        dynamicVoxels.put(cell.voxel, drvt);
                    }
                    drvt.indexes.add(i * 1024 + k * 32 + j);
                }
                bakedBlockId++;
            }
        }
    }
    // Parse output neatly
    int[][][] sizes = new int[ChunkMeshDataSubtypes.VertexLayout.values().length][ChunkMeshDataSubtypes.LodLevel.values().length][ChunkMeshDataSubtypes.ShadingType.values().length];
    ;
    int[][][] offsets = new int[ChunkMeshDataSubtypes.VertexLayout.values().length][ChunkMeshDataSubtypes.LodLevel.values().length][ChunkMeshDataSubtypes.ShadingType.values().length];
    ;
    int currentOffset = 0;
    // Compute total size to create final bytebuffer
    int sizeInBytes = 0;
    for (VertexLayout vertexLayout : VertexLayout.values()) for (LodLevel lodLevel : LodLevel.values()) for (ShadingType renderPass : ShadingType.values()) {
        int vertexLayoutIndex = vertexLayout.ordinal();
        int lodLevelIndex = lodLevel.ordinal();
        int renderPassIndex = renderPass.ordinal();
        final ByteBuffer relevantByteBuffer = cmd.byteBuffers[vertexLayoutIndex][lodLevelIndex][renderPassIndex];
        // / vertexLayout.bytesPerVertex;
        sizeInBytes += relevantByteBuffer.position();
    }
    ByteBuffer finalData = MemoryUtil.memAlloc(sizeInBytes);
    MemFreeByteBuffer wrappedBuffer = new MemFreeByteBuffer(finalData);
    // For EACH section, make offset and shite
    for (VertexLayout vertexLayout : VertexLayout.values()) for (LodLevel lodLevel : LodLevel.values()) for (ShadingType renderPass : ShadingType.values()) {
        int vertexLayoutIndex = vertexLayout.ordinal();
        int lodLevelIndex = lodLevel.ordinal();
        int renderPassIndex = renderPass.ordinal();
        // Else it gets really long for no reason
        final ByteBuffer relevantByteBuffer = cmd.byteBuffers[vertexLayoutIndex][lodLevelIndex][renderPassIndex];
        offsets[vertexLayoutIndex][lodLevelIndex][renderPassIndex] = currentOffset;
        sizes[vertexLayoutIndex][lodLevelIndex][renderPassIndex] = relevantByteBuffer.position() / vertexLayout.bytesPerVertex;
        // Move the offset accordingly
        currentOffset += relevantByteBuffer.position();
        // Limit the temporary byte buffer and fill the main buffer with it
        relevantByteBuffer.limit(relevantByteBuffer.position());
        relevantByteBuffer.position(0);
        finalData.put(relevantByteBuffer);
    }
    finalData.flip();
    ChunkMeshDataSections newRenderData = new ChunkMeshDataSections(wrappedBuffer, sizes, offsets);
    DynamicallyRenderedVoxelType[] bakedDrvt = new DynamicallyRenderedVoxelType[dynamicVoxels.size()];
    Iterator<DynamicallyRenderedVoxelType> i = dynamicVoxels.values().iterator();
    for (int j = 0; j < dynamicVoxels.size(); j++) {
        if (i.hasNext())
            bakedDrvt[j] = i.next();
        else {
            logger.error("while baking dynamicVoxelTypes array the iterator returned less than dynamicVoxels.size() elements");
            logger.error("cancelling");
            bakedDrvt = null;
            break;
        }
    }
    newRenderData.dynamicVoxelTypes = bakedDrvt;
    chunk.getChunkRenderData().setData(newRenderData);
    chunk.chunkRenderData.unbakedUpdates.addAndGet(-updatesToConsider);
    return true;
}
Also used : ChunkRenderable(io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable) HashMap(java.util.HashMap) VoxelBakerCubic(io.xol.chunkstories.api.rendering.voxel.VoxelBakerCubic) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) VoxelBakerHighPoly(io.xol.chunkstories.api.rendering.voxel.VoxelBakerHighPoly) CubicChunk(io.xol.chunkstories.world.chunk.CubicChunk) LodLevel(io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.LodLevel) Voxel(io.xol.chunkstories.api.voxel.Voxel) VoxelDynamicRenderer(io.xol.chunkstories.api.rendering.voxel.VoxelDynamicRenderer) UnexecutableTaskException(io.xol.chunkstories.api.exceptions.tasks.UnexecutableTaskException) ByteBuffer(java.nio.ByteBuffer) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) VertexLayout(io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.VertexLayout) Vector3dc(org.joml.Vector3dc) ChunkRenderer(io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderer) ShadingType(io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.ShadingType) DynamicallyRenderedVoxelType(io.xol.chunkstories.renderer.chunks.ChunkMeshDataSections.DynamicallyRenderedVoxelType) VoxelRenderer(io.xol.chunkstories.api.rendering.voxel.VoxelRenderer)

Example 3 with MemFreeByteBuffer

use of io.xol.chunkstories.client.util.MemFreeByteBuffer 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 4 with MemFreeByteBuffer

use of io.xol.chunkstories.client.util.MemFreeByteBuffer 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)

Example 5 with MemFreeByteBuffer

use of io.xol.chunkstories.client.util.MemFreeByteBuffer in project chunkstories by Hugobros3.

the class FarTerrainNoMeshRenderer method generateGrid.

VertexBufferGL generateGrid(int gridSubdivisions) {
    VertexBufferGL grid = new VertexBufferGL();
    ByteBuffer bb = MemoryUtil.memAlloc((gridSubdivisions + 1) * (gridSubdivisions + 1) * 2 * 3 * (3 * 4));
    bb.order(ByteOrder.LITTLE_ENDIAN);
    MemFreeByteBuffer mbb = new MemFreeByteBuffer(bb);
    int s = 32 / gridSubdivisions;
    for (int a = 0; a < gridSubdivisions + 1; a++) for (int b = 0; b < gridSubdivisions + 1; b++) {
        int i = a * s;
        int j = b * s;
        bb.putFloat(i + 0);
        bb.putFloat(0);
        bb.putFloat(j + 0);
        bb.putFloat(i + s);
        bb.putFloat(0);
        bb.putFloat(j + s);
        bb.putFloat(i + s);
        bb.putFloat(0);
        bb.putFloat(j + 0);
        bb.putFloat(i + 0);
        bb.putFloat(0);
        bb.putFloat(j + 0);
        bb.putFloat(i + 0);
        bb.putFloat(0);
        bb.putFloat(j + s);
        bb.putFloat(i + s);
        bb.putFloat(0);
        bb.putFloat(j + s);
    }
    bb.flip();
    grid.uploadData(mbb);
    return grid;
}
Also used : VertexBufferGL(io.xol.chunkstories.renderer.opengl.vbo.VertexBufferGL) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer) ByteBuffer(java.nio.ByteBuffer) MemFreeByteBuffer(io.xol.chunkstories.client.util.MemFreeByteBuffer)

Aggregations

MemFreeByteBuffer (io.xol.chunkstories.client.util.MemFreeByteBuffer)6 ByteBuffer (java.nio.ByteBuffer)6 VertexBufferGL (io.xol.chunkstories.renderer.opengl.vbo.VertexBufferGL)3 Location (io.xol.chunkstories.api.Location)2 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)2 Player (io.xol.chunkstories.api.player.Player)2 Shader (io.xol.chunkstories.api.rendering.shader.Shader)2 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)2 HeightmapImplementation (io.xol.chunkstories.world.summary.HeightmapImplementation)2 Vector2d (org.joml.Vector2d)2 UnexecutableTaskException (io.xol.chunkstories.api.exceptions.tasks.UnexecutableTaskException)1 VoxelBakerCubic (io.xol.chunkstories.api.rendering.voxel.VoxelBakerCubic)1 VoxelBakerHighPoly (io.xol.chunkstories.api.rendering.voxel.VoxelBakerHighPoly)1 VoxelDynamicRenderer (io.xol.chunkstories.api.rendering.voxel.VoxelDynamicRenderer)1 VoxelRenderer (io.xol.chunkstories.api.rendering.voxel.VoxelRenderer)1 LodLevel (io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.LodLevel)1 ShadingType (io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.ShadingType)1 VertexLayout (io.xol.chunkstories.api.rendering.world.chunk.ChunkMeshDataSubtypes.VertexLayout)1 ChunkRenderable (io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable)1 ChunkRenderer (io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderer)1