Math 如何在opengl中计算围绕点的圆(由texel组成)

Math 如何在opengl中计算围绕点的圆(由texel组成),math,opengl,opengl-es,Math,Opengl,Opengl Es,我正在计算碎片着色器中一个点周围的一个圆。问题是纹理的变化部分不是一个圆,而是一个椭圆形。形状实际上取决于纹理的形状。如果纹理是一个完美的正方形,我会得到一个完美的圆形,但当它是矩形时,我会得到一个椭圆形。这是当前片段着色器: varying highp vec2 textureCoordinate; uniform sampler2D inputImageTexture; uniform highp vec2 center; uniform highp float radius; unifo

我正在计算碎片着色器中一个点周围的一个圆。问题是纹理的变化部分不是一个圆,而是一个椭圆形。形状实际上取决于纹理的形状。如果纹理是一个完美的正方形,我会得到一个完美的圆形,但当它是矩形时,我会得到一个椭圆形。这是当前片段着色器:

varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;

uniform highp vec2 center;
uniform highp float radius;
uniform highp float scale;


void main()
{
   highp vec2 textureCoordinateToUse = textureCoordinate;
   highp float dist = distance(center, textureCoordinate);
   textureCoordinateToUse -= center;
   if (dist < radius)
   {
     highp float percent = 1.0 - ((radius - dist) / radius) * scale;
     percent = percent * percent;

     textureCoordinateToUse = textureCoordinateToUse * percent;
     textureCoordinateToUse += center;

     gl_FragColor = texture2D(inputImageTexture, textureCoordinateToUse );
   }

   textureCoordinateToUse += center;
   gl_FragColor = texture2D(inputImageTexture,textureCoordinate);   
}
改变highp vec2纹理坐标;
均匀的二维纹理;
均匀高p-vec2中心;
均匀高p浮动半径;
均匀高p浮标;
void main()
{
highp vec2 TextureCoordinatose=纹理坐标;
highp float dist=距离(中心,纹理坐标);
纹理坐标-=中心;
if(距离<半径)
{
高P浮动百分比=1.0-((半径-距离)/半径)*刻度;
百分比=百分比*百分比;
纹理坐标=纹理坐标*百分比;
纹理坐标+=中心;
gl_FragColor=纹理2D(输入图像纹理,纹理坐标);
}
纹理坐标+=中心;
gl_FragColor=纹理2D(输入图像纹理,纹理坐标);
}
更新着色器代码:

highp float aspectRatio = 854.0 / 480.0;
     //highp vec2 textureCoordinateToUse = textureCoordinate;
     highp vec2 textureCoordinateToUse = vec2(textureCoordinate.x, (textureCoordinate.y *    aspectRatio + 0.5 - 0.5 * aspectRatio));
   highp float dist = distance(center, textureCoordinateToUse);
   textureCoordinateToUse -= center;
   if (dist < radius)
   {
     highp float percent = 1.0 - ((radius - dist) / radius) * scale;
     percent = percent * percent;

     textureCoordinateToUse = textureCoordinateToUse * percent;
     textureCoordinateToUse += center;

     gl_FragColor = texture2D(inputImageTexture, textureCoordinateToUse );
     return;
   }

   textureCoordinateToUse += center;
   gl_FragColor = texture2D(inputImageTexture,textureCoordinate); 
highp float aspectRatio=854.0/480.0;
//highp vec2 TextureCoordinatose=纹理坐标;
highp vec2 TextureCoordinatore=vec2(TextureCoordination.x,(TextureCoordination.y*aspectRatio+0.5-0.5*aspectRatio));
highp float dist=距离(中心,纹理坐标);
纹理坐标-=中心;
if(距离<半径)
{
高P浮动百分比=1.0-((半径-距离)/半径)*刻度;
百分比=百分比*百分比;
纹理坐标=纹理坐标*百分比;
纹理坐标+=中心;
gl_FragColor=纹理2D(输入图像纹理,纹理坐标);
返回;
}
纹理坐标+=中心;
gl_FragColor=纹理2D(输入图像纹理,纹理坐标);

我看到您试图使用我的凸出变形碎片着色器。虽然你还没问过问题,但我想我可能知道你想要什么

如果为矩形输入纹理提供规格化0.0-1.0范围内的纹理坐标,则上述操作将在椭圆区域而不是圆形区域上进行。这是因为上面的计算工作在纹理坐标空间,而不是图像坐标空间

要更正此问题,可以执行以下两种操作之一。首先,您可以提供纹理坐标,用于说明图像的纵横比(其中一个坐标的最大值不为1.0)

第二,你可以像我一样,把图像的纵横比作为一个统一的输入,并用它来校正图像的矩形性质。如果使用图像的宽度和高度之间的比率为着色器提供了一致的
aspectRatio
,则可以使用替换着色器主体中的第一行

 highp vec2 textureCoordinateToUse = vec2(textureCoordinate.x, (textureCoordinate.y * aspectRatio + 0.5 - 0.5 * aspectRatio));

它将在一个圆形区域上运行。

您能在代码中添加一些注释吗?什么是比例,什么是百分比=1.0-((半径-距离)/半径)*比例;应该代表什么?问题是如何得到一个圆而不是一个椭圆形。比例是1。百分比会影响圆形/椭圆形的内容,但不会影响其形状,因此请不要注意它。Brad Larson将进行救援。感谢您的重播和GPUImage。正如您所说,这段代码来自凸起着色器。我用新代码编辑了我的帖子。我得到了圆形,但是凸起效果没有应用到正确的纹理坐标上。有什么帮助吗?谢谢,我忘了告诉你我用用户触摸屏输入了中央制服。应用线条(本文更新了当前代码)后,凸出效果仅应用于屏幕的1×1正方形中心。因此,当我触摸屏幕的左下角时,凸起效果出现在屏幕中心的1乘1正方形的左下角。很糟糕的解释,但我希望你理解我的问题。非常感谢。