使用3D纹理时结果不正确

Hol*_*Cat 2 opengl 3d textures opengl-4

我正在使用现代的OpenGL 4.3核心.

我刚刚意识到1024p x 1024p的tileset对我来说太小了.所以,我用1024p x 1024p x 4p 3D纹理替换它.(我知道,这不是最好的解决方案,我最好使用2D纹理数组.我只是想知道为什么我当前的解决方案不起作用.)

xy纹理坐标工作正常,和以前一样.z也工作,但有点不正确.我希望第一层有z == 0.0第二层 - z == 0.3333333第三层 - 第四层 - 第四层z == 0.66666666- z == 1.0.

第1层和第4层按预期工作.但是,0.333333330.66666666给我的不正确的结果:
0.33333333 -与第二层混合>第一层
0.66666666 -与第4层混合>第3层
(我用线性过滤,这就是为什么他们搅和在一起.)

我试图为第二层和第三层选择正确的z值:
z == 0.38
第三层显示正常时,第二次显示正常z == 0.62
(当然这些数字是近似的.)

任何想法为什么会发生这种情况以及如何解决这个问题


这是我创建纹理的方式:

glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &maintex);
glBindTexture(GL_TEXTURE_3D, maintex);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA8, TEXSIZE, TEXSIZE, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE, textemp);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
Run Code Online (Sandbox Code Playgroud)

这是我的着色器:

const char *vertex = 1+R"(
#version 430
uniform layout(location = 3) vec2 fac;
in layout(location = 0) vec2 vpos; // vertex coords
in layout(location = 1) vec3 tpos; // texture coords
in layout(location = 2) vec4 c_off;  // color offset    = vec4(0,0,0,0) by default
in layout(location = 3) vec4 c_mult; // color multiplicator    = vec4(1,1,1,1) by default
// These two are used to do some sort of gamma correction 

out vec3 var_tpos;
out vec4 var_c_off;
out vec4 var_c_mult;
void main()
{
    var_tpos = tpos;
    var_c_off = c_off;
    var_c_mult = c_mult;
    gl_Position = vec4(vpos.x * fac.x - 1, vpos.y * fac.y + 1, 0, 1);
})";


const char *fragment = 1+R"(
#version 430
uniform layout(location = 0) sampler3D tex;
in vec3 var_tpos;
in vec4 var_c_off;
in vec4 var_c_mult;
out vec4 color;
void main()
{
    color = texture(tex, vec3(var_tpos.x / 1024, var_tpos.y / 1024, var_tpos.z));
    color.x *= var_c_mult.x;
    color.y *= var_c_mult.y;
    color.z *= var_c_mult.z;
    color.w *= var_c_mult.w;
    color   += var_c_off;
})";
Run Code Online (Sandbox Code Playgroud)

BDL*_*BDL 8

那是因为0是左纹理元素的"最左边"部分,1是最右边纹理元素中最右边的点.假设我们有4个纹素,比坐标如下:

0       1/4      1/2      3/4       1
|  tx1   |  tx2   |  tx3   |  tx4   |
Run Code Online (Sandbox Code Playgroud)

因为你需要准确地击中体素的中心,你必须使用比你预期的高1/2体素的地址(+ 0.25/2 = +0.125),因此体素中心如下所述.下图.

|  tx1   |  tx2   |  tx3   |  tx4   |
    |        |        |        |
  0.125    0.375     0.625    0.875
Run Code Online (Sandbox Code Playgroud)