XNA 拉错了纹理?

发布于 2024-12-02 17:26:30 字数 5180 浏览 3 评论 0原文

我正在制作(另一个)《我的世界》克隆,并且遇到了一个有趣的问题。我有一个公共枚举,列出了特定立方体可以是的所有立方体类型,并且我有一个保存立方体的 3d 数组。每个立方体都有特定的类型,我迭代该数组以获取每个立方体的顶点,然后将这些顶点传递到为特定立方体类型指定的顶点缓冲区。当我创建一个随机的立方体数组或单个立方体,并告诉它它应该是什么纹理时,所有东西都会按预期绘制。我现在正在尝试弄清楚如何绘制草立方体的随机“表面”,并用泥土立方体填充 y 轴上那些下方的所有内容。但最奇怪的事情发生了,最上面的立方体是泥土,它用草立方体填充了所有底部的立方体!当我禁用循环以用泥土填充地下时,最顶部的立方体将按预期显示草。

这是我认为代码的相关部分。这是设置立方体类型的位置:

            // Create a random surface level
        Perlin perlin = new Perlin();

        for (int x = 0; x < Game.ChunkWidth_X; x++)
        {
            for (int z = 0; z < Game.ChunkDepth_Z; z++)
            {
                double XVal = Convert.ToDouble(x) * 1.1;
                double ZVal = Convert.ToDouble(z) * 1.1;
                double YVal = Game.ChunkHeight_Y / 2 * 1.1;

                double PerlinValue = perlin.GetValue(XVal, YVal, ZVal);
                int YVal_new = Convert.ToInt32(YVal + (PerlinValue * 10));
                if (YVal_new > Game.ChunkHeight_Y - 1) { YVal_new = Game.ChunkHeight_Y - 1; }
                if (YVal_new < 0) { YVal_new = 0; }

                // Set the grass cube
                Cube NewCube = new Cube(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(x, YVal_new, z));
                NewCube.cubeType = CubeType.Grass;
                CubeGrid[x, YVal_new, z] = NewCube;

                // Fill below it with dirt
                for (int y = YVal_new - 1; y >= 0; y--)
                {
                    Cube NewCube2 = new Cube(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(x, y, z));
                    NewCube2.cubeType = CubeType.Dirt;
                    CubeGrid[x, y, z] = NewCube2;
                }

                // Fill above it with air
                for (int y = YVal_new + 1; y < Game.ChunkHeight_Y; y++)
                {
                    Cube NewCube2 = new Cube(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(x, y, z));
                    NewCube2.cubeType = CubeType.Air;
                    CubeGrid[x, y, z] = NewCube2;
                }

            }
        }

这是我将顶点放入适当缓冲区的位置:

            Dictionary<CubeType, List<VertexPositionNormalTexture>> DrawableVertices = new Dictionary<CubeType, List<VertexPositionNormalTexture>>();

        // Get the proper vertices for each cube type and put in the appropriate dictionary
        for (int x = 0; x < Game.ChunkWidth_X; x++)
        { 
            for (int z = 0; z < Game.ChunkDepth_Z; z++)
            {
                for (int y = 0; y < Game.ChunkHeight_Y; y++)
                {
                    CubeGrid[x,y,z].CreateVertices();
                    string test = CubeGrid[x, y, z].cubeType.ToString();

                    foreach (VertexPositionNormalTexture TargetVertex in CubeGrid[x, y, z].DisplayableVertices)
                    {
                        if (!DrawableVertices.ContainsKey(CubeGrid[x, y, z].cubeType))
                        {
                            List<VertexPositionNormalTexture> NewList = new List<VertexPositionNormalTexture>();
                            NewList.Add(TargetVertex);
                            DrawableVertices.Add(CubeGrid[x, y, z].cubeType, NewList);
                        }
                        else
                        {
                            DrawableVertices[CubeGrid[x, y, z].cubeType].Add(TargetVertex);
                        }
                    }
                }
            }
        }

