tags:

views:

106

answers:

3

If there is an array of ar[5000] then how could I find where element [5][5][4] would be if this was a 3 dimensional array? Thanks

I'm mapping pixels: imagine a bimap of [768 * 1024 * 4] where would pixel [5][5][4] be?

I want to make this:

static GLubyte checkImage[checkImageHeight][checkImageWidth][4];

static GLuint texName;
bool itt;
void makeCheckImage(void)
{
    Bitmap *b = new Bitmap(L"c:/boo.png");

    int i, j, c;
    Color cul;

    for (i = 0; i < checkImageHeight; i++) {
        for (j = 0; j < checkImageWidth; j++) {
            b->GetPixel(j,i,&cul);


            checkImage[i][j][0] = (GLubyte) cul.GetR();
            checkImage[i][j][1] = (GLubyte) cul.GetG();
            checkImage[i][j][2] = (GLubyte) cul.GetB();
            checkImage[i][j][3] = (GLubyte) cul.GetA();
        }
    }
    delete(b);
}

work without making a multidimensional array. width = 512, height = 1024....

+7  A: 

That will depend on the dimensions of the imaginary three dimensional array as well as how its laid out. Sometimes the formula is ar[z * x_size * y_size + y * x_size + x] == ar[z][y][x], but without knowing the size of each dimension and what order they're in, there's no way to tell for sure.

Bill Carey
+1 Beat me to it
Lazarus
Not `ar[z][y][x]`?
JAB
Fair enough, JAB. I'll change that.
Bill Carey
I need x y z though, im trying to map textures in OpenGL
Milo
Swap 'x' and 'z' in the above example? It's not materially different is it?
cyborg
@cyborg: If you're not replacing *every* instance of `ar[z][y][x]`, having exact matching is necessary. Otherwise, translations, rotations, etc. have no effect (except perhaps on memory locality).
Brian
I think he's responded in one of the answers but basically what I was getting at is that if he wants to reorder the x, y and z it's just a renaming exercise - presuming he has control over his data structures. (Which looks like the case from the question).
cyborg
A: 

You would need to know the size of the dimensions. If you wish to interpret it as a ar[m][n][p] array, such that mnp=5000, ar[5][5][4] would correspond to ar[5*m*n+5*n+4].

This is because C++ works in row-major order.

Pieter
A: 

[x * z_size * y_size + y * z_size + z] worked

Milo