Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
896 views
in Technique[技术] by (71.8m points)

opengl - Incorrect result when using 3D textures

I'm using modern OpenGL 4.3 core.

I just realized that 1024p x 1024p tileset is too small for my needs. So, I replaced it with 1024p x 1024p x 4p 3D texture. (I know, it's not the best solution, and I better to use 2D texture array. I'm just wondering why my current solution doesn't work.)

x and y texture coordinates work fine, same as before. z also work, but a bit incorrectly. I would expect the 1st layer to have z == 0.0, 2nd layer - z == 0.3333333, 3rd layer - z == 0.66666666 and 4rd one - z == 1.0.

The 1st and 4th layers work as expected. But 0.33333333 and 0.66666666 give me incorrect results:
0.33333333 -> 1st layer mixed with 2nd layer
0.66666666 -> 3rd layer mixed with 4th layer
(I'm using linear filtering, that's why they mix together.)

I tried to pick the right z values for 2nd and 3rd layers:
2nd displays fine when z == 0.38
3rd displays fine when z == 0.62
(These numbers are approximate, of course.)

Any ideas why this happen and how I can fix this?


This is how I create the texture:

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

And it's my shader:

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;
})";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

That's because 0 is the "leftmost" part of the left texel and 1 is the rightmost point in the rightmost texel. Assume we have 4 texels, than the coordinates are as follows:

0       1/4      1/2      3/4       1
|  tx1   |  tx2   |  tx3   |  tx4   |

since you need to hit exactly the center of a voxel, you have to use addresses that are 1/2 voxel higher then you expected (+0.25/2 = +0.125), thus the voxel centers are as described in the bottom line of the following diagram.

|  tx1   |  tx2   |  tx3   |  tx4   |
    |        |        |        |
  0.125    0.375     0.625    0.875

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...