Search in sources :

Example 6 with Vector3dc

use of org.joml.Vector3dc 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 7 with Vector3dc

use of org.joml.Vector3dc in project chunkstories-api by Hugobros3.

the class WorldEnvironment method setupShadowColors.

/**
 * Set-ups the shader constants used to render shadows
 */
public default void setupShadowColors(RenderingInterface renderer, Shader shader) {
    Vector3dc cameraPosition = renderer.getCamera().getCameraPosition();
    shader.setUniform1f("shadowStrength", 1.0f);
    shader.setUniform3f("sunColor", getSunlightColor(cameraPosition));
    shader.setUniform3f("shadowColor", getShadowColor(cameraPosition));
}
Also used : Vector3dc(org.joml.Vector3dc)

Example 8 with Vector3dc

use of org.joml.Vector3dc 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 9 with Vector3dc

use of org.joml.Vector3dc in project chunkstories by Hugobros3.

the class FarTerrainMeshRenderer method renderTerrain.

public void renderTerrain(RenderingInterface renderer, ReadyVoxelMeshesMask mask) {
    // Check for world updates
    Vector3dc cameraPosition = renderer.getCamera().getCameraPosition();
    int xCoordinates = ((int) (double) cameraPosition.x());
    int zCoordinates = ((int) (double) cameraPosition.z());
    xCoordinates %= world.getWorldSize();
    zCoordinates %= world.getWorldSize();
    if (xCoordinates < 0)
        xCoordinates += world.getWorldSize();
    if (zCoordinates < 0)
        zCoordinates += world.getWorldSize();
    int chunkCoordinatesX = xCoordinates / 32;
    int chunkCoordinatesZ = zCoordinates / 32;
    if (centerChunkX != chunkCoordinatesX || centerChunkZ != chunkCoordinatesZ) {
        centerChunkX = chunkCoordinatesX;
        centerChunkZ = chunkCoordinatesZ;
        this.markFarTerrainMeshDirty();
    }
    // Setup shader etc
    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));
    // renderingContext.bindTexture2D("vegetationColorTexture", worldRenderer.getGrassTexture());
    terrainShader.setUniform1f("mapSize", world.getSizeInChunks() * 32);
    // TODO hidden inputs ?
    if (renderer.getClient().getInputsManager().getInputByName("wireframeFarTerrain").isPressed() && ClientLimitations.isDebugAllowed)
        renderer.setPolygonFillMode(PolygonFillMode.WIREFRAME);
    if (!renderer.getClient().getInputsManager().getInputByName("hideFarTerrain").isPressed() && ClientLimitations.isDebugAllowed)
        drawTerrainBits(renderer, mask, terrainShader);
    renderer.setPolygonFillMode(PolygonFillMode.FILL);
}
Also used : Vector3dc(org.joml.Vector3dc) Texture2D(io.xol.chunkstories.api.rendering.textures.Texture2D) Shader(io.xol.chunkstories.api.rendering.shader.Shader)

Example 10 with Vector3dc

use of org.joml.Vector3dc in project chunkstories by Hugobros3.

the class DefaultWorldCollisionsManager method raytraceSolid.

