GPUImageLineGenerator.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #import "GPUImageLineGenerator.h"
  2. NSString *const kGPUImageLineGeneratorVertexShaderString = SHADER_STRING
  3. (
  4. attribute vec4 position;
  5. void main()
  6. {
  7. gl_Position = position;
  8. }
  9. );
  10. #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
  11. NSString *const kGPUImageLineGeneratorFragmentShaderString = SHADER_STRING
  12. (
  13. uniform lowp vec3 lineColor;
  14. void main()
  15. {
  16. gl_FragColor = vec4(lineColor, 1.0);
  17. }
  18. );
  19. #else
  20. NSString *const kGPUImageLineGeneratorFragmentShaderString = SHADER_STRING
  21. (
  22. uniform vec3 lineColor;
  23. void main()
  24. {
  25. gl_FragColor = vec4(lineColor, 1.0);
  26. }
  27. );
  28. #endif
  29. @interface GPUImageLineGenerator()
  30. - (void)generateLineCoordinates;
  31. @end
  32. @implementation GPUImageLineGenerator
  33. @synthesize lineWidth = _lineWidth;
  34. #pragma mark -
  35. #pragma mark Initialization and teardown
  36. - (id)init;
  37. {
  38. if (!(self = [super initWithVertexShaderFromString:kGPUImageLineGeneratorVertexShaderString fragmentShaderFromString:kGPUImageLineGeneratorFragmentShaderString]))
  39. {
  40. return nil;
  41. }
  42. runSynchronouslyOnVideoProcessingQueue(^{
  43. lineWidthUniform = [filterProgram uniformIndex:@"lineWidth"];
  44. lineColorUniform = [filterProgram uniformIndex:@"lineColor"];
  45. self.lineWidth = 1.0;
  46. [self setLineColorRed:0.0 green:1.0 blue:0.0];
  47. });
  48. return self;
  49. }
  50. - (void)dealloc
  51. {
  52. if (lineCoordinates)
  53. {
  54. free(lineCoordinates);
  55. }
  56. }
  57. #pragma mark -
  58. #pragma mark Rendering
  59. - (void)generateLineCoordinates;
  60. {
  61. lineCoordinates = calloc(1024 * 4, sizeof(GLfloat));
  62. }
  63. - (void)renderLinesFromArray:(GLfloat *)lineSlopeAndIntercepts count:(NSUInteger)numberOfLines frameTime:(CMTime)frameTime;
  64. {
  65. if (self.preventRendering)
  66. {
  67. return;
  68. }
  69. if (lineCoordinates == NULL)
  70. {
  71. [self generateLineCoordinates];
  72. }
  73. // Iterate through and generate vertices from the slopes and intercepts
  74. NSUInteger currentVertexIndex = 0;
  75. NSUInteger currentLineIndex = 0;
  76. NSUInteger maxLineIndex = numberOfLines *2;
  77. while(currentLineIndex < maxLineIndex)
  78. {
  79. GLfloat slope = lineSlopeAndIntercepts[currentLineIndex++];
  80. GLfloat intercept = lineSlopeAndIntercepts[currentLineIndex++];
  81. if (slope > 9000.0) // Vertical line
  82. {
  83. lineCoordinates[currentVertexIndex++] = intercept;
  84. lineCoordinates[currentVertexIndex++] = -1.0;
  85. lineCoordinates[currentVertexIndex++] = intercept;
  86. lineCoordinates[currentVertexIndex++] = 1.0;
  87. }
  88. else
  89. {
  90. lineCoordinates[currentVertexIndex++] = -1.0;
  91. lineCoordinates[currentVertexIndex++] = slope * -1.0 + intercept;
  92. lineCoordinates[currentVertexIndex++] = 1.0;
  93. lineCoordinates[currentVertexIndex++] = slope * 1.0 + intercept;
  94. }
  95. }
  96. runSynchronouslyOnVideoProcessingQueue(^{
  97. [GPUImageContext setActiveShaderProgram:filterProgram];
  98. outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:[self sizeOfFBO] textureOptions:self.outputTextureOptions onlyTexture:NO];
  99. [outputFramebuffer activateFramebuffer];
  100. glClearColor(0.0, 0.0, 0.0, 0.0);
  101. glClear(GL_COLOR_BUFFER_BIT);
  102. glBlendEquation(GL_FUNC_ADD);
  103. glBlendFunc(GL_ONE, GL_ONE);
  104. glEnable(GL_BLEND);
  105. glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, lineCoordinates);
  106. glDrawArrays(GL_LINES, 0, ((unsigned int)numberOfLines * 2));
  107. glDisable(GL_BLEND);
  108. [self informTargetsAboutNewFrameAtTime:frameTime];
  109. });
  110. }
  111. - (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates;
  112. {
  113. // Prevent rendering of the frame by normal means
  114. }
  115. #pragma mark -
  116. #pragma mark Accessors
  117. - (void)setLineWidth:(CGFloat)newValue;
  118. {
  119. _lineWidth = newValue;
  120. [GPUImageContext setActiveShaderProgram:filterProgram];
  121. glLineWidth(newValue);
  122. }
  123. - (void)setLineColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent;
  124. {
  125. GPUVector3 lineColor = {redComponent, greenComponent, blueComponent};
  126. [self setVec3:lineColor forUniform:lineColorUniform program:filterProgram];
  127. }
  128. @end