OpenGL 3D纹理坐标 [英] OpenGL 3D Texture Coordinates
问题描述
我在OpenGL中使用3d纹理时遇到问题。
I've got a problem with a 3d texture in OpenGL.
我通过
glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, 640, 480, 8, 0, GL_RED, GL_UNSIGNED_BYTE, tex3Ddata);
,其中tex3Ddata共包含640x480位图的8个切片。现在,当我尝试使用纹理坐标访问不同的切片时,由于某种原因,图像会相互融合,因此我没有正确设置坐标,但我不知道为什么。纹理切片本身是8位单色。
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.
显示代码:
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为8,所以我希望切片会可通过Z坐标0.0f,1 / 7、2 / 7,...,1.0f进行访问。但是它总是插值的。我测试了tx = 0.5f;同样,但这也是图像的混合体。 x / y坐标正常工作,并且按预期方式放置了8个四边形,只是对多维数据集的切片不能像我期望的那样工作。
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.
有什么想法我在这里做错了吗? (我无法在3d纹理上找到等效的答案/示例。)
Any ideas what I am doing wrong here? (I was not able to find an equivalent answer / example on 3d textures).
我还通过上传8次相同的图像来测试数据是否正确,并且效果很好(因为插值会生成原始图像,所以我在那里得到原始图像)。
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).
推荐答案
会发生什么就是说,那些纹理坐标不指向纹理像素中心,而是指向纹理像素之间的边缘。假设您有尺寸为8的一维纹理
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 |
然后第一个 |
处于纹理状态坐标0.0,最后一个 |
处于纹理坐标1.0
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 |
让我写下整套分数:
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 |
您想击中的是纹素中心,即条之间的数字。 | 1 |位于0/8至1/8之间的中间,即(0/8 + 1/8)/ 2 = 1/16,| 2 |介于1/8和2/8 =(1/8 + 2/8)/ 3 = 3/16,依此类推。
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.
因此,您想要的纹理坐标地址为
1/16
3/16
5/16
7/16
9/16
11/16
13 / 16
15/16
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
或更一般的形式:(1 + 2 * t)/ 2 * dim
这篇关于OpenGL 3D纹理坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!