How to project top and bottom area of openGL contr

2020-02-14 10:35发布

问题:

Using below code I can display an image in openGL control. Which is in rectangular shape. Now I want to project top and bottom area of this rectangular to a cylindrical shape.I mean need to perform a rectangular to cylidrical projection on openGL. How can I achieve this?

private void CreateShaders()
{
    /***********Vert Shader********************/
    vertShader = GL.CreateShader(ShaderType.VertexShader);
    GL.ShaderSource(vertShader, @"attribute vec3 a_position;
                                varying vec2 vTexCoord;
                                void main() {
                                vTexCoord = (a_position.xy + 1) / 2;
                                gl_Position = vec4(a_position, 1);
                                }");
    GL.CompileShader(vertShader);

    /***********Frag Shader ****************/
    fragShader = GL.CreateShader(ShaderType.FragmentShader);
    GL.ShaderSource(fragShader, @"precision highp float;
    uniform sampler2D sTexture; varying vec2 vTexCoord;
                         void main ()
                         {
            //  vec4 color  = texture2D (sTexture, vTexCoord);
            vec2 x =vTexCoord - vec2(0.5);
            float r = length(x);//radious
            float u = r*atan( vTexCoord.x/sqrt(r*r-(vTexCoord.x*vTexCoord.x )));
            float v = (r*vTexCoord.y)/sqrt(r*r );
            vec4 color   = texture2D(sTexture, vec2(u, v));
            gl_FragColor = color;
                         }");
    GL.CompileShader(fragShader);
}

Hope making some changes on vTexCoord of shader code will do the result.

回答1:

If you want to project a 2D textur on a 2d plane as it would be a 3D cylinder, then you have to transform the texture coordinate by an arcus function (asin or acos) in the fragment shader.

The texture coordinate in the range [0, 1] have to be associated to an angle on a circle in the range [-90°, 90°] by the asin function. This angle can be linear mapped to the new texture coordinate in the range [0, 1].

The input to the function is an angle and the return value is a distance:

float u = asin( vTexCoord.x*2.0-1.0 ) / 3.141593 + 0.5;

Vertex shader:

attribute vec3 a_position;
varying vec2 vTexCoord;

void main()
{
    vTexCoord   = (a_position.xy + 1) / 2;
    gl_Position = vec4(a_position, 1);
}

Fragment shader:

precision highp float;
uniform sampler2D sTexture;
varying vec2 vTexCoord;

void main()
{
    float u = asin( vTexCoord.x*2.0-1.0 ) / 3.141593 + 0.5;
    float v = vTexCoord.y; 

    vec4 color   = texture2D(sTexture, vec2(u, v));
    gl_FragColor = color;
}

See the difference between the result of the original code and the code which uses the asin mapping:


In the projection to a 2D plane, the top and bottom of the cylinder is an ellipse, which can be expressed by:

float b = 0.3;
float y = b * sqrt(1.0 - x*x)

The projection of the texture has to be squeezed at the top and the bottom to form an elliptical shape:

float v_scale = (1.0 + b) / (1.0 + y);
float v = (pos.y * v_scale) * 0.5 + 0.5;

The area which is clipped has to be discarded by using the discard keyword in the fragment shader:

if ( v < 0.0 || v > 1.0 )
    discard;

See the difference between the result without the elliptical distortion and the code which uses the elliptical distortion:


The fragment shader which combines the asin texture coordinate mapping and the elliptical distortion:

Fragment shader:

precision highp float;
uniform sampler2D sTexture;
varying vec2 vTexCoord;

void main()
{
    vec2  pos     = vTexCoord.xy * 2.0 - 1.0;
    float b       = 0.3;
    float v_scale = (1.0 + b) / (1.0 + b * sqrt(1.0 - pos.x*pos.x));

    float u = asin( pos.x ) / 3.1415 + 0.5;
    float v = (pos.y * v_scale) * 0.5 + 0.5;
    if ( v < 0.0 || v > 1.0 )
        discard;

    vec3 texColor = texture2D( u_texture, vec2(u, v) ).rgb;
    gl_FragColor  = vec4( texColor.rgb, 1.0 );
}

The combined result: