- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我试图在 XNA 中使用这段代码绘制一个三角形:
VertexPositionColor[] vertices = new VertexPositionColor[3];
vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
vertices[0].Color = Color.Red;
vertices[1].Position = new Vector3(0, 0.5f, 0f);
vertices[1].Color = Color.Green;
vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
vertices[2].Color = Color.Yellow;
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, vertices, 0, 1);
但是,一旦我运行它,应用程序就会关闭,并抛出 InvalidOperationException。这是WP7应用程序。我错过了什么吗?提前感谢您的帮助。
最佳答案
The documentation表示 DrawUserPrimitives
在以下情况下抛出 InvalidOperationException
:
A valid vertex shader and pixel shader was not set before calling DrawUserPrimitives. Both a valid vertex shader and pixel shader (or valid effect) must be set on the device before any draw operations may be performed.
(它还说如果你的顶点无效它会抛出 - 但它们对我来说看起来没问题。)
您需要在图形设备上设置一个Effect
。具体来说,您需要在调用 DrawUserPrimitives
之前调用 EffectPass.Apply
。一个简单的开始方法是使用 BasicEffect
。下面是一些代码,适合放在 Draw
方法中,以说明这一点:
// These three lines are required if you use SpriteBatch, to reset the states that it sets
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
// Transform your model to place it somewhere in the world
basicEffect.World = Matrix.CreateRotationZ(MathHelper.PiOver4) * Matrix.CreateTranslation(0.5f, 0, 0); // for sake of example
//basicEffect.World = Matrix.Identity; // Use this to leave your model at the origin
// Transform the entire world around (effectively: place the camera)
basicEffect.View = Matrix.CreateLookAt(new Vector3(0, 0, -3), Vector3.Zero, Vector3.Up);
// Specify how 3D points are projected/transformed onto the 2D screen
basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45),
(float)GraphicsDevice.Viewport.Width / (float)GraphicsDevice.Viewport.Height, 1.0f, 100.0f);
// Tell BasicEffect to make use of your vertex colors
basicEffect.VertexColorEnabled = true;
// I'm setting this so that *both* sides of your triangle are drawn
// (so it won't be back-face culled if you move it, or the camera around behind it)
GraphicsDevice.RasterizerState = RasterizerState.CullNone;
// Render with a BasicEffect that was created in LoadContent
// (BasicEffect only has one pass - but effects in general can have many rendering passes)
foreach(EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
// This is the all-important line that sets the effect, and all of its settings, on the graphics device
pass.Apply();
// Here's your code:
VertexPositionColor[] vertices = new VertexPositionColor[3];
vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
vertices[0].Color = Color.Red;
vertices[1].Position = new Vector3(0, 0.5f, 0f);
vertices[1].Color = Color.Green;
vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
vertices[2].Color = Color.Yellow;
GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, vertices, 0, 1);
}
关于c# - DrawUserPrimitives 无效操作异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14793268/
我试图在 XNA 中使用这段代码绘制一个三角形: VertexPositionColor[] vertices = new VertexPositionColor[3]; vertices[0].Po
我正在尝试使用 3D 基元在 Monogame(适用于 Windows8)上绘制一个简单的彩色矩形 DrawUserPrimitives和 VertexPositionColor ,但我唯一能看到的是
如何使用 MonoGame 启用抗锯齿到 DrawUserPrimitives ? 我看到了另一个问题:Drawing Bezier curves in MonoGame (XNA) produces
所以这是我今天早些时候提出的问题的延续。我使用 XNA 3.1 的 DrawUserPrimitives 方法为自己构建了一些漂亮的带状轨迹,主要是通过在运动发生时扩展多边形。除了一件事——抗锯齿外,
我正在使用 XNA 3.1 制作一些东西,并且我正在尝试使用 DrawUserPrimitives 创建 2D 功能区轨迹。我的算法简单地在每一帧添加新的顶点(假设发生了一些移动),并且还遍历顶点并随
我是一名优秀的程序员,十分优秀!