Advertisement
Guest User

Quad.cs

a guest
Oct 18th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using StarsGame.Universe_Objects.Planets;
  8.  
  9. namespace StarsGame
  10. {
  11.     public class Quad
  12.     {
  13.         public PlanetGeometry.FACING_DIRECTION FaceName;
  14.  
  15.         private Triangle[] triangles;
  16.         public Triangle[] Triangles
  17.         {
  18.             get { return triangles; }
  19.         }
  20.  
  21.         private VertexPositionNormalTexture[] corners;
  22.         public VertexPositionNormalTexture[] Corners
  23.         {
  24.             get { return corners; }
  25.             set { corners = value; }
  26.         }
  27.  
  28.         private VertexPositionNormalColor[] colorCorners;
  29.         public VertexPositionNormalColor[] ColorCorners
  30.         {
  31.             get { return colorCorners; }
  32.             set { colorCorners = value; }
  33.         }
  34.  
  35.         public Quad(VertexPositionNormalTexture[] corners, PlanetGeometry.FACING_DIRECTION faceName)
  36.         {
  37.             this.corners = corners;
  38.             triangles = CreateTriangles();
  39.            
  40.             this.FaceName = faceName;
  41.             //calc normal from face name
  42.             GenerateNormal();
  43.         }
  44.  
  45.         public Quad()
  46.         {
  47.  
  48.         }
  49.  
  50.         private Triangle[] CreateTriangles()
  51.         {
  52.             Triangle tri1 = new Triangle(
  53.                 new int[3] { 0, 1, 3 }); //top left, bottom right, bottom left
  54.             Triangle tri2 = new Triangle(
  55.                 new int[3] { 1, 2, 3 }); //top right, bottom right, bottom left
  56.             return new Triangle[2] { tri1, tri2 };
  57.         }
  58.  
  59.         public Vector3[] GetCornerPositions()
  60.         {
  61.             Vector3[] positions = new Vector3[4];
  62.             for (int p = 0; p < 4; p++)
  63.             {
  64.                 positions[p] = corners[p].Position;
  65.             }
  66.             return positions;
  67.         }
  68.  
  69.         public void CreateColorCorners(Color color)
  70.         {
  71.             colorCorners = new VertexPositionNormalColor[4];
  72.             for (int c = 0; c < 4; c++)
  73.             {
  74.                 colorCorners[c] = new VertexPositionNormalColor(corners[c].Position, color, corners[c].Normal);
  75.             }
  76.         }
  77.  
  78.         public bool CorrectTriangles(Vector3 centerOfBlock)
  79.         {
  80.             bool a = triangles[0].CheckIndices(this, centerOfBlock, FaceName);
  81.             bool b = triangles[1].CheckIndices(this, centerOfBlock, FaceName);
  82.             if (!(a && b))
  83.             {
  84.                 CreateColorCorners(Color.Red);
  85.                 return false;
  86.             }
  87.             else
  88.             {
  89.                 return true;
  90.             }
  91.         }
  92.  
  93.         private void GenerateNormal()
  94.         {
  95.             Vector3[] cornerPositions = GetCornerPositions();
  96.             Vector3 a = cornerPositions[2] - cornerPositions[0];
  97.             Vector3 b = cornerPositions[2] - cornerPositions[1];
  98.             Vector3 normal = Vector3.Cross(a, b);
  99.             for (int v = 0; v < corners.Length; v++)
  100.             {
  101.                 corners[v].Normal = normal;
  102.             }
  103.         }
  104.        
  105.     }
  106. }
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement