我一直在开发iOS应用程序,它在手指触摸图像上应用模糊。我使用OpenGL。我写过顶点&片段着色器应用高斯模糊。当我将整个图像矩形坐标(0-1)传递到顶点着色器时,它会将模糊应用于整个图像而没有任何问题。现在,我试图用手指触摸做同样的事情。我捕获触摸点,转换为0-1范围并将该点传递到着色器。但它不会模糊,而是会扰乱原始图像。 下面是@ touch move执行的主要代码:
-(void)setupVBOsBlur:(CGPoint)start For:(CGPoint)end
{
static GLfloat* vertexBuffer = NULL;
static NSUInteger vertexMax = 64;
NSUInteger vertexCount = 0,
count,
i;
// Convert locations from Points to Pixels
CGFloat scale = self.contentScaleFactor;
start.x *= scale;
start.y *= scale;
end.x *= scale;
end.y *= scale;
// Allocate vertex array buffer
if(vertexBuffer == NULL)
vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));
// Add points to the buffer so there are drawing points every X pixels
count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y))) , 1);
for(i = 0; i < count; ++i) {
if(vertexCount == vertexMax) {
vertexMax = 2 * vertexMax;
vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
}
vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
vertexCount += 1;
}
GLuint vb;
glGenBuffers(1, &vb);
glBindBuffer(GL_ARRAY_BUFFER, vb);
glBufferData(GL_ARRAY_BUFFER, vertexCount*2*sizeof(GLfloat), vertexBuffer, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, 0);
GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, backingWidth, 0, backingHeight, -1, 1);
GLKMatrix4 modelViewMatrix = GLKMatrix4Identity; // this sample uses a constant identity modelView matrix
mvpMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
[self compileShadersForFingerBlur];
//set MVP
glUniformMatrix4fv(mvpMatrixSlot, 1, GL_FALSE, mvpMatrix.m);
//glUniform2f(myTextCoordSlot, start.x/320.0, ( self.bounds.size.height - start.y)/480.0);
glUniform2f(myTextCoordSlot, start.x/320.0, ( self.bounds.size.height - start.y)/480.0);
glUniform1i(amount, 0);
/*
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureHandle);
glUniform1i(textureUniformSlot, 0);
*/
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glDrawArrays(GL_POINTS, 0, vertexCount);
[eaglContext presentRenderbuffer:GL_RENDERBUFFER];
}
以下是我的顶点和片段着色器: 顶点着色器(水平模糊):
/* HBlurVertexShader.glsl */
attribute vec4 Position;
uniform mat4 MVP;
uniform vec2 myTextCoord;
varying vec2 v_texCoord;
varying vec2 v_blurTexCoords[14];
void main()
{
gl_PointSize = 5.0;
gl_Position = MVP * Position;
v_texCoord = myTextCoord;
v_blurTexCoords[ 0] = v_texCoord + vec2(-0.028, 0.0);
v_blurTexCoords[ 1] = v_texCoord + vec2(-0.024, 0.0);
v_blurTexCoords[ 2] = v_texCoord + vec2(-0.020, 0.0);
v_blurTexCoords[ 3] = v_texCoord + vec2(-0.016, 0.0);
v_blurTexCoords[ 4] = v_texCoord + vec2(-0.012, 0.0);
v_blurTexCoords[ 5] = v_texCoord + vec2(-0.008, 0.0);
v_blurTexCoords[ 6] = v_texCoord + vec2(-0.004, 0.0);
v_blurTexCoords[ 7] = v_texCoord + vec2( 0.004, 0.0);
v_blurTexCoords[ 8] = v_texCoord + vec2( 0.008, 0.0);
v_blurTexCoords[ 9] = v_texCoord + vec2( 0.012, 0.0);
v_blurTexCoords[10] = v_texCoord + vec2( 0.016, 0.0);
v_blurTexCoords[11] = v_texCoord + vec2( 0.020, 0.0);
v_blurTexCoords[12] = v_texCoord + vec2( 0.024, 0.0);
v_blurTexCoords[13] = v_texCoord + vec2( 0.028, 0.0);
}
顶点着色器(垂直模糊)
/* VBlurVertexShader.glsl */
varying vec2 v_texCoord;
varying vec2 v_blurTexCoords[14];
void main()
{
v_blurTexCoords[ 0] = v_texCoord + vec2(0.0, -0.028);
v_blurTexCoords[ 1] = v_texCoord + vec2(0.0, -0.024);
v_blurTexCoords[ 2] = v_texCoord + vec2(0.0, -0.020);
v_blurTexCoords[ 3] = v_texCoord + vec2(0.0, -0.016);
v_blurTexCoords[ 4] = v_texCoord + vec2(0.0, -0.012);
v_blurTexCoords[ 5] = v_texCoord + vec2(0.0, -0.008);
v_blurTexCoords[ 6] = v_texCoord + vec2(0.0, -0.004);
v_blurTexCoords[ 7] = v_texCoord + vec2(0.0, 0.004);
v_blurTexCoords[ 8] = v_texCoord + vec2(0.0, 0.008);
v_blurTexCoords[ 9] = v_texCoord + vec2(0.0, 0.012);
v_blurTexCoords[10] = v_texCoord + vec2(0.0, 0.016);
v_blurTexCoords[11] = v_texCoord + vec2(0.0, 0.020);
v_blurTexCoords[12] = v_texCoord + vec2(0.0, 0.024);
v_blurTexCoords[13] = v_texCoord + vec2(0.0, 0.028);
}
Fragment Shader:
precision mediump float;
uniform sampler2D texture;
uniform int amount;
varying vec2 v_texCoord;
varying vec2 v_blurTexCoords[14];
void main()
{
gl_FragColor = vec4(0.0);
if(amount > 6)
{
gl_FragColor += texture2D(texture, v_blurTexCoords[ 0])*0.0044299121055113265;
gl_FragColor += texture2D(texture, v_blurTexCoords[ 1])*0.00895781211794;
gl_FragColor += texture2D(texture, v_blurTexCoords[ 2])*0.0215963866053;
gl_FragColor += texture2D(texture, v_blurTexCoords[ 3])*0.0443683338718;
gl_FragColor += texture2D(texture, v_blurTexCoords[ 4])*0.0776744219933;
gl_FragColor += texture2D(texture, v_blurTexCoords[ 5])*0.115876621105;
gl_FragColor += texture2D(texture, v_blurTexCoords[ 6])*0.147308056121;
gl_FragColor += texture2D(texture, v_texCoord)*0.159576912161;
gl_FragColor += texture2D(texture, v_blurTexCoords[ 7])*0.147308056121;
gl_FragColor += texture2D(texture, v_blurTexCoords[ 8])*0.115876621105;
gl_FragColor += texture2D(texture, v_blurTexCoords[ 9])*0.0776744219933;
gl_FragColor += texture2D(texture, v_blurTexCoords[10])*0.0443683338718;
gl_FragColor += texture2D(texture, v_blurTexCoords[11])*0.0215963866053;
gl_FragColor += texture2D(texture, v_blurTexCoords[12])*0.00895781211794;
gl_FragColor += texture2D(texture, v_blurTexCoords[13])*0.0044299121055113265;
}
else
gl_FragColor = texture2D(texture, v_texCoord);
}
答案 0 :(得分:0)
继续发表评论:
你正在采取的方法似乎并不是一个非常好的方法。修复评论中的两件事仍然会产生其他问题。想到1,如果用户在同一个地方拖动2行,会发生什么?图像会在那个位置被模糊两次吗?它可能但我可以向你保证它会失去它的功能。
要更加全面地了解您要做的事情:您希望用户能够选择要模糊的部分图像,这正是您应该做的。这导致您需要另一个仅存储该数据的缓冲区,或者像素是否模糊(也可以存储模糊的强度)。所以一般来说我建议创建一个带附加纹理的FBO(帧缓冲对象)。然后移动已有的绘图代码(带点)在该缓冲区上绘制特定颜色。现在,每次FBO更改时,您需要使用2个纹理(原始和FBO)绘制原始FBO纹理为空的纹理图像以及纹理已满的模糊图像重绘整个主缓冲区。此过程还表明您在加载时间中模糊整个图像并使用3个纹理(原始,模糊,画布)。
FBO方法非常灵活,在提到的情况下,你可以使用它来模糊位置。您可以使用不同的颜色通道获得多种效果和强度,并且与您在主缓冲区中使用的资源无关。另一方面,它可能有点难以实现:FBO大小必须是2的幂,你需要只使用它的某个部分(不是问题),将其纹理应用到主缓冲区将迫使你计算一个新的纹理坐标缓冲区(中等数学问题),如果你应用一些缩放和移动的东西可以变得非常困难(大问题)。从你的案例中猜测你可以更轻松地做到这一点。
不是为FBO绘制一些颜色,而是可以使用一些可以附加到主帧缓冲区的其他缓冲区:仅绘制到深度缓冲区或模板缓冲区(小设置),因为您可能不使用它们所有。现在最后你可能甚至不在缓冲区上使用alpha通道:
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE)
)并像以前一样绘制原始图像。 / LI>
1.0
的颜色并重复使用您已有的绘图代码,除非您只绘制颜色,而不是模糊纹理glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA)
并绘制全屏模糊纹理。一切都完成了,这里只是一个小小的注释:如果你在某个时候想要一个图像快照而你将使用glReadPixels
,你需要了解你的alpha通道是一团糟,你需要在创建时跳过它来自这些数据的CGImage。