-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDVGOglEffectAnimatedTrackFrameRemap.m
More file actions
348 lines (322 loc) · 17.9 KB
/
DVGOglEffectAnimatedTrackFrameRemap.m
File metadata and controls
348 lines (322 loc) · 17.9 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#import "DVGOglEffectAnimatedTrackFrameRemap.h"
enum
{
UNIFORM_ANIMREMAP_BLN
};
// default shader - as is, no blending
static NSString* kEffectFallthrouVertexShader = SHADER_STRING
(
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}
);
static NSString* kEffectFallthrouFragmentShader = SHADER_STRING
(
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
void main()
{
gl_FragColor = texture2D(inputImageTexture, textureCoordinate);
}
);
// usually used shader - blending with alpha
static NSString* kEffectBlendAlphaVertexShader = SHADER_STRING
(
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}
);
static NSString* kEffectBlendAlphaFragmentShader = SHADER_STRING
(
const highp float kEps = 0.001;
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform lowp float blendingFactor;
void main()
{
lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
textureColor = vec4(textureColor.r*blendingFactor,textureColor.g*blendingFactor,textureColor.b*blendingFactor,blendingFactor);
gl_FragColor = textureColor;
}
);
// brightness-based shader: low-values in any channel makes blending edge
static NSString* kEffectBlendBrightnessVertexShader = SHADER_STRING
(
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}
);
static NSString* kEffectBlendBrightnessFragmentShader = SHADER_STRING
(
const highp float kEps = 0.1;
varying highp vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform lowp float blendingFactor;
void main()
{
lowp float blendingFactor2 = 1.0;
lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
if(textureColor.r < kEps && textureColor.g < kEps && textureColor.b < kEps){
lowp float mincolo = min(textureColor.r, textureColor.g);
mincolo = min(mincolo, textureColor.b);
blendingFactor2 = mincolo/kEps;
}
textureColor = vec4(textureColor.r*blendingFactor*blendingFactor2,textureColor.g*blendingFactor*blendingFactor2,textureColor.b*blendingFactor*blendingFactor2,blendingFactor*blendingFactor2);
gl_FragColor = textureColor;
}
);
@interface DVGOglEffectAnimatedTrackFrameRemap ()
@end
@implementation DVGOglEffectAnimatedTrackFrameRemap
- (id)init
{
self = [super init];
if(self) {
}
return self;
}
-(void)prepareOglResources
{
[super prepareOglResources];
[self prepareVertexShader:kEffectFallthrouVertexShader withFragmentShader:kEffectFallthrouFragmentShader
withAttribs:@[
@[@(ATTRIB_VERTEX_RPL), @"position"],
@[@(ATTRIB_TEXCOORD_RPL), @"inputTextureCoordinate"]
]
withUniforms:@[
@[@(UNIFORM_RENDER_TRANSFORM_RPL), @"renderTransform"],
@[@(UNIFORM_SHADER_SAMPLER_RPL), @"inputImageTexture"]
]
];
[self prepareVertexShader:kEffectBlendAlphaVertexShader withFragmentShader:kEffectBlendAlphaFragmentShader
withAttribs:@[
@[@(ATTRIB_VERTEX_RPL), @"position"],
@[@(ATTRIB_TEXCOORD_RPL), @"inputTextureCoordinate"]
]
withUniforms:@[
@[@(UNIFORM_RENDER_TRANSFORM_RPL), @"renderTransform"],
@[@(UNIFORM_SHADER_SAMPLER_RPL), @"inputImageTexture"],
@[@(UNIFORM_ANIMREMAP_BLN), @"blendingFactor"]
]
];
[self prepareVertexShader:kEffectBlendBrightnessVertexShader withFragmentShader:kEffectBlendBrightnessFragmentShader
withAttribs:@[
@[@(ATTRIB_VERTEX_RPL), @"position"],
@[@(ATTRIB_TEXCOORD_RPL), @"inputTextureCoordinate"]
]
withUniforms:@[
@[@(UNIFORM_RENDER_TRANSFORM_RPL), @"renderTransform"],
@[@(UNIFORM_SHADER_SAMPLER_RPL), @"inputImageTexture"],
@[@(UNIFORM_ANIMREMAP_BLN), @"blendingFactor"]
]
];
}
-(void)releaseOglResources
{
[super releaseOglResources];
}
- (void)renderIntoPixelBuffer:(CVPixelBufferRef)destBuffer
prevBuffer:(CVPixelBufferRef)prevBuffer
trackBuffer:(CVPixelBufferRef)trackBuffer
trackOrient:(DVGGLRotationMode)trackOrientation
atTime:(CGFloat)time withTween:(float)tweenFactor
{
[self prepareContextForRendering];
if(trackBuffer == nil){
// Adjusting previous frame, not track
trackBuffer = prevBuffer;
trackOrientation = kDVGGLNoRotation;
}
CVOpenGLESTextureRef trckBGRATexture = [self bgraTextureForPixelBuffer:trackBuffer];
CVOpenGLESTextureRef destBGRATexture = [self bgraTextureForPixelBuffer:destBuffer];
// Attach the destination texture as a color attachment to the off screen frame buffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, CVOpenGLESTextureGetTarget(destBGRATexture), CVOpenGLESTextureGetName(destBGRATexture), 0);
CGFloat vport_w = CVPixelBufferGetWidth(destBuffer);//CVPixelBufferGetWidthOfPlane(destBuffer, 0);// ios8 compatible way
CGFloat vport_h = CVPixelBufferGetHeight(destBuffer);//CVPixelBufferGetHeightOfPlane(destBuffer, 0);// ios8 compatible way
glViewport(0, 0, (int)vport_w, (int)vport_h);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
CVOpenGLESTextureRef prevBGRATexture = nil;
if(prevBuffer != nil){
prevBGRATexture = [self bgraTextureForPixelBuffer:prevBuffer];
[self activateContextShader:1];
glActiveTexture(GL_TEXTURE0);
glBindTexture(CVOpenGLESTextureGetTarget(prevBGRATexture), CVOpenGLESTextureGetName(prevBGRATexture));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
static const GLfloat backgroundVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
};
glUniform1i([self getActiveShaderUniform:UNIFORM_SHADER_SAMPLER_RPL], 0);
glVertexAttribPointer(ATTRIB_VERTEX_RPL, 2, GL_FLOAT, 0, 0, backgroundVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX_RPL);
glVertexAttribPointer(ATTRIB_TEXCOORD_RPL, 2, GL_FLOAT, 0, 0, [DVGOglEffectBase textureCoordinatesForRotation:trackOrientation]);
glEnableVertexAttribArray(ATTRIB_TEXCOORD_RPL);
// Draw the background frame
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(CVOpenGLESTextureGetTarget(trckBGRATexture), CVOpenGLESTextureGetName(trckBGRATexture));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatus(GL_FRAMEBUFFER));
goto bail;
}
if(self.layerBlendingMethod == DVGOglEffectATFRBlendingMethodBrightness){
[self activateContextShader:3];
}else
{
[self activateContextShader:2];
}
CGFloat blendFactor = 1.0;
CGFloat track_w = CVPixelBufferGetWidth(trackBuffer);
CGFloat track_h = CVPixelBufferGetHeight(trackBuffer);
CGPoint p1 = CGPointMake(-1.0f, -1.0f);
CGPoint p2 = CGPointMake(1.0f, -1.0f);
CGPoint p3 = CGPointMake(-1.0f, 1.0f);
CGPoint p4 = CGPointMake(1.0f, 1.0f);
if(self.frameMovementAnimations != nil){
CGFloat layerValues[kDVGVITimelineKeyLast] = {0};
[self.frameMovementAnimations fetchKeyedValues:layerValues atTime:time];
CGAffineTransform trf = CGAffineTransformIdentity;
trf = CGAffineTransformTranslate(trf, layerValues[kDVGVITimelineXPosKey], layerValues[kDVGVITimelineYPosKey]);
if(self.adjustScaleForAspectRatio){
trf = CGAffineTransformScale(trf, 1.0, track_w/track_h);// Accounting for aspect ratio
}
trf = CGAffineTransformScale(trf, layerValues[kDVGVITimelineXScaleKey], layerValues[kDVGVITimelineYScaleKey]);
trf = CGAffineTransformRotate(trf, layerValues[kDVGVITimelineRotationKey]);
if(self.adjustScaleForAspectRatio){
trf = CGAffineTransformScale(trf, 1.0, track_h/track_w);// Unwrapping aspect ratio
}
p1 = CGPointApplyAffineTransform(p1, trf);
p2 = CGPointApplyAffineTransform(p2, trf);
p3 = CGPointApplyAffineTransform(p3, trf);
p4 = CGPointApplyAffineTransform(p4, trf);
blendFactor = blendFactor * layerValues[kDVGVITimelineAlphaKey];
}
GLfloat backgroundVertices[] = {
p1.x, p1.y,
p2.x, p2.y,
p3.x, p3.y,
p4.x, p4.y,
};
glUniform1i([self getActiveShaderUniform:UNIFORM_SHADER_SAMPLER_RPL], 0);
glVertexAttribPointer(ATTRIB_VERTEX_RPL, 2, GL_FLOAT, 0, 0, backgroundVertices);
glEnableVertexAttribArray(ATTRIB_VERTEX_RPL);
GLfloat const* textureCoords = [DVGOglEffectBase textureCoordinatesForRotation:trackOrientation];
CGPoint tp1 = CGPointMake(textureCoords[0]-0.5, textureCoords[1]-0.5);
CGPoint tp2 = CGPointMake(textureCoords[2]-0.5, textureCoords[3]-0.5);
CGPoint tp3 = CGPointMake(textureCoords[4]-0.5, textureCoords[5]-0.5);
CGPoint tp4 = CGPointMake(textureCoords[6]-0.5, textureCoords[7]-0.5);
if(self.textureMovementAnimations != nil){
CGFloat layerValues[kDVGVITimelineKeyLast] = {0};
[self.textureMovementAnimations fetchKeyedValues:layerValues atTime:time];
CGAffineTransform trf = CGAffineTransformIdentity;
trf = CGAffineTransformScale(trf, 1.0, track_w/track_h);// Accounting for aspect ration
trf = CGAffineTransformTranslate(trf, layerValues[kDVGVITimelineXPosKey], layerValues[kDVGVITimelineYPosKey]);
trf = CGAffineTransformScale(trf, layerValues[kDVGVITimelineXScaleKey], layerValues[kDVGVITimelineYScaleKey]);
trf = CGAffineTransformRotate(trf, layerValues[kDVGVITimelineRotationKey]);
trf = CGAffineTransformScale(trf, 1.0, track_h/track_w);// Unwrapping aspect ration
tp1 = CGPointApplyAffineTransform(tp1, trf);
tp2 = CGPointApplyAffineTransform(tp2, trf);
tp3 = CGPointApplyAffineTransform(tp3, trf);
tp4 = CGPointApplyAffineTransform(tp4, trf);
blendFactor = blendFactor * layerValues[kDVGVITimelineAlphaKey];
}
GLfloat textureCoordsModified[] = {
tp1.x+0.5, tp1.y+0.5,
tp2.x+0.5, tp2.y+0.5,
tp3.x+0.5, tp3.y+0.5,
tp4.x+0.5, tp4.y+0.5,
};
glVertexAttribPointer(ATTRIB_TEXCOORD_RPL, 2, GL_FLOAT, 0, 0, textureCoordsModified);
glEnableVertexAttribArray(ATTRIB_TEXCOORD_RPL);
glUniform1f([self getActiveShaderUniform:UNIFORM_ANIMREMAP_BLN], blendFactor);
// Draw the background frame
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glFlush();
bail:
if(prevBGRATexture){
CFRelease(prevBGRATexture);
}
if(trckBGRATexture){
CFRelease(trckBGRATexture);
}
if(destBGRATexture){
CFRelease(destBGRATexture);
}
[self releaseContextForRendering];
}
+(DVGKeyframedAnimationScene*)staticSceneWithScale:(NSArray*)scaleXY andOffset:(NSArray*)offsetXY andRotation:(CGFloat)rotation {
CGFloat pscale1x = [[scaleXY objectAtIndex:0] doubleValue];
CGFloat pscale1y = [[scaleXY objectAtIndex:1] doubleValue];
CGFloat poffset1x = [[offsetXY objectAtIndex:0] doubleValue];
CGFloat poffset1y = [[offsetXY objectAtIndex:1] doubleValue];
DVGKeyframedAnimationScene* scene = [[DVGKeyframedAnimationScene alloc] init];
DVGKeyframedAnimationTimeline* tlr = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineRotationKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:rotation easing:kDVGVITimelineInterpolationLinear]]];
DVGKeyframedAnimationTimeline* tlx = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineXPosKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:poffset1x easing:kDVGVITimelineInterpolationLinear]]];
DVGKeyframedAnimationTimeline* tly = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineYPosKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:poffset1y easing:kDVGVITimelineInterpolationLinear]]];
DVGKeyframedAnimationTimeline* tlsx = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineXScaleKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:pscale1x easing:kDVGVITimelineInterpolationLinear]]];
DVGKeyframedAnimationTimeline* tlsy = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineYScaleKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:pscale1y easing:kDVGVITimelineInterpolationLinear]]];
scene.timelines = @[tlr, tlx, tly, tlsx, tlsy];
return scene;
}
+(DVGKeyframedAnimationScene*)slideSceneWithScale:(NSArray*)scaleXYXY andOffset:(NSArray*)offsetXYXY andRotation:(NSArray*)rotations forTime:(CGFloat)d {
CGFloat pscale1x = [[[scaleXYXY objectAtIndex:0] objectAtIndex:0] doubleValue];
CGFloat pscale1y = [[[scaleXYXY objectAtIndex:0] objectAtIndex:1] doubleValue];
CGFloat pscale2x = [[[scaleXYXY objectAtIndex:1] objectAtIndex:0] doubleValue];
CGFloat pscale2y = [[[scaleXYXY objectAtIndex:1] objectAtIndex:1] doubleValue];
CGFloat poffset1x = [[[offsetXYXY objectAtIndex:0] objectAtIndex:0] doubleValue];
CGFloat poffset1y = [[[offsetXYXY objectAtIndex:0] objectAtIndex:1] doubleValue];
CGFloat poffset2x = [[[offsetXYXY objectAtIndex:1] objectAtIndex:0] doubleValue];
CGFloat poffset2y = [[[offsetXYXY objectAtIndex:1] objectAtIndex:1] doubleValue];
CGFloat r1 = [[rotations objectAtIndex:0] doubleValue];
CGFloat r2 = [[rotations objectAtIndex:1] doubleValue];
DVGKeyframedAnimationScene* scene = [[DVGKeyframedAnimationScene alloc] init];
DVGKeyframedAnimationTimeline* tlr = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineRotationKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:r1 easing:kDVGVITimelineInterpolationLinear],
[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:d value:r2 easing:kDVGVITimelineInterpolationLinear]]];
DVGKeyframedAnimationTimeline* tlx = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineXPosKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:poffset1x easing:kDVGVITimelineInterpolationLinear],
[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:d value:poffset2x easing:kDVGVITimelineInterpolationLinear]]];
DVGKeyframedAnimationTimeline* tly = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineYPosKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:poffset1y easing:kDVGVITimelineInterpolationLinear],
[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:d value:poffset2y easing:kDVGVITimelineInterpolationLinear]]];
DVGKeyframedAnimationTimeline* tlsx = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineXScaleKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:pscale1x easing:kDVGVITimelineInterpolationLinear],
[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:d value:pscale2x easing:kDVGVITimelineInterpolationLinear]]];
DVGKeyframedAnimationTimeline* tlsy = [DVGKeyframedAnimationTimeline timelineWithKey:kDVGVITimelineYScaleKey objectIndex:0
keyFrames:@[[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:0 value:pscale1y easing:kDVGVITimelineInterpolationLinear],
[DVGKeyframedAnimationTimelineKeyframe keyframeWithTime:d value:pscale2y easing:kDVGVITimelineInterpolationLinear]]];
scene.timelines = @[tlr, tlx, tly, tlsx, tlsy];
return scene;
}
@end