我正在尝试学习如何使用XNA中的基元为uni项目创建3D形状。我已经找到了几个关于这个主题的教程,我认为我已经掌握了基础知识,但是当我开始在我自己的项目代码上开始时,我遇到了一个问题。首先,我试图创建一个三角形,代码如下:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
VertexPositionColor[] vertices;
VertexBuffer vertexBuffer;
BasicEffect effect;
Camera camera;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
public void setUpVertices()
{
vertices=new VertexPositionColor[3];
vertices[0] = new VertexPositionColor(new Vector3(0, 1, 0), Color.Blue);
vertices[1] = new VertexPositionColor(new Vector3(1, -1, 0), Color.Red);
vertices[2]= new VertexPositionColor(new Vector3(-1,-1,0),Color.Green);
}
protected override void Initialize()
{
camera = new Camera(this, new Vector3(0, 0, 5),Vector3.Zero, Vector3.Up);
Components.Add(camera);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
setUpVertices();
vertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), vertices.Length, BufferUsage.None);
vertexBuffer.SetData(vertices);
effect = new BasicEffect(GraphicsDevice);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.SetVertexBuffer(vertexBuffer);
effect.World = Matrix.Identity;
effect.View = camera.view;
effect.Projection = camera.projection;
effect.VertexColorEnabled = true;
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 1);
}
base.Draw(gameTime);
}
}
代码本身编译但屏幕上没有呈现三角形,我无法弄清楚原因。
我已经按照教程说明了三角形现在应该出现在屏幕上,正确(我已经多次检查过),现在我不知道该做什么。
任何帮助或建议都会非常感激,因为我真的在努力使用三角形条带。
注意:相机类使一个简单的3D静态相机指向原点7
修改
相机构造函数的代码
public Camera(Game game, Vector3 pos, Vector3 target, Vector3 up)
: base(game)
{
// TODO: Construct any child components here
projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,(float)Game.Window.ClientBounds.Width /(float)Game.Window.ClientBounds.Height,1, 100);
}
答案 0 :(得分:0)
也许这是一个剔除问题,请尝试添加:
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
答案 1 :(得分:0)
我在相机构造函数中看不到camera.view
但您使用它:effect.View = camera.view;
。也许你没有设置那个变量?