Skip to main content
GameDev.net gamedev.net
🔒 Locked

1D array as a 4D array for 3D texture

Started by hahadamn Jan 20, 2004 at 4:01 AM 0 replies 750+ views
Original Post
hahadamn
hahadamn
Hi, I have allocated a 1D array to store and access like a 4D array because I need to use this array as a 3D texture that is of the RGBA format. so i need something like tex[x][y][z][4]. i have allocated like this: char *tex = calloc(x*y*z*4, sizeof(char)); now, when i access this array from a 4 level for loop i have been doing: tex[i + j*x + k*x*y + l*x*y*z] however, this is not packed correctly. what am i doing wrong? and how should i access this texture to properly store the RGBA values for this 3D texture using this 1D array. Thanks.
sup sup
hahadamn
hahadamn
well I have found an answer and am posting just in case someone else runs into the same problem. opengl packs its RGBA textures with the RGBA components right next to each other. here is an example of how you would allocate and access your 1D array to be used as an RGBA 3D texture.

unsigned char * vol = (unsigned char *)calloc(xsize*ysize*zsize*4, sizeof(unsigned char));

// in this case x y and z correspond to intervals between 0 and
// xsize-1, ysize-1, zsize-1 respectively. c's interval is between
// 0-3 since we are dealing with an RGBA texture.
unsigned char getValueAt(int x, int y, int z, int c)
{
return vol[(x + y*xsize + z*xsize*ysize)*4 + c];
}

hope this helps some of the few people who are dealing with 3D textures...most likely academia. Thanks.

[edited by - hahadamn on January 21, 2004 1:11:54 AM]
sup sup

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.