2

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?

toeplitz
  • 777
  • 1
  • 8
  • 27
  • 2
    Sorry, but I'm not following, you read `width * height * depth` bytes from the file and then cast to rgba? Shouldn't you either be treating the data as grayscale or read (and allocate) four times as many bytes? As for displaying I think you can just draw a polygon with the right 3d texture coordinates. – user786653 Oct 19 '11 at 20:12
  • You are right. The dataset is from this site: http://www.gris.uni-tuebingen.de/edu/areas/scivis/volren/datasets/datasets.html, I changed the code now to reflect RGB. I am able to display the skull (last image on the site) when I read it in like this. – toeplitz Oct 19 '11 at 20:40
  • No reason other then I want to have an understanding of 2D texture slicing before rendering with a 3D texture. – toeplitz Oct 19 '11 at 20:45
  • So you just want to extract a 2D image somewhere perpendicular to the X, Y, or Z axis? Not some arbitrarily oriented plane? – genpfault Oct 19 '11 at 20:59
  • 1
    yes, perpendicular to x,y or z. – toeplitz Oct 19 '11 at 21:02

1 Answers1

0

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • Thanks. The follow-up this then how is my rgb image[256*256*256] (1d) array is organized with respect to a 3D array? – toeplitz Oct 19 '11 at 21:27
  • 2
    3D coordinates (x,y,z) to 1D: `image[(x*yw+y)*zw+z]`, where `yw` and `zw` are the sizes of your 3D array in that dimension. – Kevin Reid Oct 19 '11 at 21:42