错误XNA Framework VertexBuffer IndexBuffer使用情况

本文关键字:用情 情况 IndexBuffer VertexBuffer XNA Framework 错误 | 更新日期: 2023-09-27 17:59:54

我在完成XNA 4.0的一本书后正在创建一个游戏。它将是3D的,但我已经陷入了创建地形的困境。。。

更新:从这里开始的一切都是更新。。。地形更新:

public void Update(Matrix view, Matrix projection)
    {
        View = view;
        Projection = projection;
        World = Matrix.CreateTranslation(-Width / 2f, 0, Height / 2f);
    }

地形绘制:

public void Draw(GraphicsDevice g)
    {
        effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
        effect.Parameters["xView"].SetValue(View);
        effect.Parameters["xProjection"].SetValue(Projection);
        effect.Parameters["xWorld"].SetValue(World);
        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
         //g.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionColorNormal.VertexDeclaration);
          g.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
        }
    }

评论行正在工作,在这两种情况下,我都能看到地形。。。以下代码用于初始化顶点和索引缓冲区:

private void SetUpVertices(GraphicsDevice g)
    {
        float currentH;
        int currentI;
        vertices = new VertexPositionColorNormal[Width * Height];
        for (int x = 0; x < Width; x++)
        {
            for (int y = 0; y < Height; y++)
            {
                currentH = heightData[x,y];
                currentI = x + y * Width;
                vertices[currentI].Position = new Vector3(x, currentH , -y);
                if (currentH < minH + (maxH - minH) / 3)
                    vertices[currentI].Color = Color.ForestGreen;
                else if (currentH < maxH - (maxH - minH) / 3)
                    vertices[currentI].Color = Color.LawnGreen;
                else
                    vertices[currentI].Color = Color.White;
            }
        }
        SetUpIndices(g);
    }
    private void SetUpIndices(GraphicsDevice g)
    {
        indices = new int[(Width - 1) * (Height - 1) * 6];
        int counter = 0;
        for (int y = 0; y < Height - 1; y++)
        {
            for (int x = 0; x < Width - 1; x++)
            {
                int lowerLeft = x + y * Width;
                int lowerRight = (x + 1) + y * Width;
                int topLeft = x + (y + 1) * Width;
                int topRight = (x + 1) + (y + 1) * Width;
                indices[counter++] = topLeft;
                indices[counter++] = lowerRight;
                indices[counter++] = lowerLeft;
                indices[counter++] = topLeft;
                indices[counter++] = topRight;
                indices[counter++] = lowerRight;
            }
        }
        SetUpNormals(g);
    }
    private void SetUpNormals(GraphicsDevice g)
    {
        for (int i = 0; i < vertices.Length; i++)
        {
            vertices[i].Normal = Vector3.Zero;
        }
        int[] index = new int[3];
        Vector3 s1, s2, n;
        for (int i = 0; i < vertices.Length / 3; i++)
        {
            for (int y = 0; y < 3; y++)
                index[y] = indices[i * 3 + y];
            s1 = vertices[index[0]].Position - vertices[index[2]].Position;
            s2 = vertices[index[0]].Position - vertices[index[1]].Position;
            n = Vector3.Cross(s1, s2);
            for (int y = 0; y < 3; y++)
            {
                vertices[index[y]].Normal += n;
                vertices[index[y]].Normal.Normalize();
            }
        }
        FillBuffers(g);
    }
    private void FillBuffers(GraphicsDevice g)
    {
        VertexBuffer = new VertexBuffer(g, VertexPositionColorNormal.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
        VertexBuffer.SetData(vertices);
        IndexBuffer = new IndexBuffer(g, typeof(int), indices.Length, BufferUsage.WriteOnly);
        IndexBuffer.SetData(indices);
        g.Indices = IndexBuffer;
        g.SetVertexBuffer(VertexBuffer);
    }

我不认为这是一个错误,因为它正在与另一条线路合作。我使用的.fx文件可能有错误。如果你这么认为的话,我将切换到BasicEffects。。。(您可能会注意到,代码来自http://www.riemers.net/eng/Tutorials/XNA/Csharp/series1.php)

谢谢你的帮助。。。

您的,Florian

错误XNA Framework VertexBuffer IndexBuffer使用情况

(问题原始修订版的答案。)

您没有在图形设备上设置顶点缓冲区和索引缓冲区。这两行代码(未经测试)应该可以满足您的需要:

g.GraphicsDevice.Indices = indexBuffer;
g.GraphicsDevice.SetVertexBuffer(vertexBuffer);

将它们放置在设置效果参数(ef)之后,循环之前。

顶点缓冲区提供异常消息所要求的顶点声明。


问题更新后编辑:在新版本中,您正在设置顶点和索引缓冲区,但它位于错误的位置。您需要在每帧的图形设备上设置它们。只有在FillBuffers中设置代码后,没有任何更改,代码才能工作。但我猜这些东西是在你的类的Draw方法之外绘制的?

如果其他东西是SpriteBatch,即使,它也可以使用顶点缓冲区和索引缓冲区。所以它会重置你的设置。(值得一提的是,它还设置了渲染状态-在这种情况下,您可能需要查看本文。)