[Solved] 2nd FF texture unit can not be used!?

mehdix

20-03-2008 17:30:30

Hi John, Hi all! I think found a bug:

If another texture unit is added to the trees material of the samples, like the following:
material SOLID/TEX/wood7.jpg
{
technique
{
pass
{
ambient 0.4 0.4 0.4 1
diffuse 1 1 1 1

scene_blend alpha_blend

texture_unit
{
texture wood7.jpg
}
texture_unit
{
texture Leaves.png
}
}
}
}

the result will be problematic:


The problem exists with v1.04 too.
Thanks.

JohnJ

21-03-2008 16:52:18

Ok, thanks for reporting. I can confirm this problem on my computer also.

So far it seems that the second texture unit is only using (0,0) UVs, which is making the trunks fully transparent (the top-left corner of Leaves.png has 0 alpha). It's probably just a simple shader error.

JohnJ

23-03-2008 01:21:26

It looks like this problem isn't specifically a bug in PagedGeometry, but an issue that results in the fact that PagedGeometry is applying a vertex shader to your geometry. According to sinbad (answering my question in [i]this thread[/i]), when vertex shaders are used, the texture coordinate set assigned to the texture units are ignored.

When blending the leaves texture on the trunk, it was looking for a second texture coordinate set which doesn't exist, which resulted in the texture coordinates (0,0) being used for the leaves. This made the entire tree trunk transparent because the pixel in Leaves.png at (0,0) has 0 alpha (opacity).

This means that whenever using PagedGeometry with materials containing more than one texture unit from a single UV coordinate set, you'll have to add a pixel shader to it to ensure it renders correctly, because fixed-function just won't work out. This isn't something I can change unfortunately - it's just a little quirk in the way fixed function / shaders are mixed in current video cards.

I'll try to give you a simple shader that fixes your current problem (but I'm unfamiliar with pixel shaders so it might take a little while). If I could I'd make PagedGeometry automatically apply a pixel shader to fix the problem but unfortunately depending on the material (how many texture units, etc.), a different shader will need to be applied, so I guess I'll have to leave it up to the user.

JohnJ

23-03-2008 04:02:28

Here's a working pixel shader you can apply to your material to fix the problem:

CG script:
//TreePS.cg
void main(
varying float2 uv : TEXCOORD0,
uniform sampler2D tex0 : TEXUNIT0,
uniform sampler2D tex1 : TEXUNIT1,

float4 color : COLOR,
out float4 oColor : COLOR
)
{
float4 c1 = tex2D(tex0, uv);
float4 c2 = tex2D(tex1, uv);
oColor = color * c1 * c2;
}


Ogre fragment program definition:
//TreeShaders.program
fragment_program TreeShader cg
{
source TreePS.cg
entry_point main
profiles ps_2_0 arbfp1 fp20
}


Ogre material:
material SOLID/TEX/wood7.jpg
{
technique
{
pass
{
ambient 0.4 0.4 0.4 1
diffuse 1 1 1 1

//scene_blend alpha_blend

fragment_program_ref TreeShader //<-- here's what you add to link the material to the pixel shader
{
}

texture_unit
{
texture wood7.jpg
}
texture_unit
{
texture Leaves.png
}
}
}
}


And that should fix the problem you're getting :)

Now if the effect you were trying to achieve was to apply a leaf-like growth on the tree bark, this isn't going to do it. A simple modification of the shader, however, can get a vine-like growth on the trees quite nicely:
//TreePS.cg
void main(
varying float2 uv : TEXCOORD0,
uniform sampler2D tex0 : TEXUNIT0,
uniform sampler2D tex1 : TEXUNIT1,

float4 color : COLOR,
out float4 oColor : COLOR
)
{
float4 c1 = tex2D(tex0, uv);
float4 c2 = tex2D(tex1, uv);
oColor = color * (c1 + (c2 * c2.a));
}

mehdix

23-03-2008 14:12:10

Thanks very much for your great support John. :D