Skip to content

Commit

Permalink
added openGL 2 shaders (#version 120)
Browse files Browse the repository at this point in the history
  • Loading branch information
d3cod3 committed Jan 23, 2019
1 parent 1f2082c commit e59a722
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Image warping, as used in this addon, is the process of manipulating an image so

* openFrameworks 0.9 and up
* OpenGL 3 and up (programmable pipeline)
* **ADDED** OpenGL 2 compatibility (#version 120 shaders included in openGL2_shaders folder)
* The included shaders only work with normalized textures (`GL_TEXTURE_2D`) but can be easily modified to work with rectangle textures

#### Controls
Expand Down
19 changes: 19 additions & 0 deletions openGL2_shaders/ControlPoint.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#version 120

varying vec3 v;
varying vec3 N;

varying vec2 vTexCoord;
varying vec4 vColor;

void main(void) {

vec2 coord = gl_TexCoord[0].st;
vec2 uv = coord * 2.0 - 1.0;
float d = dot(uv, uv);
float rim = smoothstep(0.7, 0.8, d);
rim += smoothstep(0.3, 0.4, d) - smoothstep(0.5, 0.6, d);
rim += smoothstep(0.1, 0.0, d);
gl_FragColor = mix(vec4( 0.0, 0.0, 0.0, 0.25), vColor, rim);

}
23 changes: 23 additions & 0 deletions openGL2_shaders/ControlPoint.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#version 120

varying vec3 v;
varying vec3 N;

attribute vec4 iPositionScale;
attribute vec4 iColor;

attribute vec4 position;
attribute vec2 texcoord;
attribute vec4 color;

varying vec2 vTexCoord;
varying vec4 vColor;

void main(void) {

vTexCoord = texcoord;
vColor = gl_Color * iColor;
gl_Position = gl_ModelViewProjectionMatrix * vec4(position.xy * iPositionScale.z + iPositionScale.xy, position.zw);


}
55 changes: 55 additions & 0 deletions openGL2_shaders/WarpBilinear.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#version 120

varying vec3 v;
varying vec3 N;

uniform sampler2DRect uTexture;
uniform vec4 uExtends;
uniform vec3 uLuminance;
uniform vec3 uGamma;
uniform vec4 uEdges;
uniform vec4 uCorners;
uniform float uExponent;
uniform bool uEditing;

float map(float value,float inMin,float inMax,float outMin,float outMax){
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

float grid(vec2 uv,vec2 size){
vec2 coord = uv / size;
vec2 grid = abs(fract(coord - 0.5) - 0.5) / (2.0 * fwidth(coord));
float line = min(grid.x, grid.y);
return 1.0 - min(line, 1.0);
}

void main(void)
{

vec2 coord = gl_TexCoord[0].st;
vec4 texColor = texture2DRect(uTexture, coord);

vec2 mapCoord = vec2(map(coord.x, uCorners.x, uCorners.z, 0.0, 1.0), map(coord.y, uCorners.y, uCorners.w, 0.0, 1.0));

float a = 1.0;
if (uEdges.x > 0.0) a *= clamp(mapCoord.x / uEdges.x, 0.0, 1.0);
if (uEdges.y > 0.0) a *= clamp(mapCoord.y / uEdges.y, 0.0, 1.0);
if (uEdges.z > 0.0) a *= clamp((1.0 - mapCoord.x) / uEdges.z, 0.0, 1.0);
if (uEdges.w > 0.0) a *= clamp((1.0 - mapCoord.y) / uEdges.w, 0.0, 1.0);

const vec3 one = vec3(1.0);
vec3 blend = (a < 0.5) ? (uLuminance * pow(2.0 * a, uExponent)) : one - (one - uLuminance) * pow(2.0 * (1.0 - a), uExponent);

texColor.rgb *= pow(blend, one / uGamma);

if (uEditing)
{
float f = grid(mapCoord.xy * uExtends.xy, uExtends.zw);
vec4 gridColor = vec4(1.0f);
gl_FragColor = mix(texColor, gridColor, f);
}
else
{
gl_FragColor = texColor;
}
}
12 changes: 12 additions & 0 deletions openGL2_shaders/WarpBilinear.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 120

varying vec3 v;
varying vec3 N;

void main(){
v = vec3(gl_ModelViewMatrix * gl_Vertex);
N = normalize(gl_NormalMatrix * gl_Normal);

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
36 changes: 36 additions & 0 deletions openGL2_shaders/WarpPerspective.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#version 120

varying vec3 v;
varying vec3 N;

uniform sampler2DRect uTexture;
uniform vec3 uLuminance;
uniform vec3 uGamma;
uniform vec4 uEdges;
uniform vec4 uCorners;
uniform float uExponent;

float map(float value, float inMin, float inMax, float outMin, float outMax){
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}

void main(void){

vec2 coord = gl_TexCoord[0].st;
vec4 texColor = texture2DRect(uTexture, coord);

vec2 mapCoord = vec2(map(coord.x, uCorners.x, uCorners.z, 0.0, 1.0), map(coord.y, uCorners.y, uCorners.w, 0.0, 1.0));

float a = 1.0;
if (uEdges.x > 0.0) a *= clamp(mapCoord.x / uEdges.x, 0.0, 1.0);
if (uEdges.y > 0.0) a *= clamp(mapCoord.y / uEdges.y, 0.0, 1.0);
if (uEdges.z > 0.0) a *= clamp((1.0 - mapCoord.x) / uEdges.z, 0.0, 1.0);
if (uEdges.w > 0.0) a *= clamp((1.0 - mapCoord.y) / uEdges.w, 0.0, 1.0);

const vec3 one = vec3(1.0);
vec3 blend = (a < 0.5) ? (uLuminance * pow(2.0 * a, uExponent)) : one - (one - uLuminance) * pow(2.0 * (1.0 - a), uExponent);

texColor.rgb *= pow(blend, one / uGamma);

gl_FragColor = texColor;
}
12 changes: 12 additions & 0 deletions openGL2_shaders/WarpPerspective.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#version 120

varying vec3 v;
varying vec3 N;

void main(){
v = vec3(gl_ModelViewMatrix * gl_Vertex);
N = normalize(gl_NormalMatrix * gl_Normal);

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}

0 comments on commit e59a722

Please sign in to comment.