1
0
mirror of synced 2024-12-11 07:16:05 +01:00
Switch-Toolbox/Switch_Toolbox_Library/OpenGL/OpenGLHelper.cs

56 lines
1.7 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)
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);
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)
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;
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;
}
}
}