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 && tex1.y <= 0.8)
gl_FragColor = value2;
else
gl_FragColor = value1;
}
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?
again I think you want
gl_TexCoord[1] = gl_MultiTexCoord1;
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.
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
suggestion: use “subimage” to make this more simple.
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 >= 0.2 && gl_TexCoord[0].y >= 0.2 &&
gl_TexCoord[0].x <= 0.8 && gl_TexCoord[0].y <= 0.8)
gl_FragColor = value2;
else
gl_FragColor = value1;
}
yes! thanks for the correction.
system
Closed
October 19, 2021, 7:38pm
8
This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.