How does a single GLSL shader use a texture unit that can be 2D and 3D ?

How does a single GLSL shader use a texture unit that can be 2D and 3D ?

Post by Cubitu » Sat, 02 Jun 2007 00:01:08


Hello NG,

I'm using both 2D and 3D textures in a single texture unit. Everything
works like a charm in fixed functionality mode.

However, troubles appear when I try to use the textures in a GLSL
fragment shader.

I've used an uniform variable to specify the texture mode I want to use:
0 for no texturing
1 for 2D texturing
2 for 3D texturing.

Used separately, the two following shaders work as intended.

With the first shader, the objects that should use a 3D texture use the
last 2D texture bound. The opposite occurs with the second shader.

--8<------8<------8<--

uniform int textureMode;
uniform sampler2D tex2D;

void main()
{
vec4 color;
color = gl_Color;

if (textureMode == 1)
color = texture2D(tex2D, gl_TexCoord[0].xy);

gl_FragColor = color;
}

--8<------8<------8<--

uniform int textureMode;
uniform sampler3D tex3D;

void main()
{
vec4 color;
color = gl_Color;

if (textureMode == 2)
color = texture3D(tex3D, gl_TexCoord[0].xyz);

gl_FragColor = color;
}

--8<------8<------8<--

Now... I don't get any result when combining them. No compiling/linking
error, but nothing happens :

--8<------8<------8<--

uniform int textureMode;
uniform sampler2D tex2D;
uniform sampler3D tex3D;

void main()
{
vec4 color;
color = gl_Color;

if (textureMode == 1)
color = texture2D(tex2D, gl_TexCoord[0].xy);
if (textureMode == 2)
color = texture3D(tex3D, gl_TexCoord[0].xyz);

gl_FragColor = color;
}

--8<------8<------8<--

Any clue about what is happening and what I am doing wrong?


Thanks!
 
 
 

How does a single GLSL shader use a texture unit that can be 2D and 3D ?

Post by Cubitu » Sat, 02 Jun 2007 00:14:25

Oh boy... Let's make this easier for everybody :

How can I design a shader that works with 2D and 3D textures ?

There, see? Much, much better.

Thanks, again.

 
 
 

How does a single GLSL shader use a texture unit that can be 2D and 3D ?

Post by jukk » Sat, 02 Jun 2007 15:18:37


YMMV, but it might be a better idea to have three shaders and activate
the one you want to use. Why would that be a problem?
 
 
 

How does a single GLSL shader use a texture unit that can be 2D and 3D ?

Post by Cubitu » Sat, 02 Jun 2007 22:33:24


XXXX@XXXXX.COM a rit :


Because I hadn't thought about it... Maybe somewhere, subconsciously, I
was thinking that loading a shader, then replacing it, then loading the
1st again would have more performance impacts than checking a variable
in a single shader. Which, in the first place, might as well be
completely false.

So... I'm gonna follow your idea. Thanks a lot :-)