4

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アプリケーションです。何か不足していますか?事前にご協力いただきありがとうございます。

4

2 に答える 2

9

ドキュメントには、次の場合にDrawUserPrimitivesスローされると記載されています。InvalidOperationException

DrawUserPrimitives を呼び出す前に、有効な頂点シェーダーとピクセル シェーダーが設定されていませんでした。描画操作を実行する前に、有効な頂点シェーダーとピクセル シェーダー (または有効な効果) の両方をデバイスに設定する必要があります。

(頂点が無効な場合はスローされるとも言われますが、私には問題ないように見えます。)

Effectグラフィックス デバイスにを設定する必要があります。EffectPass.Apply具体的には、 を呼び出す前に呼び出す必要がありますDrawUserPrimitives。簡単に開始するには、 を使用し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);
}
于 2013-02-10T09:53:53.463 に答える
-3

その例外 (InvalidOperationException) は通常、コンポーネントが予期しない状態にあることを検出したときにスローされます。したがって、あなたの場合、DrawUserPrimitives を呼び出す前に、GraphicsDevice に他のプロパティを設定する必要がないことを確認してください。

于 2013-02-10T00:35:05.820 に答える