Xna 4.0 3D 顶点示例

发布于 2024-09-19 14:43:27 字数 300 浏览 5 评论 0 原文

我目前正在尝试通过组合两个三角形来制作一个简单的正方形,就像 Riemer 的教程中那样(教程链接),但由于从 3.x 到 4.0 发生了很多变化,我发现这很困难。 我还想知道如何纹理这个“正方形”,所以如果有人可以通过给出一些例子或其他方式帮助我,我将不胜感激:)

谢谢!

  • 基本的

I'm currently trying to make a simple square by combining two triangles, like in the tutorials by Riemer (Link to tutorial), but since a lot has changed from 3.x to 4.0, I find it difficult.
I would also like to know how to texture this "square", so if anyone could help me by giving some example or whatsoever, I would appreciate it :)

Thanks!

  • Basic

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

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

发布评论

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

评论(1

删除→记忆 2024-09-26 14:45:20

下面是一个绘制简单纹理正方形的 XNA 4.0 程序示例。它需要将 Green-gel-x 纹理(来自 wiki-commons - 代码中的链接)添加到内容项目中(或替换为您自己的纹理)。绘制纹理正方形后,会在顶部绘制一个线框正方形,以便您可以看到三角形。此示例使用正交投影和 BasicEffect 而不是效果文件,但在其他方面与您链接到的 Riemer 教程类似。

为了执行纹理化,每个顶点需要一个纹理坐标。对于在正方形表面上平铺一次的纹理,左上角顶点的纹理坐标为 (0, 0),右下角顶点的纹理坐标为 (1, 1),依此类推。如果您想在正方形上平铺纹理两次,可以将所有右下角纹理坐标设置为 2,而不是 1。

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        const string TEXTURE_NAME = "Green-gel-x";  // http://upload.wikimedia.org/wikipedia/commons/9/99/Green-gel-x.png
        const int TOP_LEFT = 0;
        const int TOP_RIGHT = 1;
        const int BOTTOM_RIGHT = 2;
        const int BOTTOM_LEFT = 3;
        RasterizerState WIREFRAME_RASTERIZER_STATE = new RasterizerState() { CullMode = CullMode.None, FillMode = FillMode.WireFrame };

        GraphicsDeviceManager graphics;
        BasicEffect effect;
        Texture2D texture;
        VertexPositionColorTexture[] vertexData;
        int[] indexData;
        Matrix viewMatrix;
        Matrix projectionMatrix;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            effect = new BasicEffect(graphics.GraphicsDevice);

            SetUpVertices(Color.White);
            SetUpCamera();
            SetUpIndices();

            base.Initialize();
        }

        private void SetUpVertices(Color color)
        {
            const float HALF_SIDE = 200.0f;
            const float Z = 0.0f;

            vertexData = new VertexPositionColorTexture[4];
            vertexData[TOP_LEFT] = new VertexPositionColorTexture(new Vector3(-HALF_SIDE, HALF_SIDE, Z), color, new Vector2(0, 0));
            vertexData[TOP_RIGHT] = new VertexPositionColorTexture(new Vector3(HALF_SIDE, HALF_SIDE, Z), color, new Vector2(1, 0));
            vertexData[BOTTOM_RIGHT] = new VertexPositionColorTexture(new Vector3(HALF_SIDE, -HALF_SIDE, Z), color, new Vector2(1, 1));
            vertexData[BOTTOM_LEFT] = new VertexPositionColorTexture(new Vector3(-HALF_SIDE, -HALF_SIDE, Z), color, new Vector2(0, 1));
        }

        private void SetUpIndices()
        {
            indexData = new int[6];
            indexData[0] = TOP_LEFT;
            indexData[1] = BOTTOM_RIGHT;
            indexData[2] = BOTTOM_LEFT;

            indexData[3] = TOP_LEFT;
            indexData[4] = TOP_RIGHT;
            indexData[5] = BOTTOM_RIGHT;
        }

        private void SetUpCamera()
        {
            viewMatrix = Matrix.Identity;
            projectionMatrix = Matrix.CreateOrthographic(Window.ClientBounds.Width, Window.ClientBounds.Height, -1.0f, 1.0f);
        }

        protected override void LoadContent()
        {
            texture = Content.Load<Texture2D>(TEXTURE_NAME);
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // Draw textured box
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;  // vertex order doesn't matter
            GraphicsDevice.BlendState = BlendState.NonPremultiplied;    // use alpha blending
            GraphicsDevice.DepthStencilState = DepthStencilState.None;  // don't bother with the depth/stencil buffer

            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;
            effect.Texture = texture;
            effect.TextureEnabled = true;
            effect.DiffuseColor = Color.White.ToVector3();
            effect.CurrentTechnique.Passes[0].Apply();

            GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertexData, 0, 4, indexData, 0, 2);

            // Draw wireframe box
            GraphicsDevice.RasterizerState = WIREFRAME_RASTERIZER_STATE;    // draw in wireframe
            GraphicsDevice.BlendState = BlendState.Opaque;                  // no alpha this time

            effect.TextureEnabled = false;
            effect.DiffuseColor = Color.Black.ToVector3();
            effect.CurrentTechnique.Passes[0].Apply();

            GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertexData, 0, 4, indexData, 0, 2);

            base.Draw(gameTime);
        }
    }
}

