eirys / ft_vox

A procedural voxel terrain rendering engine in C++ and Vulkan
Apache License 2.0
0 stars 0 forks source link

Fix pos building #10

Closed eirys closed 12 months ago

eirys commented 12 months ago

CPP code

inline int32_t  toChunkPos(float x, float y, float z) noexcept {
    // TODO: Add render distance in ubo
    int32_t chunk_address =
        (int32_t)x / CHUNK_SIZE |
        ((int32_t)z / CHUNK_SIZE) << 8 |
        ((int32_t)y / CHUNK_SIZE) << 16;

    x = (int32_t)x % CHUNK_SIZE;
    y = (int32_t)y % CHUNK_SIZE;
    z = (int32_t)z % CHUNK_SIZE;

    return
        static_cast<int32_t>(x) |
        static_cast<int32_t>(y) << 4 |
        static_cast<int32_t>(z) << 8 |
        chunk_address << 12;
}

HLSL code

// Extract actual vertex position from input
float4  extractPos(int input) {
    // Local position in chunk
    float3  pos = float3(
        input & 0xF,
        (input >> 4) & 0xF,
        (input >> 8) & 0xF);

    // Chunk position in world
    int chunk_x = (input >> 12) & 0xFF;
    int chunk_z = (input >> 20) & 0xFF;
    int chunk_y = (input >> 28) & 0xF;

    return float4(
        (chunk_x * 16) + pos.x,
        (chunk_y * 16) + pos.y,
        (chunk_z * 16) + pos.z,
        1.0
    );
}
eirys commented 12 months ago

fixed.