1

在阅读了 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

谢谢你的帮助...

你的,弗洛里安

4

1 回答 1

1

(回答问题的原始修订。)

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

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

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

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


问题更新后编辑:在您的新版本中,您正在设置顶点和索引缓冲区 - 但它位于错误的位置。您需要在每帧将它们设置到图形设备上。只有在您将它们设置为FillBuffers. 但我猜这些东西是在你班级的Draw方法之外绘制的?

If that something else is a SpriteBatch, even it works using vertex buffers and index buffers. So it will reset your settings. (It's worth adding that it also sets render states - in which case you might need to see this article.)

于 2013-02-18T08:18:58.027 回答