Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Pyramid
- {
- class Prism
- {
- #region Variables
- /// <summary>
- /// Number of prism faces
- /// </summary>
- private int prismSides;
- /// <summary>
- /// Height of the prism
- /// </summary>
- private float prismHeight;
- /// <summary>
- /// Diameter of the prism
- /// </summary>
- private float prismRadius;
- /// <summary>
- /// Placeholder for the texture on the sides
- /// </summary>
- private Texture2D prismSideTexture;
- /// <summary>
- /// prism BasicEffect
- /// </summary>
- private BasicEffect effect;
- /// <summary>
- /// Bottom face vertices
- /// </summary>
- public VertexPositionTexture[] BotFace { get; set; }
- /// <summary>
- /// Top face vertices
- /// </summary>
- public VertexPositionTexture[] TopFace { get; set; }
- /// <summary>
- /// Sides vertices
- /// </summary>
- public VertexPositionTexture[] Sides { get; set; }
- /// <summary>
- /// Stores the top and bot base coords
- /// </summary>
- private List<Vector2> baseCoords = new List<Vector2>();
- /// <summary>
- /// IndexBuffer index for base and top
- /// </summary>
- public short[] TopBotIndex;
- /// <summary>
- /// IndexBuffer index for sides
- /// </summary>
- public short[] SideIndex;
- /// <summary>
- /// Bottom vertex buffer
- /// </summary>
- private VertexBuffer botVertexBuffer;
- /// <summary>
- /// Top Vertex buffer
- /// </summary>
- private VertexBuffer topVertexBuffer;
- /// <summary>
- /// Side Vertex Buffer
- /// </summary>
- private VertexBuffer sideVertexBuffer;
- /// <summary>
- /// Top/bottom index Buffer
- /// </summary>
- private IndexBuffer topBotBuffer;
- /// <summary>
- /// Sides index Buffer
- /// </summary>
- private IndexBuffer sidesBuffer;
- /// <summary>
- /// The World Matrix
- /// </summary>
- private Matrix worldMatrix;
- #endregion
- /// <summary>
- /// Initializes the Prism class
- /// </summary>
- /// <param name="nSides">Number prism faces, min 3</param>
- /// <param name="height">Height of the prism, min 1</param>
- /// <param name="radius">Diameter of the prism, min 1</param>
- /// <param name="sideTexture">Texture of the prism faces</param>
- /// <param name="topBotTexture">Textue of the top and bottom sides</param>
- public Prism(GraphicsDevice device, int nSides, float height, float radius, Texture2D sideTexture)
- {
- // Does a simple side number check
- if (nSides < 3)
- prismSides = 3;
- else
- prismSides = nSides;
- // Does a simple height check
- if (height < 1f)
- prismHeight = 1f;
- else
- prismHeight = height;
- //Does a simple diameter check
- if (radius < 1f)
- prismRadius = 1f;
- else
- prismRadius = radius;
- // initializes the vertices list
- if (BotFace == null)
- BotFace = new VertexPositionTexture[prismSides + 1];
- if (TopFace == null)
- TopFace = new VertexPositionTexture[prismSides + 1];
- if (Sides == null)
- Sides = new VertexPositionTexture[2 * prismSides + 2]; // 4 vertices for each side + 2 more to close it
- if (SideIndex == null)
- SideIndex = new short[2 * prismSides + 2];
- if(worldMatrix == null)
- worldMatrix = Matrix.Identity;
- // initializes the indexbuffer
- if (TopBotIndex == null)
- TopBotIndex = new short[prismSides * 2 + 2];
- prismSideTexture = sideTexture;
- PrismEffect(device);
- BuildTopBot();
- BuildSides();
- CreateBuffers(device);
- }
- /// <summary>
- /// Builds top and bottom layer
- /// </summary>
- private void BuildTopBot()
- {
- baseCoords = Helper.GetCirclePositions(prismRadius, prismSides);
- // stores the data into 2 arrays
- //sets the middle vertice
- TopFace[0] = new VertexPositionTexture(new Vector3(0f, prismHeight, 0f), new Vector2(0.0f, 0.0f));
- BotFace[0] = new VertexPositionTexture(new Vector3(0f, 0f, 0f), new Vector2(0.0f, 0.0f));
- for (int i = 0; i < baseCoords.Count; i++)
- {
- TopFace[i + 1] = new VertexPositionTexture(new Vector3(baseCoords[i].X, prismHeight, baseCoords[i].Y), new Vector2(0.0f, 0.0f));
- BotFace[i + 1] = new VertexPositionTexture(new Vector3(baseCoords[i].X, 0f, baseCoords[i].Y), new Vector2(0.0f, 0.0f));
- }
- // Builds the indexbuffer
- for (int i = 0; i < prismSides + 1; i++)
- {
- if (i < prismSides)
- TopBotIndex[2 * i] = (short)(i + 1);
- else
- TopBotIndex[2 * i] = 1;
- TopBotIndex[2 * i + 1] = 0;
- }
- }
- /// <summary>
- /// Builds the sides of the prism
- /// </summary>
- private void BuildSides()
- {
- float x;
- for (int i = 0; i < prismSides + 1; i++)
- {
- x = (float)i / (float)prismSides; // gets texutre absiss
- if (i < prismSides)
- {
- Sides[2 * i + 1] = new VertexPositionTexture(new Vector3(baseCoords[i].X, prismHeight, baseCoords[i].Y), new Vector2(x, 0.0f));
- Sides[2 * i] = new VertexPositionTexture(new Vector3(baseCoords[i].X, 0.0f, baseCoords[i].Y), new Vector2(x, 1.0f));
- }
- else // closes the prism with the 2 last vertices
- {
- Sides[2 * i + 1] = new VertexPositionTexture(new Vector3(baseCoords[0].X, prismHeight, baseCoords[0].Y), new Vector2(x, 0.0f));
- Sides[2 * i] = new VertexPositionTexture(new Vector3(baseCoords[0].X, 0.0f, baseCoords[0].Y), new Vector2(x, 1.0f));
- }
- }
- // draws the vertices by the creation order
- for (int i = 0; i < 2 * prismSides + 2; i++)
- SideIndex[i] = (short)i;
- }
- /// <summary>
- /// Handles the vertex and index buffers creation
- /// </summary>
- /// <param name="device"></param>
- private void CreateBuffers(GraphicsDevice device)
- {
- botVertexBuffer = new VertexBuffer(device, typeof(VertexPositionTexture), BotFace.Length, BufferUsage.None);
- botVertexBuffer.SetData<VertexPositionTexture>(BotFace);
- topVertexBuffer = new VertexBuffer(device, typeof(VertexPositionTexture), TopFace.Length, BufferUsage.None);
- topVertexBuffer.SetData<VertexPositionTexture>(TopFace);
- topBotBuffer = new IndexBuffer(device, typeof(short), TopBotIndex.Length, BufferUsage.None);
- topBotBuffer.SetData<short>(TopBotIndex);
- sideVertexBuffer = new VertexBuffer(device, typeof(VertexPositionTexture), SideIndex.Length, BufferUsage.None);
- sideVertexBuffer.SetData<VertexPositionTexture>(Sides);
- sidesBuffer = new IndexBuffer(device, typeof(short), SideIndex.Length, BufferUsage.None);
- sidesBuffer.SetData<short>(SideIndex);
- }
- //TODO : Put all the drawing and moving logic to the Draw Triangles class
- /// <summary>
- /// Prism effect
- /// </summary>
- /// <param name="device"></param>
- public void PrismEffect(GraphicsDevice device)
- {
- effect = new BasicEffect(device);
- float aspectRatio = (float)device.Viewport.Width / device.Viewport.Height;
- effect.View = Matrix.CreateLookAt(new Vector3(0f, 4f, 0f), Vector3.Zero, Vector3.Up);
- effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10.0f);
- effect.LightingEnabled = false;
- effect.TextureEnabled = true;
- effect.Texture = prismSideTexture;
- }
- public void Draw(GraphicsDevice device)
- {
- //World Matrix
- effect.World = worldMatrix;
- effect.CurrentTechnique.Passes[0].Apply();
- //Prism top face render
- device.Indices = topBotBuffer;
- device.SetVertexBuffer(botVertexBuffer);
- device.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, TopFace, 0, prismSides + 1, TopBotIndex, 0, prismSides * 2);
- //Prism bottom face render
- device.Indices = topBotBuffer;
- device.SetVertexBuffer(botVertexBuffer);
- device.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, BotFace, 0, prismSides + 1, TopBotIndex, 0, prismSides * 2);
- //Prism sides render
- device.Indices = sidesBuffer;
- device.SetVertexBuffer(sideVertexBuffer);
- device.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleStrip, Sides, 0, Sides.Length, SideIndex, 0, prismSides * 2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement