From 804e386ca49d3ec1902e692ad2c7b0f56426f9a3 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 19 Nov 2012 00:45:47 +0000 Subject: [PATCH] neercs: do not pass the texture coordinates from the application to the GL server; instead, simply deduce them from the vertex coordinates and interpolate them in the fragment shader. --- neercs/video/blurh.lolfx | 8 ++++++-- neercs/video/blurv.lolfx | 8 ++++++-- neercs/video/color.lolfx | 5 ++--- neercs/video/copper.lolfx | 10 +++++++--- neercs/video/glow.lolfx | 10 ++++++++-- neercs/video/mirror.lolfx | 10 +++++++--- neercs/video/noise.lolfx | 9 ++++----- neercs/video/postfx.lolfx | 5 ++--- neercs/video/radial.lolfx | 10 +++++++--- neercs/video/remanence.lolfx | 10 +++++++--- neercs/video/render.cpp | 17 +++++++++-------- neercs/video/simple.lolfx | 8 ++++++-- 12 files changed, 71 insertions(+), 39 deletions(-) diff --git a/neercs/video/blurh.lolfx b/neercs/video/blurh.lolfx index 6208419..4c4dbee 100644 --- a/neercs/video/blurh.lolfx +++ b/neercs/video/blurh.lolfx @@ -2,23 +2,27 @@ #version 120 +varying vec2 pass_TexCoord; + void main() { gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; + pass_TexCoord = vec2(0.5, 0.5) + 0.5 * gl_Vertex.xy; } [frag.glsl] #version 120 +varying vec2 pass_TexCoord; + uniform sampler2D texture; uniform vec2 radius; void main(void) { vec4 total = vec4(0.0); - vec2 p = gl_TexCoord[0].xy; + vec2 p = pass_TexCoord; float mask = 2.0-p.x*(6.0-p.x*6.0)*p.y*(6.0-p.y*6.0); float b = radius.x+radius.y*mask; total += texture2D(texture,vec2(p.x-b*4.0,p.y))*0.04; diff --git a/neercs/video/blurv.lolfx b/neercs/video/blurv.lolfx index f07338a..c719180 100644 --- a/neercs/video/blurv.lolfx +++ b/neercs/video/blurv.lolfx @@ -2,23 +2,27 @@ #version 120 +varying vec2 pass_TexCoord; + void main() { gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; + pass_TexCoord = vec2(0.5, 0.5) + 0.5 * gl_Vertex.xy; } [frag.glsl] #version 120 +varying vec2 pass_TexCoord; + uniform sampler2D texture; uniform vec2 radius; void main(void) { vec4 total = vec4(0.0); - vec2 p = gl_TexCoord[0].xy; + vec2 p = pass_TexCoord; float mask = 2.0-p.x*(6.0-p.x*6.0)*p.y*(6.0-p.y*6.0); float b = radius.x+radius.y*mask; total += texture2D(texture,vec2(p.x,p.y-b*4.0))*0.04; diff --git a/neercs/video/color.lolfx b/neercs/video/color.lolfx index e3fac7a..e28bd6f 100644 --- a/neercs/video/color.lolfx +++ b/neercs/video/color.lolfx @@ -5,7 +5,6 @@ void main() { gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; } [frag.glsl] @@ -20,7 +19,7 @@ uniform float flash; void main(void) { - vec2 p = gl_FragCoord.xy/screen_size.xy; + vec2 p = gl_FragCoord.xy / screen_size.xy; vec3 c = texture2D(texture,p).xyz; float a = (c.x + c.y + c.z) / 3.0; @@ -32,4 +31,4 @@ void main(void) c += flash; // flash gl_FragColor = vec4(c, 1.0); -} \ No newline at end of file +} diff --git a/neercs/video/copper.lolfx b/neercs/video/copper.lolfx index 95a6d90..4dc7e17 100644 --- a/neercs/video/copper.lolfx +++ b/neercs/video/copper.lolfx @@ -2,16 +2,20 @@ #version 120 +varying vec2 pass_TexCoord; + void main() { gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; + pass_TexCoord = vec2(0.5, 0.5) + 0.5 * gl_Vertex.xy; } [frag.glsl] #version 120 +varying vec2 pass_TexCoord; + uniform sampler2D texture; uniform vec2 screen_size; uniform float time; @@ -20,7 +24,7 @@ uniform vec3 mask_color; void main(void) { - vec2 p = gl_TexCoord[0].xy; + vec2 p = pass_TexCoord; vec3 source = texture2D(texture, p).xyz; vec3 color = vec3(0.5); @@ -35,4 +39,4 @@ void main(void) color.z = float(int(color.z * 8.0) * 32) / 256.0; gl_FragColor = vec4((source == mask_color) ? color : source, 1.0); -} \ No newline at end of file +} diff --git a/neercs/video/glow.lolfx b/neercs/video/glow.lolfx index 8540b91..0a74056 100644 --- a/neercs/video/glow.lolfx +++ b/neercs/video/glow.lolfx @@ -2,21 +2,27 @@ #version 120 +varying vec2 pass_TexCoord; + void main() { gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; + pass_TexCoord = vec2(0.5, 0.5) + 0.5 * gl_Vertex.xy; } [frag.glsl] #version 120 +varying vec2 pass_TexCoord; + uniform sampler2D glow; uniform sampler2D source; uniform vec2 mix; void main(void) { - gl_FragColor = texture2D(source,gl_TexCoord[0].xy)*mix.x+texture2D(glow,gl_TexCoord[0].xy)*mix.y; + gl_FragColor = texture2D(source, pass_TexCoord) * mix.x + + texture2D(glow, pass_TexCoord) * mix.y; } + diff --git a/neercs/video/mirror.lolfx b/neercs/video/mirror.lolfx index c30ec33..f19aa93 100644 --- a/neercs/video/mirror.lolfx +++ b/neercs/video/mirror.lolfx @@ -2,23 +2,27 @@ #version 120 +varying vec2 pass_TexCoord; + void main() { gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; + pass_TexCoord = vec2(0.5, 0.5) + 0.5 * gl_Vertex.xy; } [frag.glsl] #version 120 +varying vec2 pass_TexCoord; + uniform sampler2D texture; uniform vec2 screen_size; uniform vec4 mirror; void main(void) { - vec2 p = gl_TexCoord[0].xy; + vec2 p = pass_TexCoord; vec3 s = texture2D(texture, p).xyz; vec3 c = vec3(0.0); @@ -28,4 +32,4 @@ void main(void) if(p.y > 1.0 - mirror.y) c += texture2D(texture, vec2(p.x * (0.8 + 2.0 * (1.0 - p.y)) + 0.1 - 1.0 * (1.0 - p.y), - mirror.y - (mirror.y + p.y) * mirror.w)).xyz * (mirror.z / mirror.y * (1.0 - p.y)); gl_FragColor = vec4(s + c, 1.0); -} \ No newline at end of file +} diff --git a/neercs/video/noise.lolfx b/neercs/video/noise.lolfx index c7a48df..0b817ce 100644 --- a/neercs/video/noise.lolfx +++ b/neercs/video/noise.lolfx @@ -4,8 +4,7 @@ void main() { - gl_Position=gl_Vertex; - gl_TexCoord[0]=gl_MultiTexCoord0; + gl_Position = gl_Vertex; } [frag.glsl] @@ -19,9 +18,9 @@ uniform vec2 offset; uniform float noise; uniform vec3 retrace; -float rand(in vec2 p,in float t,in float v) +float rand(in vec2 p, in float t, in float v) { - return fract(sin(dot(p+mod(t,1.0),vec2(12.9898,78.2333)))*v); + return fract(sin(dot(p + mod(t, 1.0), vec2(12.9898, 78.2333))) * v); } void main(void) @@ -36,4 +35,4 @@ void main(void) c*=1.0+(noise-noise*2.0*r1); // noise c-=retrace.x*0.01*mod(p.y*retrace.y+time*retrace.z,1.0); // retrace gl_FragColor=vec4(c,1.0); -} \ No newline at end of file +} diff --git a/neercs/video/postfx.lolfx b/neercs/video/postfx.lolfx index 9bcc681..4614530 100644 --- a/neercs/video/postfx.lolfx +++ b/neercs/video/postfx.lolfx @@ -4,8 +4,7 @@ void main() { - gl_Position=gl_Vertex; - gl_TexCoord[0]=gl_MultiTexCoord0; + gl_Position = gl_Vertex; } [frag.glsl] @@ -104,4 +103,4 @@ void main(void) c*=letterbox(z,corner.x+2.0,corner.y,corner.z); // corner c+=(g3+g4)*glass.z; // glass gl_FragColor=vec4(c,1.0); -} \ No newline at end of file +} diff --git a/neercs/video/radial.lolfx b/neercs/video/radial.lolfx index c115f8e..7cb606e 100644 --- a/neercs/video/radial.lolfx +++ b/neercs/video/radial.lolfx @@ -2,16 +2,20 @@ #version 120 +varying vec2 pass_TexCoord; + void main() { gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; + pass_TexCoord = vec2(0.5, 0.5) + 0.5 * gl_Vertex.xy; } [frag.glsl] #version 120 +varying vec2 pass_TexCoord; + uniform sampler2D texture; uniform vec2 screen_size; uniform vec4 radial; @@ -25,7 +29,7 @@ vec3 deform(in vec2 p) void main(void) { - vec2 p = -1.0+2.0*gl_TexCoord[0].xy; + vec2 p = -1.0 + 2.0 * pass_TexCoord; vec2 s = p; vec3 source=deform(s); @@ -41,4 +45,4 @@ void main(void) s += d; } gl_FragColor = vec4(source + color * radial.w, 1.0); -} \ No newline at end of file +} diff --git a/neercs/video/remanence.lolfx b/neercs/video/remanence.lolfx index dc9fb7a..3bef08e 100644 --- a/neercs/video/remanence.lolfx +++ b/neercs/video/remanence.lolfx @@ -2,24 +2,28 @@ #version 120 +varying vec2 pass_TexCoord; + void main() { gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; + pass_TexCoord = vec2(0.5, 0.5) + 0.5 * gl_Vertex.xy; } [frag.glsl] #version 120 +varying vec2 pass_TexCoord; + uniform sampler2D source; uniform sampler2D buffer; uniform vec2 mix; void main(void) { - vec4 old_color = texture2D(buffer, gl_TexCoord[0].xy); - vec4 new_color = texture2D(source, gl_TexCoord[0].xy); + vec4 old_color = texture2D(buffer, pass_TexCoord); + vec4 new_color = texture2D(source, pass_TexCoord); /* The old way */ //gl_FragColor = new_color * mix.x + old_color * mix.y; diff --git a/neercs/video/render.cpp b/neercs/video/render.cpp index af410a7..f834815 100644 --- a/neercs/video/render.cpp +++ b/neercs/video/render.cpp @@ -67,8 +67,13 @@ float main_angle = 0.0f; // main angle float part_angle = 0.0f; // part angle float fx_angle; // current angle /* fs_quad variable */ -float fs_quad_vtx[] = {-1.0f, 1.0f, 0, 1.0f, -1.0f, -1.0f, 0, 1.0f, 1.0f, -1.0f, 0, 1.0f, 1.0f, 1.0f, 0, 1.0f}; -float fs_quad_tex[] = {0, 1.0f, 0, 0, 1.0f, 0, 1.0f, 1.0f}; +float fs_quad_vtx[] = +{ + -1.0f, 1.0f, 0, 1.0f, + -1.0f, -1.0f, 0, 1.0f, + 1.0f, -1.0f, 0, 1.0f, + 1.0f, 1.0f, 0, 1.0f +}; /* flash variable */ bool flash_flag = false; // flag float flash_angle = 0; // angle @@ -1471,7 +1476,7 @@ void Render::Draw2D() glLoadMatrixf(&m[0][0]); glMatrixMode(GL_MODELVIEW); - fx_angle=main_angle-part_angle; + fx_angle = main_angle - part_angle; } void Render::Draw3D() @@ -1485,9 +1490,6 @@ void Render::Draw3D() glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(4, GL_FLOAT, 0, fs_quad_vtx); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 0, fs_quad_tex); - if (m_shader_copper) { // shader copper @@ -1723,11 +1725,10 @@ void Render::Draw3D() ShaderSimple(fbo_front, 0); glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); } Render::~Render() { if (m_fps_debug) Ticker::Unref(m_fps_debug); -} \ No newline at end of file +} diff --git a/neercs/video/simple.lolfx b/neercs/video/simple.lolfx index 5a78403..eaa5cfe 100644 --- a/neercs/video/simple.lolfx +++ b/neercs/video/simple.lolfx @@ -2,20 +2,24 @@ #version 120 +varying vec2 pass_TexCoord; + void main() { gl_Position = gl_Vertex; - gl_TexCoord[0] = gl_MultiTexCoord0; + pass_TexCoord = vec2(0.5, 0.5) + 0.5 * gl_Vertex.xy; } [frag.glsl] #version 120 +varying vec2 pass_TexCoord; + uniform sampler2D texture; void main(void) { - gl_FragColor = texture2D(texture, gl_TexCoord[0].xy); + gl_FragColor = texture2D(texture, pass_TexCoord); }