PDA

View Full Version : twist effect



huaner
05-07-2007, 12:50 AM
hi:
i write a effect of shader that a quad has two texture and the second texture inside the first texure.the shader as fellow:


vertex:
varying vec2 tex1;
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord0;
tex1 = gl_MultiTexCoord0;
gl_Position = ftransform();
}
fragment:
uniform sampler2D TextureUnit0;
uniform sampler2D TextureUnit1;
varying vec2 tex1;
void main()
{
vec4 value1 = texture2D(TextureUnit0, gl_TexCoord[0]);
vec4 value2 = texture2D(TextureUnit1, gl_TexCoord[1]);

if(tex1.x >= 0.2 && tex1.y >= 0.2 &&
tex1.x <= 0.8 &amp;&amp; tex1.y <= 0.8)
gl_FragColor = value2;
else
gl_FragColor = value1;
}
http://blog.163.com/chunjiao_106/blog/getBlog.do?bid=_fks__aB0ExprET-V6OsKCyQARN2EKcj0kp8E
the problem is there is a black quad below the effect quad, and turn back the background picture. May the texture coordnate that i deal with is wrong?

predaeus
05-07-2007, 01:14 AM
again I think you want
gl_TexCoord[1] = gl_MultiTexCoord1;

huaner
05-07-2007, 02:24 AM
Hi:
predaeus. if i rectify it by
gl_TexCoord[1] = gl_MultiTexCoord1;
the result is aslo wrong. and i have side if i complie ogl procedure with this shader, the result is right.

kewuwsi
06-06-2007, 01:52 PM
looks the shaders are OK, seems the two textures are sharing same coordinates(gl_TexCoord[0] == gl_TexCoord[1]).
So, check the OGL application to make sure the two textures are aligned well.
also, I think you don't have to use the variable tex1,since gl_TexCoord[0] == tex1

kewuwsi
06-06-2007, 01:56 PM
suggestion: use "subimage" to make this more simple.

V-man
06-06-2007, 09:30 PM
As far as I know, it should not even compile

This
vec4 value1 = texture2D(TextureUnit0, gl_TexCoord[0]);

should be

vec4 value1 = texture2D(TextureUnit0, gl_TexCoord[0].xy);


Here is a optimized version of your shader


vertex:

void main()
{
gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;

gl_Position = ftransform();
}

fragment:
uniform sampler2D TextureUnit0;
uniform sampler2D TextureUnit1;

void main()
{
vec4 value1 = texture2D(TextureUnit0, gl_TexCoord[0].xy);
vec4 value2 = texture2D(TextureUnit1, gl_TexCoord[0].xy);

if(gl_TexCoord[0].x &amp;gt;= 0.2 &amp;&amp; gl_TexCoord[0].y &amp;gt;= 0.2 &amp;&amp;
gl_TexCoord[0].x &amp;lt;= 0.8 &amp;&amp; gl_TexCoord[0].y &amp;lt;= 0.8)
gl_FragColor = value2;
else
gl_FragColor = value1;
}

kewuwsi
06-07-2007, 07:03 AM
yes! thanks for the correction.