什麼工作,我正在寫一RectangleGenerator。新的RectangleGenerator的關鍵是繪製GL_TRIANGLES。這可以作出
glDrawArrays(GLenum(GL_TRIANGLES), 0, GLsizei(positions.count) * 2)
一種優化是使用GL_Elements和GL_TRIANGLE_STRIP,雖然我沒能明白這一點。
完整的解決方案應該支持的邊框,邊框寬度和cornerRadius。現在簡單的矩形就足夠了。下面的完整代碼。
let vertex = "attribute vec4 position; void main(){ gl_Position = position; }"
let fragment = "uniform lowp vec3 fillColor; void main(){ gl_FragColor = vec4(fillColor, 1.0); }"
public class RectangleGenerator: ImageGenerator {
let rectangleShader:ShaderProgram
var uniformSettings = ShaderUniformSettings()
public var fillColor:Color = Color.green { didSet { uniformSettings["fillColor"] = fillColor } }
public override init(size:Size) {
rectangleShader = crashOnShaderCompileFailure("RectangleGenerator"){try sharedImageProcessingContext.programForVertexShader(vertex, fragmentShader:fragment)}
super.init(size:size)
({fillColor = Color.green})()
}
public func renderRectangles(_ positions:[Position]) {
guard positions.count > 3 else { return }
imageFramebuffer.activateFramebufferForRendering()
rectangleShader.use()
uniformSettings.restoreShaderSettings(rectangleShader)
clearFramebufferWithColor(Color.transparent)
guard let positionAttribute = rectangleShader.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)
glBlendEquation(GLenum(GL_FUNC_ADD))
glBlendFunc(GLenum(GL_ONE), GLenum(GL_ONE))
glEnable(GLenum(GL_BLEND))
glDrawArrays(GLenum(GL_TRIANGLES), 0, GLsizei(positions.count) * 2)
glDisable(GLenum(GL_BLEND))
notifyTargets()
}
}
快樂編碼