Search in sources :

Example 1 with CameraInterface

use of io.xol.chunkstories.api.rendering.CameraInterface 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 2 with CameraInterface

use of io.xol.chunkstories.api.rendering.CameraInterface in project chunkstories by Hugobros3.

the class WorldRendererImplementation method renderWorld.

public void renderWorld(RenderingInterface renderingInterface) {
    ((HeightmapArrayTexture) summariesTexturesHolder).update();
    // if(RenderingConfig.doDynamicCubemaps)
    // cubemapRenderer.renderWorldCubemap(renderingInterface,
    // renderBuffers.rbEnvironmentMap, 128, true);
    // Step one, set the camera to the proper spot
    CameraInterface mainCamera = renderingInterface.getCamera();
    EntityControllable entity = world.getClient().getPlayer().getControlledEntity();
    if (entity != null)
        entity.setupCamera(renderingInterface);
    animationTimer = ((float) (System.currentTimeMillis() & 0x7FFF)) / 100.0f;
    // TODO remove entirely
    FakeImmediateModeDebugRenderer.setCamera(mainCamera);
    chunksRenderer.updatePVSSet(mainCamera);
    // Prepare matrices
    mainCamera.setupUsingScreenSize(gameWindow.getWidth(), gameWindow.getHeight());
    this.renderWorldInternal(renderingInterface);
}
Also used : HeightmapArrayTexture(io.xol.chunkstories.renderer.terrain.HeightmapArrayTexture) CameraInterface(io.xol.chunkstories.api.rendering.CameraInterface) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Example 3 with CameraInterface

use of io.xol.chunkstories.api.rendering.CameraInterface in project chunkstories by Hugobros3.

the class DebugInfoRenderer method drawF3debugMenu.

