Irregularity of texture coordinates in fragment shader

#1

Hi, All!

I try to create tests for shaders.
So, this is the reason why I use textures with so low resolution: 3x3, 4x4, 5x5.

While I’m was testing my shader some strange results was catched for function ‘textureGather’;
I localized the problem: that is irregularity of texture grid.

My VS:

#version 430

layout (location=0) in vec3 position;
layout (location=1) in vec2 texCoord;

out vec2 outTexCoord;

void main() {

  gl_Position = vec4(position, 1.0);
  outTexCoord = texCoord;
}

My FS:

#version 430

in vec2 outTexCoord;

out vec4 fragColor;

void main() {
  fragColor = vec4(outTexCoord.x, outTexCoord.y, 0, 0);
}

I use quad for rasterization:

v  1.000000  1.000000 0.000000
v -1.000000  1.000000 0.000000
v -1.000000 -1.000000 0.000000
v  1.000000 -1.000000 0.000000

vt 1.000000 0.000000
vt 0.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000

vn 0.000000 0.000000 1.000000

f 1/3/1 2/2/1 3/3/1 
f 1/1/1 3/3/1 4/4/1

Result of rendering is just texture coordinates in the texture colors: x, y. (z and w are ignored).

Result for 3x3:
0: (0,16 0,16); (0,50 0,16); (0,83 0,16);
1: (0,16 0,50); (0,75 0,75); (0,92 0,75);
2: (0,16 0,83); (0,75 0,92); (0,92 0,92);

Result for 4x4:
0: (0,13 0,13); (0,38 0,13); (0,62 0,13); (0,87 0,13);
1: (0,13 0,38); (0,69 0,69); (0,81 0,69); (0,87 0,38);
2: (0,13 0,62); (0,69 0,81); (0,81 0,81); (0,87 0,62);
3: (0,13 0,87); (0,38 0,87); (0,62 0,87); (0,87 0,87);

Result for 5x5:
0: (0,10 0,10); (0,30 0,10); (0,50 0,10); (0,70 0,10); (0,90 0,10);
1: (0,10 0,30); (0,30 0,30); (0,50 0,30); (0,70 0,30); (0,90 0,30);
2: (0,10 0,50); (0,30 0,50); (0,75 0,75); (0,85 0,75); (0,90 0,50);
3: (0,10 0,70); (0,30 0,70); (0,75 0,85); (0,85 0,85); (0,90 0,70);
4: (0,10 0,90); (0,30 0,90); (0,50 0,90); (0,70 0,90); (0,90 0,90);

You can see irregularity in some ‘x’ columns.

I’m so confused with it.
Why such irregularity is present?

Thank you for any answer!

#2

Shouldn’t the first entry be 1/1/1?

#3

Yes, it was definitely a bug!
I fixed it, but results didn’t change.

I don’t understand why, but texture indices don’t affect result at all.
Only vertices does matter and if I change them, then the result changes.

#4

Then the issue is with the draw call and whatever sets up the data for it. If you’re using VAOs, remember that the element array buffer is stored in the VAO (not in the context), so ensure that the correct VAO is bound when calling glBindBuffer(GL_ELEMENT_ARRAY_BUFFER).

I’d suggest putting a glGetBufferSubData(GL_ELEMENT_ARRAY_BUFFER) call immediately prior to the glDrawElements() call to check what’s actually being passed. This assumes that you’re using a buffer for the element array; if it’s a client-side array, just dump its contents or view it in the debugger.

#5

I use the same Mesh object in my code, also I use the same quad for rendering some texture buffers:


1 - depth buffer of current frame
2 - reprojected depth buffer from prev. frame (one of layers of mipmap)
3 - depth buffer from sun light point of view.
All of them are using the same quad mesh.

Also the scene use the same Mesh object (code).

But I understand that is something wrong with it.
May be quad uses default texture coordinates, or something like that.

#6

I made some experiments and now I’m sure that I’m using correct VAO.

As a result of experiments I have to say:
All indices groups of both faces are affecting to the rendering, excluding 1st and 3d groups of 1st face.
Thus, 1st and 3d groups of the 1st face are not reacting if I change texture index to any valid value.
It is so magical! :slight_smile:

#7

Here is 18х18 texture coordinates:

         x(0)      x(1)      x(2)      x(3)      x(4)      x(5)      x(6)      x(7)      x(8)      x(9)     x(10)     x(11)     x(12)     x(13)     x(14)     x(15)     x(16)     x(17)     x(18)
y(0):  0,03-0,03 0,08-0,03 0,13-0,03 0,18-0,03 0,24-0,03 0,29-0,03 0,34-0,03 0,40-0,03 0,45-0,03 0,50-0,03 0,55-0,03 0,60-0,03 0,66-0,03 0,71-0,03 0,76-0,03 0,82-0,03 0,87-0,03 0,92-0,03 0,97-0,03 
y(1):  0,03-0,08 0,08-0,08 0,13-0,08 0,18-0,08 0,24-0,08 0,29-0,08 0,34-0,08 0,40-0,08 0,45-0,08 0,50-0,08 0,55-0,08 0,60-0,08 0,66-0,08 0,71-0,08 0,76-0,08 0,82-0,08 0,87-0,08 0,92-0,08 0,97-0,08 
y(2):  0,03-0,13 0,08-0,13 0,13-0,13 0,18-0,13 0,24-0,13 0,29-0,13 0,34-0,13 0,40-0,13 0,45-0,13 0,50-0,13 0,55-0,13 0,60-0,13 0,66-0,13 0,71-0,13 0,76-0,13 0,82-0,13 0,87-0,13 0,92-0,13 0,97-0,13 
y(3):  0,03-0,18 0,08-0,18 0,13-0,18 0,18-0,18 0,24-0,18 0,29-0,18 0,34-0,18 0,40-0,18 0,45-0,18 0,50-0,18 0,55-0,18 0,60-0,18 0,66-0,18 0,71-0,18 0,76-0,18 0,82-0,18 0,87-0,18 0,92-0,18 0,97-0,18 
y(4):  0,03-0,24 0,08-0,24 0,13-0,24 0,18-0,24 0,24-0,24 0,29-0,24 0,34-0,24 0,40-0,24 0,45-0,24 0,50-0,24 0,55-0,24 0,60-0,24 0,66-0,24 0,71-0,24 0,76-0,24 0,82-0,24 0,87-0,24 0,92-0,24 0,97-0,24 
y(5):  0,03-0,29 0,08-0,29 0,13-0,29 0,18-0,29 0,24-0,29 0,29-0,29 0,34-0,29 0,40-0,29 0,45-0,29 0,50-0,29 0,55-0,29 0,60-0,29 0,66-0,29 0,71-0,29 0,76-0,29 0,82-0,29 0,87-0,29 0,92-0,29 0,97-0,29 
y(6):  0,03-0,34 0,08-0,34 0,13-0,34 0,18-0,34 0,24-0,34 0,29-0,34 0,34-0,34 0,40-0,34 0,45-0,34 0,50-0,34 0,55-0,34 0,60-0,34 0,66-0,34 0,71-0,34 0,76-0,34 0,82-0,34 0,87-0,34 0,92-0,34 0,97-0,34 
y(7):  0,03-0,40 0,08-0,40 0,13-0,40 0,18-0,40 0,24-0,40 0,29-0,40 0,34-0,40 0,70-0,70 0,73-0,70 0,75-0,70 0,78-0,70 0,80-0,70 0,83-0,70 0,85-0,70 0,88-0,70 0,91-0,70 0,93-0,70 0,92-0,40 0,97-0,40 
y(8):  0,03-0,45 0,08-0,45 0,13-0,45 0,18-0,45 0,24-0,45 0,29-0,45 0,34-0,45 0,70-0,73 0,73-0,73 0,75-0,73 0,78-0,73 0,80-0,73 0,83-0,73 0,85-0,73 0,88-0,73 0,91-0,73 0,93-0,73 0,92-0,45 0,97-0,45 
y(9):  0,03-0,50 0,08-0,50 0,13-0,50 0,18-0,50 0,24-0,50 0,29-0,50 0,34-0,50 0,70-0,75 0,73-0,75 0,75-0,75 0,78-0,75 0,80-0,75 0,83-0,75 0,85-0,75 0,88-0,75 0,91-0,75 0,93-0,75 0,92-0,50 0,97-0,50 
y(10): 0,03-0,55 0,08-0,55 0,13-0,55 0,18-0,55 0,24-0,55 0,29-0,55 0,34-0,55 0,70-0,78 0,73-0,78 0,75-0,78 0,78-0,78 0,80-0,78 0,83-0,78 0,85-0,78 0,88-0,78 0,91-0,78 0,93-0,78 0,92-0,55 0,97-0,55 
y(11): 0,03-0,60 0,08-0,60 0,13-0,60 0,18-0,60 0,24-0,60 0,29-0,60 0,34-0,60 0,70-0,80 0,73-0,80 0,75-0,80 0,78-0,80 0,80-0,80 0,83-0,80 0,85-0,80 0,88-0,80 0,91-0,80 0,93-0,80 0,92-0,60 0,97-0,60 
y(12): 0,03-0,66 0,08-0,66 0,13-0,66 0,18-0,66 0,24-0,66 0,29-0,66 0,34-0,66 0,70-0,83 0,73-0,83 0,75-0,83 0,78-0,83 0,80-0,83 0,83-0,83 0,85-0,83 0,88-0,83 0,91-0,83 0,93-0,83 0,92-0,66 0,97-0,66 
y(13): 0,03-0,71 0,08-0,71 0,13-0,71 0,18-0,71 0,24-0,71 0,29-0,71 0,34-0,71 0,70-0,85 0,73-0,85 0,75-0,85 0,78-0,85 0,80-0,85 0,83-0,85 0,85-0,85 0,88-0,85 0,91-0,85 0,93-0,85 0,92-0,71 0,97-0,71 
y(14): 0,03-0,76 0,08-0,76 0,13-0,76 0,18-0,76 0,24-0,76 0,29-0,76 0,34-0,76 0,70-0,88 0,73-0,88 0,75-0,88 0,78-0,88 0,80-0,88 0,83-0,88 0,85-0,88 0,88-0,88 0,91-0,88 0,93-0,88 0,92-0,76 0,97-0,76 
y(15): 0,03-0,82 0,08-0,82 0,13-0,82 0,18-0,82 0,24-0,82 0,29-0,82 0,34-0,82 0,70-0,91 0,73-0,91 0,75-0,91 0,78-0,91 0,80-0,91 0,83-0,91 0,85-0,91 0,88-0,91 0,91-0,91 0,93-0,91 0,92-0,82 0,97-0,82 
y(16): 0,03-0,87 0,08-0,87 0,13-0,87 0,18-0,87 0,24-0,87 0,29-0,87 0,34-0,87 0,70-0,93 0,73-0,93 0,75-0,93 0,78-0,93 0,80-0,93 0,83-0,93 0,85-0,93 0,88-0,93 0,91-0,93 0,93-0,93 0,92-0,87 0,97-0,87 
y(17): 0,03-0,92 0,08-0,92 0,13-0,92 0,18-0,92 0,24-0,92 0,29-0,92 0,34-0,92 0,40-0,92 0,45-0,92 0,50-0,92 0,55-0,92 0,60-0,92 0,66-0,92 0,71-0,92 0,76-0,92 0,82-0,92 0,87-0,92 0,92-0,92 0,97-0,92 
y(18): 0,03-0,97 0,08-0,97 0,13-0,97 0,18-0,97 0,24-0,97 0,29-0,97 0,34-0,97 0,40-0,97 0,45-0,97 0,50-0,97 0,55-0,97 0,60-0,97 0,66-0,97 0,71-0,97 0,76-0,97 0,82-0,97 0,87-0,97 0,92-0,97 0,97-0,97

