Switch-Toolbox/Switch_Toolbox_Library/OpenGL/OpenGLHelper.cs
KillzXGaming 871fc44178 More additions.
More progress on the basic pane animation editor. It can add and clear entries for frames, groups, etc.
Add the latest muunt editor files. A wip editor which is currently disabled. Planned to edit object placement paths paths supporting both 2D and 3D views.
2019-10-20 12:57:48 -04:00

61 lines
1.9 KiB
C#

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)
{
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);
Point coords = new Point((int)vector.X, (int)vector.Y);
return coords;
}
public static Vector4 UnProject(ref Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse, bool isTopDown)
{
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);
}
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;
}
}
}