我一直在开发 iOS 应用程序,它在图像上的手指触摸上应用模糊。我为此使用OpenGL。我已经编写了顶点和片段着色器来应用高斯模糊。当我将整个图像矩形坐标(0-1)传递到顶点着色器时,它会将模糊应用于整个图像而没有任何问题。现在,我正在尝试在手指触摸上做同样的事情。我捕获接触点,转换为 0-1 范围并将该点传递给着色器。但它不会模糊,而是会干扰原始图像。以下是在@ touch 移动时执行的主要代码:
-(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);
}
片段着色器:
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);
}