Advertisement
Guest User

BasicShape

a guest
Jan 15th, 2011
1,331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.98 KB | None | 0 0
  1.     public class BasicShape {
  2.  
  3.         public Vector3 shapeSize;
  4.         public Vector3 shapePosition;
  5.         private VertexPositionTexture[][] shapeVertices;
  6.         private int shapeTriangles;
  7.         private VertexBuffer shapeBuffer;
  8.         //public Texture2D topTexture;
  9.         //public Texture2D frontTexture;
  10.         //public Texture2D backTexture;
  11.         //public Texture2D leftTexture;
  12.         //public Texture2D rightTexture;
  13.         //public Texture2D bottomTexture;
  14.         public int topTexture;
  15.         public int frontTexture;
  16.         public int backTexture;
  17.         public int leftTexture;
  18.         public int rightTexture;
  19.         public int bottomTexture;
  20.  
  21.         public BasicShape(Vector3 size, Vector3 position) {
  22.             shapeSize = size;
  23.             shapePosition = position;
  24.  
  25.         }
  26.  
  27.         private void BuildShape() {
  28.             shapeTriangles = 12;
  29.  
  30.             shapeVertices = new VertexPositionTexture[6][];
  31.             for(int i = 0; i < 6; i++) {
  32.                 shapeVertices[i] = new VertexPositionTexture[6];
  33.             }
  34.  
  35.             Vector3 topLeftFront = shapePosition +
  36.                 new Vector3(0.0f, 1.0f, 0.0f) * shapeSize;
  37.             Vector3 bottomLeftFront = shapePosition +
  38.                 new Vector3(0.0f, 0.0f, 0.0f) * shapeSize;
  39.             Vector3 topRightFront = shapePosition +
  40.                 new Vector3(1.0f, 1.0f, 0.0f) * shapeSize;
  41.             Vector3 bottomRightFront = shapePosition +
  42.                 new Vector3(1.0f, 0.0f, 0.0f) * shapeSize;
  43.             Vector3 topLeftBack = shapePosition +
  44.                 new Vector3(0.0f, 1.0f, 1.0f) * shapeSize;
  45.             Vector3 topRightBack = shapePosition +
  46.                 new Vector3(1.0f, 1.0f, 1.0f) * shapeSize;
  47.             Vector3 bottomLeftBack = shapePosition +
  48.                 new Vector3(0.0f, 0.0f, 1.0f) * shapeSize;
  49.             Vector3 bottomRightBack = shapePosition +
  50.                 new Vector3(1.0f, 0.0f, 1.0f) * shapeSize;
  51.  
  52.             Vector3 topLeftFront2 = shapePosition +
  53.                 new Vector3(0.0f, 1.0f, 0.0f) * shapeSize;
  54.             Vector3 bottomLeftFront2 = shapePosition +
  55.                 new Vector3(0.0f, 0.0f, 0.0f) * shapeSize;
  56.             Vector3 topRightFront2 = shapePosition +
  57.                 new Vector3(1.0f, 1.0f, 0.0f) * shapeSize;
  58.             Vector3 bottomRightFront2 = shapePosition +
  59.                 new Vector3(1.0f, 0.0f, 0.0f) * shapeSize;
  60.             Vector3 topLeftBack2 = shapePosition +
  61.                 new Vector3(0.0f, 1.0f, 1.0f) * shapeSize;
  62.             Vector3 topRightBack2 = shapePosition +
  63.                 new Vector3(1.0f, 1.0f, 1.0f) * shapeSize;
  64.             Vector3 bottomLeftBack2 = shapePosition +
  65.                 new Vector3(0.0f, 0.0f, 1.0f) * shapeSize;
  66.             Vector3 bottomRightBack2 = shapePosition +
  67.                 new Vector3(1.0f, 0.0f, 1.0f) * shapeSize;
  68.  
  69.             Vector3 topLeftFront3 = shapePosition +
  70.                 new Vector3(0.0f, 1.0f, 0.0f) * shapeSize;
  71.             Vector3 bottomLeftFront3 = shapePosition +
  72.                 new Vector3(0.0f, 0.0f, 0.0f) * shapeSize;
  73.             Vector3 topRightFront3 = shapePosition +
  74.                 new Vector3(1.0f, 1.0f, 0.0f) * shapeSize;
  75.             Vector3 bottomRightFront3 = shapePosition +
  76.                 new Vector3(1.0f, 0.0f, 0.0f) * shapeSize;
  77.             Vector3 topLeftBack3 = shapePosition +
  78.                 new Vector3(0.0f, 1.0f, 1.0f) * shapeSize;
  79.             Vector3 topRightBack3 = shapePosition +
  80.                 new Vector3(1.0f, 1.0f, 1.0f) * shapeSize;
  81.             Vector3 bottomLeftBack3 = shapePosition +
  82.                 new Vector3(0.0f, 0.0f, 1.0f) * shapeSize;
  83.             Vector3 bottomRightBack3 = shapePosition +
  84.                 new Vector3(1.0f, 0.0f, 1.0f) * shapeSize;
  85.  
  86.             float tileSizeX = 1 / 16.0f;
  87.             float tileSizeY = 1 / 16.0f;
  88.  
  89.             Vector2 textureTopLeft = new Vector2(frontTexture % 16 * tileSizeX, frontTexture / 16 * tileSizeY);
  90.             Vector2 textureTopRight = new Vector2((frontTexture % 16 + 1) * tileSizeX, frontTexture / 16 * tileSizeY);
  91.             Vector2 textureBottomLeft = new Vector2(frontTexture % 16 * tileSizeX, (frontTexture / 16 + 1) * tileSizeY);
  92.             Vector2 textureBottomRight = new Vector2((frontTexture % 16 + 1) * tileSizeX, (frontTexture / 16 + 1) * tileSizeY);
  93.  
  94.             Vector2 textureTopLeft2 = new Vector2(backTexture % 16 * tileSizeX, backTexture / 16 * tileSizeY);
  95.             Vector2 textureTopRight2 = new Vector2((backTexture % 16 + 1) * tileSizeX, backTexture / 16 * tileSizeY);
  96.             Vector2 textureBottomLeft2 = new Vector2(backTexture % 16 * tileSizeX, (backTexture / 16 + 1) * tileSizeY);
  97.             Vector2 textureBottomRight2 = new Vector2((backTexture % 16 + 1) * tileSizeX, (backTexture / 16 + 1) * tileSizeY);
  98.  
  99.             Vector2 textureTopLeft3 = new Vector2(topTexture % 16 * tileSizeX, topTexture / 16 * tileSizeY);
  100.             Vector2 textureTopRight3 = new Vector2((topTexture % 16 + 1) * tileSizeX, topTexture / 16 * tileSizeY);
  101.             Vector2 textureBottomLeft3 = new Vector2(topTexture % 16 * tileSizeX, (topTexture / 16 + 1) * tileSizeY);
  102.             Vector2 textureBottomRight3 = new Vector2((topTexture % 16 + 1) * tileSizeX, (topTexture / 16 + 1) * tileSizeY);
  103.  
  104.             Vector2 textureTopLeft4 = new Vector2(bottomTexture % 16 * tileSizeX, bottomTexture / 16 * tileSizeY);
  105.             Vector2 textureTopRight4 = new Vector2((bottomTexture % 16 + 1) * tileSizeX, bottomTexture / 16 * tileSizeY);
  106.             Vector2 textureBottomLeft4 = new Vector2(bottomTexture % 16 * tileSizeX, (bottomTexture / 16 + 1) * tileSizeY);
  107.             Vector2 textureBottomRight4 = new Vector2((bottomTexture % 16 + 1) * tileSizeX, (bottomTexture / 16 + 1) * tileSizeY);
  108.  
  109.             Vector2 textureTopLeft5 = new Vector2(leftTexture % 16 * tileSizeX, leftTexture / 16 * tileSizeY);
  110.             Vector2 textureTopRight5 = new Vector2((leftTexture % 16 + 1) * tileSizeX, leftTexture / 16 * tileSizeY);
  111.             Vector2 textureBottomLeft5 = new Vector2(leftTexture % 16 * tileSizeX, (leftTexture / 16 + 1) * tileSizeY);
  112.             Vector2 textureBottomRight5 = new Vector2((leftTexture % 16 + 1) * tileSizeX, (leftTexture / 16 + 1) * tileSizeY);
  113.  
  114.             Vector2 textureTopLeft6 = new Vector2(rightTexture % 16 * tileSizeX, rightTexture / 16 * tileSizeY);
  115.             Vector2 textureTopRight6 = new Vector2((rightTexture % 16 + 1) * tileSizeX, rightTexture / 16 * tileSizeY);
  116.             Vector2 textureBottomLeft6 = new Vector2(rightTexture % 16 * tileSizeX, (rightTexture / 16 + 1) * tileSizeY);
  117.             Vector2 textureBottomRight6 = new Vector2((rightTexture % 16 + 1) * tileSizeX, (rightTexture / 16 + 1) * tileSizeY);
  118.  
  119.             // Front face.
  120.             shapeVertices[0][0] = new VertexPositionTexture(
  121.                 topLeftFront,  textureTopLeft);
  122.             shapeVertices[0][1] = new VertexPositionTexture(
  123.                 bottomLeftFront,  textureBottomLeft);
  124.             shapeVertices[0][2] = new VertexPositionTexture(
  125.                 topRightFront, textureTopRight);
  126.             shapeVertices[0][3] = new VertexPositionTexture(
  127.                 bottomLeftFront,  textureBottomLeft);
  128.             shapeVertices[0][4] = new VertexPositionTexture(
  129.                 bottomRightFront,  textureBottomRight);
  130.             shapeVertices[0][5] = new VertexPositionTexture(
  131.                 topRightFront,  textureTopRight);
  132.  
  133.             // Back face.
  134.             shapeVertices[1][0] = new VertexPositionTexture(
  135.                 topLeftBack,  textureTopRight2);
  136.             shapeVertices[1][1] = new VertexPositionTexture(
  137.                 topRightBack,  textureTopLeft2);
  138.             shapeVertices[1][2] = new VertexPositionTexture(
  139.                 bottomLeftBack,  textureBottomRight2);
  140.             shapeVertices[1][3] = new VertexPositionTexture(
  141.                 bottomLeftBack,textureBottomRight2);
  142.             shapeVertices[1][4] = new VertexPositionTexture(
  143.                 topRightBack,  textureTopLeft2);
  144.             shapeVertices[1][5] = new VertexPositionTexture(
  145.                 bottomRightBack,  textureBottomLeft2);
  146.  
  147.             // Top face.
  148.             shapeVertices[2][0] = new VertexPositionTexture(
  149.                 topLeftFront2,  textureBottomLeft3);
  150.             shapeVertices[2][1] = new VertexPositionTexture(
  151.                 topRightBack2,  textureTopRight3);
  152.             shapeVertices[2][2] = new VertexPositionTexture(
  153.                 topLeftBack2,  textureTopLeft3);
  154.             shapeVertices[2][3] = new VertexPositionTexture(
  155.                 topLeftFront2,  textureBottomLeft3);
  156.             shapeVertices[2][4] = new VertexPositionTexture(
  157.                 topRightFront2,  textureBottomRight3);
  158.             shapeVertices[2][5] = new VertexPositionTexture(
  159.                 topRightBack2,  textureTopRight3);
  160.  
  161.             // Bottom face.
  162.             shapeVertices[3][0] = new VertexPositionTexture(
  163.                 bottomLeftFront2,  textureTopLeft4);
  164.             shapeVertices[3][1] = new VertexPositionTexture(
  165.                 bottomLeftBack2,  textureBottomLeft4);
  166.             shapeVertices[3][2] = new VertexPositionTexture(
  167.                 bottomRightBack2,  textureBottomRight4);
  168.             shapeVertices[3][3] = new VertexPositionTexture(
  169.                 bottomLeftFront2,  textureTopLeft4);
  170.             shapeVertices[3][4] = new VertexPositionTexture(
  171.                 bottomRightBack2,  textureBottomRight4);
  172.             shapeVertices[3][5] = new VertexPositionTexture(
  173.                 bottomRightFront2,  textureTopRight4);
  174.  
  175.             // Left face.
  176.             shapeVertices[4][0] = new VertexPositionTexture(
  177.                 topLeftFront3,  textureTopRight5);
  178.             shapeVertices[4][1] = new VertexPositionTexture(
  179.                 bottomLeftBack3,  textureBottomLeft5);
  180.             shapeVertices[4][2] = new VertexPositionTexture(
  181.                 bottomLeftFront3,  textureBottomRight5);
  182.             shapeVertices[4][3] = new VertexPositionTexture(
  183.                 topLeftBack3, textureTopLeft5);
  184.             shapeVertices[4][4] = new VertexPositionTexture(
  185.                 bottomLeftBack3,  textureBottomLeft5);
  186.             shapeVertices[4][5] = new VertexPositionTexture(
  187.                 topLeftFront3,  textureTopRight5);
  188.  
  189.             // Right face.
  190.             shapeVertices[5][0] = new VertexPositionTexture(
  191.                 topRightFront3,  textureTopLeft6);
  192.             shapeVertices[5][1] = new VertexPositionTexture(
  193.                 bottomRightFront3,  textureBottomLeft6);
  194.             shapeVertices[5][2] = new VertexPositionTexture(
  195.                 bottomRightBack3,  textureBottomRight6);
  196.             shapeVertices[5][3] = new VertexPositionTexture(
  197.                 topRightBack3,  textureTopRight6);
  198.             shapeVertices[5][4] = new VertexPositionTexture(
  199.                 topRightFront3,  textureTopLeft6);
  200.             shapeVertices[5][5] = new VertexPositionTexture(
  201.                 bottomRightBack3,  textureBottomRight6);
  202.         }
  203.  
  204.         public void SetTopTexture(int tex) {
  205.             topTexture = tex;
  206.         }
  207.         public void SetSideTexture(int tex) {
  208.             frontTexture = tex;
  209.             backTexture = tex;
  210.             leftTexture = tex;
  211.             rightTexture = tex;
  212.         }
  213.         public void SetBottomTexture(int tex) {
  214.             bottomTexture = tex;
  215.         }
  216.  
  217.         public void RenderShape(GraphicsDevice device, Effect effect) {
  218.             BuildShape();
  219.  
  220.             AddVertices(topTexture, shapeVertices[2]);
  221.             AddVertices(bottomTexture, shapeVertices[3]);
  222.             AddVertices(frontTexture, shapeVertices[0]);
  223.             AddVertices(backTexture, shapeVertices[1]);
  224.             AddVertices(leftTexture, shapeVertices[4]);
  225.             AddVertices(rightTexture, shapeVertices[5]);
  226.  
  227.  
  228.         }
  229.  
  230.         private void AddVertices(int texnum, VertexPositionTexture[] vertices) {
  231.             if(!MinecraftGame.CurrentGame.vertexlist.ContainsKey(texnum)) {
  232.                 MinecraftGame.CurrentGame.vertexlist.Add(texnum, new List<VertexPositionTexture>(1024));
  233.                 MinecraftGame.CurrentGame.indexlist.Add(texnum, new List<int>(1024));
  234.             }
  235.             List<VertexPositionTexture> vertlist = MinecraftGame.CurrentGame.vertexlist[texnum];
  236.             List<int> indexlist = MinecraftGame.CurrentGame.indexlist[texnum];
  237.             //int[] pending = new int[6];
  238.             //for(int i = 0; i < vertices.Length; i++) {
  239.             //    pending[i] = -1;
  240.             //    for(int j = 0; j < vertlist.Count; j++) {
  241.             //        if(vertlist[j].Position == vertices[i].Position && vertlist[j].TextureCoordinate == vertices[i].TextureCoordinate) {
  242.             //            pending[i] = j;
  243.             //            break;
  244.             //        }
  245.             //    }
  246.             //}
  247.  
  248.             for(int i = 0, j = vertlist.Count; i < vertices.Length; i++, j++) {
  249.                 //if(pending[i] == -1) {
  250.                     vertlist.Add(vertices[i]);
  251.                     indexlist.Add(j);
  252.                 //}
  253.                 //else {
  254.                 //    indexlist.Add(pending[i]);
  255.                 //}
  256.             }
  257.         }
  258.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement