developer
2023-05-20 e12c7b4c22df631ebdcd16b2f98fbef8f738f92f
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
#import "GPUImageLineGenerator.h"
 
NSString *const kGPUImageLineGeneratorVertexShaderString = SHADER_STRING
(
 attribute vec4 position;
 
 void main()
 {
     gl_Position = position;
 }
);
 
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUImageLineGeneratorFragmentShaderString = SHADER_STRING
(
 uniform lowp vec3 lineColor;
 
 void main()
 {
     gl_FragColor = vec4(lineColor, 1.0);
 }
);
#else
NSString *const kGPUImageLineGeneratorFragmentShaderString = SHADER_STRING
(
 uniform vec3 lineColor;
 
 void main()
 {
     gl_FragColor = vec4(lineColor, 1.0);
 }
);
#endif
 
@interface GPUImageLineGenerator()
 
- (void)generateLineCoordinates;
 
@end
 
@implementation GPUImageLineGenerator
 
@synthesize lineWidth = _lineWidth;
 
#pragma mark -
#pragma mark Initialization and teardown
 
- (id)init;
{
    if (!(self = [super initWithVertexShaderFromString:kGPUImageLineGeneratorVertexShaderString fragmentShaderFromString:kGPUImageLineGeneratorFragmentShaderString]))
    {
        return nil;
    }
    
    runSynchronouslyOnVideoProcessingQueue(^{
        lineWidthUniform = [filterProgram uniformIndex:@"lineWidth"];
        lineColorUniform = [filterProgram uniformIndex:@"lineColor"];
        
        self.lineWidth = 1.0;
        [self setLineColorRed:0.0 green:1.0 blue:0.0];
    });
    
    return self;
}
 
- (void)dealloc
{
    if (lineCoordinates)
    {
        free(lineCoordinates);
    }
}
 
#pragma mark -
#pragma mark Rendering
 
- (void)generateLineCoordinates;
{
    lineCoordinates = calloc(1024 * 4, sizeof(GLfloat));
}
 
- (void)renderLinesFromArray:(GLfloat *)lineSlopeAndIntercepts count:(NSUInteger)numberOfLines frameTime:(CMTime)frameTime;
{
    if (self.preventRendering)
    {
        return;
    }
    
    if (lineCoordinates == NULL)
    {
        [self generateLineCoordinates];
    }
    
    // Iterate through and generate vertices from the slopes and intercepts
    NSUInteger currentVertexIndex = 0;
    NSUInteger currentLineIndex = 0;
    NSUInteger maxLineIndex = numberOfLines *2;
    while(currentLineIndex < maxLineIndex)
    {
        GLfloat slope = lineSlopeAndIntercepts[currentLineIndex++];
        GLfloat intercept = lineSlopeAndIntercepts[currentLineIndex++];
        
        if (slope > 9000.0) // Vertical line
        {
            lineCoordinates[currentVertexIndex++] = intercept;
            lineCoordinates[currentVertexIndex++] = -1.0;
            lineCoordinates[currentVertexIndex++] = intercept;
            lineCoordinates[currentVertexIndex++] = 1.0;
        }
        else
        {
            lineCoordinates[currentVertexIndex++] = -1.0;
            lineCoordinates[currentVertexIndex++] = slope * -1.0 + intercept;
            lineCoordinates[currentVertexIndex++] = 1.0;
            lineCoordinates[currentVertexIndex++] = slope * 1.0 + intercept;
        }
    }
    
    runSynchronouslyOnVideoProcessingQueue(^{
        [GPUImageContext setActiveShaderProgram:filterProgram];
        
        outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:[self sizeOfFBO] textureOptions:self.outputTextureOptions onlyTexture:NO];
        [outputFramebuffer activateFramebuffer];
        
        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, 2, GL_FLOAT, 0, 0, lineCoordinates);
        glDrawArrays(GL_LINES, 0, ((unsigned int)numberOfLines * 2));
        
        glDisable(GL_BLEND);
 
        [self informTargetsAboutNewFrameAtTime:frameTime];
    });
}
 
- (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates;
{
    // Prevent rendering of the frame by normal means
}
 
#pragma mark -
#pragma mark Accessors
 
- (void)setLineWidth:(CGFloat)newValue;
{
    _lineWidth = newValue;
    [GPUImageContext setActiveShaderProgram:filterProgram];
    glLineWidth(newValue);
}
 
- (void)setLineColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent;
{
    GPUVector3 lineColor = {redComponent, greenComponent, blueComponent};
    
    [self setVec3:lineColor forUniform:lineColorUniform program:filterProgram];
}
 
 
@end