我正在利用Brad Larson精彩的GPUImage库进行图像处理。谢谢Brad,干得好。
我正在尝试添加一个新的过滤器,允许在实时视频源上绘制对象。
这是早期阶段。此时,我只想在视频源上绘制3个三角形。代码如下。
这似乎有效,但在应用此效果时,我会在宽高比上出现间歇性变化。然后它安定下来。 filterView是垂直拉伸的。看起来像一个闪光灯。
HEADER:
// GPUImageParticles.h
#import <GLKit/GLKit.h>
#import "GPUImageFilter.h"
@interface GPUImageParticles : GPUImageFilter{
GLint triVertexBufferIDUniform;
GLuint triVertexBufferID;
}
- (void)update:(CMTime)frameTime;
- (void)setDrawColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent;
@end
实现:
// GPUImageParticles.m
#import "GPUImageParticles.h"
// SHADERS
NSString *const kParticleGenVSString = SHADER_STRING
(
attribute vec4 position;
void main()
{
gl_Position = position;
}
);
NSString *const kParticleGenFSString = SHADER_STRING
(
uniform lowp vec3 drawColor;
void main()
{
gl_FragColor = vec4(drawColor, 1.0);
}
);
// END SHADERS
@implementation GPUImageParticles
/////////////////////////////////////////////////////////////////
// This data type is used to store information for each vertex
typedef struct {
GPUVector3 positionCoords;
}
SceneVertex;
/////////////////////////////////////////////////////////////////
// Define vertex data for a triangle to use in example
static const SceneVertex triVertices[] =
{
{{0.0f, 0.0f, 0.0f}},
{{0.0f, 0.1f, 0.0f}},
{{0.1f, 0.1f, 0.0f}},
{{-0.3f, -0.7f, 0.0}}, // lower left corner
{{ 0.3f, -0.7f, 0.0}}, // lower right corner
{{ 0.01f, 0.5f, 0.0}}, // upper left corner
{{-0.5f, -0.5f, 0.0}}, // lower left corner
{{-0.25f, -0.25f, 0.0}}, // lower right corner
{{-0.5f, -0.25f, 0.0}} // upper left corner
};
- (id)init;
{
if (!(self = [super initWithVertexShaderFromString:kParticleGenVSString fragmentShaderFromString:kParticleGenFSString]))
{
return nil;
}
runSynchronouslyOnVideoProcessingQueue(^{
triVertexBufferIDUniform =[filterProgram uniformIndex:@"triVertexBuffer"];
drawColorUniform = [filterProgram uniformIndex:@"drawColor"];
[self setDrawColorRed:1.0 green:0.0 blue:1.0];
glGenBuffers(1, // STEP 1
&triVertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, // STEP 2
triVertexBufferID);
glBufferData( // STEP 3
GL_ARRAY_BUFFER, // Initialize buffer contents
sizeof(triVertices), // Number of bytes to copy
triVertices, // Address of bytes to copy
GL_STATIC_DRAW); // Hint: cache in GPU memory
glBindBuffer(GL_ARRAY_BUFFER, 0);
});
return self;
}
- (void) dealloc;
{
}
-(void)update:(CMTime)frameTime;
{
if (self.preventRendering)
{
return;
}
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext setActiveShaderProgram:filterProgram];
[self setFilterFBO];
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ONE);
glEnable(GL_BLEND);
glVertexAttribPointer(filterPositionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(SceneVertex), triVertices);
glDrawArrays(GL_TRIANGLES, 0, (unsigned int)9);
glDisable(GL_BLEND);
[self informTargetsAboutNewFrameAtTime:frameTime];
});
}
- (void)setDrawColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent;
{
GPUVector3 drawColor = {redComponent, greenComponent, blueComponent};
[self setVec3:drawColor forUniform:drawColorUniform program:filterProgram];
}
@end
答案 0 :(得分:0)
引用Brad Larson提供的FilterShowcase示例中包含的GPUIMAGE_NOBLECORNERDETECTION示例。
GPUImageCrosshairGenerator *crosshairGenerator = [[GPUImageCrosshairGenerator alloc] init];
crosshairGenerator.crosshairWidth = 15.0;
[crosshairGenerator forceProcessingAtSize:CGSizeMake(480.0, 640.0)];
[(GPUImageHarrisCornerDetectionFilter *)filter setCornersDetectedBlock:^(GLfloat* cornerArray, NSUInteger cornersDetected, CMTime frameTime) {
[crosshairGenerator renderCrosshairsFromArray:cornerArray count:cornersDetected frameTime:frameTime];
}];
GPUImageAlphaBlendFilter *blendFilter = [[GPUImageAlphaBlendFilter alloc] init];
[blendFilter forceProcessingAtSize:CGSizeMake(480.0, 640.0)];
GPUImageGammaFilter *gammaFilter = [[GPUImageGammaFilter alloc] init];
[videoCamera addTarget:gammaFilter];
[gammaFilter addTarget:blendFilter];
[crosshairGenerator addTarget:blendFilter];
[blendFilter addTarget:filterView];