title: Convert screen coordinate to world coordinate WITHOUT GLUUNPROJECT
body: <p>I have been searching all over with Google and this site for an answer but I still get confused as to how one goes about converting the coordinates on the screen to the coordinate in model space. I do know the distance from the origin (0,0,0) to my camera, which is also the viewport's center:</p>
  
  <p><a href="https://i.stack.imgur.com/nKHCt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nKHCt.png" alt="enter image description here"></a></p>
  
  <p>If I have a different point on my view window such as (1,3) for example:</p>
  
  <p><a href="https://i.stack.imgur.com/tvQeP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tvQeP.png" alt="enter image description here"></a></p>
  
  <p>What is the best way to either mathematically calculate the new point in model view or to use matrix functions to determine it?  </p>
  
  <p>I apologize if this is considered "easy" for some people but I was hoping someone would post a good example using my sample coordinate since I am still rather confused.</p>
  
  <p>Here is the matrix call that is done on every frame:</p>
  
  <pre><code>Public Sub SetupViewport()
      Dim w As Integer = GlControl1.Width
      Dim h As Integer = GlControl1.Height
      Dim perspective1 As Matrix4 = cam.GetViewMatrix() * Matrix4.CreatePerspectiveFieldOfView(1.3F, ClientSize.Width / CSng(ClientSize.Height), 0.1F, 200.0F)
  
  
      GL.MatrixMode(MatrixMode.Projection)
      GL.LoadIdentity()
      GL.LoadMatrix(perspective1)
      GL.Viewport(0, 0, w, h)
  
  End Sub
  </code></pre>
  
  <p>This is done to allow me to rotate the camera using the cam matrix.  I know generally in Open GL that you rotate the scene and not the camera but since I am using this as a CAD program, I am rotating my camera around the origin.  </p>
  
  <p>For testing purposes I have been constructing a line from what I calculate as the calculated start point, to the origin.  As the title states, I do not wish to use gluunproject since I have read that this is based off of Tao framework so shouldn't be used.</p>
  
  <p><strong>My Draw Sub</strong></p>
  
  <pre><code>Private Sub GlControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
  
      GL.Clear(ClearBufferMask.ColorBufferBit)
      GL.Clear(ClearBufferMask.DepthBufferBit)
      GL.DepthMask(True)
      GL.Enable(EnableCap.DepthTest)
      GL.ClearDepth(1.0F)
  
  
  
      GL.MatrixMode(MatrixMode.Modelview)
      GL.LoadIdentity()
  
  
      Dim lightColor0 As Single() = {intensity, intensity, intensity, 1.0F}
      Dim lightPos0 As Single() = {camx, camy, camz, 1.0F}
  
  
      GL.Light(LightName.Light0, LightParameter.Diffuse, lightColor0)
      GL.Light(LightName.Light0, LightParameter.Position, lightPos0)
      GL.Enable(EnableCap.Light0)
  
  
  
      Dim mat_specular As Single() = {1.0F, 1.0F, 1.0F, 1.0F}
      Dim mat_shininess As Single() = {50.0F}
  
  
      GL.Material(MaterialFace.Front, MaterialParameter.Specular, mat_specular)
      GL.Material(MaterialFace.Front, MaterialParameter.Shininess, mat_shininess)
  
  
      GL.Disable(EnableCap.Lighting)
  
      GL.Begin(PrimitiveType.Lines)
      GL.Color3(Color.Red)
      GL.Vertex3(0, 0, 0)
      GL.Vertex3(100, 0, 0)
  
      GL.Color3(Color.Green)
      GL.Vertex3(0, 0, 0)
      GL.Vertex3(0, 100, 0)
  
      GL.Color3(Color.Blue)
      GL.Vertex3(0, 0, 0)
      GL.Vertex3(0, 0, 100)
      GL.End()
  
  
      GL.Begin(PrimitiveType.Lines)
      GL.Color3(Color.DarkRed)
      GL.Vertex3(0, 0, 0)
      GL.Vertex3(-100, 0, 0)
  
      GL.Color3(Color.DarkGreen)
      GL.Vertex3(0, 0, 0)
      GL.Vertex3(0, -100, 0)
  
      GL.Color3(Color.DarkBlue)
      GL.Vertex3(0, 0, 0)
      GL.Vertex3(0, 0, -100)
      GL.End()
  
      Dim projmatrix As Matrix4
      GL.GetFloat(GetPName.ProjectionMatrix, projmatrix)
      Dim mouse_ As New Vector2(_mouseStartX, _mouseStartY)
      Dim returnvec As Vector4
  
      returnvec = UnProject(projmatrix, cam.GetViewMatrix(), GlControl1.Size, mouse_)
  
      GL.Begin(PrimitiveType.Lines)
      GL.Color3(Color.Orange)
      GL.Vertex3(0, 0, 0)
      GL.Vertex3(returnvec.X, returnvec.Y, returnvec.Z)
      GL.End()
  
  
      GL.Enable(EnableCap.Lighting)
  
      draw_extras()
  
  
      GL.Flush()
      GlControl1.SwapBuffers()
  End Sub
  </code></pre>