Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Input;
- using XIG.Core.Camera;
- using XIG.Objects;
- using XIG.Core;
- using Microsoft.Xna.Framework.Graphics;
- using XIG.Units;
- namespace XIG.Physics
- {
- public class XIGPickInfo
- {
- private Matrix ProjectionMatrix;
- private Matrix ViewMatrix;
- private Matrix WorldMatrix;
- public XIGPickInfo(Matrix ProjectionMatrix, Matrix ViewMatrix, Matrix WorldMatrix)
- {
- this.ProjectionMatrix = ProjectionMatrix;
- this.ViewMatrix = ViewMatrix;
- this.WorldMatrix = WorldMatrix;
- }
- private Ray GetRay()
- {
- //Create two vectors. One close up and one far away.
- MouseState mouseState = Mouse.GetState();
- Vector3 NearMouseSource = new Vector3(mouseState.X, mouseState.Y, 0.000001f);
- Vector3 FarMouseSource = new Vector3(mouseState.X, mouseState.Y, 0.99999f);
- Vector3 NearPoint = Application.Renderer.device.Viewport.Unproject(NearMouseSource, ProjectionMatrix, ViewMatrix, WorldMatrix);
- Vector3 FarPoint = Application.Renderer.device.Viewport.Unproject(FarMouseSource, ProjectionMatrix, ViewMatrix, WorldMatrix);
- Vector3 Direction = FarPoint - NearPoint;
- Direction.Normalize();
- return new Ray(NearPoint, Direction);
- }
- public Vector3? TestRayIntersectsTriangle(ref Ray ray,
- ref Vector3 vertex1,
- ref Vector3 vertex2,
- ref Vector3 vertex3)
- {
- // Compute vectors along two edges of the triangle.
- Vector3? result = null;
- float? Distance;
- Plane plane = new Plane(vertex1, vertex2, vertex3);
- ray.Intersects(ref plane, out Distance);
- if (Distance == null)
- {
- return null;
- }
- Vector3 IntersectionPoint = ray.Position + (float)Distance * ray.Direction;
- Vector3 Normal = plane.Normal;
- Normal.Normalize();
- //Clockwise triangle definition
- Vector3 Normal1 = Vector3.Cross(vertex2 - vertex1, ray.Position - vertex1);
- if (Vector3.Dot(Normal1, Normal) < 0)
- {
- return null;
- }
- Vector3 Normal2 = Vector3.Cross(vertex3 - vertex2, ray.Position - vertex2);
- if (Vector3.Dot(Normal2, Normal) < 0)
- {
- return null;
- }
- Vector3 Normal3 = Vector3.Cross(vertex1 - vertex3, ray.Position - vertex3);
- if (Vector3.Dot(Normal3, Normal) < 0)
- {
- return null;
- }
- //We are within the triangle
- result = IntersectionPoint;
- return result;
- }
- public bool Compute()
- {
- bool Result = false;
- Ray ray = GetRay();
- foreach (XIGObject Obj in XIGRegistry.GetInstance().RegisteredObjects)
- {
- if (Obj is XIGGlobe)
- {
- XIGGlobe globe = (XIGGlobe)Obj;
- VertexPositionNormalTexture[] Vertices = globe.Sphere.vertices;
- List<ushort> Indices = globe.Sphere.indices;
- Vector3? IntersectionPoint = null;
- for (int i = 0; i < Indices.Count / 3; i = i + 3)
- {
- Vector3 VertexA = Vertices[Indices[i]].Position;
- Vector3 VertexB = Vertices[Indices[i + 1]].Position;
- Vector3 VertexC = Vertices[Indices[i + 2]].Position;
- Vector3? TestPoint = TestRayIntersectsTriangle(ref ray, ref VertexA, ref VertexB, ref VertexC);
- if (TestPoint != null)
- {
- System.Console.WriteLine("Passed");
- IntersectionPoint = TestPoint;
- }
- }
- if (IntersectionPoint != null)
- {
- XIGSphericalCoordinate coord = new XIGSphericalCoordinate((Vector3)IntersectionPoint);
- System.Console.WriteLine("Altitude: " + coord.Altitude);
- System.Console.WriteLine("Latitude: " + coord.Latitude);
- System.Console.WriteLine("Longitude: " + coord.Longitude);
- }
- Result = true;
- }
- }
- return Result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement