Read raw 3D texture and slice cube

1.6k views Asked by At

Reading a raw 3d (256x256x256 cells) texture (8bit) using the following:

f = fopen(file, "rb");
if (f == NULL) return;
int size = width * height * depth;
data = new unsigned char[size*3];
fread(data, 3, size, f);
image = reinterpret_cast<rgb *>(data);
fclose(f);

where image rgba is

typedef unsigned char byte;

    typedef struct {
        byte r;
        byte g;
        byte b;
    } rgb;

I now want to "slice" the cube in some perpendicular direction and display the data with:

glTexImage2D()

What is a clever way to go about grabbing these slices? I have tried to get my head around how to grab the data using a loop, but I am not exactly sure how the data is orgaanized and I do not find this loop to be trivial.

Could I map the data into a more trivial format so I can use intuitive slice syntax?

1

There are 1 answers

2
genpfault On BEST ANSWER

It's just a 3D array. Pick two axes to scan in the standard 2D access pattern and vary the third to change slices:

for( unsigned int slice = 0; i < numSlices; ++i )
{
    for( unsigned int x = 0; x < xSize; ++x )
    {
        for( unsigned int y = 0; y < ySize; ++y )
        {
            // XY slice
            output[x][y] = array[x][y][slice];

            // XZ slice
            output[x][y] = array[x][slice][y];

            // YZ slice
            output[x][y] = array[slice][x][y];
        }
    }
}

You can get fancier with Boost.MultiArray.