GPUImageCrosshairGenerator.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #import "GPUImageCrosshairGenerator.h"
  2. NSString *const kGPUImageCrosshairVertexShaderString = SHADER_STRING
  3. (
  4. attribute vec4 position;
  5. uniform float crosshairWidth;
  6. varying vec2 centerLocation;
  7. varying float pointSpacing;
  8. void main()
  9. {
  10. gl_Position = vec4(((position.xy * 2.0) - 1.0), 0.0, 1.0);
  11. gl_PointSize = crosshairWidth + 1.0;
  12. pointSpacing = 1.0 / crosshairWidth;
  13. centerLocation = vec2(pointSpacing * ceil(crosshairWidth / 2.0), pointSpacing * ceil(crosshairWidth / 2.0));
  14. }
  15. );
  16. #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
  17. NSString *const kGPUImageCrosshairFragmentShaderString = SHADER_STRING
  18. (
  19. uniform lowp vec3 crosshairColor;
  20. varying highp vec2 centerLocation;
  21. varying highp float pointSpacing;
  22. void main()
  23. {
  24. lowp vec2 distanceFromCenter = abs(centerLocation - gl_PointCoord.xy);
  25. lowp float axisTest = step(pointSpacing, gl_PointCoord.y) * step(distanceFromCenter.x, 0.09) + step(pointSpacing, gl_PointCoord.x) * step(distanceFromCenter.y, 0.09);
  26. gl_FragColor = vec4(crosshairColor * axisTest, axisTest);
  27. // gl_FragColor = vec4(distanceFromCenterInX, distanceFromCenterInY, 0.0, 1.0);
  28. }
  29. );
  30. #else
  31. NSString *const kGPUImageCrosshairFragmentShaderString = SHADER_STRING
  32. (
  33. GPUImageEscapedHashIdentifier(version 120)\n
  34. uniform vec3 crosshairColor;
  35. varying vec2 centerLocation;
  36. varying float pointSpacing;
  37. void main()
  38. {
  39. vec2 distanceFromCenter = abs(centerLocation - gl_PointCoord.xy);
  40. float axisTest = step(pointSpacing, gl_PointCoord.y) * step(distanceFromCenter.x, 0.09) + step(pointSpacing, gl_PointCoord.x) * step(distanceFromCenter.y, 0.09);
  41. gl_FragColor = vec4(crosshairColor * axisTest, axisTest);
  42. // gl_FragColor = vec4(distanceFromCenterInX, distanceFromCenterInY, 0.0, 1.0);
  43. }
  44. );
  45. #endif
  46. @implementation GPUImageCrosshairGenerator
  47. @synthesize crosshairWidth = _crosshairWidth;
  48. #pragma mark -
  49. #pragma mark Initialization and teardown
  50. - (id)init;
  51. {
  52. if (!(self = [super initWithVertexShaderFromString:kGPUImageCrosshairVertexShaderString fragmentShaderFromString:kGPUImageCrosshairFragmentShaderString]))
  53. {
  54. return nil;
  55. }
  56. runSynchronouslyOnVideoProcessingQueue(^{
  57. crosshairWidthUniform = [filterProgram uniformIndex:@"crosshairWidth"];
  58. crosshairColorUniform = [filterProgram uniformIndex:@"crosshairColor"];
  59. self.crosshairWidth = 5.0;
  60. [self setCrosshairColorRed:0.0 green:1.0 blue:0.0];
  61. });
  62. return self;
  63. }
  64. #pragma mark -
  65. #pragma mark Rendering
  66. - (void)renderCrosshairsFromArray:(GLfloat *)crosshairCoordinates count:(NSUInteger)numberOfCrosshairs frameTime:(CMTime)frameTime;
  67. {
  68. if (self.preventRendering)
  69. {
  70. return;
  71. }
  72. runSynchronouslyOnVideoProcessingQueue(^{
  73. [GPUImageContext setActiveShaderProgram:filterProgram];
  74. #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
  75. #else
  76. glEnable(GL_POINT_SPRITE);
  77. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
  78. #endif
  79. outputFramebuffer = [[GPUImageContext sharedFramebufferCache] fetchFramebufferForSize:[self sizeOfFBO] textureOptions:self.outputTextureOptions onlyTexture:NO];
  80. [outputFramebuffer activateFramebuffer];
  81. glClearColor(0.0, 0.0, 0.0, 0.0);
  82. glClear(GL_COLOR_BUFFER_BIT);
  83. glVertexAttribPointer(filterPositionAttribute, 2, GL_FLOAT, 0, 0, crosshairCoordinates);
  84. glDrawArrays(GL_POINTS, 0, (GLsizei)numberOfCrosshairs);
  85. [self informTargetsAboutNewFrameAtTime:frameTime];
  86. });
  87. }
  88. - (void)renderToTextureWithVertices:(const GLfloat *)vertices textureCoordinates:(const GLfloat *)textureCoordinates;
  89. {
  90. // Prevent rendering of the frame by normal means
  91. }
  92. #pragma mark -
  93. #pragma mark Accessors
  94. - (void)setCrosshairWidth:(CGFloat)newValue;
  95. {
  96. _crosshairWidth = newValue;
  97. [self setFloat:_crosshairWidth forUniform:crosshairWidthUniform program:filterProgram];
  98. }
  99. - (void)setCrosshairColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent;
  100. {
  101. GPUVector3 crosshairColor = {redComponent, greenComponent, blueComponent};
  102. [self setVec3:crosshairColor forUniform:crosshairColorUniform program:filterProgram];
  103. }
  104. @end