2019-10-15 23:26:58 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
|
|
|
namespace Toolbox.Library
|
|
|
|
|
{
|
|
|
|
|
public class OpenGLHelper
|
|
|
|
|
{
|
2019-10-20 16:57:48 +00:00
|
|
|
|
public static Point convertScreenToWorldCoords(int x, int y, bool isTopDown = false)
|
2019-10-15 23:26:58 +00:00
|
|
|
|
{
|
|
|
|
|
int[] viewport = new int[4];
|
|
|
|
|
Matrix4 modelViewMatrix, projectionMatrix;
|
|
|
|
|
GL.GetFloat(GetPName.ModelviewMatrix, out modelViewMatrix);
|
|
|
|
|
GL.GetFloat(GetPName.ProjectionMatrix, out projectionMatrix);
|
|
|
|
|
GL.GetInteger(GetPName.Viewport, viewport);
|
|
|
|
|
Vector2 mouse;
|
|
|
|
|
mouse.X = x;
|
|
|
|
|
mouse.Y = y;
|
2019-10-20 16:57:48 +00:00
|
|
|
|
Vector4 vector = UnProject(ref projectionMatrix, modelViewMatrix, new Size(viewport[2], viewport[3]), mouse, isTopDown);
|
2019-10-15 23:26:58 +00:00
|
|
|
|
Point coords = new Point((int)vector.X, (int)vector.Y);
|
2019-10-20 16:57:48 +00:00
|
|
|
|
|
2019-10-15 23:26:58 +00:00
|
|
|
|
return coords;
|
|
|
|
|
}
|
2019-10-20 16:57:48 +00:00
|
|
|
|
|
|
|
|
|
public static Vector4 UnProject(ref Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse, bool isTopDown)
|
2019-10-15 23:26:58 +00:00
|
|
|
|
{
|
|
|
|
|
Vector4 vec;
|
|
|
|
|
|
|
|
|
|
vec.X = (2.0f * mouse.X / (float)viewport.Width - 1);
|
|
|
|
|
vec.Y = -(2.0f * mouse.Y / (float)viewport.Height - 1);
|
|
|
|
|
vec.Z = 0;
|
|
|
|
|
vec.W = 1.0f;
|
|
|
|
|
|
2019-10-20 16:57:48 +00:00
|
|
|
|
if (isTopDown)
|
|
|
|
|
{
|
|
|
|
|
vec.Y = 0;
|
|
|
|
|
vec.Z = -(2.0f * mouse.Y / (float)viewport.Height - 1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 23:26:58 +00:00
|
|
|
|
Matrix4 viewInv = Matrix4.Invert(view);
|
|
|
|
|
Matrix4 projInv = Matrix4.Invert(projection);
|
|
|
|
|
|
|
|
|
|
Vector4.Transform(ref vec, ref projInv, out vec);
|
|
|
|
|
Vector4.Transform(ref vec, ref viewInv, out vec);
|
|
|
|
|
|
|
|
|
|
if (vec.W > float.Epsilon || vec.W < float.Epsilon)
|
|
|
|
|
{
|
|
|
|
|
vec.X /= vec.W;
|
|
|
|
|
vec.Y /= vec.W;
|
|
|
|
|
vec.Z /= vec.W;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|