openGL multiple textures in fragment shader

Hi,

I would like to use two textures in my openGL shader, but I didn’t get the second texture to work. Idea is to switch the texture (based on the distance to the object to another texture with different size).

The fragment shader itself is pretty simple:

[ATTACH=CONFIG]941[/ATTACH]

The vertex shader is just setting “gl_Position” and passing “vTexCoord” (which is not dynamically).

Here you can see how I transfer the texture units to the shaders:

[ATTACH=CONFIG]942[/ATTACH]

The above code snippet is using the following subroutine to load the image data…I have shortend it a bit to the relevant part:

[ATTACH=CONFIG]943[/ATTACH]

If i just use texUnit1 for both cases in the fragment shader rendering is nice, but as soon as I use both I got something like this:

[ATTACH=CONFIG]944[/ATTACH]

(The blue texture (A) is loaded via “texUnit1”)

The interesting part is, that, when I use a different camera rotation both textUnits are rendered fine:

[ATTACH=CONFIG]945[/ATTACH]

It seems that it might be a very trivial issue, as the textures seem to be loaded correctly. I have googled the whole week for a solution and tried many different solution, but I always get the same result…
Would be nice, if anybody could give me a hint…Many thanks…

Markus

[QUOTE=markusf;1264974]I would like to use two textures in my openGL shader, but I didn’t get the second texture to work. Idea is to switch the texture (based on the distance to the object to another texture with different size). …The interesting part is, that, when I use a different camera rotation both textUnits are rendered fine:

[ATTACH=CONFIG]1675[/ATTACH][/QUOTE]

Given that you can get this to work under a different camera rotation suggests that the issue isn’t a problem with loading the textures, creating the OpenGL textures, compiling/linking your shaders, feeding the textures into your shaders, etc.

Apparently, your expression for determining when to use one texture vs. another just isn’t working like you think it should.

Try changing your shader and changing g_DistToCamera to a constant set to 100 (const float g_DistToCamera = 100.0;). Do you get texture0? Now try 100000. Do you get texture1?

Assuming so, look up-stream in the vertex shader at the math for how you’re computing g_DistToCamera. Also, keep in mind that if you want all 4 points in each quad to compute the same distance, you probably want this to compute the same value for all 4 vertices.

Try moving the texture fetches out of the conditional, the texture function relies on implicit LOD which is undefined in non-uniform control flow.

vec4 color0 = texture(texUnit0, pt);
vec4 color1 = texture(texUnit1, pt);
colorOut = (g_distToCamera < 10000) ? color0 : color1;