GPUImage2:将gl纹理传递给着色器

时间:2018-01-17 08:37:43

标签: ios xcode opengl-es gpuimage

我试图修改GPUImage2 Crosshair生成器来替换由UIImage制作的gl纹理的默认十字准线,我的图像是128x128 png文件。

我使用GPUImage2 PictureInput将UIImage转换为gl纹理,它返回PictureInput实例,没有任何问题/警告。

接下来我修改CrosshairVertexShader以添加对新纹理的支持:

uniform sampler2D inputImageTexture; 
attribute vec4 position;
varying vec2 centerLocation;
varying float pointSpacing;

void main() {
    gl_Position = vec4(((position.xy * 2.0) - 1.0), 0.0, 1.0);
    gl_PointSize = crosshairWidth + 1.0;
    pointSpacing = 1.0 / crosshairWidth;
    centerLocation = vec2(
        pointSpacing * ceil(crosshairWidth / 2.0),
        pointSpacing * ceil(crosshairWidth / 2.0));
}

然后将其传递给修改过的CrosshairFragmentShader并在屏幕上渲染(?):

uniform sampler2D inputImageTexture;
varying lowp vec3 crosshairColor;
varying highp vec2 centerLocation;
varying highp float pointSpacing;

void main() {
    highp vec4 tex = texture2D(inputImageTexture, centerLocation);
    gl_FragColor = vec4(tex.r, tex.g, tex.b, tex.a);
}

以下是修改过的CrosshairGenerator的代码:

import UIKit

public class CrosshairGenerator: ImageGenerator {

    public var crosshairWidth:Float = 5.0 { didSet {    uniformSettings["crosshairWidth"] = crosshairWidth } }
    public var crosshairColor:Color = Color.green { didSet { uniformSettings["crosshairColor"] = crosshairColor } }
    public var crosshairImage: PictureInput = PictureInput(imageName:"monohrome.png") {
    didSet {
        uniformSettings["inputImageTexture"] = crosshairImage //Here I pass the texture to shader?
        crosshairImage.processImage()
    }

}

let crosshairShader:ShaderProgram
var uniformSettings = ShaderUniformSettings()

public override init(size:Size) {        
    crosshairShader = crashOnShaderCompileFailure("CrosshairGenerator"){try sharedImageProcessingContext.programForVertexShader(vertexShader, fragmentShader: fragmentShader)}

    super.init(size:size)

    ({crosshairWidth = 5.0})()
    ({crosshairColor = Color.green})()
    ({crosshairImage = PictureInput(imageName:"monohrome.png")})()
}

public func renderCrosshairs(_ positions:[Position]) {
    imageFramebuffer.activateFramebufferForRendering()
    imageFramebuffer.timingStyle = .stillImage 
#if GL
    glEnable(GLenum(GL_POINT_SPRITE))
    glEnable(GLenum(GL_VERTEX_PROGRAM_POINT_SIZE))
#else
    glEnable(GLenum(GL_POINT_SPRITE_OES))
#endif

    crosshairShader.use()
    uniformSettings.restoreShaderSettings(crosshairShader)

    clearFramebufferWithColor(Color.transparent)

    guard let positionAttribute = crosshairShader.attributeIndex("position") else { fatalError("A position attribute was missing from the shader program during rendering.") }

    let convertedPositions = positions.flatMap{$0.toGLArray()}
    glVertexAttribPointer(positionAttribute, 2, GLenum(GL_FLOAT), 0, 0, convertedPositions)

    glDrawArrays(GLenum(GL_POINTS), 0, GLsizei(positions.count))

    notifyTargets()
  }
}

角点检测器的初始化保持不变:

FilterOperation(
    filter:{HarrisCornerDetector()},
    listName:"Harris corner detector",
    titleName:"Harris Corner Detector",
    sliderConfiguration:.enabled(minimumValue:0.01, maximumValue:0.70, initialValue:0.20),
    sliderUpdateCallback: {(filter, sliderValue) in
        filter.threshold = sliderValue
    },
    filterOperationType:.custom(filterSetupFunction:{(camera, filter, outputView) in
        let castFilter = filter as! HarrisCornerDetector
        // TODO: Get this more dynamically sized
#if os(iOS)
        let crosshairGenerator = CrosshairGenerator(size:Size(width:480, height:640))
#else
        let crosshairGenerator = CrosshairGenerator(size:Size(width:1280, height:720))
#endif
        crosshairGenerator.crosshairWidth = 30.0

        castFilter.cornersDetectedCallback = { corners in
            crosshairGenerator.renderCrosshairs(corners)
        }

        camera --> castFilter

        let blendFilter = AlphaBlend()
        camera --> blendFilter --> outputView
        crosshairGenerator --> blendFilter
        return blendFilter
    })
)

代码编译并正常工作,但在屏幕上呈现时纹理看起来是空的(彩色方块):

see screenshot

如果我注释掉这一行 - uniformSettings [" inputImageTexture"] = crosshairImage - 结果保持不变,这让我觉得纹理根本没有传递给着色器。

我在这里失踪了什么?

0 个答案:

没有答案