-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathGPUImageSolidColorGenerator.m
More file actions
125 lines (99 loc) · 3.51 KB
/
Copy pathGPUImageSolidColorGenerator.m
File metadata and controls
125 lines (99 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#import "GPUImageSolidColorGenerator.h"
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUSolidColorFragmentShaderString = SHADER_STRING
(
precision lowp float;
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform vec4 color;
uniform float useExistingAlpha;
void main()
{
lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
gl_FragColor = vec4(color.rgb, max(textureColor.a, 1.0 - useExistingAlpha));
}
);
#else
NSString *const kGPUSolidColorFragmentShaderString = SHADER_STRING
(
varying vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform vec4 color;
uniform float useExistingAlpha;
void main()
{
vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
gl_FragColor = vec4(color.rgb, max(textureColor.a, 1.0 - useExistingAlpha));
}
);
#endif
@implementation GPUImageSolidColorGenerator
@synthesize color = _color;
- (id)init;
{
if (!(self = [super initWithFragmentShaderFromString:kGPUSolidColorFragmentShaderString]))
{
return nil;
}
colorUniform = [filterProgram uniformIndex:@"color"];
useExistingAlphaUniform = [filterProgram uniformIndex:@"useExistingAlpha"];
_color = (GPUVector4){0.0f, 0.0f, 0.5f, 1.0f};
self.useExistingAlpha = NO;
return self;
}
- (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates;
{
if (self.preventRendering)
{
return;
}
runSynchronouslyOnVideoProcessingQueue(^{
[GPUImageContext setActiveShaderProgram:filterProgram];
outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:[self sizeOfFBO] textureOptions:self.outputTextureOptions onlyTexture:NO];
[outputFramebuffer activateFramebuffer];
glClearColor(_color.one, _color.two, _color.three, _color.four);
glClear(GL_COLOR_BUFFER_BIT);
});
}
#pragma mark -
#pragma mark Accessors
- (void)forceProcessingAtSize:(CGSize)frameSize;
{
[super forceProcessingAtSize:frameSize];
if (!CGSizeEqualToSize(inputTextureSize, CGSizeZero))
{
[self newFrameReadyAtTime:kCMTimeIndefinite atIndex:0];
}
}
- (void)addTarget:(id<GPUImageInput>)newTarget atTextureLocation:(NSInteger)textureLocation;
{
[super addTarget:newTarget atTextureLocation:textureLocation];
if (!CGSizeEqualToSize(inputTextureSize, CGSizeZero))
{
runSynchronouslyOnVideoProcessingQueue(^{
[newTarget setInputSize:inputTextureSize atIndex:textureLocation];
[newTarget newFrameReadyAtTime:kCMTimeIndefinite atIndex:textureLocation];
});
}
}
- (void)setColor:(GPUVector4)newValue;
{
[self setColorRed:newValue.one green:newValue.two blue:newValue.three alpha:newValue.four];
}
- (void)setColorRed:(CGFloat)redComponent green:(CGFloat)greenComponent blue:(CGFloat)blueComponent alpha:(CGFloat)alphaComponent;
{
_color.one = (GLfloat)redComponent;
_color.two = (GLfloat)greenComponent;
_color.three = (GLfloat)blueComponent;
_color.four = (GLfloat)alphaComponent;
// [self setVec4:_color forUniform:colorUniform program:filterProgram];
runAsynchronouslyOnVideoProcessingQueue(^{
[self newFrameReadyAtTime:kCMTimeIndefinite atIndex:0];
});
}
- (void)setUseExistingAlpha:(BOOL)useExistingAlpha;
{
_useExistingAlpha = useExistingAlpha;
[self setInteger:(useExistingAlpha ? 1 : 0) forUniform:useExistingAlphaUniform program:filterProgram];
}
@end