JTalton's picture

Glu.UnProject replacement for OpenTK.Math

Project:The Open Toolkit library
Version:1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:confirmed
Description

While Glu support has been marked deprecated and moved to OpenTK.Compatability, the replacement OpenTK.Math does not have a UnProject function.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

#1

Status:open» confirmed

Function definition (we can do better):

int gluUnProject(
      winx,
      winy,
      winz,
    const GLdouble modelMatrix,
    const GLdouble projMatrix,
    const GLint viewport,
      objx,
      objy,
      objz);

Probably belongs to MathHelper class.

karlschiffmann's picture

#2

Any idea when this feature may be added to the OpenTK release? Thank you...

the Fiddler's picture

#3

Unless someone posts a patch, I'd guess soon after opentk-1.0-final is out.

aklev's picture

#4

    Public Function UnProject(ByVal w As Vector4d, ByVal modelMatrix As Matrix4d, ByVal projMatrix As Matrix4d, _
                              ByVal viewport() As Double, ByRef obj As Vector4d) As Boolean
 
        Dim p As Matrix4d = Matrix4d.Mult(modelMatrix, projMatrix)
        Dim finalMatrix As Matrix4d = Matrix4d.Invert(p)
        Dim [in] As Vector4d = w
 
        ' Map x and y from window coordinates 
        [in].X = ([in].X - viewport(0)) / viewport(2)
        [in].Y = ([in].Y - viewport(1)) / viewport(3)
        ' Map to range -1 to 1 
        [in].X = [in].X * 2.0 - 1.0
        [in].Y = [in].Y * 2.0 - 1.0
        [in].Z = [in].Z * 2.0 - 1.0
        [in].W = 1.0
        Dim out As Vector4d = Vector4d.Transform([in], finalMatrix)
 
        If out.Z = 0.0 Then
            Return False
        End If
 
        out.X /= out.W
        out.Y /= out.W
        out.Z /= out.W
        obj = out
 
        Return True
 
    End Function
the Fiddler's picture

#5

Thanks, I will add this to trunk.

aklev's picture

#6

Small inaccuracy ...

    Public Function UnProject(ByVal w As Vector4d, ByVal modelMatrix As Matrix4d, ByVal projMatrix As Matrix4d, _
                              ByVal viewport() As Double, ByRef obj As Vector4d) As Boolean
 
        Dim p As Matrix4d = Matrix4d.Mult(modelMatrix, projMatrix)
        Dim finalMatrix As Matrix4d = Matrix4d.Invert(p)
        Dim [in] As Vector4d = w
 
        ' Map x and y from window coordinates 
        [in].X = ([in].X - viewport(0)) / viewport(2)
        [in].Y = ([in].Y - viewport(1)) / viewport(3)
        ' Map to range -1 to 1 
        [in].X = [in].X * 2.0 - 1.0
        [in].Y = [in].Y * 2.0 - 1.0
        [in].Z = [in].Z * 2.0 - 1.0
        [in].W = 1.0
        Dim out As Vector4d = Vector4d.Transform([in], finalMatrix)
 
        If out.W = 0.0 Then  ' Small inaccuracy ...
            Return False
        End If
 
        out.X /= out.W
        out.Y /= out.W
        out.Z /= out.W
        obj = out
 
        Return True
 
    End Function
kuma's picture

#7

Version:0.9.9-3» 1.x-dev

After two year in the last version not found unproject and project functions in OpenTK.

in this post I found UnProject function ok but I not have skill for create a Project function.

Do you can help me?

Robmaister's picture

#8

If you want to project, just use Vector4.Transform. Anyways, if you want a C# implementation of UnProject to include in MathHelper or whatever:

public static Vector4 UnProject(ref Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse)
{
	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;
}
kuma's picture

#9

thank you Robmaister for Unproject function in c# but I don't understand how to use vector4.transform for Project function

kuma's picture

#10

ok I found the way:

public static Vector4 Project(OpenTK.Vector4 objPos, Matrix4 projection, Matrix4 view, Size viewport)
{
Vector4 vec = objPos;

vec = Vector4.Transform(vec, Matrix4.Mult(projection, view));

vec.X = (vec.X + 1) * (viewport.Width / 2);
vec.Y = (vec.Y + 1) * (viewport.Height / 2);

return vec;
}