3
我目前正在嘗試通過組合兩個三角形來製作一個簡單的正方形,就像Riemer的教程(Link to tutorial)一樣,但由於很多已從3.x更改爲4.0,所以我發現它很困難。 我也想知道如何紋理這個「正方形」,所以如果任何人可以通過舉一些例子或任何幫助我,我將不勝感激:)Xna 4.0 3D頂點示例
謝謝!
- 基本
我目前正在嘗試通過組合兩個三角形來製作一個簡單的正方形,就像Riemer的教程(Link to tutorial)一樣,但由於很多已從3.x更改爲4.0,所以我發現它很困難。 我也想知道如何紋理這個「正方形」,所以如果任何人可以通過舉一些例子或任何幫助我,我將不勝感激:)Xna 4.0 3D頂點示例
謝謝!
下面是一個例子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);
}
}
}
謝謝!正是我所需要的:)現在我只需要成千上萬的這些,彼此相鄰的瓷磚:) – Basic 2010-09-14 18:31:37