我正在尝试读取 AVAsset 并在资产的每一帧中,使用 GPU 进行平移或旋转操作。
从我的研究中,我发现了一种将 CMSampleBuffer 转换为 GPU 纹理的方法。我正在使用以下方法来做到这一点:
func texture(fromBuffer sampleBuffer: CMSampleBuffer) -> GLuint {
var texture : GLuint = 0
glGenTextures(1, &texture)
glBindTexture(GLenum(GL_TEXTURE_2D), texture)
glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_LINEAR)
glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MAG_FILTER), GL_LINEAR)
glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_S), GL_CLAMP_TO_EDGE)
glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_WRAP_T), GL_CLAMP_TO_EDGE)
if let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)
glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), GLsizei(height), 0, GLenum(GL_BGRA), GLenum(GL_UNSIGNED_BYTE), CVPixelBufferGetBaseAddress(pixelBuffer))
CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
}
return texture
}
现在我需要用这个纹理做我想要的操作,但这就是我卡住的地方。从一个 Objc 示例中,我翻译了一个函数,该函数接收一个纹理作为参数并返回一个 CGImage。
这是方法:
func processTexture(_ texture: GLuint, width: Int, height: Int) -> CGImage? {
var newImage : CGImage?
var frameBuffer : GLuint = 0
glGenFramebuffers(1, &frameBuffer)
glBindFramebuffer(GLenum(GL_FRAMEBUFFER), frameBuffer)
var colorRenderbuffer : GLuint = 0
glGenRenderbuffers(1, &colorRenderbuffer)
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), colorRenderbuffer)
glRenderbufferStorage(GLenum(GL_RENDERBUFFER), GLenum(GL_RGBA8_OES), GLsizei(width), GLsizei(height))
glFramebufferRenderbuffer(GLenum(GL_FRAMEBUFFER), GLenum(GL_COLOR_ATTACHMENT0), GLenum(GL_RENDERBUFFER), colorRenderbuffer)
let status = glCheckFramebufferStatus(GLenum(GL_FRAMEBUFFER))
if status != GLenum(GL_FRAMEBUFFER_COMPLETE) {
print("ERROR")
} else {
glViewport(0, 0, GLsizei(width), GLsizei(height))
glClearColor(0.0, 0.0, 0.0, 1.0)
glClear(GLbitfield(GL_COLOR_BUFFER_BIT))
let data = UnsafeMutablePointer<Data>.allocate(capacity: width * height * 4)
glReadPixels(0, 0, GLsizei(width), GLsizei(height), GLenum(GL_RGBA), GLenum(GL_UNSIGNED_BYTE), data)
let cgDataProviderReleaseDataCallback: CGDataProviderReleaseDataCallback = { (info: UnsafeMutableRawPointer?, data: UnsafeRawPointer, size: Int) -> () in
return
}
if let dataProvider = CGDataProvider(dataInfo: nil, data: data, size: width * height * 4, releaseData: cgDataProviderReleaseDataCallback) {
let colorSpace = CGColorSpaceCreateDeviceRGB()
newImage = CGImage(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: width * 4, space: colorSpace, bitmapInfo: .byteOrder32Big, provider: dataProvider, decode: nil, shouldInterpolate: true, intent: .defaultIntent)
}
}
return newImage
}
但是我找不到在哪里可以对这段代码执行平移和旋转的操作,或者我该怎么做。
很高兴有人已经在 iOS 上使用过 OpenGL,可以给我帮助,甚至告诉我在哪里可以找到此类操作的示例。