PDA

View Full Version : openGL multiple textures in fragment shader



markusf
03-20-2015, 09:53 AM
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:

1671

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:

1672


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

1673


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:

1674


(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:

1675

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

Dark Photon
03-20-2015, 04:39 PM
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:

1675


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.

Firadeoclus
03-23-2015, 03:57 AM
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;