public void drawF3debugMenu(RenderingInterface renderingInterface) {
    CameraInterface camera = renderingInterface.getCamera();
    Entity playerEntity = client.getPlayer().getControlledEntity();
    /*int timeTook = Client.profiler.timeTook();
		String debugInfo = Client.profiler.reset("gui").toString();
		if (timeTook > 400)
			System.out.println("Lengty frame, printing debug information : \n" + debugInfo);*/
    // Memory usage
    long total = Runtime.getRuntime().totalMemory();
    long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    // By default use the camera position
    int bx = ((int) camera.getCameraPosition().x());
    int by = ((int) camera.getCameraPosition().y());
    int bz = ((int) camera.getCameraPosition().z());
    int lx = bx, ly = by, lz = bz;
    // If the player can look use that
    if (playerEntity != null && playerEntity instanceof EntityControllable) {
        Location loc = ((EntityControllable) playerEntity).getBlockLookingAt(true);
        if (loc != null) {
            lx = (int) loc.x();
            ly = (int) loc.y();
            lz = (int) loc.z();
        }
    }
    int raw_data = world.peekRaw(lx, ly, lz);
    CellData cell = world.peekSafely(lx, ly, lz);
    // System.out.println(VoxelFormat.id(raw_data));
    int cx = bx / 32;
    int cy = by / 32;
    int cz = bz / 32;
    int csh = world.getRegionsSummariesHolder().getHeightAtWorldCoordinates(bx, bz);
    // Obtain the angle the player is facing
    VoxelSide side = VoxelSide.TOP;
    float angleX = -1;
    if (playerEntity != null && playerEntity instanceof EntityLiving)
        angleX = Math.round(((EntityLiving) playerEntity).getEntityRotationComponent().getHorizontalRotation());
    double dx = Math.sin(angleX / 360 * 2.0 * Math.PI);
    double dz = Math.cos(angleX / 360 * 2.0 * Math.PI);
    if (Math.abs(dx) > Math.abs(dz)) {
        if (dx > 0)
            side = VoxelSide.RIGHT;
        else
            side = VoxelSide.LEFT;
    } else {
        if (dz > 0)
            side = VoxelSide.FRONT;
        else
            side = VoxelSide.BACK;
    }
    // Count all the entities
    int ec = 0;
    IterableIterator<Entity> i = world.getAllLoadedEntities();
    while (i.hasNext()) {
        i.next();
        ec++;
    }
    Chunk current = world.getChunk(cx, cy, cz);
    int x_top = renderingInterface.getWindow().getHeight() - 16;
    Font font = null;
    font = renderingInterface.getFontRenderer().getFont("LiberationSans-Regular", 12);
    if (font == null)
        font = renderingInterface.getFontRenderer().getFont("LiberationSans-Regular", 12);
    int lineHeight = font.getLineHeight();
    int posx, posy;
    String text;
    posx = 8;
    posy = x_top - posx;
    text = GLCalls.getStatistics() + " Chunks in view : " + world.getWorldRenderer().getChunksRenderer().getChunksVisible() + " Entities " + ec + " Particles :" + ((ClientParticlesRenderer) world.getParticlesManager()).count() + " #FF0000Render FPS: " + Client.getInstance().getGameWindow().getFPS() + " avg: " + Math.floor(10000.0 / Client.getInstance().getGameWindow().getFPS()) / 10.0 + " #00FFFFSimulation FPS: " + world.getWorldRenderer().getWorld().getGameLogic().getSimulationFps();
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "RAM usage : " + used / 1024 / 1024 + " / " + total / 1024 / 1024 + " MB used, chunks loaded in ram: " + world.getRegionsHolder().countChunksWithData() + "/" + world.getRegionsHolder().countChunks() + " " + Math.floor(world.getRegionsHolder().countChunksWithData() * 4 * 32 * 32 * 32 / (1024L * 1024 / 100f)) / 100f + "MB used by chunks";
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    long totalVram = (renderingInterface.getTotalVramUsage()) / 1024 / 1024;
    posy -= lineHeight;
    text = "VRAM usage : " + totalVram + "MB as " + Texture2DGL.getTotalNumberOfTextureObjects() + " textures using " + Texture2DGL.getTotalVramUsage() / 1024 / 1024 + "MB + " + VertexBufferGL.getTotalNumberOfVerticesObjects() + " vbos using " + renderingInterface.getVertexDataVramUsage() / 1024 / 1024 + " MB";
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "Worker threads: " + world.getGameContext().tasks() + " - " + world.ioHandler.toString();
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "Position : x:" + bx + " y:" + by + " z:" + bz + " dir: " + angleX + " side: " + side + " #FF0000Block looking at#FFFFFF : pos: " + lx + ": " + ly + ": " + lz + " data: " + raw_data + " voxel_type: " + cell.getVoxel().getName() + " sl:" + cell.getSunlight() + " bl: " + cell.getBlocklight() + " meta:" + cell.getMetaData() + " csh:" + csh;
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "Current Summary : " + world.getRegionsSummariesHolder().getHeightmapChunkCoordinates(cx, cz);
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    posy -= lineHeight;
    text = "Current Region : " + world.getRegionChunkCoordinates(cx, cy, cz);
    renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    if (current == null) {
        posy -= lineHeight;
        text = "Current chunk null";
        renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    } else if (current instanceof ChunkRenderable) {
        ChunkRenderDataHolder chunkRenderData = ((ClientChunk) current).getChunkRenderData();
        if (chunkRenderData != null) {
            posy -= lineHeight;
            text = "Current Chunk : " + current + " - " + chunkRenderData.toString();
            renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
        } else {
            posy -= lineHeight;
            text = "Current Chunk : " + current + " - No rendering data";
            renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
        }
    }
    if (playerEntity != null && playerEntity instanceof Entity) {
        posy -= lineHeight;
        text = "Controlled Entity : " + playerEntity;
        renderingInterface.getFontRenderer().drawStringWithShadow(font, posx, posy, text, 1, 1, new Vector4f(1));
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) VoxelSide(io.xol.chunkstories.api.voxel.VoxelSide) EntityLiving(io.xol.chunkstories.api.entity.EntityLiving) ChunkRenderable(io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) ClientChunk(io.xol.chunkstories.world.chunk.ClientChunk) CellData(io.xol.chunkstories.api.world.cell.CellData) Font(io.xol.chunkstories.api.rendering.text.FontRenderer.Font) ChunkRenderDataHolder(io.xol.chunkstories.renderer.chunks.ChunkRenderDataHolder) Vector4f(org.joml.Vector4f) CameraInterface(io.xol.chunkstories.api.rendering.CameraInterface) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) Location(io.xol.chunkstories.api.Location) ClientParticlesRenderer(io.xol.chunkstories.renderer.particles.ClientParticlesRenderer)

Aggregations

CameraInterface (io.xol.chunkstories.api.rendering.CameraInterface)3 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)2 Location (io.xol.chunkstories.api.Location)1 Entity (io.xol.chunkstories.api.entity.Entity)1 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)1 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)1 Font (io.xol.chunkstories.api.rendering.text.FontRenderer.Font)1 ChunkRenderable (io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable)1 VoxelSide (io.xol.chunkstories.api.voxel.VoxelSide)1 CellData (io.xol.chunkstories.api.world.cell.CellData)1 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)1 ChunkRenderDataHolder (io.xol.chunkstories.renderer.chunks.ChunkRenderDataHolder)1 ClientParticlesRenderer (io.xol.chunkstories.renderer.particles.ClientParticlesRenderer)1 RegionMesh (io.xol.chunkstories.renderer.terrain.FarTerrainBaker.RegionMesh)1 HeightmapArrayTexture (io.xol.chunkstories.renderer.terrain.HeightmapArrayTexture)1 ClientChunk (io.xol.chunkstories.world.chunk.ClientChunk)1 HeightmapImplementation (io.xol.chunkstories.world.summary.HeightmapImplementation)1 Vector4f (org.joml.Vector4f)1