summaryrefslogtreecommitdiff
path: root/gui/src/ofxGpuLutBlend.cpp
diff options
context:
space:
mode:
authorTim Redfern <tim@getdrop.com>2018-09-10 23:24:49 +0100
committerTim Redfern <tim@getdrop.com>2018-09-10 23:24:49 +0100
commita0b504d11542097843db77653d3e26516a892593 (patch)
tree5bec9beb2a84a831059ada24c5523f27193ee8e2 /gui/src/ofxGpuLutBlend.cpp
parentfa5fc1eeaf7925024575f7154be1684534a62071 (diff)
lut blend
Diffstat (limited to 'gui/src/ofxGpuLutBlend.cpp')
-rw-r--r--gui/src/ofxGpuLutBlend.cpp127
1 files changed, 127 insertions, 0 deletions
diff --git a/gui/src/ofxGpuLutBlend.cpp b/gui/src/ofxGpuLutBlend.cpp
new file mode 100644
index 0000000..85a0811
--- /dev/null
+++ b/gui/src/ofxGpuLutBlend.cpp
@@ -0,0 +1,127 @@
+#include "ofxGpuLutBlend.h"
+
+ofxGpuLutBlend::ofxGpuLutBlend(){}
+ofxGpuLutBlend::~ofxGpuLutBlend(){}
+
+void ofxGpuLutBlend::load(ofTexture lutTexture){
+
+ if(ofIsGLProgrammableRenderer()){
+ vertexShader = "#version 150\n";
+ vertexShader += STRINGIFY(
+ uniform mat4 projectionMatrix;
+ uniform mat4 modelViewMatrix;
+ uniform mat4 modelViewProjectionMatrix;
+
+ in vec4 position;
+ in vec2 texcoord;
+
+ out vec2 texCoordVarying;
+
+ void main()
+ {
+ texCoordVarying = texcoord;
+ gl_Position = modelViewProjectionMatrix * position;
+ }
+ );
+
+ fragmentShader = "#version 150\n";
+ fragmentShader += STRINGIFY(
+ uniform sampler2DRect tex;
+ uniform sampler2DRect lut;
+ uniform float blend;
+
+ in vec2 texCoordVarying;
+
+ out vec4 fragColor;
+
+ // Texture coordinates
+ vec2 texcoord0 = texCoordVarying;
+
+ float size = 64.0;
+
+ void main( void )
+ {
+ vec3 rawColor = texture(tex, texcoord0).rgb;
+ //float rawAlpha = texture(tex, texcoord0).a;
+
+ if (rawAlpha <= 0.0) {
+ fragColor = vec4(rawColor, 0.0);
+ }
+ else {
+ vec3 originalColor = floor(texture(tex, texcoord0).rgb * vec3(size - 1.0));
+ vec2 blueIndex = vec2(mod(originalColor.b, sqrt(size)), floor(originalColor.b / sqrt(size)));
+ vec2 index = vec2((size * blueIndex.x + originalColor.r) + 0.5, (size * blueIndex.y + originalColor.g) + 0.5);
+ fragColor = vec4(texture(lut, index).rgb, blend);
+ }
+ }
+ );
+
+ lutShader.setupShaderFromSource(GL_VERTEX_SHADER, vertexShader);
+ lutShader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragmentShader);
+ lutShader.bindDefaults();
+ lutShader.linkProgram();
+ }
+ else {
+ fragmentShader = "#version 120\n#extension GL_ARB_texture_rectangle : enable\n";
+ fragmentShader += STRINGIFY(
+ uniform sampler2DRect tex;
+ uniform sampler2DRect lut;
+ uniform float blend;
+
+ float size = 64.0;
+
+ void main( void )
+ {
+ vec3 rawColor = texture2DRect(tex, gl_TexCoord[0].st).rgb;
+ //float rawAlpha = texture2DRect(tex, gl_TexCoord[0].st).a;
+
+ if (blend <= 0.0) {
+ gl_FragColor = vec4(rawColor, 0.0);
+ }
+ else {
+ vec3 originalColor = floor(texture2DRect(tex, gl_TexCoord[0].st).rgb * vec3(size - 1.0));
+ vec2 blueIndex = vec2(mod(originalColor.b, sqrt(size)), floor(originalColor.b / sqrt(size)));
+ vec2 index = vec2((size * blueIndex.x + originalColor.r) + 0.5, (size * blueIndex.y + originalColor.g) + 0.5);
+ gl_FragColor = vec4(texture2DRect(lut, index).rgb, blend);
+ }
+ }
+ );
+
+
+ lutShader.unload();
+ lutShader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragmentShader);
+ lutShader.linkProgram();
+ }
+
+ lut.setTextureWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE);
+ lut.setTextureMinMagFilter(GL_NEAREST, GL_NEAREST);
+ if(!ofGetUsingArbTex()){
+ ofEnableArbTex();
+ lut = lutTexture;
+ ofDisableArbTex();
+ }else{
+ lut = lutTexture;
+ }
+ blendAmt=1.0f;
+}
+void ofxGpuLutBlend::setBlend(float amt){
+ blendAmt=amt;
+}
+void ofxGpuLutBlend::load(ofImage lutImage){
+ load(lutImage.getTexture());
+}
+
+void ofxGpuLutBlend::load(string path){
+ lutImage.load(path);
+ load(lutImage.getTexture());
+}
+
+void ofxGpuLutBlend::begin(){
+ lutShader.begin();
+ lutShader.setUniformTexture("lut", lut, 1);
+ lutShader.setUniform1f("blend", blendAmt);
+}
+
+void ofxGpuLutBlend::end(){
+ lutShader.end();
+} \ No newline at end of file