Sign in to follow this  

[MDX] Terrain rendering problems using multiple vertex stream

This topic is 3197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have converted my terrain to use multiple stream to allow me to have a bigger landscape. I've manage to make it work. However, after a few frames a rendering bug is showing up. Some triangle start to flicker and they seems to change over time or when i move the camera. I've tried to debug the problem using PIX, but everytimes a take a snapshot everythings is fine (image is ok). This is the relevent part of my shader:
struct VS_INPUT
{
    float2 Position : TEXCOORD0;
    float2 TexCoord : TEXCOORD1;	
    float1 HeightPosition : TEXCOORD2;
};


VS_OUTPUT VS(VS_INPUT input)
{
    VS_OUTPUT output = (VS_OUTPUT)0;
      
    float4 position = float4(input.Position.x, input.HeightPosition.x, input.Position.y, 1);
    
    output.Position = mul(position, g_WorldViewProjection);
    output.TexCoord = input.TexCoord;                       
        
    return output;    
}

And the relevent part from my code
    public struct TerrainVertex
    {
        public float x, z;
        public float u1, v1;
    }

    public struct TerrainHeightVertex
    {
        public float y;
    }

    VertexElement[] vertexDeclaration = new VertexElement[] 
    { 
        new VertexElement(0, 0, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),              
        new VertexElement(0, 8, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 1),
        new VertexElement(1, 16, DeclarationType.Float1, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 2),
        VertexElement.VertexDeclarationEnd,
    };

    public void Draw(Matrix viewMatrix, Matrix projectionMatrix)
    {          
        _graphicsDevice.RenderState.ZBufferEnable = true;
        _graphicsDevice.RenderState.ZBufferWriteEnable = true;
        _graphicsDevice.RenderState.CullMode = Cull.CounterClockwise;

        // disabled alpha-blending and alpha testing
        _graphicsDevice.RenderState.AlphaBlendEnable = false;            
        _graphicsDevice.RenderState.AlphaTestEnable = false;
            
            
        _graphicsDevice.SetStreamSource(0, _gridVertexBuffer, 0, Marshal.SizeOf(typeof(TerrainVertex)));
        _graphicsDevice.SetStreamSource(1, _heightVertexBuffer, 0, Marshal.SizeOf(typeof(TerrainHeightVertex)));
        _graphicsDevice.Indices = _indexBuffer;
        _graphicsDevice.VertexDeclaration = _vertexDeclaration;


        int passCount = _effect.Begin(FX.None);

        for (int iPass = 0; iPass < passCount; iPass++)
        {
            _effect.BeginPass(iPass);
            _graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, _vertexCount, 0, _primitiveCount);
            _effect.EndPass();
        }
            
        _effect.End();

    }

And the results: renderbug image So i'm asking here, since i dont have any good ideas on what could cause this strange behaviour. If i didnt post enought, just ask me the relevent part missing. Thanks to look into it with me ;) Lowrad

Share this post


Link to post
Share on other sites
I really feel bad right now... thanks for reminnding me to reinstall DX SDK so i could get the debug runtime running.

My error was pretty obvious:
Direct3D9: (ERROR) :Vertex stride in stream 1 is less than in the declaration

I forget to change my declaration offsets to:

VertexElement[] vertexDeclaration = new VertexElement[]
{
new VertexElement(0, 0, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
new VertexElement(0, 8, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 1),
new VertexElement(1, 0, DeclarationType.Float1, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 2),
VertexElement.VertexDeclarationEnd,
};



Vertex declaration offset are stream based. duh! :)

Thanks!

Share this post


Link to post
Share on other sites

This topic is 3197 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this