Skip to content

Instantly share code, notes, and snippets.

@tgfrerer
Last active February 2, 2018 16:05
Show Gist options
  • Save tgfrerer/6009069 to your computer and use it in GitHub Desktop.
Save tgfrerer/6009069 to your computer and use it in GitHub Desktop.
circle mask shader - use these with openGL 2.0
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
mPlayer.loadMovie("test_movie.mov");
circleShader.load("circle_mask");
mPlayer.play();
}
//--------------------------------------------------------------
void testApp::update(){
mPlayer.update();
}
//--------------------------------------------------------------
void testApp::draw(){
circleShader.begin();
mPlayer.getTextureReference().draw(ofVec2f(0,0));
circleShader.end();
}
#version 120
#extension GL_ARB_texture_rectangle : enable
uniform sampler2DRect tex;
void main()
{
float texWidth=1920.0; // width in pixels
float texHeight=1080.0; // height in pixels
float edge = 500 / texWidth; // value in pixels, i.e. circle will have a 500 px radius
float edgewidth = 10.0 / texWidth; // value in pixels, i.e. will give you a 10 px blurred edge…
float heightWidthRatio = texHeight / texWidth;
vec2 normalizedTexCoord = (vec2(gl_TexCoord[0].x / texWidth, gl_TexCoord[0].y / texHeight ) ) - vec2(0.5, 0.5) ;
normalizedTexCoord.y = normalizedTexCoord.y * heightWidthRatio;
float opacity = clamp(0.0,1.0,1.0 - smoothstep(edge, edge+edgewidth, (length(normalizedTexCoord))));
gl_FragColor = vec4((gl_Color * texture2DRect(tex, gl_TexCoord[0].xy)).rgb,opacity);
}
#version 120
#extension GL_ARB_texture_rectangle : enable
void main()
{
gl_FrontColor = gl_Color;
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
@kaspesla
Copy link

kaspesla commented Feb 2, 2018

In case someone else ends up trying to use this code as an example, there is a mistake here with the use of clamp. The arguments are in the wrong order. I also had to change the last line.
Here is the last two lines fixed:

    float opacity =  clamp(1.0 - smoothstep(edge, edge+edgewidth, (length(normalizedTexCoord))),0.0,1.0);    
    gl_FragColor = vec4(texture2DRect(tex, gl_TexCoord[0].xy).rgb, opacity);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment