gpt4 book ai didi

c# - 如何投影OpenGL控件的顶部和底部区域(圆柱投影)?

转载 作者:行者123 更新时间:2023-11-30 21:30:50 25 4
gpt4 key购买 nike

使用下面的代码,我可以在 openGL 控件中显示图像。呈长方形。现在我想将这个矩形的顶部和底部区域投影为圆柱形。我的意思是需要在 openGL 上执行矩形到圆柱形的投影。我怎样才能做到这一点?

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

希望对着色器代码的 vTexCoord 进行一些更改会产生结果。

enter image description here

最佳答案

如果你想在 2d 平面上投影 2D 纹理,就像它是 3D 圆柱体一样,那么你必须通过 arcus function 转换纹理坐标。 (asinacos)在片段着色器中。

[0, 1] 范围内的纹理坐标必须通过 asin 函数与 [-90°, 90°] 范围内的圆上的角度相关联。这个角度可以线性映射到[0, 1]范围内的新纹理坐标。

函数的输入是一个角度,返回值是一个距离:

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

顶点着色器:

attribute vec3 a_position;
varying vec2 vTexCoord;

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

片段着色器:

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

查看原始代码的结果与使用asin映射的代码之间的区别:


在二维平面的投影中,圆柱体的顶部和底部是一个 ellipse , 可以表示为:

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

纹理的投影必须在顶部和底部进行挤压以形成椭圆形:

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

必须使用 discard 丢弃被剪裁的区域fragment shader 中的关键字:

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

看看没有椭圆失真的结果和使用椭圆失真的代码之间的区别:


结合asin纹理坐标映射和椭圆变形的片段着色器:

片段着色器:

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

合并结果:

关于c# - 如何投影OpenGL控件的顶部和底部区域(圆柱投影)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53943867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com