3
votes

I want to apply an uniform checkerboard texture to a cylinder surface of height h, and semiradii (a,b).

I've implemented this shader:

Vertex shader:

varying vec2 texture_coordinate;
float twopi = 6.283185307;
float pi=3.141592654;

float ra = 1.5;
float rb= 1.0;

void main()
{
// Transforming The Vertex
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
// -pi/2 < theta < pi/2
float theta =  (atan2( rb*gl_Vertex.y , ra*gl_Vertex.x)+pi*0.5)/pi;

// Passing The Texture Coordinate Of Texture Unit 0 To The Fragment Shader
    texture_coordinate = vec2(  theta , -(-gl_Vertex.z+0.5) );
}

Fragment shader:

varying vec2 texture_coordinate;
uniform sampler2D my_color_texture;
void main()
{
    // Sampling The Texture And Passing It To The Frame Buffer
    gl_FragColor = texture2D(my_color_texture, texture_coordinate);
}

while on client side I've specified the following options:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

My texture is a 3768x1200 checkerboard. Now I would like that the texture is applied in order to keep the checkerboard uniform (squares without stretch), but I obtain a correct aspect ratio only in the less curved part of the surface, while on the more curved parts the tiles are stretched.

I would like to understand how to apply the texture without distorting and stretching it, maybe by repeating the texture instead of stretching it.

In the first image the tiles on the less curved part are square

while where the curvature is higher they are more stretched

I also have a problem of strange flickering on the borders of the texture, where the two borders intersect, how to solve it (it can be seen in the second image)?

1
I'm not clear on what your problem is, you say that the squares are not "square" but that's as you'd expect when you view something at an angle with a perspective view, unless I've misunderstood your question.jcoder
no the perspective is here only to show the shape of the object. What I'm asking is what kind of texture coordinate function should I use in order to prevent the stretching of tiles on the borders of the ellipse. Should I modify the texture itself?linello
Are you trying to map the texture as if it were projected onto the object from the camera?Bojangles

1 Answers

2
votes

You can modify the texture coordinates to "shrink" it on an object a bit. What you can't do is to parametrize the texture coordinates to scale non-linearly.

So, the options are:

  1. Quantize the sampling, modifying texture coordinates to better accomodate the non-circularity (dynamic, but quality is low when using low-poly tesselation; it's the simplest solution to implement, though).
  2. Use fragment shader to scale texture coordinates non-linearly (possibly a bit more complicated, but dynamic and giving quite good results, depending on the texture size, filtering used and the texture contents(!))
  3. Modify the texture (static solution - will work only for given Ra/Rb ratio. However, the quality will be the best possible).

As to the flickering on the borders, you have to generate mipmaps for your textures.

Let me know if you need more information.