Skip to content

Commit

Permalink
Minor cleanup around additive blending.
Browse files Browse the repository at this point in the history
  • Loading branch information
BradLarson committed Mar 8, 2017
1 parent 23e2089 commit 8c3bdee
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
6 changes: 2 additions & 4 deletions framework/Source/Mac/PictureOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ public class PictureOutput: ImageConsumer {
clearFramebufferWithColor(Color.transparent)

// Need the blending here to enable non-1.0 alpha on output image
glBlendEquation(GLenum(GL_FUNC_ADD))
glBlendFunc(GLenum(GL_ONE), GLenum(GL_ONE))
glEnable(GLenum(GL_BLEND))
enableAdditiveBlending()

renderQuadWithShader(sharedImageProcessingContext.passthroughShader, uniformSettings:ShaderUniformSettings(), vertices:standardImageVertices, inputTextures:[framebuffer.texturePropertiesForOutputRotation(.noRotation)])

glDisable(GLenum(GL_BLEND))
disableBlending()

framebuffer.unlock()

Expand Down
10 changes: 10 additions & 0 deletions framework/Source/OpenGLRendering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,16 @@ func attachStencilBuffer(width:GLint, height:GLint) throws -> GLuint {
return stencilBuffer
}

public func enableAdditiveBlending() {
glBlendEquation(GLenum(GL_FUNC_ADD))
glBlendFunc(GLenum(GL_ONE), GLenum(GL_ONE))
glEnable(GLenum(GL_BLEND))
}

public func disableBlending() {
glDisable(GLenum(GL_BLEND))
}

extension String {
func withNonZeroSuffix(_ suffix:Int) -> String {
if suffix == 0 {
Expand Down
8 changes: 3 additions & 5 deletions framework/Source/Operations/Histogram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ public class Histogram: BasicOperation {

clearFramebufferWithColor(Color.black)

glBlendEquation(GLenum(GL_FUNC_ADD))
glBlendFunc(GLenum(GL_ONE), GLenum(GL_ONE))
glEnable(GLenum(GL_BLEND))

enableAdditiveBlending()

shader.use()
guard let positionAttribute = shader.attributeIndex("position") else { fatalError("A position attribute was missing from the shader program during rendering.") }
glVertexAttribPointer(positionAttribute, 4, GLenum(GL_UNSIGNED_BYTE), 0, (GLint(downsamplingFactor) - 1) * 4, data)
Expand All @@ -85,7 +83,7 @@ public class Histogram: BasicOperation {
glDrawArrays(GLenum(GL_POINTS), 0, inputSize.width * inputSize.height / GLint(downsamplingFactor))
}

glDisable(GLenum(GL_BLEND))
disableBlending()
data.deallocate(capacity:inputByteSize)
}
}
7 changes: 3 additions & 4 deletions framework/Source/Operations/LineGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ public class LineGenerator: ImageGenerator {
let lineEndpoints = lines.flatMap{$0.toGLEndpoints()}
glVertexAttribPointer(positionAttribute, 2, GLenum(GL_FLOAT), 0, 0, lineEndpoints)

glBlendEquation(GLenum(GL_FUNC_ADD))
glBlendFunc(GLenum(GL_ONE), GLenum(GL_ONE))
glEnable(GLenum(GL_BLEND))

enableAdditiveBlending()

glDrawArrays(GLenum(GL_LINES), 0, GLsizei(lines.count) * 2)

glDisable(GLenum(GL_BLEND))
disableBlending()

notifyTargets()
}
Expand Down

0 comments on commit 8c3bdee

Please sign in to comment.