3d Xna 4.0三维顶点示例
我目前正试图通过组合两个三角形来制作一个简单的正方形,就像Riemer()的教程中那样,但是由于从3.x到4.0有很多变化,我发现这很困难。 我还想知道如何制作这个“正方形”,所以如果有人能给我举一些例子或其他什么,我将不胜感激:) 谢谢3d Xna 4.0三维顶点示例,3d,xna,textures,vertex,xna-4.0,3d,Xna,Textures,Vertex,Xna 4.0,我目前正试图通过组合两个三角形来制作一个简单的正方形,就像Riemer()的教程中那样,但是由于从3.x到4.0有很多变化,我发现这很困难。 我还想知道如何制作这个“正方形”,所以如果有人能给我举一些例子或其他什么,我将不胜感激:) 谢谢 基本的 下面是一个XNA 4.0程序示例,它绘制了一个简单的纹理正方形。它需要将Green-gel-x纹理(来自wiki commons-代码中的链接)添加到内容项目中(或替换为您自己的纹理)。绘制纹理正方形后,将在顶部绘制线框正方形,以便可以看到三角形。此
- 基本的
- 下面是一个XNA 4.0程序示例,它绘制了一个简单的纹理正方形。它需要将Green-gel-x纹理(来自wiki commons-代码中的链接)添加到内容项目中(或替换为您自己的纹理)。绘制纹理正方形后,将在顶部绘制线框正方形,以便可以看到三角形。此示例使用正交投影和基本效果而不是效果文件,但在其他方面与链接到的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);
}
}
}
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
命名空间窗口游戏
{
公共类游戏1:Microsoft.Xna.Framework.Game
{
常量字符串纹理\u 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;
光栅化器状态线框\光栅化器\状态=新光栅化器状态(){CullMode=CullMode.None,FillMode=FillMode.WIREFRAME};
图形管理器图形;
基本效应;
纹理2D纹理;
VertexPositionColorTexture[]顶点数据;
int[]索引数据;
矩阵视图矩阵;
矩阵投影矩阵;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
效果=新的基本效果(graphics.GraphicsDevice);
设置顶点(颜色。白色);
设置摄像头();
设置索引();
base.Initialize();
}
私有空心设置顶点(颜色)
{
恒浮子半侧=200.0f;
常数浮动Z=0.0f;
vertexData=新的VertexositionColorTexture[4];
vertexData[左上]=新的VertexositionColorTexture(新矢量3(-HALF_边,HALF_边,Z),颜色,新矢量2(0,0));
vertexData[TOP_RIGHT]=新的VertexositionColorTexture(新矢量3(半_边,半_边,Z),颜色,新矢量2(1,0));
vertexData[右下]=新的VertexositionColorTexture(新矢量3(半半半侧,-半半半侧,Z),颜色,新矢量2(1,1));
vertexData[BOTTOM_LEFT]=新的VertexositionColorTexture(新矢量3(-HALF_SIDE,-HALF_SIDE,Z),颜色,新矢量2(0,1));
}
私有索引()
{
indexData=新整数[6];
indexData[0]=左上角;
indexData[1]=右下角;
indexData[2]=左下角;
indexData[3]=左上角;
indexData[4]=右上角;
indexData[5]=右下角;
}
专用照相机()
{
viewMatrix=Matrix.Identity;
projectionMatrix=Matrix.CreateOrthographic(Window.ClientBounds.Width,Window.ClientBounds.Height,-1.0f,1.0f);
}
受保护的覆盖void LoadContent()
{
纹理=Content.Load(纹理名称);
}
受保护覆盖无效更新(游戏时间游戏时间)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
//绘制纹理框
GraphicsDevice.RasterizerState=RasterizerState.CullNone;//顶点顺序无关紧要
GraphicsDevice.BlendState=BlendState.nonpremultipled;//使用alpha混合
GraphicsDevice.DepthStencilState=DepthStencilState.None;//不用担心深度/模具缓冲区
effect.View=viewMatrix;
效果:投影=投影矩阵;
效果:纹理=纹理;
effect.TextureEnabled=真;
effect.DiffuseColor=Color.White.ToVector3();
effect.currentTechnical.Passes[0]。Apply();
GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangelList,vertexData,0,4,indexData,0,2);
//拉线框
GraphicsDevice.RasterizerState=线框\光栅化器\状态;//绘制线框
GraphicsDevice.BlendState=BlendState.不透明;//这次没有alpha
effect.TextureEnabled=false;
effect.DiffuseColor=Color.Black.ToVector3();
effect.currentTechnical.Passes[0]。Apply();
GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangelList,vertexData,0,4,indexData,0,2);
基础。抽签(游戏时间);
}
}
}
谢谢!正是我所需要的:)现在我只需要成千上万个这样的,贴在一起:)