开发者

Looking for SLAB6 implementation

开发者 https://www.devze.com 2023-03-19 17:27 出处:网络
I\'m looking to implement SLAB6 into m开发者_如何转开发y raycaster, especially the kv6 support for voxelmodels. However the SLAB6 source by Ken Silverman is totally unreadably (mostly ASM) so I was ho

I'm looking to implement SLAB6 into m开发者_如何转开发y raycaster, especially the kv6 support for voxelmodels. However the SLAB6 source by Ken Silverman is totally unreadably (mostly ASM) so I was hoping someone could point me to a proper C / Java source to load kv6 models or maybe to explain me the workings in some pseudocode preferably (since I want to know how to support the kv6, I know how it works). Thanks, Kaj

EDIT: the implementation would be in Java.


I found some code in an application called VoxelGL (author not mentioned in sourcecode):

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;
    }
}

And:

#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];
}

And:

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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消