I doubled the question from stackoverflow here, and will delete the opposite of a question that gave me the answer.

I have the Draw method in one of my renderers, that loops through the dictionary and gets precollected and preinitialized buffers. When dictionary has only one element, everything is just fine. But with more elements what I get on the screen is only the data from the last buffer (I suppose, not sure)

My Draw method:

public void Draw(GameTime gameTime)
    if (!_areStaticEffectsSet)
//          blockEffect.Parameters["TextureAtlas"].SetValue(textureAtlas);



            _areStaticEffectsSet = true;


        var viewFrustum = new BoundingFrustum(_player.CameraView * _player.CameraProjection);

        _graphicsDevice.BlendState = BlendState.Opaque;
        _graphicsDevice.DepthStencilState = DepthStencilState.Default;            

        foreach (KeyValuePair<int, Texture2D> textureAtlas in textureAtlases)

            foreach (EffectPass pass in blockEffect.CurrentTechnique.Passes)

                //TODO: Отправлять сгруппированные из чанков по атласам текстур VertexBuffer и IndexBuffer
                foreach (Chunk chunk in _world.Chunks.Values)
                    if (chunk == null || chunk.IsDisposed)

                    if (chunk.BoundingBox.Intersects(viewFrustum) && chunk.GetBlockIndexBuffer(textureAtlas.Key) != null)
                        lock (chunk)
                            if (chunk.GetBlockIndexBuffer(textureAtlas.Key).IndexCount > 0)
                                VertexBuffer vertexBuffer = chunk.GetBlockVertexBuffer(textureAtlas.Key);
                                IndexBuffer indexBuffer = chunk.GetBlockIndexBuffer(textureAtlas.Key);
                                //if (chunk.DrawIndex == new Vector3i(0, 0, 0))
                                    //if (textureAtlas.Key == -1)
                                        //var varray = new []
                                            //new VertexPositionTextureLight(new Vector3(0,68,0), new Vector2(0,1),1,new Vector3(0,0,0), new Vector3(1,1,1)), 
                                            //new VertexPositionTextureLight(new Vector3(0,68,1), new Vector2(0,1),1,new Vector3(0,0,0), new Vector3(1,1,1)), 
                                            //new VertexPositionTextureLight(new Vector3(1,68,0), new Vector2(0,1),1,new Vector3(0,0,0), new Vector3(1,1,1)) 

                                        //var iarray = new short[] {0, 1, 2};
                                        //vertexBuffer = new VertexBuffer(_graphicsDevice, typeof(VertexPositionTextureLight), varray.Length, BufferUsage.WriteOnly);
                                        //indexBuffer = new IndexBuffer(_graphicsDevice, IndexElementSize.SixteenBits, iarray.Length, BufferUsage.WriteOnly);


                                _graphicsDevice.Indices = indexBuffer;
                                _graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertexBuffer.VertexCount, 0, indexBuffer.IndexCount / 3);                                    

Noteworthy things about the code:

XNA version is 4.0.

I've commented the debugging code in the loop, but left it for it may bring some insight.

I try not only to change vertices/indices in the loop, but textureAtlas also. Code in the shader about textureAtlas:

Texture TextureAtlas;
sampler TextureAtlasSampler = sampler_state
    texture = <TextureAtlas>;
    magfilter = POINT;
    minfilter = POINT;
    mipfilter = POINT;
    AddressU = WRAP;
    AddressV = WRAP;

struct VSInput
    float4 Position : POSITION0;    
    float2 TexCoords1 : TEXCOORD0;
    float SunLight : COLOR0;
    float3 LocalLight : COLOR1;
    float3 Normal : NORMAL0;

VertexPositionTextureLight is my own realization of IVertexType.

So, do anybody know about this problem, or see the wrongness in my code (that's far more likely)?

  • 2
    \$\begingroup\$ It cannot realistically overdraw data from the previous buffers (because it's thoroughly tested). It is more likely that the data is the same in all buffers or GetBlockVertexBuffer doesn't work as expected. And since everything seems to be fine here, those are the two most likely causes of this problem. \$\endgroup\$ – snake5 Oct 2 '12 at 9:52

The problem is not in overdrawing but indeed in wrong VertexBuffer or/and wrong IndexBuffer content as snake5 noticed.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.