private Location raytraceSolid(Vector3dc initialPosition, Vector3dc directionIn, double limit, boolean outer, boolean selectable) {
    Vector3d direction = new Vector3d();
    directionIn.normalize(direction);
    // direction.scale(0.02);
    // float distance = 0f;
    CellData cell;
    // Voxel vox;
    int x, y, z;
    x = (int) Math.floor(initialPosition.x());
    y = (int) Math.floor(initialPosition.y());
    z = (int) Math.floor(initialPosition.z());
    // DDA algorithm
    // It requires double arrays because it works using loops over each dimension
    double[] rayOrigin = new double[3];
    double[] rayDirection = new double[3];
    rayOrigin[0] = initialPosition.x();
    rayOrigin[1] = initialPosition.y();
    rayOrigin[2] = initialPosition.z();
    rayDirection[0] = direction.x();
    rayDirection[1] = direction.y();
    rayDirection[2] = direction.z();
    int[] voxelCoords = new int[] { x, y, z };
    int[] voxelDelta = new int[] { 0, 0, 0 };
    double[] deltaDist = new double[3];
    double[] next = new double[3];
    int[] step = new int[3];
    int side = 0;
    // Prepare distances
    for (int i = 0; i < 3; ++i) {
        double deltaX = rayDirection[0] / rayDirection[i];
        double deltaY = rayDirection[1] / rayDirection[i];
        double deltaZ = rayDirection[2] / rayDirection[i];
        deltaDist[i] = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ);
        if (rayDirection[i] < 0.f) {
            step[i] = -1;
            next[i] = (rayOrigin[i] - voxelCoords[i]) * deltaDist[i];
        } else {
            step[i] = 1;
            next[i] = (voxelCoords[i] + 1.f - rayOrigin[i]) * deltaDist[i];
        }
    }
    do {
        // DDA steps
        side = 0;
        for (int i = 1; i < 3; ++i) {
            if (next[side] > next[i]) {
                side = i;
            }
        }
        next[side] += deltaDist[side];
        voxelCoords[side] += step[side];
        voxelDelta[side] += step[side];
        x = voxelCoords[0];
        y = voxelCoords[1];
        z = voxelCoords[2];
        cell = world.peekSafely(x, y, z);
        if (cell.getVoxel().getDefinition().isSolid() || (selectable && cell.getVoxel().getDefinition().isSelectable())) {
            boolean collides = false;
            for (CollisionBox box : cell.getTranslatedCollisionBoxes()) {
                // System.out.println(box);
                Vector3dc collisionPoint = box.lineIntersection(initialPosition, direction);
                if (collisionPoint != null) {
                    collides = true;
                // System.out.println("collides @ "+collisionPoint);
                }
            }
            if (collides) {
                if (!outer)
                    return new Location(world, x, y, z);
                else {
                    // Back off a bit
                    switch(side) {
                        case 0:
                            x -= step[side];
                            break;
                        case 1:
                            y -= step[side];
                            break;
                        case 2:
                            z -= step[side];
                            break;
                    }
                    return new Location(world, x, y, z);
                }
            }
        }
    // distance += deltaDist[side];
    } while (voxelDelta[0] * voxelDelta[0] + voxelDelta[1] * voxelDelta[1] + voxelDelta[2] * voxelDelta[2] < limit * limit);
    return null;
}
Also used : Vector3dc(org.joml.Vector3dc) Vector3d(org.joml.Vector3d) CellData(io.xol.chunkstories.api.world.cell.CellData) CollisionBox(io.xol.chunkstories.api.physics.CollisionBox) Location(io.xol.chunkstories.api.Location)

Aggregations

Vector3dc (org.joml.Vector3dc)15 Vector3d (org.joml.Vector3d)9 Location (io.xol.chunkstories.api.Location)6 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)5 Entity (io.xol.chunkstories.api.entity.Entity)4 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)4 WorldClient (io.xol.chunkstories.api.world.WorldClient)4 CellData (io.xol.chunkstories.api.world.cell.CellData)4 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)3 HitBox (io.xol.chunkstories.api.entity.EntityLiving.HitBox)3 Voxel (io.xol.chunkstories.api.voxel.Voxel)3 Shader (io.xol.chunkstories.api.rendering.shader.Shader)2 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)2 WorldCell (io.xol.chunkstories.api.world.World.WorldCell)2 EntityPlayer (io.xol.chunkstories.core.entity.EntityPlayer)2 Matrix4f (org.joml.Matrix4f)2 Vector3f (org.joml.Vector3f)2 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)1 ClientPacketsProcessor (io.xol.chunkstories.api.client.net.ClientPacketsProcessor)1 Controller (io.xol.chunkstories.api.entity.Controller)1