C# 三角带
我正在努力学习如何使用XNA中的原语为uni的一个项目创建3D形状。我已经找到了一些关于这个主题的教程,我想我已经掌握了基本知识,但是当我开始为这个项目编写自己的代码时,我遇到了一个问题。首先,我试图创建一个三角形,代码如下:C# 三角带,c#,xna,primitive,C#,Xna,Primitive,我正在努力学习如何使用XNA中的原语为uni的一个项目创建3D形状。我已经找到了一些关于这个主题的教程,我想我已经掌握了基本知识,但是当我开始为这个项目编写自己的代码时,我遇到了一个问题。首先,我试图创建一个三角形,代码如下: public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; VertexPositionC
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);
}
}
可能是筛选问题,请尝试添加:
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
我看不到摄影机构造函数中的
camera.view
,但您使用它:effect.view=camera.view代码>。可能您没有设置该变量?我在Draw函数中有这一行。我认为问题不在缓冲区使用中。无,我通常使用缓冲区使用。WriteOnly
。您用于矩阵.CreatePerspectiveFieldOfView的参数是什么?可能远平面太近了。我尝试将其修改为BufferUsage.WriteOnly
,但没有改变:(尝试添加GraphicsDevice.RasterizerState=RasterizerState.CullNone;
但仍然没有变化,我尝试了无消隐,逆时针和顺时针消隐。这证明是一个相当棘手的问题。CreatePerspectiveFieldOfView
的远平面参数如何?是的!视图是在摄影机类b中声明的。)ut从未分配到。我需要在camera类中使用view=Matrix.CreateLookAt(pos,target,up);
。感谢您的帮助!
GraphicsDevice.RasterizerState = RasterizerState.CullNone;