3D texture indexing

I’m hoping to find out if what I am doing wrong with indices. I have a 3D texture of dimensions: width * height * 10

I use 4 float representation:

texdata = (float*)calloc( 10 * 4 * width * height, sizeof(float));

In 2D I usually go by:

// pixel index
int pindex = y * width + x;
float *pixel = texdata + 4*pindex;
r = pixel[0]; 
g = pixel[1];
b = pixel[2];

For 3D texture I have assumed that RGBA remain in sequence and depth means an extra image to skip over:

// voxel index
int vindex = z * width * height + pindex;
float *pixel = texdata + 4*vindex;

I used all the 3D texture stuff, and prefer no interpolation.

glBindTexture(GL_TEXTURE_3D, tex3d);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, width, height, 10, 
                        0, GL_RGBA,  GL_FLOAT, texdata);

In fragment shader I use the call:

texture3D(texdata, vec3( s, t, 0 ) );

and I see the first layer just fine. but for:

texture3D(texdata, vec3( s, t, 0+ ) );

I still see the first layer!

I have no shader compile errors, and I don’t get any GL errors for that frame. The render of the first layer is correct. I re-rendered each layer in 2D GL with expected results (albeit my packing order). Is there some packing form that I didn’t declare in PixelStore?

I can’t figure it out :frowning:

What exactly 0+ is?

I think he means any number greater than zero.

Well, yes probably :slight_smile:

I meant, how exactly he computes those values?

If you mean the

vec3(s,t, 0+)

values I don’t use texture coords, I generate my own. Nevertheless the values s,t I generate give the correct index and image, but rendering the >0 layers are the problem. I would like to know if the method of indexing 3D texture is the right way.

It’s kind of hard to say, since you haven’t told us what the “0+” numbers are. For example, if the 0+ numbers were numbers between 0 and 1, then your texture coordinates probably work. If however, the “0+” numbers are layer indices (0, 1, 2, 3, …) instead of normalized texture coordinates just like the S and T values, then clearly your texture coordinates are wrong.

For the record, all three texture coordinates of a 3D texture are normalized. 0 represents the smallest value in that direction, and 1 represents the largest. For all three dimensions.

Thank you Alfonse! you saved my sanity!

Yes, normalised texture coordinates damn me again! I was doing silly thing like

vec3(s, t, 2.0)

instead of

vec3(s, t, 2.0/10.0 )

. I am indeed now getting the right layers for the 0-9.

Out of curiosity I noticed that 10.0/10.0 ie layer 11 undefined, or tex coord (s, t, >=1) shows layer 0. This is interesting since I was always seeing the 0 layer :stuck_out_tongue:

What wrapping mode are you using on the third coordinate?

I did not specify any glTexParameter, so default GL_REPEAT is being used.

Also, if I use a power of 2 number of layers, the behaviour is correct. so for 4 layers, i access them as 0/4, 1/4, 2/4, 3/4.

I played around with a few wrapping modes for GL_TEXTURE_WRAP_R 4/4 is the same as layer 0, except for GL_CLAMP_TO_BORDER which was not proper.