2010-09-14 75 views
3

我目前正在尝试通过组合两个三角形来制作一个简单的正方形,就像Riemer的教程(Link to tutorial)一样,但由于很多已从3.x更改为4.0,所以我发现它很困难。 我也想知道如何纹理这个“正方形”,所以如果任何人可以通过举一些例子或任何帮助我,我将不胜感激:)Xna 4.0 3D顶点示例

谢谢!

  • 基本

回答

8

下面是一个例子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); 
     } 
    } 
} 
+0

谢谢!正是我所需要的:)现在我只需要成千上万的这些,彼此相邻的瓷砖:) – Basic 2010-09-14 18:31:37