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 总共有 8 个 640x480 位图切片.现在,当我尝试使用纹理坐标访问不同的切片时,由于某种原因,图像相互融合,因此我没有正确设置坐标,但我不知道为什么.纹理切片本身是 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/163/165/167/169/1611/1613/1615/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屋!