绘图与多个侧面纹理立方体XNA 4.0 [英] Drawing a textured cube with multiple sides in XNA 4.0

查看:142
本文介绍了绘图与多个侧面纹理立方体XNA 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在敲我的头靠在这个问题小时。我想要做的就是画一个立方体,每边不同的纹理;或者更具体地说,我希望能够指定我每边想要的质感。我这里使用的例子 来入手,然后试图进一步发展它,所以我可以有一个以上的质感。但是,无论我做什么,它仍然只使用应用到效果的最后质地,漠视任何以前的任务。这里是我的造型类:

I have been banging my head against this problem for hours now. What I want to do is draw a cube, with different textures on each side; or more specifically, I want to be able to specify whatever texture I want per side. I used the example here to start with, and then attempted to develop it further, so I could have more than one texture. However, no matter what I do, it still only uses the last texture that is applied to the effect, and pays no heed to any previous assignments. Here is my shape class:

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);

 }



}

}

和在我的游戏Draw方法:

And in the Draw method for my game:

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);   

  }



正如你所看到的,我加载不同的纹理,但结果是这样的:

As you can see, I am loading different textures, yet the result is this:

我相信,纹理都不同,但相同的纹理绘制得到各方。我是否需要为每方一个独立的影响?这肯定似乎有点小题大做。

I am sure that the textures are different, yet the same texture gets drawn on all sides. Do I need a separate effect for each side? That definitely seems like overkill.

推荐答案

在效果设置的任何参数都不会应用,直到调用EffectPass.Apply()。这是因为将更改应用到一个效果是昂贵的,你可能要一气呵成执行多次更改

Any parameters set on the effect aren't applied until you call EffectPass.Apply(). This is because applying changes to an effect is expensive and you might want to perform multiple changes in one go.

您RenderShape功能应该是这个样子:

Your RenderShape function should look something like:

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);
    }
}

这篇关于绘图与多个侧面纹理立方体XNA 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