Advertisement
Guest User

Piramide com Indices e Buffers

a guest
Oct 10th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. public Poligono(GraphicsDevice graphics, Random random, int nLados, Vector3 posicao)
  2.         {
  3.  
  4.             //O tamanho do array de vertices é o número de lados + 1 (vértice do topo)
  5.             vertexes = new VertexPositionColor[nLados + 1];
  6.  
  7.             //Posição do vértice do topo
  8.             Vector3 topo = new Vector3(posicao.X, posicao.Y + 1, posicao.Z);
  9.  
  10.             //Guarda o ângulo de cada vértice
  11.             float angulo = 0;
  12.             //Diferença de ângulo para cada ponto
  13.             float step = MathHelper.ToRadians(360 / nLados);
  14.  
  15.             //Criar os vértices da base
  16.             for (int i = 0; i < nLados; i++)
  17.             {
  18.                 vertexes[i] = new VertexPositionColor(new Vector3(
  19.                     posicao.X + (float)Math.Cos(angulo),
  20.                     posicao.Y,
  21.                     posicao.Z + (float)-Math.Sin(angulo)),
  22.                     new Color (( byte)random.Next( 0, 255 ), (byte) random.Next(0, 255), (byte)random.Next( 0, 255 )));
  23.                 //Incrementar o ângulo para o próximo ponto
  24.                 angulo += step;
  25.             }
  26.             //Vértice de topo
  27.             vertexes[nLados] = new VertexPositionColor(topo, Color.Black);
  28.  
  29.             //Inicializar o array de índices
  30.             indices = new short[2 * nLados + 1];
  31.  
  32.             //Gerar os índices
  33.             for(int i = 0; i < nLados; i++){
  34.                indices[2 * i] = (short)(i % nLados);
  35.                indices[2 * i + 1] = (short)nLados;
  36.             }
  37.  
  38.             //Enviar os buffers para a memória de GPU
  39.             vertexBuffer = new VertexBuffer(graphics, typeof(VertexPositionColor), vertexes.Length, BufferUsage.WriteOnly);
  40.             vertexBuffer.SetData<VertexPositionColor>(vertexes);
  41.  
  42.             indexBuffer = new IndexBuffer(graphics, typeof(short), indices.Length, BufferUsage.WriteOnly);
  43.             indexBuffer.SetData<short>(indices);
  44.            
  45.         }
  46.  
  47. public void Draw(GraphicsDevice graphics, BasicEffect efeito)
  48.         {
  49.  
  50.             //World, View, Projection
  51.             efeito.World = Camera.World;
  52.             efeito.View = Camera.View;
  53.             efeito.Projection = Camera.Projection;
  54.  
  55.             foreach (EffectPass pass in efeito.CurrentTechnique.Passes)
  56.             {
  57.                 pass.Apply();
  58.  
  59.                 //Definir os buffers a utilizar
  60.                 graphics.SetVertexBuffer(vertexBuffer);
  61.                 graphics.Indices = indexBuffer;
  62.  
  63.                 //Desenhar as primitivas
  64.                 graphics.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, vertexes.Length, 0, indices.Length - 2);
  65.             }
  66.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement