简体   繁体   中英

Texture mapping on a cylinder

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.

在第一个图像中,较小弯曲部分上的瓦片是方形的

而曲率越高,它们就越拉伸

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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