XNA

2011-08-30 11 views
6

में 2 डी बहुभुज बनाना मेरे पास कुछ समस्या है। मैं XNA के लिए नया हूँ और एक बहुभुज आकार कि कुछ इस तरह दिखता आकर्षित करने के लिए चाहते हैं (अंत में, मैं इन बिंदु यादृच्छिक होना चाहते हैं):XNA

Polygon within a rectangle

तो मैं कुछ लेख पढ़ सकते हैं और यह है कि क्या जब मैं Render फोन यह कुछ वर्गों ड्राइंग के रूप में अगर यह एक पाश में, जहां शुरू होता है है

private VertexPositionColor[] vertices; 

public TextureClass() 
{ 
    setupVertices(); 
} 

public override void Render(SpriteBatch spriteBatch) 
{ 
    Texture2D texture = createTexture(spriteBatch); 
    spriteBatch.Draw(texture, new Rectangle((int)vertices[0].Position.X, (int)vertices[0].Position.Y, 30, 30), Color.Brown); 
} 

private Texture2D createTexture(SpriteBatch spriteBatch) 
{ 
    Texture2D texture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color); 
    texture.SetData<Color>(new Color[] { Color.Brown }); 
    return texture; 
} 

: मैं के साथ समाप्त हो गया। मैं बस अनुमान लगा रहा हूं कि मैं इसे सब गलत कर रहा हूं। अगर कोई मुझे सही दिशा में इंगित करता है तो मैं इसे प्यार करूंगा। बस एक बहुभुज बनाना और इसे चित्रित करना। यह बहुत आसान लग रहा था ...

+0

क्या आप उन्हें पहली बार सेट करने के बाद शिखर बदलते हैं? – Cameron

+0

नहीं, मैं केवल –

+0

पर रेंडर कहता हूं आप कहां से रेंडर कह रहे हैं? –

उत्तर

3

यहाँ यह क्या मैं अभी कर सकते है।

एक कक्षा जो कुछ वांछित असाइनमेंट के साथ मूलभूत प्रभाव उत्पन्न करती है।

public class StandardBasicEffect : BasicEffect 
{ 
    public StandardBasicEffect(GraphicsDevice graphicsDevice) 
     : base(graphicsDevice) 
    { 
     this.VertexColorEnabled = true; 
     this.Projection = Matrix.CreateOrthographicOffCenter(
      0, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0, 0, 1); 
    } 

    public StandardBasicEffect(BasicEffect effect) 
     : base(effect) { } 

    public BasicEffect Clone() 
    { 
     return new StandardBasicEffect(this); 
    } 
} 

यहाँ मेरी PolygonShape वर्ग

/// <summary> 
/// A Polygon object that you will be able to draw. 
/// Animations are being implemented as we speak. 
/// </summary> 
/// <param name="graphicsDevice">The graphicsdevice from a Game object</param> 
/// <param name="vertices">The vertices in a clockwise order</param> 
public PolygonShape(GraphicsDevice graphicsDevice, VertexPositionColor[] vertices) 
{ 
    this.graphicsDevice = graphicsDevice; 
    this.vertices = vertices; 
    this.triangulated = false; 

    triangulatedVertices = new VertexPositionColor[vertices.Length * 3]; 
    indexes = new int[vertices.Length]; 
} 

/// <summary> 
/// Triangulate the set of VertexPositionColors so it will be drawn correcrly   
/// </summary> 
/// <returns>The triangulated vertices array</returns>} 
public VertexPositionColor[] Triangulate() 
{ 
    calculateCenterPoint();{ 
    setupIndexes(); 
    for (int i = 0; i < indexes.Length; i++) 
    { 
     setupDrawableTriangle(indexes[i]); 
    } 

    triangulated = true; 
    return triangulatedVertices; 
} 

/// <summary> 
/// Calculate the center point needed for triangulation. 
/// The polygon will be irregular, so this isn't the actual center of the polygon 
/// but it will do for now, as we only need an extra point to make the triangles with</summary> 
private void calculateCenterPoint() 
{ 
    float xCount = 0, yCount = 0; 

    foreach (VertexPositionColor vertice in vertices) 
    { 
     xCount += vertice.Position.X; 
     yCount += vertice.Position.Y; 
    } 

    centerPoint = new Vector3(xCount/vertices.Length, yCount/vertices.Length, 0); 
} 

private void setupIndexes() 
{ 
    for (int i = 1; i < triangulatedVertices.Length; i = i + 3) 
    { 
     indexes[i/3] = i - 1; 
    } 
} 

private void setupDrawableTriangle(int index) 
{ 
    triangulatedVertices[index] = vertices[index/3]; //No DividedByZeroException?... 
    if (index/3 != vertices.Length - 1) 
     triangulatedVertices[index + 1] = vertices[(index/3) + 1]; 
    else 
     triangulatedVertices[index + 1] = vertices[0]; 
    triangulatedVertices[index + 2].Position = centerPoint; 
} 

/// <summary> 
/// Draw the polygon. If you haven't called Triangulate yet, I wil do it for you. 
/// </summary> 
/// <param name="effect">The BasicEffect needed for drawing</param> 
public void Draw(BasicEffect effect) 
{ 
    try 
    { 
     if (!triangulated) 
      Triangulate(); 

     draw(effect); 
    } 
    catch (Exception exception) 
    { 
     throw exception; 
    } 
} 

private void draw(BasicEffect effect) 
{ 
    effect.CurrentTechnique.Passes[0].Apply(); 
    graphicsDevice.DrawUserPrimitives<VertexPositionColor>(
     PrimitiveType.TriangleList, triangulatedVertices, 0, vertices.Length); 
} 

क्षमा करें यह एक तरह से बहुत कुछ है,। अब मेरी अगली खोज के लिए। एनीमेशन मेरे बहुभुज।

आशा है कि इससे एक ही समस्या वाले साथी लोगों की मदद मिलेगी।

1

मैं एक भौतिकी सिमुलेशन, जहां मैं (आप अधिक काम उदाहरण के लिए इस कार्य के लिए गूगल या MSDN चाहिए।)

GraphicsDevice.DrawIndexedPrimitives साथ बक्से बाउंडिंग आकर्षित करने के लिए किया था पर अतीत में XNA के साथ काम किया

नीचे दिए गए कोड का उपयोग मैंने अपनी परियोजना में 3 डी ज्यामिति बनाने के लिए किया था।

/// <summary> 
/// Draw the primitive. 
/// </summary> 
/// <param name="world">World Matrix</param> 
/// <param name="view">View Matrix</param> 
/// <param name="projection">Projection Matrix</param> 
/// <param name="color">Color of the primitive</param> 
public void Draw(Matrix world, Matrix view, Matrix projection, Color color) 
{ 
    _mGraphicsDevice.VertexDeclaration = _mVertexDeclaration; 
    _mGraphicsDevice.Vertices[0].SetSource(_mVertexBuffer, 0, VertexPositionNormal.SizeInBytes); 
    _mGraphicsDevice.Indices = _mIndexBuffer; 

    _mBasicEffect.DiffuseColor = color.ToVector3(); 
    _mBasicEffect.World = _mTransform * world; 
    _mBasicEffect.View = view; 
    _mBasicEffect.Projection = projection; 

    int primitiveCount = _mIndex.Count/3; 

    _mBasicEffect.Begin(); 
    foreach (EffectPass pass in _mBasicEffect.CurrentTechnique.Passes) 
    { 
     pass.Begin(); 
     _mGraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _mVertex.Count, 0, primitiveCount); 
     pass.End(); 
    } 
    _mBasicEffect.End(); 
} 

