1
0
mirror of synced 2024-12-03 19:47:29 +01:00
Switch-Toolbox/Switch_Toolbox_Library/OpenGL/OpenGLHelper.cs

62 lines
1.9 KiB
C#
Raw Normal View History

2019-10-16 01:26:58 +02: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
{
public static Point convertScreenToWorldCoords(int x, int y, bool isTopDown = false)
2019-10-16 01:26:58 +02: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;
Vector4 vector = UnProject(ref projectionMatrix, modelViewMatrix, new Size(viewport[2], viewport[3]), mouse, isTopDown);
2019-10-16 01:26:58 +02:00
Point coords = new Point((int)vector.X, (int)vector.Y);
2019-10-16 01:26:58 +02:00
return coords;
}
public static Vector4 UnProject(ref Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse, bool isTopDown)
2019-10-16 01:26:58 +02: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;
if (isTopDown)
{
vec.Y = 0;
vec.Z = -(2.0f * mouse.Y / (float)viewport.Height - 1);
}
2019-10-16 01:26:58 +02: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;
}
}
}