2013-06-24 6 views
0

은 이미 그들이 XNA에서 2D 선을 그릴하는 방법을 설명합니다,하지만 난이 exeption를 얻을XNA - 그리기 2D 라인

  { 
       int points = 3;//I tried different values(1,2,3,4) 
       VertexPositionColor[] primitiveList = new VertexPositionColor[points]; 

       for (int x = 0; x < points/2; x++) 
       { 
        for (int y = 0; y < 2; y++) 
        { 
         primitiveList[(x * 2) + y] = new VertexPositionColor(
          new Vector3(x * 100, y * 100, 0), Color.White); 
        } 
       } 
       // Initialize an array of indices of type short. 
       short[] lineListIndices = new short[(points * 2) - 2]; 

       // Populate the array with references to indices in the vertex buffer 
       for (int i = 0; i < points - 1; i++) 
       { 
        lineListIndices[i * 2] = (short)(i); 
        lineListIndices[(i * 2) + 1] = (short)(i + 1); 
       } 
       GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
        PrimitiveType.LineList, 
        primitiveList, 
        0, // vertex buffer offset to add to each element of the index buffer 
        8, // number of vertices in pointList 
        lineListIndices, // the index buffer 
        0, // first index element to read 
        7 // number of primitives to draw 
        ); <---This parameter must be a valid index within the array. Parameter name: primitiveCount 
      } 

(스크립트를 참조)하지만 난 해결하는 방법에 아무 생각이 여기 http://msdn.microsoft.com/en-us/library/bb196414.aspx#ID2EEF 보았다 이, 이것은 3d 렌더링을 사용하여 2D 그래픽을 내 처음부터

배경 :

메신저 XNA에 대한 2D 게임 엔진을 제작하고, 나는 이미 당신을 알고, 간단한 그림을 그리기위한 클래스를 만들 을 원한다 1x1을 사용할 수있다. 그리기를위한 Texture2D 픽셀 속임수지만, GPU가 이것을 쉽게 처리 할 수있는 동안 CPU가 모든 계산을 수행 할 것이기 때문에이 방법도 알고 싶습니다.

+0

이 라이브러리를 사용하고 있습니다. https://bitbucket.org/C3/2d-xna-primitives/wiki/Home – Dmi7ry

답변

0

오류가 발생하지 않았으므로 그 그림을 그려 보겠습니다. 모양이

폐쇄 여부를해야하는지 내가 한 줄을 그리기 위해이 확장 방법을 사용

, 당신은 closed가 정의도 포인트로 만든 모양을 그릴 것입니다 흰색의 1x1 텍스처

public static void DrawLine(this SpriteBatch spriteBatch, Vector2 begin, Vector2 end, Color color, int width = 1) 
{ 
    Rectangle r = new Rectangle((int)begin.X, (int)begin.Y, (int)(end - begin).Length()+width, width); 
    Vector2 v = Vector2.Normalize(begin - end); 
    float angle = (float)Math.Acos(Vector2.Dot(v, -Vector2.UnitX)); 
    if (begin.Y > end.Y) angle = MathHelper.TwoPi - angle; 
    spriteBatch.Draw(Pixel, r, null, color, angle, Vector2.Zero, SpriteEffects.None, 0); 
} 

이 필요합니다

public static void DrawPolyLine(this SpriteBatch spriteBatch, Vector2[] points, Color color, int width = 1, bool closed = false) 
    { 


     for (int i = 0; i < points.Length - 1; i++) 
      spriteBatch.DrawLine(points[i], points[i + 1], color, width); 
     if (closed) 
      spriteBatch.DrawLine(points[points.Length - 1], points[0], color, width); 

    } 
0

가장 먼저 정의한 것은 정점입니다. 이것은 약간 이상해 보인다. points3이면 외부 루프는 0..0이고 내부 루프는 0..1입니다. 따라서 두 개의 정점이 있습니다. 이 점들로 그릴 수있는 선은 하나뿐입니다. points = 4을 지정하면 실제로 4 점을 얻게됩니다.

반복되는 정점이없는 연속 선을 그리는 것처럼 보입니다. 따라서 색인화 된 표현 (색인 버퍼 포함)이 실제로 필요하지 않습니다. 꼭지점을 하나씩 지정하면 충분합니다.

연속 선을 그리려면 PrimitiveType.LineStrip을 사용해야합니다. 이렇게하면 정점이 선과 자동으로 연결됩니다. 따라서 points - 1 색인이 필요합니다 (실제로 사용하려는 경우).

  1. 당신은 정점의 수는 8라고, 확실히 사실이 아니다 : DrawUserIndexedPrimitives()에 대한 호출에서

    예외의 원인이 일부 잘못된 인수가 있습니다. (points/2) * 2입니다.
  2. 당신은 프리미티브의 개수가 7이라고 말하면 진실도 아닙니다. 이 값은 points - 1이어야합니다.

그러나 다시 정점과 인덱스의 정의가 일관성이 없습니다. 계속하기 전에이 문제를 해결해야합니다.