简体   繁体   English

OpenGL 3D纹理坐标

[英]OpenGL 3D Texture Coordinates

I've got a problem with a 3d texture in OpenGL. 我在OpenGL中遇到了3D纹理问题。

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. tex3Ddata共有8片640x480位图。 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. 纹理切片本身是8位单色。

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. g_numCameras是8,所以我希望切片可以通过Z坐标0.0f,1 / 7,2 / 7,...,1.0f访问。 Yet it's always interpolated. 但它始终是插值的。 I tested with tx=0.5f; 我测试了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. x / y坐标正常工作,8个四边形按预期放置,只是通过立方体的切片不能按照我预期的方式工作。

Any ideas what I am doing wrong here? 我在这里做错了什么想法? (I was not able to find an equivalent answer / example on 3d textures). (我无法在3d纹理上找到相应的答案/示例)。

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 假设你有一个8维的1D纹理

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

Then the very first | 然后是第一个| is at texture coordinate 0.0, and the very last | 纹理坐标是0.0,最后是| is at texture coordinate 1.0 在纹理坐标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, ie the numbers between the bars. 你想要击中的是纹素中心,即条形之间的数字。 |1| | 1 | is in the middle between 0/8 to 1/8, ie (0/8+1/8)/2 = 1/16, |2| 在0/8到1/8之间,即(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/8和2/8之间=(1/8 + 2/8)/ 3 = 3/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/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 或者以更一般的形式: (1 + 2*t) / 2*dim

Your expectations are off. 你的期望已经过去了。

each slice i (indexing from 0) is available at (i+0.5) / num_slices . 每个切片i (从0开始索引)在(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.f表示在第一个切片和边界之间插入,1.f表示在最后一个切片和边界之间插入一半(假设是钳位)。

0.5, as it turns out, is exactly in the middle, hence between slices 3 and 4 (if starting to count at 0). 事实证明,0.5正好在中间,因此在切片3和4之间(如果开始计数为0)。 So you get a blend between the 2 slices. 所以你得到了两片之间的混合。

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. 我认为应该关闭插值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM