Textures flickering

Hi, I have a scene with some sprites in it. I am using alpha blending on the sprites for transparency and everything is working pretty well however whenever the sprites are more than a certain distance from the camera the textures start to flicker (different pixels vary in intensity). I have tried using two different forms of texture loading:

myTexture = ResourceCache.GetGlobalInstance().CreateTextureFromFile(dev, "..\\..\\Textures\\myTexture.tga");

and

myTexture = TextureLoader.FromFile(dev, "..\\..\\Textures\\\\myTexture.tga",1024,1024,0,0,Direct3D.Format.A8R8G8B8, Pool.Managed, Filter.Linear, Filter.Linear, 0);

both give the same result. With the second form I have played around with mipmap levels and filters but nothing I've tried seems to fix it.

I've coded exactly the same kind of thing in openGL and never had this problem. I must be missing something.

Also, in general what are the best filters to use for textures (I know it will depend on use) and how can you tell what settings the first texture loading example above will set by default. It doesn't seem to be documented anywhere. Nor for the default version of the second statement.

Any help would be much appreciated. 



Answer this question

Textures flickering

  • bibber

    Make sure you have your MIPFILTER set to LINEAR. Otherwise, just because you've got a full mipmap chain, doesn't mean it's being used!

    The other thing this might be is Z-fighting. As polygons get further from the viewer, the Z precision decreases, so you may be getting problems there. Try turning the Z buffer off (D3DRS_ZENABLE to false). IF that produces more consistent results (even if they're wrong!) then you need to start looking at your Z precision.

  • Arash Bannazadeh

    Textures flickering based on distance from the camera is usually a sign that you don't have any mip-mapping (as you seem to have spotted). However, looking at your code fragment you're setting "0" mip levels, which tells D3DX you want a full chain created. D3DX can't create mip chains for non-power-2 sizes, but again, you're using 1024x1024.. strange!

    Do you have any multi-pass or general blending involved any depth or stencil testing

    Regarding the texture filtering parameters - they're D3DX specific. Basically, if the texture in your file is NOT the same size as you requested then D3DX will resize it accordingly, and in doing so will use the specified filter. It's nothing to do with rendering - that's handled by the sampler states.

    The best rendering filter does depend on what you're using... LINEAR is generally a good all-rounder, but starts to fail (looks very blurry) when the scaling is greater than 2x. Anisotropic usually gives good results - but seems to be more dependent on the underlying hardware.

    hth
    Jack


  • Andrew Arnott - MSFT

     stuntpope wrote:
    When you suggest setting the MIPFILTER to LINEAR do you mean as in


    myTexture = TextureLoader.FromFile(dev, "..\\..\\Textures\\myTexture.tga",1024,1024,0,0,Direct3D.Format.A8R8G8B8, Pool.Managed, Filter.Linear, Filter.Linear, 0);

    or is there somewhere elase that I need to set this as well


    As I mentioned in my previous post, changing the Filter.Linear part of the TextureLoader changes how *D3DX* will do stuff, not how the actual rendering works Smile

    I find it hard to get anywhere with the MDX documentation, but I think the following is what you want:

    device.SetSamplerState( 0, SamplerStageStates.MinFilter, TextureFilter.Linear );
    device.SetSamplerState( 0, SamplerStageStates.MagFilter, TextureFilter.Linear );
    device.SetSamplerState( 0, SamplerStageStates.MipFilter, TextureFilter.Linear );

    Looking in the docs, those will all be set to TextureFilter.None by default. Could well be that those three lines will solve the whole thing for you....

    hth
    Jack



  • rusha

    Thanks Jack, you are awsome. My problem is solved. I used to have these settings worked out in openGL but managed direcx is so poorly documented and I haven't seen any examples with this in it.

    I wish there was a managed directx text that was something like the awsome openGL super Bible (don't remember the author). Or even just adequate explanations of all the functions would be good.

    Thanks again Jack Smile.

  • ivolved

    Thanks for the suggestions guys. I beleive that I have disabled z-buffering with the line

    device.RenderState.ZBufferEnable = false;

    but I still have the same problem. When you suggest setting the MIPFILTER to LINEAR do you mean as in

    myTexture = TextureLoader.FromFile(dev, "..\\..\\Textures\\myTexture.tga",1024,1024,0,0,Direct3D.Format.A8R8G8B8, Pool.Managed, Filter.Linear, Filter.Linear, 0);

    or is there somewhere elase that I need to set this as well

    I am using alpha blending and the scene is depth sorted which is why I don't really need the depth buffer for these sprites.

    Here are the render states that I am using:

    device.RenderState.CullMode = Cull.None;
    device.RenderState.Lighting = true;
    device.RenderState.ZBufferEnable = false;
    device.RenderState.AlphaBlendEnable =
    true;
    device.RenderState.SourceBlend =
    Blend.SourceAlpha;
    device.RenderState.DestinationBlend =
    Blend.InvSourceAlpha;

    The textures are stored as 32 bit tga files.


  • Textures flickering