2011-01-12 17 views
5

में एकाधिक पक्षों के साथ एक बनावट घन तैयार करना अब मैं इस समस्या के खिलाफ घंटों तक अपने सिर पर टक्कर लगी हूं। मैं जो करना चाहता हूं वह प्रत्येक तरफ विभिन्न बनावट के साथ एक घन खींचता है; या अधिक विशेष रूप से, मैं जो भी बनावट चाहता हूं उसे निर्दिष्ट करने में सक्षम होना चाहता हूं। मैंने उदाहरण के लिए here उदाहरण का उपयोग किया, और फिर इसे और विकसित करने का प्रयास किया, इसलिए मेरे पास एक से अधिक बनावट हो सकती थी। हालांकि, कोई फर्क नहीं पड़ता कि मैं क्या करता हूं, यह अभी भी केवल अंतिम बनावट का उपयोग करता है जो प्रभाव पर लागू होता है, और किसी भी पिछले असाइनमेंट पर ध्यान नहीं देता है।XNA 4.0

public class BasicShape { 

public Vector3 shapeSize; 
public Vector3 shapePosition; 
private VertexPositionNormalTexture[][] shapeVertices; 
private int shapeTriangles; 
private VertexBuffer shapeBuffer; 
public Texture2D topTexture; 
public Texture2D frontTexture; 
public Texture2D backTexture; 
public Texture2D leftTexture; 
public Texture2D rightTexture; 
public Texture2D bottomTexture; 

public BasicShape(Vector3 size, Vector3 position) { 
    shapeSize = size; 
    shapePosition = position; 
} 

private void BuildShape() { 
    shapeTriangles = 12; 

    shapeVertices = new VertexPositionNormalTexture[6][]; 
    for(int i = 0; i < 6; i++) { 
     shapeVertices[i] = new VertexPositionNormalTexture[6]; 
    } 

    Vector3 topLeftFront = shapePosition + 
    new Vector3(0.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomLeftFront = shapePosition + 
    new Vector3(0.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topRightFront = shapePosition + 
    new Vector3(1.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomRightFront = shapePosition + 
    new Vector3(1.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topLeftBack = shapePosition + 
    new Vector3(0.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 topRightBack = shapePosition + 
    new Vector3(1.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 bottomLeftBack = shapePosition + 
    new Vector3(0.0f, 0.0f, 1.0f) * shapeSize; 
    Vector3 bottomRightBack = shapePosition + 
    new Vector3(1.0f, 0.0f, 1.0f) * shapeSize; 

    Vector3 topLeftFront2 = shapePosition + 
    new Vector3(0.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomLeftFront2 = shapePosition + 
    new Vector3(0.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topRightFront2 = shapePosition + 
    new Vector3(1.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomRightFront2 = shapePosition + 
    new Vector3(1.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topLeftBack2 = shapePosition + 
    new Vector3(0.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 topRightBack2 = shapePosition + 
    new Vector3(1.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 bottomLeftBack2 = shapePosition + 
    new Vector3(0.0f, 0.0f, 1.0f) * shapeSize; 
    Vector3 bottomRightBack2 = shapePosition + 
    new Vector3(1.0f, 0.0f, 1.0f) * shapeSize; 

    Vector3 topLeftFront3 = shapePosition + 
    new Vector3(0.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomLeftFront3 = shapePosition + 
    new Vector3(0.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topRightFront3 = shapePosition + 
    new Vector3(1.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomRightFront3 = shapePosition + 
    new Vector3(1.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 topLeftBack3 = shapePosition + 
    new Vector3(0.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 topRightBack3 = shapePosition + 
    new Vector3(1.0f, 1.0f, 1.0f) * shapeSize; 
    Vector3 bottomLeftBack3 = shapePosition + 
    new Vector3(0.0f, 0.0f, 1.0f) * shapeSize; 
    Vector3 bottomRightBack3 = shapePosition + 
    new Vector3(1.0f, 0.0f, 1.0f) * shapeSize; 

    Vector3 frontNormal = new Vector3(0.0f, 0.0f, 1.0f) * shapeSize; 
    Vector3 backNormal = new Vector3(0.0f, 0.0f, -1.0f) * shapeSize; 
    Vector3 topNormal = new Vector3(0.0f, 1.0f, 0.0f) * shapeSize; 
    Vector3 bottomNormal = new Vector3(0.0f, -1.0f, 0.0f) * shapeSize; 
    Vector3 leftNormal = new Vector3(-1.0f, 0.0f, 0.0f) * shapeSize; 
    Vector3 rightNormal = new Vector3(1.0f, 0.0f, 0.0f) * shapeSize; 

    Vector2 textureTopLeft = new Vector2(1f * shapeSize.X, 0.0f * shapeSize.Y); 
    Vector2 textureTopRight = new Vector2(0.0f * shapeSize.X, 0.0f * shapeSize.Y); 
    Vector2 textureBottomLeft = new Vector2(1f * shapeSize.X, 1f * shapeSize.Y); 
    Vector2 textureBottomRight = new Vector2(0.0f * shapeSize.X, 1f * shapeSize.Y); 

    // Front face. 
    shapeVertices[0][0] = new VertexPositionNormalTexture(
    topLeftFront, frontNormal, textureTopLeft); 
    shapeVertices[0][1] = new VertexPositionNormalTexture(
    bottomLeftFront, frontNormal, textureBottomLeft); 
    shapeVertices[0][2] = new VertexPositionNormalTexture(
    topRightFront, frontNormal, textureTopRight); 
    shapeVertices[0][3] = new VertexPositionNormalTexture(
    bottomLeftFront, frontNormal, textureBottomLeft); 
    shapeVertices[0][4] = new VertexPositionNormalTexture(
    bottomRightFront, frontNormal, textureBottomRight); 
    shapeVertices[0][5] = new VertexPositionNormalTexture(
    topRightFront, frontNormal, textureTopRight); 

    // Back face. 
    shapeVertices[1][0] = new VertexPositionNormalTexture(
    topLeftBack, backNormal, textureTopRight); 
    shapeVertices[1][1] = new VertexPositionNormalTexture(
    topRightBack, backNormal, textureTopLeft); 
    shapeVertices[1][2] = new VertexPositionNormalTexture(
    bottomLeftBack, backNormal, textureBottomRight); 
    shapeVertices[1][3] = new VertexPositionNormalTexture(
    bottomLeftBack, backNormal, textureBottomRight); 
    shapeVertices[1][4] = new VertexPositionNormalTexture(
    topRightBack, backNormal, textureTopLeft); 
    shapeVertices[1][5] = new VertexPositionNormalTexture(
    bottomRightBack, backNormal, textureBottomLeft); 

    // Top face. 
    shapeVertices[2][0] = new VertexPositionNormalTexture(
    topLeftFront2, topNormal, textureBottomLeft); 
    shapeVertices[2][1] = new VertexPositionNormalTexture(
    topRightBack2, topNormal, textureTopRight); 
    shapeVertices[2][2] = new VertexPositionNormalTexture(
    topLeftBack2, topNormal, textureTopLeft); 
    shapeVertices[2][3] = new VertexPositionNormalTexture(
    topLeftFront2, topNormal, textureBottomLeft); 
    shapeVertices[2][4] = new VertexPositionNormalTexture(
    topRightFront2, topNormal, textureBottomRight); 
    shapeVertices[2][5] = new VertexPositionNormalTexture(
    topRightBack2, topNormal, textureTopRight); 

    // Bottom face. 
    shapeVertices[3][0] = new VertexPositionNormalTexture(
    bottomLeftFront2, bottomNormal, textureTopLeft); 
    shapeVertices[3][1] = new VertexPositionNormalTexture(
    bottomLeftBack2, bottomNormal, textureBottomLeft); 
    shapeVertices[3][2] = new VertexPositionNormalTexture(
    bottomRightBack2, bottomNormal, textureBottomRight); 
    shapeVertices[3][3] = new VertexPositionNormalTexture(
    bottomLeftFront2, bottomNormal, textureTopLeft); 
    shapeVertices[3][4] = new VertexPositionNormalTexture(
    bottomRightBack2, bottomNormal, textureBottomRight); 
    shapeVertices[3][5] = new VertexPositionNormalTexture(
    bottomRightFront2, bottomNormal, textureTopRight); 

    // Left face. 
    shapeVertices[4][0] = new VertexPositionNormalTexture(
    topLeftFront3, leftNormal, textureTopRight); 
    shapeVertices[4][1] = new VertexPositionNormalTexture(
    bottomLeftBack3, leftNormal, textureBottomLeft); 
    shapeVertices[4][2] = new VertexPositionNormalTexture(
    bottomLeftFront3, leftNormal, textureBottomRight); 
    shapeVertices[4][3] = new VertexPositionNormalTexture(
    topLeftBack3, leftNormal, textureTopLeft); 
    shapeVertices[4][4] = new VertexPositionNormalTexture(
    bottomLeftBack3, leftNormal, textureBottomLeft); 
    shapeVertices[4][5] = new VertexPositionNormalTexture(
    topLeftFront3, leftNormal, textureTopRight); 

    // Right face. 
    shapeVertices[5][0] = new VertexPositionNormalTexture(
    topRightFront3, rightNormal, textureTopLeft); 
    shapeVertices[5][1] = new VertexPositionNormalTexture(
    bottomRightFront3, rightNormal, textureBottomLeft); 
    shapeVertices[5][2] = new VertexPositionNormalTexture(
    bottomRightBack3, rightNormal, textureBottomRight); 
    shapeVertices[5][3] = new VertexPositionNormalTexture(
    topRightBack3, rightNormal, textureTopRight); 
    shapeVertices[5][4] = new VertexPositionNormalTexture(
    topRightFront3, rightNormal, textureTopLeft); 
    shapeVertices[5][5] = new VertexPositionNormalTexture(
    bottomRightBack3, rightNormal, textureBottomRight); 
} 

public void SetTopTexture(Texture2D tex) { 
    topTexture = tex; 
} 
public void SetSideTexture(Texture2D tex) { 
    frontTexture = tex; 
    backTexture = tex; 
    leftTexture = tex; 
    rightTexture = tex; 
} 
public void SetBottomTexture(Texture2D tex) { 
    bottomTexture = tex; 
} 

public void RenderShape(GraphicsDevice device, Effect effect) { 
    BuildShape(); 


    effect.Parameters["xTexture"].SetValue(topTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[2], 0, 2); 
    effect.Parameters["xTexture"].SetValue(bottomTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[3], 0, 2); 
    effect.Parameters["xTexture"].SetValue(frontTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[0], 0, 2); 
    effect.Parameters["xTexture"].SetValue(backTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[1], 0, 2); 
    effect.Parameters["xTexture"].SetValue(leftTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[4], 0, 2); 
    effect.Parameters["xTexture"].SetValue(rightTexture); 
    device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[5], 0, 2); 

} 

}

और मेरे खेल के लिए ड्रा विधि में:

cubeEffect.CurrentTechnique = cubeEffect.Techniques["Textured"]; 
    foreach(EffectPass pass in cubeEffect.CurrentTechnique.Passes) { 
     pass.Apply(); 
     BasicShape s = new BasicShape(new Vector3(1, 1, 1), new Vector3(0, 0, 3)); 
     s.SetTopTexture(TextureLoader.GetTexture(4)); 
     s.SetSideTexture(TextureLoader.GetTexture(35)); 
     s.SetBottomTexture(TextureLoader.GetTexture(4)); 
     s.RenderShape(GraphicsDevice, cubeEffect); 

    } 

आप देख सकते हैं, मैं अलग बनावट लोड हो रहा हूँ, फिर भी परिणाम यह है यहाँ मेरी आकार वर्ग है :

my cube http://www.tinyimg.org/images/769MinecraftClassic_2011_.bmp

मुझे यकीन है कि बनावट अलग हैं रहा हूँ, फिर भी वही बनावट सभी तरफ खींची जाती है। क्या मुझे प्रत्येक तरफ एक अलग प्रभाव की ज़रूरत है? यह निश्चित रूप से ओवरकिल की तरह लगता है।

+0

आपको लगता है कि जांच लिया है 'TextureLoader.GetTexture (35)' लौटा रहा है कि तुम क्या इसे वापस करने की उम्मीद:

आपका RenderShape समारोह की तरह कुछ देखना चाहिए? – ChrisF

+0

@ChrisF - ठीक है, बनावट 35 एकमात्र बनावट दिखा रही है, इसलिए मुझे लगता है कि यह है। इसके अलावा, अगर मैं रेंडरशैप में ड्रॉ कोड की पहली दो पंक्तियां लेता हूं और इसे अंत में डालता हूं, तो यह बनावट 4 प्रस्तुत करता है। इसलिए यह केवल अंतिम लागू बनावट को प्रस्तुत करता है। – Bevin

+0

यह उपयोगी जानकारी की तरह लगता है कि आपको अपने प्रश्न में शामिल करना चाहिए। मैं जवाब के साथ मदद नहीं कर सकता - मैं xna से परिचित नहीं हूँ। – ChrisF

उत्तर

4

प्रभाव पर सेट किए गए किसी भी पैरामीटर तब तक लागू नहीं होते जब तक आप EffectPass.Apply() को कॉल नहीं करते। ऐसा इसलिए है क्योंकि प्रभाव में परिवर्तन लागू करना महंगा है और आप एक बार में कई बदलाव करना चाहेंगे।

public void RenderShape(GraphicsDevice device, Effect effect) 
{ 
    BuildShape(); 

    foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
    { 
     effect.Parameters["xTexture"].SetValue(topTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[2], 0, 2); 

     effect.Parameters["xTexture"].SetValue(bottomTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[3], 0, 2); 

     effect.Parameters["xTexture"].SetValue(frontTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[0], 0, 2); 

     effect.Parameters["xTexture"].SetValue(backTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[1], 0, 2); 

     effect.Parameters["xTexture"].SetValue(leftTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[4], 0, 2); 

     effect.Parameters["xTexture"].SetValue(rightTexture); 
     pass.Apply(); 
     device.DrawUserPrimitives(PrimitiveType.TriangleList, shapeVertices[5], 0, 2); 
    } 
} 
+0

@Empyrean - तो, ​​क्या आप कह रहे हैं कि यह करने का यह अनुशंसित तरीका नहीं है? एक तेज़ तरीका क्या होगा? मुझे इन क्यूबों में से बहुत सारे आकर्षित करना होगा, इसलिए मुझे जितना संभव हो उतना अनुकूलित करना होगा। – Bevin

+0

आप प्रदर्शन समस्याओं में भाग लेने से पहले आमतौर पर केवल कुछ सौ राज्य परिवर्तन (बैचों कहा जाता है) बना सकते हैं। आपके कोड में, प्रत्येक घन के प्रतिपादन में 6 बैचों होते हैं। इसका मतलब है कि आप अपने framerate अस्वीकार्य होने से पहले 100 से कम cubes प्रस्तुत करने में सक्षम हो जाएगा। आप जितना संभव हो उतना ड्रॉ कॉल चाहते हैं, इसलिए एक ड्रॉ कॉल में एक ही बनावट के साथ सभी घन चेहरे खींचे। बेहतर अभी तक अपने छः बनावट को एक बनावट (बनावट बनावट) में मिलाएं। DrawUserPrimitive कॉल की तुलना में पीसी पर भी वर्टेक्स बफर बहुत तेज हैं। – Empyrean

+0

सबसे तेज़ तरीका यह है कि आपके क्यूब्स हिलते हैं या नहीं, इस पर निर्भर करते हुए स्थिर या गतिशील वर्टेक्स बफर पर या तो एक कॉल में सभी क्यूब्स को एटलाज़िंग और रेंडर करना होगा। यदि उनमें से कुछ स्थानांतरित नहीं होते हैं, तो उन लोगों के लिए एक स्थिर वर्टेक्स बफर का उपयोग करें और जो गतिशील वर्टेक्स बफर करते हैं। डुप्लिकेट शिखर का उपयोग करने से बचने के लिए आपको इंडेक्स का भी उपयोग करना चाहिए। डुप्लिकेट शिखर दोनों को बदलना होगा और वे आपके मेष में थोड़ी अलग तरीके से सीम दे सकते हैं। – Empyrean