यह फ़ंक्शन एक ज्यामिति वस्तु (वर्ग) के एक सदस्य तरीका है और खेल वर्ग 'Draw(GameTime) विधि

+0

क्या आप मुझे दिखा सकते हैं कि _mVertexDeclaration, _mIndexBuffer और _mVertexBuffer ऑब्जेक्ट कैसा दिखते हैं? –

+0

वे बिल्कुल वैसा ही दिखते हैं जो वे हैं। GraphicsDevice.VertexDeclaration, GraphicsDevice.Indices और Microsoft.Xna.Framework.Graphics.VertexBuffer क्रमशः API के लिए API देखें। – Jake

2

इस कोड को 2 डी लाइनों आकर्षित करने के लिए उपयोगी है, कुछ calcs एक में किया जा सकता है से कहा जाता है initilization कॉल, लेकिन मैं सभी को एक साथ रखने के लिए इस उदाहरण के लिए पसंद करते हैं।

public void DrawLine(VertexPositionColor[] Vertices) 
    {   
     Game.GraphicsDevice.DepthStencilState = DepthStencilState.Default; 

     Vector2 center; 
     center.X = Game.GraphicsDevice.Viewport.Width * 0.5f; 
     center.Y = Game.GraphicsDevice.Viewport.Height * 0.5f; 

     Matrix View = Matrix.CreateLookAt(new Vector3(center, 0), new Vector3(center, 1), new Vector3(0, -1, 0)); 
     Matrix Projection = Matrix.CreateOrthographic(center.X * 2, center.Y * 2, -0.5f, 1); 
     Effect EffectLines = Game.Content.Load<Effect>("lines"); 
     EffectLines.CurrentTechnique = EffectLines.Techniques["Lines"]; 

     EffectLines.Parameters["xViewProjection"].SetValue(View * Projection); 
     EffectLines.Parameters["xWorld"].SetValue(Matrix.Identity); 

     foreach (EffectPass pass in EffectLines.CurrentTechnique.Passes) 
     { 
      pass.Apply(); 
      Game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor> 
       (PrimitiveType.LineList, Vertices, 0, Vertices.Length/2); 
     }    
    } 

LINES.FX

uniform float4x4 xWorld; 
uniform float4x4 xViewProjection; 

void VS_Basico(in float4 inPos : POSITION, in float4 inColor: COLOR0, out float4  outPos: POSITION, out float4 outColor:COLOR0) 
{ 
    float4 tmp = mul (inPos, xWorld); 
    outPos = mul (tmp, xViewProjection); 
    outColor = inColor; 
} 

float4 PS_Basico(in float4 inColor:COLOR) :COLOR 
{ 
return inColor; 
} 


technique Lines 
{ 
pass Pass0 
    { 
     VertexShader = compile vs_2_0 VS_Basico(); 
     PixelShader = compile ps_2_0 PS_Basico(); 
     FILLMODE = SOLID; 
     CULLMODE = NONE;   
    } 
} 
+0

मैंने आपके समाधान की कोशिश की लेकिन मुझे यह त्रुटि मिल रही है: किसी भी ड्रॉ ऑपरेशन किए जाने से पहले एक वर्टेक्स शेडर और पिक्सेल शेडर दोनों डिवाइस पर सेट होना चाहिए। –

+0

मैंने लाइन.एफएक्स में पिक्सेल शेडर जोड़ा है – Blau

संबंधित मुद्दे