opengl - How to flip Y-axis in GLSL shader before gl_FragColor = texture2D(*,*); -
i need flip textures upside-down in shaders before applying perspective transformations. modified verttexcoord in vert.glsl, don't know use in swap.glsl. way
gl_fragcolor = texture2d(texture, verttexcoord );
does not work, because need texture modified in perspective.
vert.glsl:
#define processing_color_shader uniform mat4 transform; uniform mat4 texmatrix; attribute vec4 vertex; attribute vec4 color; attribute vec2 texcoord; varying vec4 vertcolor; varying vec4 verttexcoord; void main() { gl_position = transform * vertex; vertcolor = color; verttexcoord = texmatrix * vec4(texcoord, 1.0, 1.0); }
swap.glsl:
#ifdef gl_es precision highp float; #endif // general parameters uniform sampler2d from; uniform sampler2d to; uniform float progress; uniform vec2 resolution; uniform float reflection; uniform float perspective; uniform float depth; varying vec4 vertcolor; varying vec4 verttexcoord; const vec4 black = vec4(0.0, 0.0, 0.0, 1.0); const vec2 boundmin = vec2(0.0, 0.0); const vec2 boundmax = vec2(1.0, 1.0); bool inbounds (vec2 p) { return all(lessthan(boundmin, p)) && all(lessthan(p, boundmax)); } vec2 project (vec2 p) { return p * vec2(1.0, -1.2) + vec2(0.0, -0.02); } vec4 bgcolor (vec2 p, vec2 pfr, vec2 pto) { vec4 c = black; pfr = project(pfr); if (inbounds(pfr)) { c += mix(black, texture2d(from, pfr), reflection * mix(1.0, 0.0, pfr.y)); } pto = project(pto); if (inbounds(pto)) { c += mix(black, texture2d(to, pto), reflection * mix(1.0, 0.0, pto.y)); } return c; } void main() { vec2 p = gl_fragcoord.xy / resolution.xy; vec2 pfr, pto = vec2(-1.); float size = mix(1.0, depth, progress); float persp = perspective * progress; pfr = (p + vec2(-0.0, -0.5)) * vec2(size/(1.0-perspective*progress), size/(1.0-size*persp*p.x)) + vec2(0.0, 0.5); size = mix(1.0, depth, 1.-progress); persp = perspective * (1.-progress); pto = (p + vec2(-1.0, -0.5)) * vec2(size/(1.0-perspective*(1.0-progress)), size/(1.0-size*persp*(0.5-p.x))) + vec2(1.0, 0.5); bool fromover = progress < 0.5; if (fromover) { if (inbounds(pfr)) { gl_fragcolor = texture2d(from, pfr); } else if (inbounds(pto)) { gl_fragcolor = texture2d(to, pto); } else { gl_fragcolor = bgcolor(p, pfr, pto); } } else { if (inbounds(pto)) { gl_fragcolor = texture2d(to, pto); } else if (inbounds(pfr)) { gl_fragcolor = texture2d(from, pfr); } else { gl_fragcolor = bgcolor(p, pfr, pto); } } }
you sample texture at
(u,v)
if want flip y-axis, sample at
(u, 1.0f -v)
so updated main
like:
void main() { gl_position = transform * vertex; vertcolor = color; newtcoord = texcoord; newtcoord.y = 1.0 - newtcoord.y; verttexcoord = vec4(newtcoord, 1.0, 1.0); }
Comments
Post a Comment