这是它的第二部分:

            foreach (KeyValuePair<CubeType, List<VertexPositionNormalTexture>> KVP in DrawableVertices)
        {
            VertexBuffer cubeBuffer = new VertexBuffer(device, typeof(VertexPositionNormalTexture), KVP.Value.Count, BufferUsage.WriteOnly);
            cubeBuffer.SetData(KVP.Value.ToArray());

            // Update our collection of vertex buffers
            CubeType_VertexBuffers[KVP.Key] = cubeBuffer;

            // Get the triangle count for the buffer
            CubeType_TriangleCount[KVP.Key] = KVP.Value.Count / 3;

        }

最后,这是我的绘制:

            // Go through each vertex buffer we have created, and draw it.
        foreach (KeyValuePair<CubeType, VertexBuffer> KVP in CubeType_VertexBuffers)
        {
            foreach (EffectPass pass in testEffect.CurrentTechnique.Passes)
            {
                if (CubeType_TriangleCount[KVP.Key] > 0) // if this buffer has triangles, draw it.
                {
                    pass.Apply();
                    testEffect.View = camera.ViewMatrix;
                    testEffect.TextureEnabled = true;
                    testEffect.Projection = camera.ProjectionMatrix;
                    testEffect.World = worldMatrix;
                    testEffect.Texture = CubeType_Texture[KVP.Key];

                    device.SetVertexBuffer(KVP.Value);
                    device.DrawPrimitives(PrimitiveType.TriangleList, 0, CubeType_TriangleCount[KVP.Key]);
                }
            }
        }


        base.Draw(gameTime);

最奇怪的是,当我手动设置立方体类型时,所有内容按预期使用正确的纹理进行绘制。我还应该尝试解决哪些其他问题?我尝试为每种立方体类型制作特定效果,但无济于事。

I am making (another) MineCraft clone, and I've run into an interesting problem. I have a public enum that lists all the cube types a particular cube can be, and I have a 3d array that holds cubes. Each cube has a specific type, and I iterate through this array to get the vertices for each cube, then pass those vertices to a vertex buffer designated for a particular cube type. When I create a random array of cubes, or a single cube, and tell it what texture it should be everything draws as expected. I'm now trying to figure out how to draw a random "surface" of grass cubes, and fill everything below those on the y-axis with dirt cubes. The strangest thing is happening though, the top most cube is dirt and it fills all the bottom ones with grass cubes! When I disable the loop to fill the underground with dirt, the top most cube is displaying grass as intended.

Here is what I believe to be the relevant parts of the code. Here is where the cube type is set:

            // Create a random surface level
        Perlin perlin = new Perlin();

        for (int x = 0; x < Game.ChunkWidth_X; x++)
        {
            for (int z = 0; z < Game.ChunkDepth_Z; z++)
            {
                double XVal = Convert.ToDouble(x) * 1.1;
                double ZVal = Convert.ToDouble(z) * 1.1;
                double YVal = Game.ChunkHeight_Y / 2 * 1.1;

                double PerlinValue = perlin.GetValue(XVal, YVal, ZVal);
                int YVal_new = Convert.ToInt32(YVal + (PerlinValue * 10));
                if (YVal_new > Game.ChunkHeight_Y - 1) { YVal_new = Game.ChunkHeight_Y - 1; }
                if (YVal_new < 0) { YVal_new = 0; }

                // Set the grass cube
                Cube NewCube = new Cube(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(x, YVal_new, z));
                NewCube.cubeType = CubeType.Grass;
                CubeGrid[x, YVal_new, z] = NewCube;

                // Fill below it with dirt
                for (int y = YVal_new - 1; y >= 0; y--)
                {
                    Cube NewCube2 = new Cube(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(x, y, z));
                    NewCube2.cubeType = CubeType.Dirt;
                    CubeGrid[x, y, z] = NewCube2;
                }

                // Fill above it with air
                for (int y = YVal_new + 1; y < Game.ChunkHeight_Y; y++)
                {
                    Cube NewCube2 = new Cube(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(x, y, z));
                    NewCube2.cubeType = CubeType.Air;
                    CubeGrid[x, y, z] = NewCube2;
                }

            }
        }

This is where I pull the vertices to put into the appropriate buffer:

            Dictionary<CubeType, List<VertexPositionNormalTexture>> DrawableVertices = new Dictionary<CubeType, List<VertexPositionNormalTexture>>();

        // Get the proper vertices for each cube type and put in the appropriate dictionary
        for (int x = 0; x < Game.ChunkWidth_X; x++)
        { 
            for (int z = 0; z < Game.ChunkDepth_Z; z++)
            {
                for (int y = 0; y < Game.ChunkHeight_Y; y++)
                {
                    CubeGrid[x,y,z].CreateVertices();
                    string test = CubeGrid[x, y, z].cubeType.ToString();

                    foreach (VertexPositionNormalTexture TargetVertex in CubeGrid[x, y, z].DisplayableVertices)
                    {
                        if (!DrawableVertices.ContainsKey(CubeGrid[x, y, z].cubeType))
                        {
                            List<VertexPositionNormalTexture> NewList = new List<VertexPositionNormalTexture>();
                            NewList.Add(TargetVertex);
                            DrawableVertices.Add(CubeGrid[x, y, z].cubeType, NewList);
                        }
                        else
                        {
                            DrawableVertices[CubeGrid[x, y, z].cubeType].Add(TargetVertex);
                        }
                    }
                }
            }
        }

Here is the second part of it:

            foreach (KeyValuePair<CubeType, List<VertexPositionNormalTexture>> KVP in DrawableVertices)
        {
            VertexBuffer cubeBuffer = new VertexBuffer(device, typeof(VertexPositionNormalTexture), KVP.Value.Count, BufferUsage.WriteOnly);
            cubeBuffer.SetData(KVP.Value.ToArray());

            // Update our collection of vertex buffers
            CubeType_VertexBuffers[KVP.Key] = cubeBuffer;

            // Get the triangle count for the buffer
            CubeType_TriangleCount[KVP.Key] = KVP.Value.Count / 3;

        }

Lastly, here is my draw:

            // Go through each vertex buffer we have created, and draw it.
        foreach (KeyValuePair<CubeType, VertexBuffer> KVP in CubeType_VertexBuffers)
        {
            foreach (EffectPass pass in testEffect.CurrentTechnique.Passes)
            {
                if (CubeType_TriangleCount[KVP.Key] > 0) // if this buffer has triangles, draw it.
                {
                    pass.Apply();
                    testEffect.View = camera.ViewMatrix;
                    testEffect.TextureEnabled = true;
                    testEffect.Projection = camera.ProjectionMatrix;
                    testEffect.World = worldMatrix;
                    testEffect.Texture = CubeType_Texture[KVP.Key];

                    device.SetVertexBuffer(KVP.Value);
                    device.DrawPrimitives(PrimitiveType.TriangleList, 0, CubeType_TriangleCount[KVP.Key]);
                }
            }
        }


        base.Draw(gameTime);

The weirdest thing is that when I manually set cube types everything draws with the proper texture as expected. What other things should I try to troubleshoot? I tried making a specific effect for each cube type to no avail.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够钟 2024-12-09 17:26:30

在绝望中尝试了一些随机的事情之后,我找到了解决这个问题的方法。事实证明,如果您对不同的纹理使用相同的 BasicEffect,它只会使用分配给它的最后一个纹理。我正在迭代 VertexBuffers 列表并为每个顶点分配不同的纹理。当所有内容都转移到视频卡时,只有最后使用的纹理被渲染,或者看起来是这样。

解决方案是为我需要的每个纹理创建一个单独的 BasicEffect,并仅将所需的 VertexBuffer 分配给特定的 BasicEffect。

After trying a bunch of random things in desperation, I found a fix for this. It turns out that if you use the same BasicEffect for different textures, it only uses the last texture assigned to it. I was iterating through a list of VertexBuffers and assigning a different texture for each one. By the time everything made it over to the video card, only the last texture used was rendered, or so it appears.

The solution was to create a separate BasicEffect for each texture I needed and assign only the VertexBuffers needed to the particular BasicEffect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文