2

I've got a problem with a 3d texture in OpenGL.

I set the texture via

glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, 640, 480, 8, 0, GL_RED, GL_UNSIGNED_BYTE, tex3Ddata);

with tex3Ddata being in total 8 slices of 640x480 bitmaps. Now when I try to access the different slices with texture coordinates, for some reason the images blend into each other, thus I don't properly set the coordinates, yet I do not know why. The texture slices itself are 8bit monochrome.

Code for the displaying:

for(unsigned int i = 0; i < g_numCameras; i++) {
    float tx = ((float)i) / ((float)g_numCameras - 1);
    //tx = 0.5f; //this is for testing only
    float fI = ((float)i) / ((float)g_numCameras);
    if( i < (g_numCameras >> 1)) {
        glTexCoord3f(0.0f, 1.0f, tx); 
        glVertex3f( -1.0f + fI * 4.0f, 0.0f,  0.5f);

        glTexCoord3f(1.0f, 1.0f, tx); 
        glVertex3f( -1.0f + (fI + 1.0f / ((float)g_numCameras)) * 4.0f, 0.0f,  0.5f);

        glTexCoord3f(1.0f, 0.0f, tx); 
        glVertex3f( -1.0f + (fI + 1.0f / ((float)g_numCameras)) * 4.0f,  1.0f,  0.5f);

        glTexCoord3f(0.0f, 0.0f, tx); 
        glVertex3f( -1.0f + fI * 4.0f,  1.0f,  0.5f);
    }
    else {
        fI -= 0.5f;
        glTexCoord3f(0.0f, 1.0f, tx); 
        glVertex3f( -1.0f + fI * 4.0f, -1.0f,  0.5f);

        glTexCoord3f(1.0f, 1.0f, tx); 
        glVertex3f( -1.0f + (fI + 1.0f / ((float)g_numCameras)) * 4.0f, -1.0f,  0.5f);

        glTexCoord3f(1.0f, 0.0f, tx); 
        glVertex3f( -1.0f + (fI + 1.0f / ((float)g_numCameras)) * 4.0f,  0.0f,  0.5f);

        glTexCoord3f(0.0f, 0.0f, tx); 
        glVertex3f( -1.0f + fI * 4.0f,  0.0f,  0.5f);
    }
}

g_numCameras is 8, so I would expect the slices to be accessible via the Z-coordinates 0.0f, 1/7, 2/7, ..., 1.0f. Yet it's always interpolated. I tested with tx=0.5f; as well, but this is also a mix of images. The x/y coordinates work properly, and the 8 quads are placed as expected, just the slicing through the cube doesn't work the way I would have expected it.

Any ideas what I am doing wrong here? (I was not able to find an equivalent answer / example on 3d textures).

I've also tested whether the data is proper by uploading 8 times the same image, and that worked just fine (since interpolation will result in original image, I got the original image there).

3 Answers 3

13

What happens to you is, that those texture coordinates don't address the texel centers, but edges between the texels. Let's say you've got a 1D texture of dimension 8

| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

Then the very first | is at texture coordinate 0.0, and the very last | is at texture coordinate 1.0

0.0                             1.0
0/8                             8/8
 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

Let me write the whole set of fractions:

0/8 1/8 2/8 3/8 4/8 5/8 6/8 7/8 8/8
 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |

What you want to hit are the texel centers, i.e the numbers between the bars. |1| is in the middle between 0/8 to 1/8, i.e. (0/8+1/8)/2 = 1/16, |2| is between 1/8 and 2/8 = (1/8 + 2/8)/3 = 3/16 and so on.

So the texture coordinates you want to address are 1/16 3/16 5/16 7/16 9/16 11/16 13/16 15/16

or in more general form: (1 + 2*t) / 2*dim

Sign up to request clarification or add additional context in comments.

Comments

3

Your expectations are off.

each slice i (indexing from 0) is available at (i+0.5) / num_slices.

0.f means interpolate halfway between the first slice and the border, 1.f means interpolate half-way between the last slice and the border (assuming clamping).

0.5, as it turns out, is exactly in the middle, hence between slices 3 and 4 (if starting to count at 0). So you get a blend between the 2 slices.

Comments

2

Is it acceptable to call the following:

glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

That should turn off interpolation, I think.

1 Comment

I tried this, since I thought the same, yet the interpolation in the cube still happened

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.