Here is an example XNA 4.0 program that draws a simple textured square. It requires the Green-gel-x texture (from wiki-commons - link in code) added to the content project (or replaced with your own texture). After the textured square is drawn, a wireframe square is drawn over the top so you can see the triangles. This example uses an orthographic projection and a BasicEffect instead of an effect file but is otherwise similar to the Riemer tutorial you linked to.

To perform the texturing each vertex requires a texture coordinate. For a texture that tiles once across the surface of the square the texture coordinates are (0, 0) for the top left vertex and (1, 1) for the bottom right vertex and so on. If you wanted to tile the texture across the square twice, you could set all the bottom right texture coordinates to 2, instead of 1.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        const string TEXTURE_NAME = "Green-gel-x";  // http://upload.wikimedia.org/wikipedia/commons/9/99/Green-gel-x.png
        const int TOP_LEFT = 0;
        const int TOP_RIGHT = 1;
        const int BOTTOM_RIGHT = 2;
        const int BOTTOM_LEFT = 3;
        RasterizerState WIREFRAME_RASTERIZER_STATE = new RasterizerState() { CullMode = CullMode.None, FillMode = FillMode.WireFrame };

        GraphicsDeviceManager graphics;
        BasicEffect effect;
        Texture2D texture;
        VertexPositionColorTexture[] vertexData;
        int[] indexData;
        Matrix viewMatrix;
        Matrix projectionMatrix;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            effect = new BasicEffect(graphics.GraphicsDevice);

            SetUpVertices(Color.White);
            SetUpCamera();
            SetUpIndices();

            base.Initialize();
        }

        private void SetUpVertices(Color color)
        {
            const float HALF_SIDE = 200.0f;
            const float Z = 0.0f;

            vertexData = new VertexPositionColorTexture[4];
            vertexData[TOP_LEFT] = new VertexPositionColorTexture(new Vector3(-HALF_SIDE, HALF_SIDE, Z), color, new Vector2(0, 0));
            vertexData[TOP_RIGHT] = new VertexPositionColorTexture(new Vector3(HALF_SIDE, HALF_SIDE, Z), color, new Vector2(1, 0));
            vertexData[BOTTOM_RIGHT] = new VertexPositionColorTexture(new Vector3(HALF_SIDE, -HALF_SIDE, Z), color, new Vector2(1, 1));
            vertexData[BOTTOM_LEFT] = new VertexPositionColorTexture(new Vector3(-HALF_SIDE, -HALF_SIDE, Z), color, new Vector2(0, 1));
        }

        private void SetUpIndices()
        {
            indexData = new int[6];
            indexData[0] = TOP_LEFT;
            indexData[1] = BOTTOM_RIGHT;
            indexData[2] = BOTTOM_LEFT;

            indexData[3] = TOP_LEFT;
            indexData[4] = TOP_RIGHT;
            indexData[5] = BOTTOM_RIGHT;
        }

        private void SetUpCamera()
        {
            viewMatrix = Matrix.Identity;
            projectionMatrix = Matrix.CreateOrthographic(Window.ClientBounds.Width, Window.ClientBounds.Height, -1.0f, 1.0f);
        }

        protected override void LoadContent()
        {
            texture = Content.Load<Texture2D>(TEXTURE_NAME);
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // Draw textured box
            GraphicsDevice.RasterizerState = RasterizerState.CullNone;  // vertex order doesn't matter
            GraphicsDevice.BlendState = BlendState.NonPremultiplied;    // use alpha blending
            GraphicsDevice.DepthStencilState = DepthStencilState.None;  // don't bother with the depth/stencil buffer

            effect.View = viewMatrix;
            effect.Projection = projectionMatrix;
            effect.Texture = texture;
            effect.TextureEnabled = true;
            effect.DiffuseColor = Color.White.ToVector3();
            effect.CurrentTechnique.Passes[0].Apply();

            GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertexData, 0, 4, indexData, 0, 2);

            // Draw wireframe box
            GraphicsDevice.RasterizerState = WIREFRAME_RASTERIZER_STATE;    // draw in wireframe
            GraphicsDevice.BlendState = BlendState.Opaque;                  // no alpha this time

            effect.TextureEnabled = false;
            effect.DiffuseColor = Color.Black.ToVector3();
            effect.CurrentTechnique.Passes[0].Apply();

            GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertexData, 0, 4, indexData, 0, 2);

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