Advertisement
Guest User

Untitled

a guest
May 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.78 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.Input;
  7. using XIG.Core.Camera;
  8. using XIG.Objects;
  9. using XIG.Core;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using XIG.Units;
  12.  
  13. namespace XIG.Physics
  14. {
  15.     public class XIGPickInfo
  16.     {
  17.         private Matrix ProjectionMatrix;
  18.         private Matrix ViewMatrix;
  19.         private Matrix WorldMatrix;
  20.  
  21.         public XIGPickInfo(Matrix ProjectionMatrix, Matrix ViewMatrix, Matrix WorldMatrix)
  22.         {
  23.             this.ProjectionMatrix = ProjectionMatrix;
  24.             this.ViewMatrix = ViewMatrix;
  25.             this.WorldMatrix = WorldMatrix;
  26.         }
  27.  
  28.         private Ray GetRay()
  29.         {
  30.             //Create two vectors. One close up and one far away.
  31.             MouseState mouseState = Mouse.GetState();
  32.             Vector3 NearMouseSource = new Vector3(mouseState.X, mouseState.Y, 0.000001f);
  33.             Vector3 FarMouseSource = new Vector3(mouseState.X, mouseState.Y, 0.99999f);
  34.  
  35.             Vector3 NearPoint = Application.Renderer.device.Viewport.Unproject(NearMouseSource, ProjectionMatrix, ViewMatrix, WorldMatrix);
  36.             Vector3 FarPoint = Application.Renderer.device.Viewport.Unproject(FarMouseSource, ProjectionMatrix, ViewMatrix, WorldMatrix);
  37.  
  38.             Vector3 Direction = FarPoint - NearPoint;
  39.             Direction.Normalize();
  40.  
  41.             return new Ray(NearPoint, Direction);
  42.         }
  43.  
  44.         public Vector3? TestRayIntersectsTriangle(ref Ray ray,
  45.                                           ref Vector3 vertex1,
  46.                                           ref Vector3 vertex2,
  47.                                           ref Vector3 vertex3)
  48.         {
  49.             // Compute vectors along two edges of the triangle.
  50.             Vector3? result = null;
  51.            
  52.             float? Distance;
  53.  
  54.             Plane plane = new Plane(vertex1, vertex2, vertex3);
  55.             ray.Intersects(ref plane, out Distance);
  56.  
  57.             if (Distance == null)
  58.             {
  59.                 return null;
  60.             }
  61.  
  62.             Vector3 IntersectionPoint = ray.Position + (float)Distance * ray.Direction;
  63.            
  64.             Vector3 Normal = plane.Normal;
  65.             Normal.Normalize();
  66.  
  67.             //Clockwise triangle definition
  68.             Vector3 Normal1 = Vector3.Cross(vertex2 - vertex1, ray.Position - vertex1);
  69.             if (Vector3.Dot(Normal1, Normal) < 0)
  70.             {
  71.                 return null;
  72.             }
  73.  
  74.             Vector3 Normal2 = Vector3.Cross(vertex3 - vertex2, ray.Position - vertex2);
  75.             if (Vector3.Dot(Normal2, Normal) < 0)
  76.             {
  77.                 return null;
  78.             }
  79.  
  80.             Vector3 Normal3 = Vector3.Cross(vertex1 - vertex3, ray.Position - vertex3);
  81.             if (Vector3.Dot(Normal3, Normal) < 0)
  82.             {
  83.                 return null;
  84.             }
  85.            
  86.             //We are within the triangle
  87.             result = IntersectionPoint;
  88.             return result;            
  89.         }
  90.  
  91.        public bool Compute()
  92.        {
  93.            bool Result = false;
  94.            Ray ray = GetRay();
  95.  
  96.            foreach (XIGObject Obj in XIGRegistry.GetInstance().RegisteredObjects)
  97.            {
  98.                if (Obj is XIGGlobe)
  99.                {
  100.                    XIGGlobe globe = (XIGGlobe)Obj;
  101.                    VertexPositionNormalTexture[] Vertices = globe.Sphere.vertices;
  102.                    List<ushort> Indices = globe.Sphere.indices;
  103.  
  104.                    Vector3? IntersectionPoint = null;
  105.  
  106.                    for (int i = 0; i < Indices.Count / 3; i = i + 3)
  107.                    {
  108.                        Vector3 VertexA = Vertices[Indices[i]].Position;
  109.                        Vector3 VertexB = Vertices[Indices[i + 1]].Position;
  110.                        Vector3 VertexC = Vertices[Indices[i + 2]].Position;
  111.  
  112.                        Vector3? TestPoint = TestRayIntersectsTriangle(ref ray, ref VertexA, ref VertexB, ref VertexC);
  113.  
  114.                        if (TestPoint != null)
  115.                        {
  116.                            System.Console.WriteLine("Passed");
  117.                            IntersectionPoint = TestPoint;
  118.                        }
  119.                    }
  120.                    if (IntersectionPoint != null)
  121.                    {
  122.                        XIGSphericalCoordinate coord = new XIGSphericalCoordinate((Vector3)IntersectionPoint);
  123.                        System.Console.WriteLine("Altitude: " + coord.Altitude);
  124.                        System.Console.WriteLine("Latitude: " + coord.Latitude);
  125.                        System.Console.WriteLine("Longitude: " + coord.Longitude);
  126.  
  127.                    }
  128.                    Result = true;
  129.                }
  130.            }
  131.            return Result;
  132.        }
  133.  
  134.  
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement