OpenGL 3D 纹理坐标

发布于 2024-10-27 11:45:04 字数 1763 浏览 1 评论 0原文

我在 OpenGL 中遇到 3D 纹理问题。

我通过 tex3Ddata 设置纹理

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

,总共 8 个 640x480 位图切片。现在,当我尝试使用纹理坐标访问不同的切片时,由于某种原因,图像相互混合,因此我没有正确设置坐标,但我不知道为什么。纹理切片本身是 8 位单色。

显示代码:

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 个四边形按预期放置,只是对立方体的切片没有按照我预期的方式工作。

有什么想法我在这里做错了吗? (我无法找到 3D 纹理的等效答案/示例)。

我还通过上传 8 次相同的图像来测试数据是否正确,效果很好(因为插值会产生原始图像,所以我在那里得到了原始图像)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿童谣 2024-11-03 11:45:04

发生在您身上的是,这些纹理坐标并不寻址纹理像素中心,而是寻址纹理像素之间的边缘。假设你有一个维度为 8 的 1D 纹理

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

,那么第一个 | 位于纹理坐标 0.0,最后一个 | 位于纹理坐标 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 之间,依此类推。

所以你想要解决的纹理坐标是
1/16
3/16
5/16
7/16
9/16
11/16
13/16
15/16

或更一般的形式:(1 + 2*t) / 2*dim

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

热风软妹 2024-11-03 11:45:04

你的期望落空了。

每个切片 i(从 0 开始索引)可在 (i+0.5) / num_slices 处获取。

0.f 表示在第一个切片和边界之间的中间插值,1.f 表示在最后一个切片和边界之间的中间插值(假设夹紧)。

事实证明,0.5 正好位于中间,因此位于切片 3 和 4 之间(如果从 0 开始计数)。这样你就得到了两片之间的混合。

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.

冰火雁神 2024-11-03 11:45:04

调用以下内容是否可以接受:

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

我认为这应该关闭插值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文