You can see strange rectangle with discontinuity from around area - x(7 - 16), y(7 - 16).
Look at ‘x’ coordinate in it (y value is strange too, but no so obviously).

The quad with two triangles does not explain such result at all.
Btw, when I render simple texture from file using my ‘quad.obj’, it looks very well.
While I’m going deeper the situation is becoming more and more magical. :slight_smile:

#8

@nimelord, skimming this thread, it’s not clear to me exactly what you’re testing and what your puzzlement is.

I get that you’re trying to figure something out which you attribute to “irregularity of texture grid”, and that you see some strange shapes in the rasterized texture coordinates.

But what exactly did you render with your shaders, with what state, and with what setup? Please post a short, stand-alone GLUT (or GLFW) test program that illustrates your test case.

I infer that you think you’ve only rendered the 2 tris making up a quad using the verts listed above. However, your face indexing puzzled me for a second. In the future, to avoid confusion I’d suggest posting exactly what you’re sending GL for vertex data. I see now what you’re doing. However, this is all 1-based. You’re converting to 0-based in your code, right? Otherwise you may not be sending down the verts you think you are (which would explain your output).

Scanning the table, it somewhat looks like you’ve rendered at least one triangle on top of a texture-filling quad, but looking for patterns in text number tables isn’t the best 1st step for readers or a good use of time (most aren’t going to bother). Use graphics to make this easy. Just cook an image with Xs converted to colors, and then cook one with Ys converted to colors. That’s a better first preview for you and thread readers than sifting through this table. That’s a 2nd step.

#9

Actually I try to build silhouette map by the depth map.
I use textureGather function for an algorithm.
The silhouette map was not exact result as I expected.
That fact makes me check textureGather on exact texture.

So, I decided to make some experimants using map with extra low resolutions, such as 3x3, 4x4, 5x5.

First of all I builded depth map and try to emit texture coordinates as an output texture.
But I recieved irregular texture coordinates in the output textue.

Exact shaders you can see int the topic.

I was confused by the irregularity of thexture coordinates.
You can see results in the topic for 3x3, 4x4, 5x5.

But It seems I localized the problem.
If I set altha to 1.0 instead variable then all is ok with regularity.

Actually I think the coordinates of the texture always was - ok.
This just some sort of normazisation of the output color.