寻找SLAB6实施

时间:2011-07-11 19:33:22

标签: voxel

我正在寻求将SLAB6应用到我的raycaster中,尤其是对voxelmodels的kv6支持。然而,Ken Silverman的SLAB6源代码完全不可读(主要是ASM),所以我希望有人可以指向一个合适的C / Java源来加载kv6模型,或者可能最好解释一些伪代码的工作原理(因为我想知道)如何支持kv6,我知道它是如何工作的)。谢谢,Kaj

编辑:实现将使用Java。

1 个答案:

答案 0 :(得分:0)

我在名为VoxelGL的应用程序中找到了一些代码(作者未在源代码中提及):

void CVoxelWorld::generateSlabFromData(unsigned char *data, VoxelData *vdata, Slab *slab)
{
    int currentpattern = 1;
    int i = 0;
    int n, totalcount, v, count;
    n = 0;
    v = 0;
    while (1)
    {
        while (data[i] == currentpattern)
        {
            if (currentpattern == 1)
                v++;
            i++;
            if (i == 256)
                break;
        }
        n++;
        if (i == 256)
        {
            if (currentpattern == 0)
                n--;
            break;
        }
        currentpattern ^= 1;
    }
    slab->nentries = n;
    if (slab->description != 0)delete [] slab->description;
    if (slab->data != 0)delete [] slab->data;
    slab->description = new int[n];
    slab->data = new VoxelData[v];

    totalcount = 0;
    v = 0;
    currentpattern = 1;

    for (i = 0; i < n; i++)
    {
        count = 0;
        while (data[totalcount] == currentpattern)
        {
            count++;
            totalcount++;
            if (totalcount == 256)
                break;
        }
        slab->description[i] = count-1;
        if (i % 2 == 0)
        {
            memcpy(slab->data + v, vdata + totalcount - count, 3 * count);
            v += count;
        }
        currentpattern ^= 1;
    }
}

#define clustersize 8
Slab *CVoxelWorld::getSlab(int x, int z)
{
    int xgrid = x / clustersize;
    int ygrid = z / clustersize;
    int clusteroffset = xgrid * 1024 * clustersize + ygrid * clustersize * clustersize;

    return &m_data[clusteroffset + (x & (clustersize - 1)) + (z & (clustersize - 1)) * clustersize];
}

int CVoxelWorld::isSolid(int x, int y, int z)
{
    Slab *slab;

    if (y < 0 || y > 256)
        return 0;

    slab = getSlab(x, z);
    int counter = 0;
    for (int i = 0; i < slab->nentries; i++)
    {
        int height = slab->description[i] + 1;
        if (i % 2 == 0)
        {
            if (y >= counter && y < counter + height)
                return 1;
        }
        counter += height;
    }
    return 0;
}